@@ -46,6 +46,9 @@ class Transformer(object):
46
46
Other:
47
47
frontend/parse_lib.py (turn on print_parse_tree)
48
48
49
+ Public methods:
50
+ Expr, OilAssign
51
+ atom, trailer, etc. are private, named after productions in grammar.pgen2.
49
52
"""
50
53
def __init__ (self , gr ):
51
54
# type: (Grammar) -> None
@@ -70,7 +73,7 @@ def _AssocBinary(self, children):
70
73
assert isinstance (op .tok , syntax_asdl .token )
71
74
return expr .Binary (op .tok , self .Expr (left ), right )
72
75
73
- def _Trailer (self , base , p_trailer ):
76
+ def trailer (self , base , p_trailer ):
74
77
# type: (expr_t, PNode) -> expr_t
75
78
children = p_trailer .children
76
79
op_tok = children [0 ].tok
@@ -105,47 +108,6 @@ def _Trailer(self, base, p_trailer):
105
108
106
109
raise AssertionError (op_tok )
107
110
108
- def OilAssign (self , pnode ):
109
- # type: (PNode) -> command__OilAssign
110
- """Transform an Oil assignment statement."""
111
- typ = pnode .typ
112
- children = pnode .children
113
-
114
- if typ == grammar_nt .oil_var :
115
- # assign: lvalue_list [type_expr] '=' testlist (';' | '\n')
116
-
117
- #log('len(children) = %d', len(children))
118
-
119
- lvalue = self .Expr (children [0 ]) # could be a tuple
120
- #log('lvalue %s', lvalue)
121
-
122
- n = len (children )
123
- if n == 4 :
124
- op_tok = children [1 ].tok
125
- rhs = children [2 ]
126
- elif n == 5 :
127
- # TODO: translate type expression
128
- op_tok = children [2 ].tok
129
- rhs = children [3 ]
130
-
131
- else :
132
- raise AssertionError (n )
133
-
134
- # The caller should fill in the keyword token.
135
- # TODO: type expression
136
- return command .OilAssign (None , lvalue , op_tok , self .Expr (rhs ))
137
-
138
- if typ == grammar_nt .oil_setvar :
139
- # oil_setvar: lvalue_list (augassign | '=') testlist (Op_Semi | Op_Newline)
140
- lvalue = self .Expr (children [0 ]) # could be a tuple
141
- op_tok = children [1 ].tok
142
- rhs = children [2 ]
143
- return command .OilAssign (None , lvalue , op_tok , self .Expr (rhs ))
144
-
145
- nt_name = self .number2symbol [typ ]
146
- raise AssertionError (
147
- "PNode type %d (%s) wasn't handled" % (typ , nt_name ))
148
-
149
111
def atom (self , children ):
150
112
# type: (List[PNode]) -> expr_t
151
113
"""Handles alternatives of 'atom' where there is more than one child."""
@@ -245,7 +207,7 @@ def Expr(self, pnode):
245
207
for i in xrange (1 , n ):
246
208
pnode = children [i ]
247
209
tok = pnode .tok
248
- base = self ._Trailer (base , pnode )
210
+ base = self .trailer (base , pnode )
249
211
250
212
return base
251
213
@@ -332,3 +294,43 @@ def Expr(self, pnode):
332
294
else :
333
295
raise AssertionError (tok .id )
334
296
297
+ def OilAssign (self , pnode ):
298
+ # type: (PNode) -> command__OilAssign
299
+ """Transform an Oil assignment statement."""
300
+ typ = pnode .typ
301
+ children = pnode .children
302
+
303
+ if typ == grammar_nt .oil_var :
304
+ # assign: lvalue_list [type_expr] '=' testlist (';' | '\n')
305
+
306
+ #log('len(children) = %d', len(children))
307
+
308
+ lvalue = self .Expr (children [0 ]) # could be a tuple
309
+ #log('lvalue %s', lvalue)
310
+
311
+ n = len (children )
312
+ if n == 4 :
313
+ op_tok = children [1 ].tok
314
+ rhs = children [2 ]
315
+ elif n == 5 :
316
+ # TODO: translate type expression
317
+ op_tok = children [2 ].tok
318
+ rhs = children [3 ]
319
+
320
+ else :
321
+ raise AssertionError (n )
322
+
323
+ # The caller should fill in the keyword token.
324
+ # TODO: type expression
325
+ return command .OilAssign (None , lvalue , op_tok , self .Expr (rhs ))
326
+
327
+ if typ == grammar_nt .oil_setvar :
328
+ # oil_setvar: lvalue_list (augassign | '=') testlist (Op_Semi | Op_Newline)
329
+ lvalue = self .Expr (children [0 ]) # could be a tuple
330
+ op_tok = children [1 ].tok
331
+ rhs = children [2 ]
332
+ return command .OilAssign (None , lvalue , op_tok , self .Expr (rhs ))
333
+
334
+ nt_name = self .number2symbol [typ ]
335
+ raise AssertionError (
336
+ "PNode type %d (%s) wasn't handled" % (typ , nt_name ))
0 commit comments