diff --git a/armaclass/parser.py b/armaclass/parser.py index 894aea9..c651caf 100644 --- a/armaclass/parser.py +++ b/armaclass/parser.py @@ -5,6 +5,7 @@ QUOTE = '"' SEMICOLON = ';' +COLON = ":" EQUALS = '=' CURLY_OPEN = '{' CURLY_CLOSE = '}' @@ -14,6 +15,8 @@ MINUS = '-' SLASH = '/' ASTERISK = '*' +BACKSLASH = '\\' +HASH = '#' VALID_NAME_CHAR = string.ascii_letters + string.digits + '_.\\' @@ -186,6 +189,20 @@ def parseWhitespace(self): except IndexError: pass + def parseMacro(self): + macro = "" + try: + while ( + self.raw[self.currentPosition] not in "\r\n" + or self.raw[self.currentPosition - 1: self.currentPosition + 1] in ("\\\r", "\\\n") + ): + macro += self.current() + self.next() + except IndexError: + pass + + return macro + def parseProperty(self, context): name = self.parsePropertyName() @@ -195,7 +212,7 @@ def parseProperty(self, context): name = self.parsePropertyName() self.parseWhitespace() - if self.current() == ':': + if self.current() == COLON: self.next() self.parseWhitespace() self.parsePropertyName() @@ -203,7 +220,7 @@ def parseProperty(self, context): current = self.current() - + if current == SQUARE_OPEN: self.ensure(self.next() == SQUARE_CLOSE) self.next() @@ -215,6 +232,16 @@ def parseProperty(self, context): value = self.parseArray() + elif name.startswith(HASH): + macros = context.get(name, []) + macros.append(self.parseMacro()) + context[name] = macros + return + + elif current == SEMICOLON: + value = self.dict() + self.parseWhitespace() + elif current == EQUALS: self.next() self.parseWhitespace() diff --git a/test/test_basic.py b/test/test_basic.py index 541ddf6..1def1f3 100644 --- a/test/test_basic.py +++ b/test/test_basic.py @@ -58,6 +58,10 @@ def test_ignore_inheritance(self): testString = 'class Moo : foo {};' self.assertEqual(parse(testString), {'Moo': {}}) + def test_macros(self): + self.assertEqual(parse('#include "cfgSurfaces.h"'), {"#include": ['"cfgSurfaces.h"']}) + self.assertEqual(parse('#define TESTING firstline = 1;\\\nsecondline = 2;'), {"#define": ['TESTING firstline = 1;\\\nsecondline = 2;']}) + def test_line_comments(self): self.assertEqual(parse('// foo comment'), {}) self.assertEqual(parse('// foo comment\nx=2;'), {'x': 2})