-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove defaults for regular expressions for comments (#314)
fixes #312 * [codegen] remder REs for comments into the tokenizer/buffer and parser * [codegen] do not render a default tokenizer/buffer * [config] do not set defaults for comment REs * [parser] upgrade to the ParserConfig protocol * [test] add unit test for no default comment regexes
- Loading branch information
Showing
13 changed files
with
92 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[mypy] | ||
python_version = 3.9 | ||
python_version = 3.12 | ||
ignore_missing_imports = True | ||
exclude = parsers|docs|build|tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,67 @@ | ||
from __future__ import annotations | ||
import re | ||
from typing import Any | ||
|
||
from tatsu.bootstrap import EBNFBootstrapParser | ||
from tatsu.semantics import ASTSemantics | ||
from tatsu.parser_semantics import EBNFGrammarSemantics | ||
from tatsu.grammars import EBNFBuffer | ||
from .infos import ParserConfig | ||
from .buffering import Buffer | ||
from .grammars import PRAGMA_RE | ||
from .semantics import ASTSemantics | ||
from .parser_semantics import EBNFGrammarSemantics | ||
from .bootstrap import EBNFBootstrapParser | ||
|
||
|
||
class EBNFBuffer(Buffer): | ||
def __init__(self, text, /, filename=None, config: ParserConfig|None = None, **settings: Any): | ||
config = ParserConfig.new( | ||
config=config, | ||
owner=self, | ||
filename=filename, | ||
**settings) | ||
super().__init__(text, config=config) | ||
|
||
def process_block(self, name, lines, index, **kwargs): | ||
i = 0 | ||
while i < len(lines): | ||
line = lines[i] | ||
if re.match(PRAGMA_RE, line): | ||
directive, arg = line.split('#', 1)[1], '' | ||
if '::' in directive: | ||
directive, arg = directive.split('::', 1) | ||
directive, arg = directive.strip(), arg.strip() | ||
i = self.pragma(name, directive, arg, lines, index, i) | ||
else: | ||
i += 1 | ||
return lines, index | ||
|
||
def pragma(self, source, name, arg, lines, index, i): | ||
# we only recognize the 'include' pragama | ||
if name == 'include': | ||
filename = arg.strip('\'"') | ||
return self.include_file(source, filename, lines, index, i, i + 1) | ||
else: | ||
return i + 1 # will be treated as a directive by the parser | ||
|
||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters