Ejemplo n.º 1
0
    def nonterminal(self, nt, args):
        # nonterminal with a (reserved) single word derivation
        no_skip = ('pass_stmt', 'continue_stmt', 'break_stmt', 'return_stmt')

        has_len = hasattr(args, '__len__')

        if nt in self.collect and len(args) > 1:
            #
            #  Collect iterated thingies together.
            #
            rv = args[0]
            for arg in args[1:]:
                rv.append(arg)
        elif (has_len and len(args) == 1 and
              hasattr(args[0], '__len__') and args[0] not in no_skip and
              len(args[0]) == 1):
            # Remove singleton derivations
            rv = GenericASTBuilder.nonterminal(self, nt, args[0])
            del args[0] # save memory
        elif (has_len and len(args) == 2 and
              hasattr(args[1], '__len__') and len(args[1]) == 0):
            # Remove trailing epsilon rules, but only when there
            # are two items.
            if hasattr(args[0], '__len__') and len(args[0]) == 1:
                # Remove singleton derivation
                rv = args[0]
            else:
                rv = GenericASTBuilder.nonterminal(self, nt, args[:1])
            del args[1] # save memory
        else:
            rv = GenericASTBuilder.nonterminal(self, nt, args)
        return rv
Ejemplo n.º 2
0
 def nonterminal(self, nt, args):
     has_len = hasattr(args, '__len__')
     if (has_len and len(args) == 1 and
         hasattr(args[0], '__len__') and len(args[0]) == 1):
         # Remove singleton derivations
         rv = GenericASTBuilder.nonterminal(self, nt, args[0])
         del args[0] # save memory
     else:
         rv = GenericASTBuilder.nonterminal(self, nt, args)
     return rv
Ejemplo n.º 3
0
 def resolve(self, list):
     if len(list) == 2 and 'funcdef' in list and 'assign' in list:
         return 'funcdef'
     if 'grammar' in list and 'expr' in list:
         return 'expr'
     # print >> sys.stderr, 'resolve', str(list)
     return GenericASTBuilder.resolve(self, list)
Ejemplo n.º 4
0
    def nonterminal(self, nt, args):
        has_len = hasattr(args, '__len__')

        collect = ('tokens',)
        if nt in collect:
            #
            #  Collect iterated thingies together.
            #
            rv = args[0]
            for arg in args[1:]:
                rv.append(arg)

        if (has_len and len(args) == 1 and
            hasattr(args[0], '__len__') and len(args[0]) == 1):
            # Remove singleton derivations
            rv = GenericASTBuilder.nonterminal(self, nt, args[0])
            del args[0] # save memory
        else:
            rv = GenericASTBuilder.nonterminal(self, nt, args)
        return rv
Ejemplo n.º 5
0
 def nonterminal(self, nt, args):
     if nt in self.collect and len(args) > 1:
         #
         #  Collect iterated thingies together. That is rather than
         #  stmts -> stmts stmt -> stmts stmt -> ...
         #  stmms -> stmt stmt ...
         #
         rv = args[0]
         rv.append(args[1])
     else:
         rv = GenericASTBuilder.nonterminal(self, nt, args)
     return rv
Ejemplo n.º 6
0
    def nonterminal(self, nt, args):
        collect = ('stmts', 'exprlist', 'kvlist', '_stmts', 'print_items')

        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
Ejemplo n.º 7
0
    def nonterminal(self, nt, args):
        # Put left-recursive list non-terminals:
        # x ::= x y
        # x ::=
        collect = ('stmts', 'comments', 'dot_names', 'dots',
                   'comp_op_exprs', 'newline_or_stmts',
                   'comma_names'
                   )
        # nonterminal with a (reserved0 single word derivation
        no_skip = ('pass_stmt', 'continue_stmt', 'break_stmt', 'return_stmt')

        has_len = hasattr(args, '__len__')

        if nt in collect and len(args) > 1:
            #
            #  Collect iterated thingies together.
            #
            rv = args[0]
            for arg in args[1:]:
                rv.append(arg)
        elif (has_len and len(args) == 1 and
              hasattr(args[0], '__len__') and args[0] not in no_skip and
              len(args[0]) == 1):
            # Remove singleton derivations
            rv = GenericASTBuilder.nonterminal(self, nt, args[0])
            del args[0] # save memory
        elif (has_len and len(args) == 2 and
              hasattr(args[1], '__len__') and len(args[1]) == 0):
            # Remove trailing epsilon rules, but only when there
            # are two items.
            if hasattr(args[0], '__len__') and len(args[0]) == 1:
                # Remove singleton derivation
                rv = args[0]
            else:
                rv = GenericASTBuilder.nonterminal(self, nt, args[:1])
            del args[1] # save memory
        else:
            rv = GenericASTBuilder.nonterminal(self, nt, args)
        return rv
Ejemplo n.º 8
0
    def nonterminal(self, nt, args):
        collect = ('stmts', 'exprlist', 'kvlist', '_stmts', 'print_items', 'kwargs',
                   # PYPY:
                   'kvlist_n')

        if nt in collect and len(args) > 1:
            #
            #  Collect iterated thingies together. That is rather than
            #  stmts -> stmts stmt -> stmts stmt -> ...
            #  stmms -> stmt stmt ...
            #
            rv = args[0]
            rv.append(args[1])
        else:
            rv = GenericASTBuilder.nonterminal(self, nt, args)
        return rv
Ejemplo n.º 9
0
 def __ambiguity(self, children):
     # only for debugging! to be removed hG/2000-10-15
     print(children)
     return GenericASTBuilder.ambiguity(self, children)
Ejemplo n.º 10
0
 def __ambiguity(self, children):
     # only for debugging! to be removed hG/2000-10-15
     print(children)
     return GenericASTBuilder.ambiguity(self, children)