Beispiel #1
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)
        mark = self.mark()
        cut = False
        if ((grammar := self.grammar())
                and (endmarker_ := self.expect('ENDMARKER'))):
            return grammar
        self.reset(mark)
        if cut: return None
        return None

    @memoize
    def grammar(self) -> Optional[Grammar]:
        # grammar: metas rules | rules
        mark = self.mark()
        cut = False
        if ((metas := self.metas()) and (rules := self.rules())):
            return Grammar(rules, metas)
        self.reset(mark)
        if cut: return None
        cut = False
        if ((rules := self.rules())):
            return Grammar(rules, [])
        self.reset(mark)
        if cut: return None
        return None

    @memoize
    def metas(self) -> Optional[MetaList]:
        # metas: meta metas | meta
        mark = self.mark()
        cut = False
        if ((meta := self.meta()) and (metas := self.metas())):