Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added proper t_program container

  • Loading branch information...
commit df8a56a6fb43fa28412d5b84764db6ab01a6d878 1 parent 3069c39
Adrian Petrescu authored
Showing with 52 additions and 28 deletions.
  1. +18 −0 pyidl/parser/ast.py
  2. +18 −13 pyidl/parser/parser.py
  3. +16 −15 tests/test_parser.py
View
18 pyidl/parser/ast.py
@@ -1,6 +1,15 @@
+class t_program(object):
+
+ def __init__(self, __ns=[], require=[], struct=[], enum=[], service=[]):
+ self.namespace = {n.lang: n for n in __ns}
+ self.__ns = __ns
+ self.struct = struct
+ self.enum = enum
+ self.service = service
class t_struct(object):
+ __type__ = "struct"
def __init__(self, name, declaration_list=[]):
self.name = name
@@ -8,7 +17,16 @@ def __init__(self, name, declaration_list=[]):
class t_enum(object):
+ __type__ = "enum"
def __init__(self, name, values=[]):
self.name = name
self.values = values
+
+
+class t_namespace(object):
+ __type__ = "_t_program__ns"
+
+ def __init__(self, lang, ns):
+ self.lang = lang
+ self.ns = ns
View
31 pyidl/parser/parser.py
@@ -14,7 +14,7 @@ def p_namespace_type(p):
def p_namespace(p):
'ns : namespace ns_type identifier'
- p[0] = (p[1], p[2], p[3])
+ p[0] = ast.t_namespace(p[2], p[3])
def p_require(p):
@@ -57,6 +57,9 @@ def p_enum_list(p):
| identifier
| enum_list COMMA assignment
| enum_list COMMA identifier'''
+
+ # Assignments are represented as (identifier, value). For the sake
+ # of consistency, we also represent non-assigned enums as (identifier, None)
if len(p) == 2:
p[0] = [(p[1], None) if type(p[1]) == str else p[1]]
if len(p) == 4:
@@ -191,23 +194,25 @@ def p_service(p):
p[0] = ('service', p[2], p[4])
-def p_document(p):
- '''document : ns
- | req
- | enum
- | struct
- | service
- | document document'''
- if len(p) == 2:
- p[0] = [p[1]]
- if len(p) == 3:
- p[0] = p[1] + p[2]
+def p_program(p):
+ '''program : ns
+ | req
+ | enum
+ | struct
+ | service
+ | program program'''
+ if (type(p[1]) == ast.t_program):
+ p[0] = ast.t_program(**{
+ key: (getattr(p[1], key, []) + getattr(p[2], key, [])) for key in ('_t_program__ns', 'require', 'enum', 'struct', 'service')
+ })
+ else:
+ p[0] = ast.t_program(**{p[1].__type__: [p[1]]})
def p_error(t):
print("Syntax error at '%s'" % t.value)
-start = 'document'
+start = 'program'
parser = yacc()
View
31 tests/test_parser.py
@@ -21,18 +21,18 @@ def read_case(self, name):
class ParserTestSuite(TestSetup, TestBaseMixin, unittest.TestCase):
def test_simple_enum(self):
- document = parser.parse(self.read_case('simple_enum'))
- assert len(document) == 1
- enum_parse = document[0]
+ program = parser.parse(self.read_case('simple_enum'))
+ assert len(program.enum) == 1
+ enum_parse = program.enum[0]
assert type(enum_parse) == ast.t_enum
assert enum_parse.name == "SomeSet"
assert set(enum_parse.values) == set([(x, None) for x in
'A', 'B', 'C'])
def test_complex_enum(self):
- document = parser.parse(self.read_case('complex_enum'))
- assert len(document) == 1
- enum_parse = document[0]
+ program = parser.parse(self.read_case('complex_enum'))
+ assert len(program.enum) == 1
+ enum_parse = program.enum[0]
assert type(enum_parse) == ast.t_enum
assert enum_parse.name == "SomeSet"
assert set(enum_parse.values) == set([
@@ -41,20 +41,21 @@ def test_complex_enum(self):
('C', 4)])
def test_simple_struct_with_namespace(self):
- document = parser.parse(self.read_case('simple_struct_with_namespace'))
- assert len(document) == 2
+ program = parser.parse(self.read_case('simple_struct_with_namespace'))
+ assert len(program.struct) == 1
- namespace_parse = document[0]
- self.assertEquals(('namespace', 'java', 'pyidl.example'), namespace_parse)
+ namespace_parse = program.namespace['java']
+ assert namespace_parse.lang == 'java'
+ assert namespace_parse.ns == 'pyidl.example'
- struct_parse = document[1]
+ struct_parse = program.struct[0]
assert type(struct_parse) is ast.t_struct
self.assertEquals('RequestObject', struct_parse.name)
self.assertEquals(('someRequest', 1, 'string'), struct_parse.declaration_list[0])
self.assertEquals(('someValue', 2, 'double'), struct_parse.declaration_list[1])
def test_complex_struct(self):
- document = parser.parse(self.read_case('complex_struct'))
- #import pdb
- #pdb.set_trace()
- print document
+ program = parser.parse(self.read_case('complex_struct'))
+
+ #assert 'java' in program.namespace
+ #assert 'cpp' in program.namespace
Please sign in to comment.
Something went wrong with that request. Please try again.