parser: Shorter code for groups

Add a "grammar" property on AstNode types so they can be used in grammar
expressions as groups
This commit is contained in:
James Westman 2022-01-17 00:04:26 -06:00
parent 8d587b62a0
commit 76f7befd68
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
10 changed files with 119 additions and 159 deletions

View file

@ -24,7 +24,6 @@ import typing as T
from collections import defaultdict
from enum import Enum
from .ast import AstNode
from .errors import assert_true, CompilerBugError, CompileError, UnexpectedTokenError
from .tokenizer import Token, TokenType
@ -77,7 +76,7 @@ class ParseGroup:
self.keys[key] = val
self.tokens[key] = token
def to_ast(self) -> AstNode:
def to_ast(self):
""" Creates an AST node from the match group. """
children = [child.to_ast() for child in self.children]
@ -522,5 +521,7 @@ def to_parse_node(value) -> ParseNode:
return Match(value)
elif isinstance(value, list):
return Sequence(*value)
elif isinstance(value, type) and hasattr(value, "grammar"):
return Group(value, getattr(value, "grammar"))
else:
return value