Пример #1
0
 def name_gather(self, node: Gather) -> str:
     self.counter += 1
     name = f"_gather_{self.counter}"
     self.counter += 1
     extra_function_name = f"_loop0_{self.counter}"
     extra_function_alt = Alt(
         [
             NamedItem(None, node.separator),
             NamedItem("elem", node.node),
         ],
         action="elem",
     )
     self.todo[extra_function_name] = Rule(
         extra_function_name,
         None,
         Rhs([extra_function_alt]),
     )
     alt = Alt([
         NamedItem("elem", node.node),
         NamedItem("seq", NameLeaf(extra_function_name)),
     ], )
     self.todo[name] = Rule(
         name,
         None,
         Rhs([alt]),
     )
     return name
Пример #2
0
 def name_loop(self, node: Plain, is_repeat1: bool) -> str:
     self.counter += 1
     if is_repeat1:
         prefix = '_loop1_'
     else:
         prefix = '_loop0_'
     name = f'{prefix}{self.counter}'  # TODO: It's ugly to signal via the name.
     self.todo[name] = Rule(name, None, Rhs([Alt([NamedItem(None, node)])]))
     return name
Пример #3
0
 def artificial_rule_from_repeat(self, node: Plain,
                                 is_repeat1: bool) -> str:
     self.counter += 1
     if is_repeat1:
         prefix = "_loop1_"
     else:
         prefix = "_loop0_"
     name = f"{prefix}{self.counter}"
     self.all_rules[name] = Rule(name, None,
                                 Rhs([Alt([NamedItem(None, node)])]))
     return name
Пример #4
0
 def __init__(
     self,
     grammar: grammar.Grammar,
     file: Optional[IO[Text]],
     *,
     tokens: Dict[int, str] = token.tok_name,
     skip_actions: bool = False,
 ):
     keywords = grammar.metas.get("keywords")
     self.use_reserved_words = self.parse_bool(keywords, "keywords", True)
     if skip_actions and ("start" not in grammar.rules
                          and "trailer" not in grammar.metas):
         first_rule = next(iter(grammar.rules))
         grammar.rules["start"] = Rule(
             "start", None,
             Rhs([Alt([NamedItem(None, NameLeaf(first_rule))])]))
     super().__init__(grammar, tokens, file)
     self.skip_actions = skip_actions
     self.callmakervisitor: PythonCallMakerVisitor = PythonCallMakerVisitor(
         self)
Пример #5
0
        return None

    @memoize
    def rule(self) -> Optional[Rule]:
        # rule: rulename memoflag? ":" alts NEWLINE INDENT more_alts DEDENT | rulename memoflag? ":" NEWLINE INDENT more_alts DEDENT | rulename memoflag? ":" alts NEWLINE
        mark = self.mark()
        cut = False
        if ((rulename := self.rulename()) and (opt := self.memoflag(), )
                and (literal := self.expect(":")) and (alts := self.alts())
                and (newline_ := self.expect('NEWLINE'))
                and (indent_ := self.expect('INDENT'))
                and (more_alts := self.more_alts())
                and (dedent_ := self.expect('DEDENT'))):
            return Rule(rulename[0],
                        rulename[1],
                        Rhs(alts.alts + more_alts.alts),
                        memo=opt)
        self.reset(mark)
        if cut: return None
        cut = False
        if ((rulename := self.rulename()) and (opt := self.memoflag(), )
                and (literal := self.expect(":"))
                and (newline_ := self.expect('NEWLINE'))
                and (indent_ := self.expect('INDENT'))
                and (more_alts := self.more_alts())
                and (dedent_ := self.expect('DEDENT'))):
            return Rule(rulename[0], rulename[1], more_alts, memo=opt)
        self.reset(mark)
        if cut: return None
        cut = False
        if ((rulename := self.rulename()) and (opt := self.memoflag(), )
Пример #6
0
 if (
     (rulename := self.rulename())
     and
     (literal := self.expect(":"))
     and
     (alts := self.alts())
     and
     (newline := self.expect('NEWLINE'))
     and
     (indent := self.expect('INDENT'))
     and
     (more_alts := self.more_alts())
     and
     (dedent := self.expect('DEDENT'))
 ):
     return Rule ( rulename [ 0 ] , rulename [ 1 ] , Rhs ( alts . alts + more_alts . alts ) )
 self.reset(mark)
 if cut: return None
 cut = False
 if (
     (rulename := self.rulename())
     and
     (literal := self.expect(":"))
     and
     (newline := self.expect('NEWLINE'))
     and
     (indent := self.expect('INDENT'))
     and
     (more_alts := self.more_alts())
     and
     (dedent := self.expect('DEDENT'))