Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add top-level constant declarations

  • Loading branch information...
commit f35d4abe46782db572235016f806ca255b25f27b 1 parent 1025194
@ramen authored
Showing with 30 additions and 0 deletions.
  1. +2 −0  phply/phpast.py
  2. +16 −0 phply/phpparse.py
  3. +12 −0 tests/test_parser.py
View
2  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'])
View
16 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'''
View
12 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"""<?
+ const foo = 42;
+ const bar = 'baz', wat = DOO;
+ ?>"""
+ expected = [
+ ConstantDeclarations([ConstantDeclaration('foo', 42)]),
+ ConstantDeclarations([ConstantDeclaration('bar', 'baz'),
+ ConstantDeclaration('wat', Constant('DOO'))]),
+ ]
+ eq_ast(input, expected)
Please sign in to comment.
Something went wrong with that request. Please try again.