Type things in the table so we can do exhaustive action matches
This commit is contained in:
parent
973445a8d8
commit
3c8ab86a90
1 changed files with 8 additions and 5 deletions
13
parser.py
13
parser.py
|
|
@ -418,6 +418,9 @@ class Error(Action):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
ParseAction = Reduce | Shift | Accept | Error
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class PossibleAction:
|
class PossibleAction:
|
||||||
name: str
|
name: str
|
||||||
|
|
@ -555,13 +558,13 @@ class ErrorCollection:
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class ParseTable:
|
class ParseTable:
|
||||||
actions: list[dict[str, Action]]
|
actions: list[dict[str, ParseAction]]
|
||||||
gotos: list[dict[str, int]]
|
gotos: list[dict[str, int]]
|
||||||
|
|
||||||
def format(self):
|
def format(self):
|
||||||
"""Format a parser table so pretty."""
|
"""Format a parser table so pretty."""
|
||||||
|
|
||||||
def format_action(actions: dict[str, Action], terminal: str):
|
def format_action(actions: dict[str, ParseAction], terminal: str):
|
||||||
action = actions.get(terminal)
|
action = actions.get(terminal)
|
||||||
match action:
|
match action:
|
||||||
case Accept():
|
case Accept():
|
||||||
|
|
@ -612,13 +615,13 @@ class TableBuilder(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
errors: ErrorCollection
|
errors: ErrorCollection
|
||||||
actions: list[dict[str, Action]]
|
actions: list[dict[str, ParseAction]]
|
||||||
gotos: list[dict[str, int]]
|
gotos: list[dict[str, int]]
|
||||||
alphabet: list[str]
|
alphabet: list[str]
|
||||||
precedence: typing.Tuple[typing.Tuple[Assoc, int], ...]
|
precedence: typing.Tuple[typing.Tuple[Assoc, int], ...]
|
||||||
transparents: set[str]
|
transparents: set[str]
|
||||||
|
|
||||||
action_row: None | list[typing.Tuple[None | Action, None | Configuration]]
|
action_row: None | list[typing.Tuple[None | ParseAction, None | Configuration]]
|
||||||
goto_row: None | list[None | int]
|
goto_row: None | list[None | int]
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
@ -707,7 +710,7 @@ class TableBuilder(object):
|
||||||
else:
|
else:
|
||||||
return self.precedence[config.core.name]
|
return self.precedence[config.core.name]
|
||||||
|
|
||||||
def _set_table_action(self, symbol_id: int, action: Action, config: Configuration | None):
|
def _set_table_action(self, symbol_id: int, action: ParseAction, config: Configuration | None):
|
||||||
"""Set the action for 'symbol' in the table row to 'action'.
|
"""Set the action for 'symbol' in the table row to 'action'.
|
||||||
|
|
||||||
This is destructive; it changes the table. It records an error if
|
This is destructive; it changes the table. It records an error if
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue