diff --git a/blueprintcompiler/language/__init__.py b/blueprintcompiler/language/__init__.py index ad4c6bc..91407b5 100644 --- a/blueprintcompiler/language/__init__.py +++ b/blueprintcompiler/language/__init__.py @@ -11,9 +11,11 @@ from .gtk_size_group import Widgets from .gtk_string_list import Strings from .gtk_styles import Styles -OBJECT_HOOKS = [menu] +from .common import * -OBJECT_CONTENT_HOOKS = [ +OBJECT_HOOKS.children = [menu] + +OBJECT_CONTENT_HOOKS.children = [ Signal, A11y, Styles, Layout, mime_types, patterns, suffixes, Widgets, Items, Strings, ] diff --git a/blueprintcompiler/language/common.py b/blueprintcompiler/language/common.py index e6e2d9e..e6f9cca 100644 --- a/blueprintcompiler/language/common.py +++ b/blueprintcompiler/language/common.py @@ -26,3 +26,7 @@ from ..lsp_utils import Completion, CompletionItemKind from ..parse_tree import * from ..parser_utils import * from ..xml_emitter import XmlEmitter + + +OBJECT_HOOKS = AnyOf() +OBJECT_CONTENT_HOOKS = AnyOf() diff --git a/blueprintcompiler/language/imports.py b/blueprintcompiler/language/imports.py new file mode 100644 index 0000000..f3ddab7 --- /dev/null +++ b/blueprintcompiler/language/imports.py @@ -0,0 +1,70 @@ +# imports.py +# +# Copyright 2022 James Westman +# +# This file is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program. If not, see . +# +# SPDX-License-Identifier: LGPL-3.0-or-later + + +from .common import * + + +class GtkDirective(AstNode): + grammar = 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"), + ) + + @validate("version") + def gtk_version(self): + if self.tokens["version"] not in ["4.0"]: + err = CompileError("Only GTK 4 is supported") + if self.tokens["version"].startswith("4"): + err.hint("Expected the GIR version, not an exact version number. Use `using Gtk 4.0;`.") + else: + err.hint("Expected `using Gtk 4.0;`") + raise err + + + @property + def gir_namespace(self): + return gir.get_namespace("Gtk", self.tokens["version"]) + + + def emit_xml(self, xml: XmlEmitter): + xml.put_self_closing("requires", lib="gtk", version=self.tokens["version"]) + + +class Import(AstNode): + grammar = Statement( + "using", + UseIdent("namespace").expected("a GIR namespace"), + UseNumberText("version").expected("a version number"), + ) + + @validate("namespace", "version") + def namespace_exists(self): + gir.get_namespace(self.tokens["namespace"], self.tokens["version"]) + + @property + def gir_namespace(self): + try: + return gir.get_namespace(self.tokens["namespace"], self.tokens["version"]) + except CompileError: + return None + + def emit_xml(self, xml): + pass diff --git a/blueprintcompiler/parse_tree.py b/blueprintcompiler/parse_tree.py index 00ef0c7..10fac2d 100644 --- a/blueprintcompiler/parse_tree.py +++ b/blueprintcompiler/parse_tree.py @@ -324,7 +324,14 @@ class AnyOf(ParseNode): """ ParseNode that attempts to match exactly one of its children. Child nodes are attempted in order. """ def __init__(self, *children): - self.children = [to_parse_node(child) for child in children] + self.children = children + + @property + def children(self): + return self._children + @children.setter + def children(self, children): + self._children = [to_parse_node(child) for child in children] def _parse(self, ctx): for child in self.children: diff --git a/blueprintcompiler/parser.py b/blueprintcompiler/parser.py index b9c5e4f..ede452c 100644 --- a/blueprintcompiler/parser.py +++ b/blueprintcompiler/parser.py @@ -40,7 +40,7 @@ def parse(tokens) -> T.Tuple[ast.UI, T.Optional[MultipleErrors]]: UseIdent("name"), ":", AnyOf( - *OBJECT_HOOKS, + OBJECT_HOOKS, object, value, ).expected("a value"), @@ -82,7 +82,7 @@ def parse(tokens) -> T.Tuple[ast.UI, T.Optional[MultipleErrors]]: [ "{", Until(AnyOf( - *OBJECT_CONTENT_HOOKS, + OBJECT_CONTENT_HOOKS, binding, property, child, @@ -116,7 +116,7 @@ def parse(tokens) -> T.Tuple[ast.UI, T.Optional[MultipleErrors]]: ast.GtkDirective, ZeroOrMore(ast.Import), Until(AnyOf( - *OBJECT_HOOKS, + OBJECT_HOOKS, template, object, ), Eof()),