diff --git a/phply/phpast.py b/phply/phpast.py index aa0b2ed..08764e4 100644 --- a/phply/phpast.py +++ b/phply/phpast.py @@ -123,3 +123,5 @@ def node(name, fields): Namespace = node('Namespace', ['name', 'nodes']) UseDeclarations = node('UseDeclarations', ['nodes']) UseDeclaration = node('UseDeclaration', ['name', 'alias']) +ConstantDeclarations = node('ConstantDeclarations', ['nodes']) +ConstantDeclaration = node('ConstantDeclaration', ['name', 'initial']) diff --git a/phply/phpparse.py b/phply/phpparse.py index cb0e75e..4ad9782 100644 --- a/phply/phpparse.py +++ b/phply/phpparse.py @@ -76,6 +76,10 @@ def p_top_statement_namespace(p): else: p[0] = ast.Namespace(p[2], p[4], lineno=p.lineno(1)) +def p_top_statement_constant(p): + 'top_statement : CONST constant_declarations SEMI' + p[0] = ast.ConstantDeclarations(p[2], lineno=p.lineno(1)) + def p_top_statement_use(p): 'top_statement : USE use_declarations SEMI' p[0] = ast.UseDeclarations(p[2], lineno=p.lineno(1)) @@ -102,6 +106,18 @@ def p_use_declaration(p): else: p[0] = ast.UseDeclaration(p[1] + p[2], p[4], lineno=p.lineno(1)) +def p_constant_declarations(p): + '''constant_declarations : constant_declarations COMMA constant_declaration + | constant_declaration''' + if len(p) == 4: + p[0] = p[1] + [p[3]] + else: + p[0] = [p[1]] + +def p_constant_declaration(p): + 'constant_declaration : STRING EQUALS static_scalar' + p[0] = ast.ConstantDeclaration(p[1], p[3], lineno=p.lineno(1)) + def p_inner_statement_list(p): '''inner_statement_list : inner_statement_list inner_statement | empty''' diff --git a/tests/test_parser.py b/tests/test_parser.py index f244d3b..6dea6b0 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -554,3 +554,15 @@ def test_use_declarations(): UseDeclaration('\\c\\d\\e', 'f')]), ] eq_ast(input, expected) + +def test_constant_declarations(): + input = r"""""" + expected = [ + ConstantDeclarations([ConstantDeclaration('foo', 42)]), + ConstantDeclarations([ConstantDeclaration('bar', 'baz'), + ConstantDeclaration('wat', Constant('DOO'))]), + ] + eq_ast(input, expected)