Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added proper t_program container

  • Loading branch information...
commit df8a56a6fb43fa28412d5b84764db6ab01a6d878 1 parent 3069c39
Adrian Petrescu authored
18 pyidl/parser/ast.py
... ... @@ -1,6 +1,15 @@
  1 +class t_program(object):
  2 +
  3 + def __init__(self, __ns=[], require=[], struct=[], enum=[], service=[]):
  4 + self.namespace = {n.lang: n for n in __ns}
  5 + self.__ns = __ns
  6 + self.struct = struct
  7 + self.enum = enum
  8 + self.service = service
1 9
2 10
3 11 class t_struct(object):
  12 + __type__ = "struct"
4 13
5 14 def __init__(self, name, declaration_list=[]):
6 15 self.name = name
@@ -8,7 +17,16 @@ def __init__(self, name, declaration_list=[]):
8 17
9 18
10 19 class t_enum(object):
  20 + __type__ = "enum"
11 21
12 22 def __init__(self, name, values=[]):
13 23 self.name = name
14 24 self.values = values
  25 +
  26 +
  27 +class t_namespace(object):
  28 + __type__ = "_t_program__ns"
  29 +
  30 + def __init__(self, lang, ns):
  31 + self.lang = lang
  32 + self.ns = ns
31 pyidl/parser/parser.py
@@ -14,7 +14,7 @@ def p_namespace_type(p):
14 14
15 15 def p_namespace(p):
16 16 'ns : namespace ns_type identifier'
17   - p[0] = (p[1], p[2], p[3])
  17 + p[0] = ast.t_namespace(p[2], p[3])
18 18
19 19
20 20 def p_require(p):
@@ -57,6 +57,9 @@ def p_enum_list(p):
57 57 | identifier
58 58 | enum_list COMMA assignment
59 59 | enum_list COMMA identifier'''
  60 +
  61 + # Assignments are represented as (identifier, value). For the sake
  62 + # of consistency, we also represent non-assigned enums as (identifier, None)
60 63 if len(p) == 2:
61 64 p[0] = [(p[1], None) if type(p[1]) == str else p[1]]
62 65 if len(p) == 4:
@@ -191,23 +194,25 @@ def p_service(p):
191 194 p[0] = ('service', p[2], p[4])
192 195
193 196
194   -def p_document(p):
195   - '''document : ns
196   - | req
197   - | enum
198   - | struct
199   - | service
200   - | document document'''
201   - if len(p) == 2:
202   - p[0] = [p[1]]
203   - if len(p) == 3:
204   - p[0] = p[1] + p[2]
  197 +def p_program(p):
  198 + '''program : ns
  199 + | req
  200 + | enum
  201 + | struct
  202 + | service
  203 + | program program'''
  204 + if (type(p[1]) == ast.t_program):
  205 + p[0] = ast.t_program(**{
  206 + key: (getattr(p[1], key, []) + getattr(p[2], key, [])) for key in ('_t_program__ns', 'require', 'enum', 'struct', 'service')
  207 + })
  208 + else:
  209 + p[0] = ast.t_program(**{p[1].__type__: [p[1]]})
205 210
206 211
207 212 def p_error(t):
208 213 print("Syntax error at '%s'" % t.value)
209 214
210   -start = 'document'
  215 +start = 'program'
211 216 parser = yacc()
212 217
213 218
31 tests/test_parser.py
@@ -21,18 +21,18 @@ def read_case(self, name):
21 21 class ParserTestSuite(TestSetup, TestBaseMixin, unittest.TestCase):
22 22
23 23 def test_simple_enum(self):
24   - document = parser.parse(self.read_case('simple_enum'))
25   - assert len(document) == 1
26   - enum_parse = document[0]
  24 + program = parser.parse(self.read_case('simple_enum'))
  25 + assert len(program.enum) == 1
  26 + enum_parse = program.enum[0]
27 27 assert type(enum_parse) == ast.t_enum
28 28 assert enum_parse.name == "SomeSet"
29 29 assert set(enum_parse.values) == set([(x, None) for x in
30 30 'A', 'B', 'C'])
31 31
32 32 def test_complex_enum(self):
33   - document = parser.parse(self.read_case('complex_enum'))
34   - assert len(document) == 1
35   - enum_parse = document[0]
  33 + program = parser.parse(self.read_case('complex_enum'))
  34 + assert len(program.enum) == 1
  35 + enum_parse = program.enum[0]
36 36 assert type(enum_parse) == ast.t_enum
37 37 assert enum_parse.name == "SomeSet"
38 38 assert set(enum_parse.values) == set([
@@ -41,20 +41,21 @@ def test_complex_enum(self):
41 41 ('C', 4)])
42 42
43 43 def test_simple_struct_with_namespace(self):
44   - document = parser.parse(self.read_case('simple_struct_with_namespace'))
45   - assert len(document) == 2
  44 + program = parser.parse(self.read_case('simple_struct_with_namespace'))
  45 + assert len(program.struct) == 1
46 46
47   - namespace_parse = document[0]
48   - self.assertEquals(('namespace', 'java', 'pyidl.example'), namespace_parse)
  47 + namespace_parse = program.namespace['java']
  48 + assert namespace_parse.lang == 'java'
  49 + assert namespace_parse.ns == 'pyidl.example'
49 50
50   - struct_parse = document[1]
  51 + struct_parse = program.struct[0]
51 52 assert type(struct_parse) is ast.t_struct
52 53 self.assertEquals('RequestObject', struct_parse.name)
53 54 self.assertEquals(('someRequest', 1, 'string'), struct_parse.declaration_list[0])
54 55 self.assertEquals(('someValue', 2, 'double'), struct_parse.declaration_list[1])
55 56
56 57 def test_complex_struct(self):
57   - document = parser.parse(self.read_case('complex_struct'))
58   - #import pdb
59   - #pdb.set_trace()
60   - print document
  58 + program = parser.parse(self.read_case('complex_struct'))
  59 +
  60 + #assert 'java' in program.namespace
  61 + #assert 'cpp' in program.namespace

0 comments on commit df8a56a

Please sign in to comment.
Something went wrong with that request. Please try again.