1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
import re
from typing import Any
from .bootstrap import EBNFBootstrapParser
from .buffering import Buffer
from .grammars import PRAGMA_RE
from .parser_semantics import EBNFGrammarSemantics
from .parserconfig import ParserConfig
from .semantics import ASTSemantics
class EBNFBuffer(Buffer):
def __init__(
self,
text,
/,
filename=None,
config: ParserConfig | None = None,
**settings: Any,
):
config = ParserConfig.new(
config=config, 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,
name: str | None = None,
config: ParserConfig | None = None,
semantics=None,
**settings: Any,
):
if semantics is None:
semantics = ASTSemantics()
config = ParserConfig.new(
config=config,
name=name,
semantics=semantics,
tokenizercls=EBNFBuffer,
**settings,
)
super().__init__(config)
class GrammarGenerator(EBNFBootstrapParser):
def __init__(
self,
name: str | None = None,
config: ParserConfig | None = None,
semantics=None,
**settings: Any,
):
if semantics is None:
semantics = EBNFGrammarSemantics(name)
config = ParserConfig.new(
config=config,
name=name,
semantics=semantics,
tokenizercls=EBNFBuffer,
**settings,
)
super().__init__(config)
|