From 42a5d83d46c2c1d317260ca36d2bb7b80827e4ba Mon Sep 17 00:00:00 2001 From: Lukasz Taczuk Date: Mon, 6 May 2024 02:51:20 +0200 Subject: [PATCH] Handle empty class. Require class qualifier if braces found --- armaclass/parser.py | 35 +++++++++++++++++++---------------- tests/test_basic_types.py | 14 +++++++++++++- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/armaclass/parser.py b/armaclass/parser.py index 7dc2f61..f3d27f9 100644 --- a/armaclass/parser.py +++ b/armaclass/parser.py @@ -317,24 +317,14 @@ def parseProperty(self, context: dict) -> cython.void: self.parseWhitespace() - if name == 'class': - name = self.parsePropertyName() - self.parseWhitespace() - - if self.current() == COLON: - self.next() - self.parseWhitespace() - self.parsePropertyName() - self.parseWhitespace() - - elif name == 'delete': + if name == 'delete': self.parsePropertyName() self.parseWhitespace() self.ensure(self.current() == SEMICOLON) self.next() return - elif name == 'import': + if name == 'import': self.parsePropertyName() self.parseWhitespace() self.ensure(self.current() == SEMICOLON) @@ -343,7 +333,23 @@ def parseProperty(self, context: dict) -> cython.void: current = self.current() - if current == SQUARE_OPEN: + if name == 'class': + name = self.parsePropertyName() + self.parseWhitespace() + + if self.current() == COLON: + self.next() + self.parseWhitespace() + self.parsePropertyName() + self.parseWhitespace() + + current = self.current() + if current == CURLY_OPEN: + value = self.parseClassValue() + elif current == SEMICOLON: + value = {} + + elif current == SQUARE_OPEN: self.ensure(self.next() == SQUARE_CLOSE) self.next() self.parseWhitespace() @@ -362,9 +368,6 @@ def parseProperty(self, context: dict) -> cython.void: self.parseWhitespace() value = self.parseNonArrayPropertyValue() - elif current == CURLY_OPEN: - value = self.parseClassValue() - elif current == SLASH: if self.next() == SLASH: self.currentPosition = self.input_string.find(NEWLINE_U, self.currentPosition) diff --git a/tests/test_basic_types.py b/tests/test_basic_types.py index cb17063..d67ba91 100644 --- a/tests/test_basic_types.py +++ b/tests/test_basic_types.py @@ -1,6 +1,6 @@ import pytest -from armaclass import parse +from armaclass import parse, ParseError def test_empty(): @@ -78,6 +78,18 @@ def test_class(): assert type(result['var']) == dict +def test_class_no_braces(): + expected = {'var': {}} + result = parse('class var ;') + assert result == expected + assert type(result['var']) == dict + + +def test_class_no_class_keyword_fails(): + with pytest.raises(ParseError): + parse('var {};') # Was incorrectly parsing this as `class var {};` + + def test_empty_array(): expected = {'var': []} result = parse('var[]={};')