forked from cantora/pyc
/
pyc_ply_parser.py
212 lines (162 loc) · 3.88 KB
/
pyc_ply_parser.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright 2013 anthony cantor
# This file is part of pyc.
#
# pyc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyc. If not, see <http://www.gnu.org/licenses/>.
import pyc_log
'''
identifier ::= (letter|"_") (letter | digit | "_")*
letter ::= lowercase | uppercase
lowercase ::= "a"..."z"
uppercase ::= "A"..."Z"
digit ::= "0"..."9"
'''
reserved = {
'print' : 'PRINT'
}
'''
'and' : 'AND',
'del' : 'DEL',
'from' : 'FROM',
'not' : 'NOT',
'while' : 'WHILE',
'as' : 'AS',
'elif' : 'ELIF',
'global' : 'GLOBAL',
'or' : 'OR',
'with' : 'WITH',
'assert' : 'ASSERT',
'else' : 'ELSE',
'if' : 'IF',
'pass' : 'PASS',
'yield' : 'YIELD',
'break' : 'BREAK',
'except' : 'EXCEPT',
'import' : 'IMPORT',
'class' : 'CLASS',
'exec' : 'EXEC',
'in' : 'IN',
'raise' : 'RAISE',
'continue': 'CONTINUE',
'finally': 'FINALLY',
'is' : 'IS',
'return' : 'RETURN',
'def' : 'DEF',
'for' : 'FOR',
'lambda' : 'LAMBDA',
'try' : 'TRY'
}
'''
literals = ['=', '(', ')', '#', '\\']
tokens = (
'INT',
'PLUS',
'MINUS',
'SEMI',
'IDENT',
) + tuple(reserved.values())
t_PLUS = r'\+'
t_MINUS = r'-'
t_SEMI = r';'
t_ignore_COMMENT = r'\#.*'
def t_IDENT(t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
t.type = reserved.get(t.value, 'IDENT')
return t
def t_FUNC_CALL(t):
r'[a-zA-Z_][a-zA-Z0-9_]*\(\)'
return t
def t_INT(t):
r'\d+'
try:
t.value = int(t.value)
except ValueError:
pyc_log.log('integer value invalid: %s' % t.value)
t.value = 0
return t
t_ignore = ' \t'
def t_newline(t):
r'(\x0d?\x0a)+'
t.lexer.lineno += t.value.count("\n")
t_ignore_ESC_NEWLINE = r'\\\x0d?\x0a'
def t_error(t):
pyc_log.log("illegal character '%s'" % t.value[0])
import pyc_util
pyc_util.add_relative_dir_to_syspath("ply")
import ply.lex
#ply.lex.lex(debug = pyc_log.isverbose())
ply.lex.lex()
import compiler
precedence = (
('left', 'PLUS', 'MINUS'),
('right', 'UMINUS'),
)
def p_module(m):
'module : statement_list'
m[0] = compiler.ast.Module(None, m[1])
def p_statement_list(sl):
'statement_list : statement statement_list'
sl[0] = sl[2]
sl[0].nodes.insert(0, sl[1])
def p_empty(t):
'empty : '
pass
def p_statement_list_empty(sl):
'statement_list : empty'
sl[0] = compiler.ast.Stmt([])
def p_statement(t):
'''statement : stmt SEMI
| stmt '''
t[0] = t[1]
def p_print_stmt(t):
'stmt : PRINT expr'
t[0] = compiler.ast.Printnl([t[2]], None)
def p_assign_stmt(t):
'stmt : IDENT "=" expr'
t[0] = compiler.ast.Assign(
[compiler.ast.AssName(t[1], 'OP_ASSIGN')],
t[3]
)
def p_discard(t):
'stmt : expr'
t[0] = compiler.ast.Discard(t[1])
def p_call_expr(t):
'expr : IDENT "(" ")"'
t[0] = compiler.ast.CallFunc(compiler.ast.Name(t[1]), [])
def p_paren_expr(t):
'expr : "(" expr ")"'
t[0] = t[2]
def p_ident_expr(t):
'expr : IDENT'
t[0] = compiler.ast.Name(t[1])
def p_plus_expr(t):
'expr : expr PLUS expr'
t[0] = compiler.ast.Add( (t[1], t[3]) )
def p_neg_expr(t):
'expr : MINUS expr %prec UMINUS'
t[0] = compiler.ast.UnarySub(t[2])
def p_int_expr(t):
'expr : INT'
t[0] = compiler.ast.Const(t[1])
def p_error(t):
try:
lineno = t.lineno
except AttributeError:
lineno = 0
raise Exception("syntax error at line %d near: '%s'" % (lineno, t.value) )
import ply.yacc
ply.yacc.yacc()
def parse(src):
# create console handler and set level to debug
#return ply.yacc.parse(src, debug=pyc_log.log_obj)
return ply.yacc.parse(src)