Allow the generator to be a little more declarative

This commit is contained in:
John Doty 2024-06-01 05:57:16 -07:00
parent e203e27407
commit 03937e62e6
2 changed files with 16 additions and 4 deletions

View file

@ -54,10 +54,11 @@ RSQUARE = Terminal("RightBracket")
class FineGrammar(Grammar):
generator = GenerateLALR
start = "File"
def __init__(self):
super().__init__(
start="File",
precedence=[
(Assoc.RIGHT, [EQUAL]),
(Assoc.LEFT, [OR]),
@ -81,7 +82,6 @@ class FineGrammar(Grammar):
#
(Assoc.NONE, [self.is_expression]),
],
generator=GenerateLALR,
)
@rule("File")

View file

@ -1854,14 +1854,26 @@ class Grammar:
def __init__(
self,
start: str,
start: str | None = None,
precedence: PrecedenceList | None = None,
generator: type[GenerateLR0] = GenerateLALR,
generator: type[GenerateLR0] | None = None,
):
if start is None:
start = getattr(self, "start", None)
if start is None:
raise ValueError(
"The default start rule must either be specified in the constructor or as an "
"attribute in the class."
)
if precedence is None:
precedence = getattr(self, "precedence", [])
assert precedence is not None
if generator is None:
generator = getattr(self, "generator", GenerateLALR)
assert generator is not None
precedence_table = {}
for prec, (associativity, symbols) in enumerate(precedence):
for symbol in symbols: