Source

uncompyle / uncompyle / Parser.py

Full commit
  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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#  Copyright (c) 1999 John Aycock
#  Copyright (c) 2000-2002 by hartmut Goebel <hartmut@goebel.noris.de>
#  Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
#
#  See main module for license.
#

__all__ = ['parse', 'AST', 'ParserError', 'Parser']

from spark import GenericASTBuilder
import string, exceptions, sys
from UserList import UserList

from Scanner import Token

class AST(UserList):
    def __init__(self, type, kids=[]):
        self.type = intern(type)
        UserList.__init__(self, kids)

    def __getslice__(self, low, high):    return self.data[low:high]
    def __eq__(self, o):
        if isinstance(o, AST):
            return self.type == o.type \
                   and UserList.__eq__(self, o)
        else:
            return self.type == o

    def __hash__(self):            return hash(self.type)

    def __repr__(self, indent=''):
        rv = str(self.type)
        for k in self:
            rv = rv + '\n' + string.replace(str(k), '\n', '\n   ')
        return rv


class ParserError(Exception):
    def __init__(self, token, offset):
        self.token = token
        self.offset = offset

    def __str__(self):
        return "Syntax error at or near `%r' token at offset %s" % \
               (self.token, self.offset)
    

class Parser(GenericASTBuilder):
    def __init__(self):
        GenericASTBuilder.__init__(self, AST, 'stmts')
        self.customized = {}

    def cleanup(self):
        """
        Remove recursive references to allow garbage
        collector to collect this object.
        """
        for dict in (self.rule2func, self.rules, self.rule2name, self.first):
            for i in dict.keys():
                dict[i] = None
        for i in dir(self):
            setattr(self, i, None)

    def error(self, token):
            raise ParserError(token, token.offset)

    def typestring(self, token):
        return token.type
    
    def p_funcdef(self, args):
        '''
        stmt ::= funcdef
        funcdef ::= mkfunc designator
        load_closure ::= load_closure LOAD_CLOSURE
        load_closure ::= LOAD_CLOSURE
        '''

    def p_list_comprehension(self, args):
        '''
        expr ::= list_compr
        list_compr ::= BUILD_LIST_0 DUP_TOP LOAD_ATTR
                designator list_iter del_stmt

        list_compr ::= BUILD_LIST_0 DUP_TOP
                designator list_iter del_stmt

        list_iter ::= list_for
        list_iter ::= list_if
        list_iter ::= lc_body

        _come_from ::= COME_FROM
        _come_from ::= 

        list_for ::= expr _for designator list_iter
                _come_from JUMP_ABSOLUTE _come_from
        list_if ::= expr condjmp list_iter
                _jump _25_come_froms

        lc_body ::= LOAD_NAME expr CALL_FUNCTION_1 POP_TOP
        lc_body ::= LOAD_FAST expr CALL_FUNCTION_1 POP_TOP

        lc_body ::= LOAD_NAME expr LIST_APPEND
        lc_body ::= LOAD_FAST expr LIST_APPEND
        '''
        
    def p_augmented_assign(self, args):
        '''
        stmt ::= augassign1
        stmt ::= augassign2
        augassign1 ::= expr expr inplace_op designator
        augassign1 ::= expr expr inplace_op ROT_THREE STORE_SUBSCR
        augassign1 ::= expr expr inplace_op ROT_TWO   STORE_SLICE+0
        augassign1 ::= expr expr inplace_op ROT_THREE STORE_SLICE+1
        augassign1 ::= expr expr inplace_op ROT_THREE STORE_SLICE+2
        augassign1 ::= expr expr inplace_op ROT_FOUR  STORE_SLICE+3
        augassign2 ::= expr DUP_TOP LOAD_ATTR expr
                inplace_op ROT_TWO   STORE_ATTR

        inplace_op ::= INPLACE_ADD
        inplace_op ::= INPLACE_SUBTRACT
        inplace_op ::= INPLACE_MULTIPLY
        inplace_op ::= INPLACE_DIVIDE
        inplace_op ::= INPLACE_TRUE_DIVIDE
        inplace_op ::= INPLACE_FLOOR_DIVIDE
        inplace_op ::= INPLACE_MODULO
        inplace_op ::= INPLACE_POWER
        inplace_op ::= INPLACE_LSHIFT
        inplace_op ::= INPLACE_RSHIFT
        inplace_op ::= INPLACE_AND
        inplace_op ::= INPLACE_XOR
        inplace_op ::= INPLACE_OR 
        '''

    def p_assign(self, args):
        '''
        stmt ::= assign
        assign ::= expr DUP_TOP designList
        assign ::= expr designator

        stmt ::= _25_assign2
        stmt ::= _25_assign3
        _25_assign2 ::= expr expr ROT_TWO designator designator
        _25_assign3 ::= expr expr expr ROT_THREE ROT_TWO designator designator designator
        '''

    def p_print(self, args):
        '''
        stmt ::= print_stmt
        stmt ::= print_stmt_nl
        stmt ::= print_nl_stmt
        print_stmt ::= expr PRINT_ITEM
        print_nl_stmt ::= PRINT_NEWLINE
        print_stmt_nl ::= print_stmt print_nl_stmt
        '''

    def p_print_to(self, args):
        '''
        stmt ::= print_to
        stmt ::= print_to_nl
        stmt ::= print_nl_to
        print_to ::= expr print_to_items POP_TOP
        print_to_nl ::= expr print_to_items PRINT_NEWLINE_TO
        print_nl_to ::= expr PRINT_NEWLINE_TO
        print_to_items ::= print_to_items print_to_item
        print_to_items ::= print_to_item
        print_to_item ::= DUP_TOP expr ROT_TWO PRINT_ITEM_TO
        '''
        # expr   print_to*   POP_TOP
        # expr { print_to* } PRINT_NEWLINE_TO

    def p_import15(self, args):
        '''
        stmt ::= importstmt
        stmt ::= importfrom

        importstmt ::= IMPORT_NAME STORE_FAST
        importstmt ::= IMPORT_NAME STORE_NAME

        importfrom ::= IMPORT_NAME importlist POP_TOP
        importlist ::= importlist IMPORT_FROM
        importlist ::= IMPORT_FROM
        '''

    def p_import20(self, args):
        '''
        stmt ::= importstmt2
        stmt ::= importfrom2
        stmt ::= importstar2
        
        stmt ::= _25_importstmt
        stmt ::= _25_importfrom
        stmt ::= _25_importstar

        importstmt2 ::= LOAD_CONST import_as
        importstar2 ::= LOAD_CONST IMPORT_NAME IMPORT_STAR

        importfrom2 ::= LOAD_CONST IMPORT_NAME importlist2 POP_TOP
        importlist2 ::= importlist2 import_as
        importlist2 ::= import_as
        import_as ::= IMPORT_NAME designator
        import_as ::= IMPORT_NAME LOAD_ATTR designator
        import_as ::= IMPORT_FROM designator

        _25_importstmt ::= LOAD_CONST LOAD_CONST import_as 
        _25_importstar ::= LOAD_CONST LOAD_CONST IMPORT_NAME IMPORT_STAR 
        _25_importfrom ::= LOAD_CONST LOAD_CONST IMPORT_NAME importlist2 POP_TOP
        '''

    def p_grammar(self, args):
        '''
        stmts ::= stmts stmt
        stmts ::= stmt

        stmts_opt ::= stmts
        stmts_opt ::= passstmt
        passstmt ::= 

        designList ::= designator designator
        designList ::= designator DUP_TOP designList

        designator ::= STORE_FAST
        designator ::= STORE_NAME
        designator ::= STORE_GLOBAL
        designator ::= STORE_DEREF
        designator ::= expr STORE_ATTR
        designator ::= expr STORE_SLICE+0
        designator ::= expr expr STORE_SLICE+1
        designator ::= expr expr STORE_SLICE+2
        designator ::= expr expr expr STORE_SLICE+3
        designator ::= store_subscr
        store_subscr ::= expr expr STORE_SUBSCR
        designator ::= unpack
        designator ::= unpack_list
        
        stmt ::= classdef
        stmt ::= call_stmt
        call_stmt ::= expr POP_TOP

        stmt ::= return_stmt
        return_stmt ::= expr RETURN_VALUE

        stmt ::= yield_stmt
        yield_stmt ::= expr YIELD_STMT
        yield_stmt ::= expr YIELD_STMT POP_TOP

        yield_stmt ::= expr YIELD_VALUE
        yield_stmt ::= expr YIELD_VALUE POP_TOP

        stmt ::= break_stmt
        break_stmt ::= BREAK_LOOP
        
        stmt ::= continue_stmt
        continue_stmt ::= JUMP_ABSOLUTE
        continue_stmt ::= CONTINUE_LOOP
        
        stmt ::= raise_stmt
        raise_stmt ::= exprlist RAISE_VARARGS
        raise_stmt ::= nullexprlist RAISE_VARARGS
        
        stmt ::= exec_stmt
        exec_stmt ::= expr exprlist DUP_TOP EXEC_STMT
        exec_stmt ::= expr exprlist EXEC_STMT
        
        stmt ::= assert
        stmt ::= assert2
        stmt ::= assert3
        stmt ::= assert4
        stmt ::= ifstmt
        stmt ::= ifelsestmt
        
        stmt ::= _25_ifstmt
        stmt ::= _25_ifelsestmt
        
        stmt ::= whilestmt
        stmt ::= while1stmt
        stmt ::= whileelsestmt
        stmt ::= while1elsestmt
        stmt ::= forstmt
        stmt ::= forelsestmt
        stmt ::= trystmt
        stmt ::= tryfinallystmt
        
        stmt ::= del_stmt
        del_stmt ::= DELETE_FAST
        del_stmt ::= DELETE_NAME
        del_stmt ::= DELETE_GLOBAL
        del_stmt ::= expr DELETE_SLICE+0
        del_stmt ::= expr expr DELETE_SLICE+1
        del_stmt ::= expr expr DELETE_SLICE+2
        del_stmt ::= expr expr expr DELETE_SLICE+3
        del_stmt ::= delete_subscr
        delete_subscr ::= expr expr DELETE_SUBSCR
        del_stmt ::= expr DELETE_ATTR
        
        kwarg   ::= LOAD_CONST expr
        
        classdef ::= LOAD_CONST expr mkfunc
                    CALL_FUNCTION_0 BUILD_CLASS designator

        condjmp    ::= JUMP_IF_FALSE POP_TOP
        condjmp    ::= JUMP_IF_TRUE  POP_TOP

        assert ::= expr JUMP_IF_FALSE POP_TOP
                expr JUMP_IF_TRUE POP_TOP
                LOAD_GLOBAL RAISE_VARARGS
                COME_FROM COME_FROM POP_TOP
        assert2 ::= expr JUMP_IF_FALSE POP_TOP
                expr JUMP_IF_TRUE POP_TOP
                LOAD_GLOBAL expr RAISE_VARARGS
                COME_FROM COME_FROM POP_TOP
        assert3 ::= expr JUMP_IF_TRUE POP_TOP
                LOAD_GLOBAL RAISE_VARARGS
                COME_FROM POP_TOP
        assert4 ::= expr JUMP_IF_TRUE POP_TOP
                LOAD_GLOBAL expr RAISE_VARARGS
                COME_FROM POP_TOP

        stmt ::= ifforstmt
        ifforstmt ::= expr condjmp SETUP_LOOP expr _for designator
        stmts_opt JUMP_ABSOLUTE
        COME_FROM POP_BLOCK _jump COME_FROM
        POP_TOP COME_FROM COME_FROM

        _jump ::= JUMP_ABSOLUTE
        _jump ::= JUMP_FORWARD

        jmp_false    ::= JUMP_IF_FALSE POP_TOP
        jmp_true    ::= JUMP_IF_TRUE  POP_TOP
        ifstmt ::= expr jmp_false stmts_opt
                _jump COME_FROM POP_TOP COME_FROM

        ifstmt ::= expr jmp_false stmts_opt
                _jump COME_FROM POP_TOP COME_FROM

        ifelsestmt ::= expr jmp_false stmts_opt
                _jump COME_FROM
                POP_TOP stmts COME_FROM

        _25_ifstmt ::= _25_bexp _25_jumptarget_e _jump _25_jumptarget_e COME_FROM
        _25_ifelsestmt ::= _25_bexp _25_jumptarget_e _jump _25_jumptarget_s COME_FROM

        _25_jumptarget_e ::= _25_come_froms stmts_opt
        _25_jumptarget_s ::= _25_come_froms stmts

        _25_come_froms ::= COME_FROM _25_come_froms
        _25_come_froms ::= POP_TOP

        stmt ::= ifandstmt
        ifandstmt ::= expr JUMP_IF_FALSE POP_TOP expr JUMP_IF_FALSE POP_TOP stmt _jump COME_FROM COME_FROM POP_TOP COME_FROM
        ifandstmt ::= expr JUMP_IF_FALSE POP_TOP expr JUMP_IF_FALSE POP_TOP stmt _jump COME_FROM COME_FROM POP_TOP

        stmt ::= ifnotstmt
        stmt ::= ifnotelsestmt
        ifnotstmt ::= expr jmp_true stmts_opt
                    _jump COME_FROM POP_TOP

        ifnotstmt ::= expr jmp_true stmts_opt
                    _jump COME_FROM POP_TOP COME_FROM

        ifnotelsestmt ::= expr jmp_true stmts_opt
                    _jump COME_FROM
                    POP_TOP stmts COME_FROM

        trystmt ::= SETUP_EXCEPT stmts_opt
                POP_BLOCK _jump
                COME_FROM except_stmt

        try_end  ::= END_FINALLY COME_FROM
        try_end  ::= except_else
        except_else ::= END_FINALLY COME_FROM stmts

        except_stmt ::= except_cond except_stmt COME_FROM
        except_stmt ::= except_conds try_end COME_FROM
        except_stmt ::= except try_end COME_FROM
        except_stmt ::= try_end

        except_conds ::= except_cond except_conds COME_FROM
        except_conds ::= 

        except_cond ::= except_cond1
        except_cond ::= except_cond2
        except_cond1 ::= DUP_TOP expr COMPARE_OP
                JUMP_IF_FALSE
                POP_TOP POP_TOP POP_TOP POP_TOP
                stmts_opt _jump COME_FROM
                POP_TOP
        except_cond2 ::= DUP_TOP expr COMPARE_OP
                JUMP_IF_FALSE
                POP_TOP POP_TOP designator POP_TOP
                stmts_opt _jump COME_FROM
                POP_TOP
        except  ::=  POP_TOP POP_TOP POP_TOP
                stmts_opt _jump

        tryfinallystmt ::= SETUP_FINALLY stmts_opt
                POP_BLOCK LOAD_CONST
                COME_FROM stmts_opt END_FINALLY

        whilestmt ::= SETUP_LOOP
                expr JUMP_IF_FALSE POP_TOP
                stmts_opt JUMP_ABSOLUTE
                COME_FROM POP_TOP POP_BLOCK COME_FROM
        while1stmt ::= SETUP_LOOP
                JUMP_FORWARD JUMP_IF_FALSE POP_TOP COME_FROM
                stmts_opt JUMP_ABSOLUTE
                COME_FROM POP_TOP POP_BLOCK COME_FROM
        whileelsestmt ::= SETUP_LOOP
                          expr JUMP_IF_FALSE POP_TOP
                stmts_opt JUMP_ABSOLUTE
                COME_FROM POP_TOP POP_BLOCK
                stmts COME_FROM
        while1elsestmt ::= SETUP_LOOP
                          JUMP_FORWARD JUMP_IF_FALSE POP_TOP COME_FROM
                stmts_opt JUMP_ABSOLUTE
                COME_FROM POP_TOP POP_BLOCK
                stmts COME_FROM

        _for ::= GET_ITER FOR_ITER
        _for ::= LOAD_CONST FOR_LOOP

        forstmt ::= SETUP_LOOP expr _for designator
                stmts_opt JUMP_ABSOLUTE
                COME_FROM POP_BLOCK COME_FROM
        forelsestmt ::= SETUP_LOOP expr _for designator
                stmts_opt JUMP_ABSOLUTE
                COME_FROM POP_BLOCK stmts COME_FROM

        stmt ::= ifforelsestmt
        ifforelsestmt ::= expr condjmp SETUP_LOOP expr _for designator
        stmts_opt JUMP_ABSOLUTE
        COME_FROM POP_BLOCK _jump COME_FROM
        POP_TOP stmts COME_FROM COME_FROM

        stmt ::= ifforstmt
        ifforstmt ::= expr condjmp SETUP_LOOP expr _for designator
        stmts_opt JUMP_ABSOLUTE
        COME_FROM POP_BLOCK _jump COME_FROM
        POP_TOP COME_FROM COME_FROM

        '''

    def p_expr(self, args):
        '''
        expr ::= load_closure mklambda
        expr ::= mklambda
        expr ::= SET_LINENO
        expr ::= LOAD_FAST
        expr ::= LOAD_NAME
        expr ::= LOAD_CONST
        expr ::= LOAD_GLOBAL
        expr ::= LOAD_DEREF
        expr ::= LOAD_LOCALS
        expr ::= expr LOAD_ATTR
        expr ::= binary_expr
        expr ::= build_list
        
        binary_expr ::= expr expr binary_op
        binary_op ::= BINARY_ADD
        binary_op ::= BINARY_SUBTRACT
        binary_op ::= BINARY_MULTIPLY
        binary_op ::= BINARY_DIVIDE
        binary_op ::= BINARY_TRUE_DIVIDE
        binary_op ::= BINARY_FLOOR_DIVIDE
        binary_op ::= BINARY_MODULO
        binary_op ::= BINARY_LSHIFT
        binary_op ::= BINARY_RSHIFT
        binary_op ::= BINARY_AND
        binary_op ::= BINARY_OR
        binary_op ::= BINARY_XOR
        binary_op ::= BINARY_POWER

        expr ::= binary_subscr
        binary_subscr ::= expr expr BINARY_SUBSCR
        expr ::= expr expr DUP_TOPX_2 BINARY_SUBSCR
        expr ::= cmp
        expr ::= expr UNARY_POSITIVE
        expr ::= expr UNARY_NEGATIVE
        expr ::= expr UNARY_CONVERT
        expr ::= expr UNARY_INVERT
        expr ::= expr UNARY_NOT
        expr ::= mapexpr
        expr ::= expr SLICE+0
        expr ::= expr expr SLICE+1
        expr ::= expr expr SLICE+2
        expr ::= expr expr expr SLICE+3
        expr ::= expr DUP_TOP SLICE+0
        expr ::= expr expr DUP_TOPX_2 SLICE+1
        expr ::= expr expr DUP_TOPX_2 SLICE+2
        expr ::= expr expr expr DUP_TOPX_3 SLICE+3
        expr ::= and
        expr ::= and2
        expr ::= or
        or   ::= expr JUMP_IF_TRUE  POP_TOP expr COME_FROM
        and  ::= expr JUMP_IF_FALSE POP_TOP expr COME_FROM
        and2 ::= _jump JUMP_IF_FALSE POP_TOP COME_FROM expr COME_FROM
        
        _25_bexp ::= expr
        _25_bexp ::= _25_and
        _25_bexp ::= _25_or
        _25_bexp_end ::= _25_and_end
        _25_bexp_end ::= _25_or_end
        
        _25_and ::= expr JUMP_IF_FALSE POP_TOP expr
        _25_and ::= _25_bexp JUMP_IF_FALSE _25_come_froms _25_bexp
        _25_and ::= _25_bexp JUMP_IF_FALSE _25_come_froms _25_bexp_end
        _25_and_end ::= expr JUMP_IF_FALSE
        
        _25_or ::= expr JUMP_IF_TRUE POP_TOP expr
        _25_or ::= _25_bexp JUMP_IF_TRUE _25_come_froms _25_bexp
        _25_or ::= _25_bexp JUMP_IF_TRUE _25_come_froms _25_bexp_end
        _25_or_end ::= expr JUMP_IF_TRUE

        cmp ::= cmp_list
        cmp ::= compare
        compare ::= expr expr COMPARE_OP
        cmp_list ::= expr cmp_list1 ROT_TWO POP_TOP
                COME_FROM
        cmp_list1 ::= expr DUP_TOP ROT_THREE
                COMPARE_OP JUMP_IF_FALSE POP_TOP
                cmp_list1 COME_FROM
        cmp_list1 ::= expr DUP_TOP ROT_THREE
                COMPARE_OP JUMP_IF_FALSE POP_TOP
                cmp_list2 COME_FROM
        cmp_list2 ::= expr COMPARE_OP JUMP_FORWARD
        mapexpr ::= BUILD_MAP kvlist

        kvlist ::= kvlist kv
        kvlist ::= kvlist kv2
        kvlist ::= kvlist kv3
        kvlist ::=

        kv ::= DUP_TOP expr ROT_TWO expr STORE_SUBSCR
        kv2 ::= DUP_TOP expr expr ROT_THREE STORE_SUBSCR
        kv3 ::= expr expr STORE_MAP

        exprlist ::= exprlist expr
        exprlist ::= expr

        nullexprlist ::=
        '''

    def nonterminal(self, nt, args):
        collect = ('stmts', 'exprlist', 'kvlist')

        if nt in collect and len(args) > 1:
            #
            #  Collect iterated thingies together.
            #
            rv = args[0]
            rv.append(args[1])
        else:
            rv = GenericASTBuilder.nonterminal(self, nt, args)
        return rv

    def __ambiguity(self, children):
        # only for debugging! to be removed hG/2000-10-15
        print children
        return GenericASTBuilder.ambiguity(self, children)

    def resolve(self, list):
        if len(list) == 2 and 'funcdef' in list and 'assign' in list:
            return 'funcdef'
        #print >> sys.stderr, 'resolve', str(list)
        return GenericASTBuilder.resolve(self, list)

nop = lambda self, args: None

p = Parser()

def parse(tokens, customize):
    #
    #  Special handling for opcodes that take a variable number
    #  of arguments -- we add a new rule for each:
    #
    #    expr ::= {expr}^n BUILD_LIST_n
    #    expr ::= {expr}^n BUILD_TUPLE_n
    #    expr ::= {expr}^n BUILD_SLICE_n
    #    unpack_list ::= UNPACK_LIST {expr}^n
    #    unpack ::= UNPACK_TUPLE {expr}^n
    #    unpack ::= UNPACK_SEQEUENE {expr}^n
    #    mkfunc ::= {expr}^n LOAD_CONST MAKE_FUNCTION_n
    #    mkfunc ::= {expr}^n load_closure LOAD_CONST MAKE_FUNCTION_n
    #    expr ::= expr {expr}^n CALL_FUNCTION_n
    #    expr ::= expr {expr}^n CALL_FUNCTION_VAR_n POP_TOP
    #    expr ::= expr {expr}^n CALL_FUNCTION_VAR_KW_n POP_TOP
    #    expr ::= expr {expr}^n CALL_FUNCTION_KW_n POP_TOP
    #
    global p
    for k, v in customize.items():
        # avoid adding the same rule twice to this parser
        if p.customized.has_key(k):
            continue
        p.customized[k] = None

        #nop = lambda self, args: None
        op = k[:string.rfind(k, '_')]
        if op in ('BUILD_LIST', 'BUILD_TUPLE'):
            rule = 'build_list ::= ' + 'expr '*v + k
        elif op == 'BUILD_SLICE':
            rule = 'expr ::= ' + 'expr '*v + k
        elif op in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
            rule = 'unpack ::= ' + k + ' designator'*v
        elif op == 'UNPACK_LIST':
            rule = 'unpack_list ::= ' + k + ' designator'*v
        elif op == 'DUP_TOPX':
            # no need to add a rule
            continue
            #rule = 'dup_topx ::= ' + 'expr '*v + k
        elif op == 'MAKE_FUNCTION':
            p.addRule('mklambda ::= %s LOAD_LAMBDA %s' %
                  ('expr '*v, k), nop)
            rule = 'mkfunc ::= %s LOAD_CONST %s' % ('expr '*v, k)
        elif op == 'MAKE_CLOSURE':
            p.addRule('mklambda ::= %s load_closure LOAD_LAMBDA %s' %
                  ('expr '*v, k), nop)
            rule = 'mkfunc ::= %s load_closure LOAD_CONST %s' % ('expr '*v, k)
            rule = 'mkfunc ::= %s closure_list LOAD_CONST %s' % ('expr '*v, k)
        elif op in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR',
                'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'):
            na = (v & 0xff)           # positional parameters
            nk = (v >> 8) & 0xff      # keyword parameters
            # number of apply equiv arguments:
            nak = ( len(op)-len('CALL_FUNCTION') ) / 3
            rule = 'expr ::= expr ' + 'expr '*na + 'kwarg '*nk \
                   + 'expr ' * nak + k
        else:
            raise 'unknown customize token %s' % k
        p.addRule(rule, nop)
    ast = p.parse(tokens)
    #p.cleanup()
    return ast