Skip to content

Commit

Permalink
[grammars] move EBNFBuffer to parser.py
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Oct 13, 2023
1 parent 33df2ca commit 82a1165
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 43 deletions.
37 changes: 0 additions & 37 deletions tatsu/grammars.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from .ast import AST
from .contexts import ParseContext
from .objectmodel import Node
from .bootstrap import EBNFBootstrapBuffer
from .infos import RuleInfo, ParserConfig
from .leftrec import Nullable, find_left_recursion
from .collections import OrderedSet as oset
Expand Down Expand Up @@ -49,42 +48,6 @@ def pythonize_name(name):
return ''.join('_' + c.lower() if c.isupper() else c for c in name)


class EBNFBuffer(EBNFBootstrapBuffer):
def __init__(
self, text, filename=None, comments_re=None, eol_comments_re=None, **kwargs):
super().__init__(
text,
filename=filename,
memoize_lookaheads=False,
comment_recovery=True,
comments_re=comments_re,
eol_comments_re=eol_comments_re,
**kwargs
)

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 ModelContext(ParseContext):
def __init__(self, rules, /, start=None, config: ParserConfig|None = None, **settings):
config = ParserConfig.new(config, **settings)
Expand Down
48 changes: 44 additions & 4 deletions tatsu/parser.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
from __future__ import annotations

from tatsu.bootstrap import EBNFBootstrapParser
from tatsu.semantics import ASTSemantics
from tatsu.parser_semantics import EBNFGrammarSemantics
from tatsu.grammars import EBNFBuffer
import re

from .bootstrap import EBNFBootstrapParser, EBNFBootstrapBuffer
from .grammars import PRAGMA_RE
from .semantics import ASTSemantics
from .parser_semantics import EBNFGrammarSemantics


COMMENTS_RE = r'(?sm)[(][*](?:.|\n)*?[)][*])'
EOL_COMMENTS_RE = r'#([^\n]*?)$'


class EBNFBuffer(EBNFBootstrapBuffer):
def __init__(
self, text, filename=None, **kwargs):
super().__init__(
text,
filename=filename,
memoize_lookaheads=False,
comment_recovery=True,
# comments_re=COMMENTS_RE,
# eol_comments_re=EOL_COMMENTS_RE,
**kwargs
)

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):
if semantics is None:
Expand Down
2 changes: 1 addition & 1 deletion test/grammar/syntax_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tatsu import tool
from tatsu.util import trim
from tatsu.codegen import codegen
from tatsu.grammars import EBNFBuffer
from tatsu.parser import EBNFBuffer


class SyntaxTests(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion test/parsing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import tatsu
from tatsu.util import trim, eval_escapes, asjson
from tatsu.grammars import EBNFBuffer
from tatsu.parser import EBNFBuffer


class MockIncludeBuffer(EBNFBuffer):
Expand Down

0 comments on commit 82a1165

Please sign in to comment.