-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathg984parser.py
executable file
·134 lines (113 loc) · 3.02 KB
/
g984parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
import re
from pprint import pprint
from copy import deepcopy
import ply.yacc as yacc
from g984lexer import tokens
def p_entities(p):
'''entities : entities entity
| entity
'''
if len(p) == 2:
p[0] = ( p[1], )
elif len(p) == 3:
p[0] = p[1] + ( p[2], )
def p_entity(p):
'''entity : header relationships attributes actions notifications
| header relationships attributes actions
| header
'''
p[0] = tuple(p[1:])
def p_header(p):
'''header : HEADER text'''
p[0] = ('header', p[1], p[2][1])
def p_relationships(p):
'''relationships : RELATIONSHIPS text'''
p[0] = ('relationships', p[2][1])
def p_attributes(p):
'''attributes : ATTRIBUTES attribs'''
p[0] = p[2]
def p_attribs(p):
'''attribs : attribute
| attribs attribute
'''
if len(p) == 2:
p[0] = ('attributes', (p[1],))
elif len(p) == 3:
p[0] = ('attributes', p[1][1] + (p[2],))
def p_attribute(p):
'''attribute : ANAME attrdesc
'''
p[0] = (p[1], p[2])
def p_attrdesc(p):
'''attrdesc : attrdesc text
| attrdesc flags
| empty
'''
if len(p) == 2:
p[0] = ('attr-desc', tuple(), '')
elif len(p) == 3:
if p[2][0] == 'flags':
p[0] = ('attr-desc', p[1][1] + (p[2],), p[1][2])
elif p[2][0] == 'text':
p[0] = ('attr-desc', p[1][1], p[1][2] + p[2][1])
def p_flags(p):
'''flags : flags flag
| flag
'''
if len(p) == 2:
p[0] = ('flags', (p[1],))
else:
p[0] = ('flags', p[1][1] + (p[2][1],))
def p_flag(p):
'flag : LPAREN text RPAREN'
flag = p[2][1]
if flag == 'mandatory':
p[0] = ('flag', 'mandatory')
elif flag == 'optional':
p[0] = ('flag', 'optional')
else:
flags = tuple()
for xflag in re.split('\W+', flag):
if xflag in ['R','W','Set-by-create']:
flags = flags + (xflag,)
if flags:
p[0] = ('flag', flags)
else:
try:
p[0] = ('size', int(re.match('(\d+).*byte', flag).groups()[0]))
except:
pass
if not p[0]:
p[0] = ('flag-ext', '(%s)' % flag)
def p_actions(p):
'''actions : ACTIONS text'''
p[0] = ('actions', p[2])
def p_notifications(p):
'''notifications : NOTIFICATIONS text'''
p[0] = ('notifications', p[2])
def p_empty(p):
'empty :'
pass
def p_text(p):
'''
text : text TEXT
| empty
'''
if len(p) == 3:
p[0] = ('text', p[1][1] + p[2])
else:
p[0] = ('text', '')
def p_error(t):
if t.type in ( 'RPAREN', 'LPAREN' ):
parser.errok()
return
raise SyntaxError('%s : line %d' % (t, t.lineno))
parser = yacc.yacc()
if __name__ == '__main__':
import sys
from g984lexer import lexer
#ast = parser.parse( sys.argv[1], debug=1, lexer=lexer)
ast = parser.parse( sys.argv[1], lexer=lexer)
pprint(ast)
#print len(ast)