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

@ -29,24 +29,6 @@ from .extensions import OBJECT_HOOKS, OBJECT_CONTENT_HOOKS
def parse(tokens) -> T.Tuple[ast.UI, T.Optional[MultipleErrors]]:
""" Parses a list of tokens into an abstract syntax tree. """
gtk_directive = Group(
ast.GtkDirective,
Statement(
Match("using").err("File must start with a \"using Gtk\" directive (e.g. `using Gtk 4.0;`)"),
Match("Gtk").err("File must start with a \"using Gtk\" directive (e.g. `using Gtk 4.0;`)"),
UseNumberText("version").expected("a version number for GTK"),
)
)
import_statement = Group(
ast.Import,
Statement(
"using",
UseIdent("namespace").expected("a GIR namespace"),
UseNumberText("version").expected("a version number"),
)
)
object = Group(
ast.Object,
None
@ -152,8 +134,8 @@ def parse(tokens) -> T.Tuple[ast.UI, T.Optional[MultipleErrors]]:
ui = Group(
ast.UI,
[
gtk_directive,
ZeroOrMore(import_statement),
ast.GtkDirective,
ZeroOrMore(ast.Import),
Until(AnyOf(
*OBJECT_HOOKS,
template,