Skip to content

Commit

Permalink
Add top-level constant declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
ramen committed Oct 10, 2010
1 parent 1025194 commit f35d4ab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions phply/phpast.py
Expand Up @@ -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'])
16 changes: 16 additions & 0 deletions phply/phpparse.py
Expand Up @@ -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))
Expand All @@ -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'''
Expand Down
12 changes: 12 additions & 0 deletions tests/test_parser.py
Expand Up @@ -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)

0 comments on commit f35d4ab

Please sign in to comment.