Skip to content

Commit

Permalink
[parser] upgrade to the ParserConfig protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Oct 13, 2023
1 parent a0490d3 commit 82185fb
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions tatsu/parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import re
from typing import Any

from .infos import ParserConfig
from .buffering import Buffer
from .grammars import PRAGMA_RE
from .semantics import ASTSemantics
Expand All @@ -10,17 +10,13 @@


class EBNFBuffer(Buffer):
def __init__(
self, text, filename=None, comments_re=None, eol_comments_re=None, **kwargs):
super().__init__(
text,
def __init__(self, text, /, filename=None, config: ParserConfig|None = None, **settings: Any):
config = ParserConfig.new(
config=config,
owner=self,
filename=filename,
memoize_lookaheads=False,
comment_recovery=True,
comments_re=comments_re,
eol_comments_re=eol_comments_re,
**kwargs
)
**settings)
super().__init__(text, config=config)

def process_block(self, name, lines, index, **kwargs):
i = 0
Expand All @@ -46,19 +42,26 @@ def pragma(self, source, name, arg, lines, index, i):


class EBNFParser(EBNFBootstrapParser):
def __init__(self, semantics=None, **kwargs):
def __init__(self, name: str | None = None, config: ParserConfig|None = None, semantics=None, **settings: Any):
if semantics is None:
semantics = ASTSemantics()
super().__init__(semantics=semantics, **kwargs)
config = ParserConfig.new(
config=config,
name=name,
semantics=semantics,
**settings)
super().__init__(config)


class GrammarGenerator(EBNFBootstrapParser):
def __init__(self, grammar_name=None, semantics=None, parseinfo=True, **kwargs):
def __init__(self, name: str | None = None, config: ParserConfig|None = None, semantics=None, **settings: Any):
if semantics is None:
semantics = EBNFGrammarSemantics(grammar_name)
super().__init__(
semantics = EBNFGrammarSemantics(name)
config = ParserConfig.new(
config=config,
name=name,
semantics=semantics,
parseinfo=parseinfo,
tokenizercls=EBNFBuffer,
**kwargs
**settings,
)
super().__init__(config)

0 comments on commit 82185fb

Please sign in to comment.