Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 197 lines (147 sloc) 3.304 kB
af36987 misc
cantora authored
1 import pyc_log
2
34cb2f7 assign expr, call expr, ident expr
cantora authored
3 '''
4 identifier ::= (letter|"_") (letter | digit | "_")*
5 letter ::= lowercase | uppercase
6 lowercase ::= "a"..."z"
7 uppercase ::= "A"..."Z"
8 digit ::= "0"..."9"
9 '''
10
11 reserved = {
88470ee reserved words
cantora authored
12 'print' : 'PRINT'
13 }
14 '''
af36987 misc
cantora authored
15 'and' : 'AND',
16 'del' : 'DEL',
17 'from' : 'FROM',
18 'not' : 'NOT',
19 'while' : 'WHILE',
20 'as' : 'AS',
21 'elif' : 'ELIF',
22 'global' : 'GLOBAL',
23 'or' : 'OR',
24 'with' : 'WITH',
25 'assert' : 'ASSERT',
26 'else' : 'ELSE',
27 'if' : 'IF',
28 'pass' : 'PASS',
29 'yield' : 'YIELD',
30 'break' : 'BREAK',
31 'except' : 'EXCEPT',
32 'import' : 'IMPORT',
33 'class' : 'CLASS',
34 'exec' : 'EXEC',
35 'in' : 'IN',
36 'raise' : 'RAISE',
37 'continue': 'CONTINUE',
38 'finally': 'FINALLY',
39 'is' : 'IS',
40 'return' : 'RETURN',
41 'def' : 'DEF',
42 'for' : 'FOR',
43 'lambda' : 'LAMBDA',
44 'try' : 'TRY'
34cb2f7 assign expr, call expr, ident expr
cantora authored
45 }
88470ee reserved words
cantora authored
46 '''
34cb2f7 assign expr, call expr, ident expr
cantora authored
47
45b4407 undo last commit on pyc_ply_parser.py
cantora authored
48 literals = ['=', '(', ')', '#', '\\']
34cb2f7 assign expr, call expr, ident expr
cantora authored
49
32f2ffe started parser using PLY
cantora authored
50 tokens = (
51 'INT',
52 'PLUS',
53 'MINUS',
34cb2f7 assign expr, call expr, ident expr
cantora authored
54 'SEMI',
200dc24 misc parser features
cantora authored
55 'IDENT',
34cb2f7 assign expr, call expr, ident expr
cantora authored
56 ) + tuple(reserved.values())
8691675 added ply distribution to makefile
cantora authored
57
58 t_PLUS = r'\+'
32f2ffe started parser using PLY
cantora authored
59 t_MINUS = r'-'
34cb2f7 assign expr, call expr, ident expr
cantora authored
60 t_SEMI = r';'
45b4407 undo last commit on pyc_ply_parser.py
cantora authored
61
61e17fa shift/reduce, p_module refactoring
cantora authored
62 t_ignore_COMMENT = r'\#.*'
34cb2f7 assign expr, call expr, ident expr
cantora authored
63
64 def t_IDENT(t):
65 r'[a-zA-Z_][a-zA-Z0-9_]*'
66 t.type = reserved.get(t.value, 'IDENT')
67 return t
8691675 added ply distribution to makefile
cantora authored
68
45b4407 undo last commit on pyc_ply_parser.py
cantora authored
69 def t_FUNC_CALL(t):
70 r'[a-zA-Z_][a-zA-Z0-9_]*\(\)'
71 return t
72
8691675 added ply distribution to makefile
cantora authored
73 def t_INT(t):
74 r'\d+'
75 try:
76 t.value = int(t.value)
77 except ValueError:
af36987 misc
cantora authored
78 pyc_log.log('integer value invalid: %s' % t.value)
8691675 added ply distribution to makefile
cantora authored
79 t.value = 0
80
81 return t
82
83 t_ignore = ' \t'
84
85 def t_newline(t):
200dc24 misc parser features
cantora authored
86 r'(\x0d?\x0a)+'
8691675 added ply distribution to makefile
cantora authored
87 t.lexer.lineno += t.value.count("\n")
32f2ffe started parser using PLY
cantora authored
88
b32362e ignore '\\\n'
cantora authored
89 t_ignore_ESC_NEWLINE = r'\\\x0d?\x0a'
90
8691675 added ply distribution to makefile
cantora authored
91 def t_error(t):
af36987 misc
cantora authored
92 pyc_log.log("illegal character '%s'" % t.value[0])
32f2ffe started parser using PLY
cantora authored
93
8691675 added ply distribution to makefile
cantora authored
94
95 import pyc_util
96 pyc_util.add_relative_dir_to_syspath("ply")
97 import ply.lex
98
576ccef turn off other debugging modes
cantora authored
99 #ply.lex.lex(debug = pyc_log.isverbose())
100 ply.lex.lex()
8691675 added ply distribution to makefile
cantora authored
101
32f2ffe started parser using PLY
cantora authored
102 import compiler
61e17fa shift/reduce, p_module refactoring
cantora authored
103
32f2ffe started parser using PLY
cantora authored
104 precedence = (
105 ('left', 'PLUS', 'MINUS'),
61e17fa shift/reduce, p_module refactoring
cantora authored
106 ('right', 'UMINUS'),
32f2ffe started parser using PLY
cantora authored
107 )
108
109
a157cd2 shift reduce fixes
cantora authored
110 def p_module(m):
111 'module : statement_list'
112 m[0] = compiler.ast.Module(None, m[1])
200dc24 misc parser features
cantora authored
113
32f2ffe started parser using PLY
cantora authored
114
a157cd2 shift reduce fixes
cantora authored
115 def p_statement_list(sl):
116 'statement_list : statement statement_list'
117 sl[0] = sl[2]
118 sl[0].nodes.insert(0, sl[1])
61e17fa shift/reduce, p_module refactoring
cantora authored
119
a157cd2 shift reduce fixes
cantora authored
120 def p_empty(t):
121 'empty : '
122 pass
61e17fa shift/reduce, p_module refactoring
cantora authored
123
a157cd2 shift reduce fixes
cantora authored
124 def p_statement_list_empty(sl):
125 'statement_list : empty'
126 sl[0] = compiler.ast.Stmt([])
32f2ffe started parser using PLY
cantora authored
127
128 def p_statement(t):
129 '''statement : stmt SEMI
200dc24 misc parser features
cantora authored
130 | stmt '''
32f2ffe started parser using PLY
cantora authored
131
132 t[0] = t[1]
133
34cb2f7 assign expr, call expr, ident expr
cantora authored
134 def p_print_stmt(t):
135 'stmt : PRINT expr'
32f2ffe started parser using PLY
cantora authored
136 t[0] = compiler.ast.Printnl([t[2]], None)
137
34cb2f7 assign expr, call expr, ident expr
cantora authored
138
139 def p_assign_stmt(t):
140 'stmt : IDENT "=" expr'
141
142 t[0] = compiler.ast.Assign(
143 [compiler.ast.AssName(t[1], 'OP_ASSIGN')],
144 t[3]
145 )
146
200dc24 misc parser features
cantora authored
147 def p_discard(t):
148 'stmt : expr'
149
150 t[0] = compiler.ast.Discard(t[1])
151
34cb2f7 assign expr, call expr, ident expr
cantora authored
152 def p_call_expr(t):
45b4407 undo last commit on pyc_ply_parser.py
cantora authored
153 'expr : IDENT "(" ")"'
154
34cb2f7 assign expr, call expr, ident expr
cantora authored
155 t[0] = compiler.ast.CallFunc(compiler.ast.Name(t[1]), [])
200dc24 misc parser features
cantora authored
156
157
158 def p_paren_expr(t):
45b4407 undo last commit on pyc_ply_parser.py
cantora authored
159 'expr : "(" expr ")"'
200dc24 misc parser features
cantora authored
160
161 t[0] = t[2]
34cb2f7 assign expr, call expr, ident expr
cantora authored
162
163 def p_ident_expr(t):
164 'expr : IDENT'
165
166 t[0] = compiler.ast.Name(t[1])
167
168 def p_plus_expr(t):
169 'expr : expr PLUS expr'
32f2ffe started parser using PLY
cantora authored
170 t[0] = compiler.ast.Add( (t[1], t[3]) )
171
34cb2f7 assign expr, call expr, ident expr
cantora authored
172 def p_neg_expr(t):
173 'expr : MINUS expr %prec UMINUS'
32f2ffe started parser using PLY
cantora authored
174 t[0] = compiler.ast.UnarySub(t[2])
175
34cb2f7 assign expr, call expr, ident expr
cantora authored
176 def p_int_expr(t):
177 'expr : INT'
32f2ffe started parser using PLY
cantora authored
178 t[0] = compiler.ast.Const(t[1])
179
180 def p_error(t):
200dc24 misc parser features
cantora authored
181 try:
182 lineno = t.lineno
183 except AttributeError:
184 lineno = 0
185
186 raise Exception("syntax error at line %d near: '%s'" % (lineno, t.value) )
32f2ffe started parser using PLY
cantora authored
187
188 import ply.yacc
189
1b7bf69 turn off debug mode on lexer
cantora authored
190 ply.yacc.yacc()
34cb2f7 assign expr, call expr, ident expr
cantora authored
191
32f2ffe started parser using PLY
cantora authored
192
193 def parse(src):
34cb2f7 assign expr, call expr, ident expr
cantora authored
194 # create console handler and set level to debug
af36987 misc
cantora authored
195
576ccef turn off other debugging modes
cantora authored
196 #return ply.yacc.parse(src, debug=pyc_log.log_obj)
197 return ply.yacc.parse(src)
Something went wrong with that request. Please try again.