Exemplo n.º 1
0
    def nonterminal(self, nt, args):
        n = len(args)

        # # Use this to find lots of singleton rule
        # if n == 1 and nt not in self.singleton:
        #     print("XXX", nt)

        if nt in self.collect and n > 1:
            #
            #  Collect iterated thingies together. That is rather than
            #  stmts -> stmts stmt -> stmts stmt -> ...
            #  stmms -> stmt stmt ...
            #
            if not hasattr(args[0], "append"):
                # Was in self.optional_nt as a single item, but we find we have
                # more than one now...
                rv = GenericASTBuilder.nonterminal(self, nt, [args[0]])
            else:
                rv = args[0]
                pass
            # In a  list-like entity where the first item goes to epsilon,
            # drop that and save the 2nd item as the first one
            if len(rv) == 0 and nt not in self.keep_epsilon:
                rv = args[1]
            else:
                rv.append(args[1])
        elif n == 1 and args[0] in self.singleton:
            rv = GenericASTBuilder.nonterminal(self, nt, args[0])
            del args[0]  # save memory
        elif n == 1 and nt in self.optional_nt:
            rv = args[0]
        else:
            rv = GenericASTBuilder.nonterminal(self, nt, args)
        return rv
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 9
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
Exemplo n.º 10
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', 'comma_fpdef_opt_eqtests',
                   )
        # 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 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
Exemplo n.º 11
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
Exemplo n.º 12
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