Skip to content

Commit

Permalink
Handle empty class. Require class qualifier if braces found
Browse files Browse the repository at this point in the history
  • Loading branch information
overfl0 committed May 6, 2024
1 parent 6899e8f commit 42a5d83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
35 changes: 19 additions & 16 deletions armaclass/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion tests/test_basic_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from armaclass import parse
from armaclass import parse, ParseError


def test_empty():
Expand Down Expand Up @@ -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[]={};')
Expand Down

0 comments on commit 42a5d83

Please sign in to comment.