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
|
# Copyright (c) 2017-2026 Juancarlo AƱez (apalala@gmail.com)
# SPDX-License-Identifier: BSD-4-Clause
import re
from typing import Any
from .bootstrap import TatSuBootstrapParser
from .buffering import Buffer
from .grammars import PRAGMA_RE
from .infos import ParserConfig
from .parser_semantics import TatSuGrammarSemantics
from .semantics import ASTSemantics
class TatSuBuffer(Buffer):
def __init__(
self,
text: str,
/,
filename: str | None = 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 TatSuParser(TatSuBootstrapParser):
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=TatSuBuffer,
**settings,
)
super().__init__(config)
class TatSuParserGenerator(TatSuBootstrapParser):
def __init__(self, name: str | None = None, semantics=None, **settings: Any):
if isinstance(semantics, type):
raise TypeError(
f'semantics must be an object instance or None, not class {semantics!r}',
)
if not semantics:
semantics = TatSuGrammarSemantics(name=name, context=self)
config = ParserConfig.new(
name=name,
semantics=semantics,
tokenizercls=TatSuBuffer,
**settings,
)
super().__init__(config)
|