Rename to blueprint-compiler

This isn't an official GTK project so better to avoid using "GTK" in the
name.
This commit is contained in:
James Westman 2021-12-01 15:35:58 -06:00
parent be3c0de670
commit 544d152fb6
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
37 changed files with 33 additions and 33 deletions

View file

@ -0,0 +1,18 @@
""" Contains all the syntax beyond basic objects, properties, signal, and
templates. """
from .gtk_a11y import a11y
from .gtk_combo_box_text import items
from .gtk_file_filter import mime_types, patterns, suffixes
from .gtk_layout import layout
from .gtk_menu import menu
from .gtk_size_group import widgets
from .gtk_string_list import strings
from .gtk_styles import styles
OBJECT_HOOKS = [menu]
OBJECT_CONTENT_HOOKS = [
a11y, styles, layout, mime_types, patterns, suffixes, widgets, items,
strings,
]

View file

@ -0,0 +1,190 @@
# gtk_a11y.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from ..ast import BaseTypedAttribute, Value
from ..ast_utils import AstNode, validate, docs
from ..completions_utils import *
from ..gir import StringType, BoolType, IntType, FloatType, GirType
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
def _get_property_types(gir):
# from <https://docs.gtk.org/gtk4/enum.AccessibleProperty.html>
return {
"autocomplete": gir.get_type("AccessibleAutocomplete", "Gtk"),
"description": StringType(),
"has_popup": BoolType(),
"key_shortcuts": StringType(),
"label": StringType(),
"level": IntType(),
"modal": BoolType(),
"multi_line": BoolType(),
"multi_selectable": BoolType(),
"orientation": gir.get_type("Orientation", "Gtk"),
"placeholder": StringType(),
"read_only": BoolType(),
"required": BoolType(),
"role_description": StringType(),
"sort": gir.get_type("AccessibleSort", "Gtk"),
"value_max": FloatType(),
"value_min": FloatType(),
"value_now": FloatType(),
"value_text": StringType(),
}
def _get_relation_types(gir):
# from <https://docs.gtk.org/gtk4/enum.AccessibleRelation.html>
widget = gir.get_type("Widget", "Gtk")
return {
"active_descendant": widget,
"col_count": IntType(),
"col_index": IntType(),
"col_index_text": StringType(),
"col_span": IntType(),
"controls": widget,
"described_by": widget,
"details": widget,
"error_message": widget,
"flow_to": widget,
"labelled_by": widget,
"owns": widget,
"pos_in_set": IntType(),
"row_count": IntType(),
"row_index": IntType(),
"row_index_text": StringType(),
"row_span": IntType(),
"set_size": IntType(),
}
def _get_state_types(gir):
# from <https://docs.gtk.org/gtk4/enum.AccessibleState.html>
return {
"busy": BoolType(),
"checked": gir.get_type("AccessibleTristate", "Gtk"),
"disabled": BoolType(),
"expanded": BoolType(),
"hidden": BoolType(),
"invalid": gir.get_type("AccessibleInvalidState", "Gtk"),
"pressed": gir.get_type("AccessibleTristate", "Gtk"),
"selected": BoolType(),
}
def _get_types(gir):
return {
**_get_property_types(gir),
**_get_relation_types(gir),
**_get_state_types(gir),
}
def _get_docs(gir, name):
return (
gir.get_type("AccessibleProperty", "Gtk").members.get(name)
or gir.get_type("AccessibleRelation", "Gtk").members.get(name)
or gir.get_type("AccessibleState", "Gtk").members.get(name)
).doc
class A11y(AstNode):
@validate("accessibility")
def container_is_widget(self):
self.validate_parent_type("Gtk", "Widget", "accessibility properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("accessibility")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class A11yProperty(BaseTypedAttribute):
@property
def tag_name(self):
name = self.tokens["name"]
gir = self.root.gir
if name in _get_property_types(gir):
return "property"
elif name in _get_relation_types(gir):
return "relation"
elif name in _get_state_types(gir):
return "state"
else:
raise CompilerBugError()
@property
def value_type(self) -> GirType:
return _get_types(self.root.gir).get(self.tokens["name"])
@validate("name")
def is_valid_property(self):
types = _get_types(self.root.gir)
if self.tokens["name"] not in types:
raise CompileError(
f"'{self.tokens['name']}' is not an accessibility property, relation, or state",
did_you_mean=(self.tokens["name"], types.keys()),
)
@docs("name")
def prop_docs(self):
if self.tokens["name"] in _get_types(self.root.gir):
return _get_docs(self.root.gir, self.tokens["name"])
a11y_prop = Group(
A11yProperty,
Statement(
UseIdent("name"),
Op(":"),
value.expected("a value"),
)
)
a11y = Group(
A11y,
Sequence(
Keyword("accessibility", True),
OpenBlock(),
Until(a11y_prop, CloseBlock()),
)
)
@completer(
applies_in=[ast.ObjectContent],
matches=new_statement_patterns,
)
def a11y_completer(ast_node, match_variables):
yield Completion(
"accessibility", CompletionItemKind.Snippet,
snippet="accessibility {\n $0\n}"
)
@completer(
applies_in=[A11y],
matches=new_statement_patterns,
)
def a11y_name_completer(ast_node, match_variables):
for name, type in _get_types(ast_node.root.gir).items():
yield Completion(name, CompletionItemKind.Property, docs=_get_docs(ast_node.root.gir, type))

View file

@ -0,0 +1,89 @@
# gtk_combo_box_text.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from ..ast import BaseTypedAttribute
from ..ast_utils import AstNode, validate
from ..completions_utils import *
from ..gir import StringType
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Items(AstNode):
@validate("items")
def container_is_combo_box_text(self):
self.validate_parent_type("Gtk", "ComboBoxText", "combo box items")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("items")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Item(BaseTypedAttribute):
tag_name = "item"
attr_name = "id"
@property
def value_type(self):
return StringType()
item = Group(
Item,
Sequence(
Optional(
Sequence(
UseIdent("name"),
Op(":"),
)
),
value,
)
)
items = Group(
Items,
Sequence(
Keyword("items", True),
OpenBracket(),
Delimited(
item,
Comma()
),
CloseBracket(),
)
)
@completer(
applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "ComboBoxText"),
matches=new_statement_patterns,
)
def items_completer(ast_node, match_variables):
yield Completion(
"items", CompletionItemKind.Snippet,
snippet="items [$0];"
)

View file

@ -0,0 +1,85 @@
# gtk_file_filter.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from .. import ast
from ..ast_utils import AstNode, validate
from ..completions_utils import *
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Filters(AstNode):
@validate()
def container_is_file_filter(self):
self.validate_parent_type("Gtk", "FileFilter", "file filter properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag(self.tokens["tag_name"])
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class FilterString(AstNode):
def emit_xml(self, xml):
xml.start_tag(self.tokens["tag_name"])
xml.put_text(self.tokens["name"])
xml.end_tag()
def create_node(tag_name: str, singular: str):
return Group(
Filters,
Sequence(
Keyword(tag_name, True),
UseLiteral("tag_name", tag_name),
OpenBracket(),
Delimited(
Group(
FilterString,
Sequence(
UseQuoted("name"),
UseLiteral("tag_name", singular),
)
),
Comma(),
),
CloseBracket(),
)
)
mime_types = create_node("mime-types", "mime-type")
patterns = create_node("patterns", "pattern")
suffixes = create_node("suffixes", "suffix")
@completer(
applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "FileFilter"),
matches=new_statement_patterns,
)
def file_filter_completer(ast_node, match_variables):
yield Completion("mime-types", CompletionItemKind.Snippet, snippet="mime-types [\"$0\"];")
yield Completion("patterns", CompletionItemKind.Snippet, snippet="patterns [\"$0\"];")
yield Completion("suffixes", CompletionItemKind.Snippet, snippet="suffixes [\"$0\"];")

View file

@ -0,0 +1,80 @@
# gtk_layout.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from ..ast import BaseAttribute
from ..ast_utils import AstNode, validate
from ..completions_utils import *
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Layout(AstNode):
@validate("layout")
def container_is_widget(self):
self.validate_parent_type("Gtk", "Widget", "layout properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("layout")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class LayoutProperty(BaseAttribute):
tag_name = "property"
@property
def value_type(self):
# there isn't really a way to validate these
return None
layout_prop = Group(
LayoutProperty,
Statement(
UseIdent("name"),
Op(":"),
value.expected("a value"),
)
)
layout = Group(
Layout,
Sequence(
Keyword("layout", True),
OpenBlock(),
Until(layout_prop, CloseBlock()),
)
)
@completer(
applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "Widget"),
matches=new_statement_patterns,
)
def layout_completer(ast_node, match_variables):
yield Completion(
"layout", CompletionItemKind.Snippet,
snippet="layout {\n $0\n}"
)

View file

@ -0,0 +1,190 @@
# gtk_menus.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from ..ast import BaseAttribute
from ..ast_utils import AstNode
from ..completions_utils import *
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Menu(AstNode):
def emit_xml(self, xml: XmlEmitter):
xml.start_tag(self.tokens["tag"], id=self.tokens["id"])
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
@property
def gir_class(self):
return self.root.gir.namespaces["Gtk"].lookup_type("Gio.MenuModel")
class MenuAttribute(BaseAttribute):
tag_name = "attribute"
@property
def value_type(self):
return None
menu_contents = Sequence()
menu_section = Group(
Menu,
Sequence(
Keyword("section"),
UseLiteral("tag", "section"),
Optional(UseIdent("id")),
menu_contents
)
)
menu_submenu = Group(
Menu,
Sequence(
Keyword("submenu"),
UseLiteral("tag", "submenu"),
Optional(UseIdent("id")),
menu_contents
)
)
menu_attribute = Group(
MenuAttribute,
Sequence(
UseIdent("name"),
Op(":"),
value.expected("a value"),
StmtEnd().expected("`;`"),
)
)
menu_item = Group(
Menu,
Sequence(
Keyword("item"),
UseLiteral("tag", "item"),
Optional(UseIdent("id")),
OpenBlock().expected("`{`"),
Until(menu_attribute, CloseBlock()),
)
)
menu_item_shorthand = Group(
Menu,
Sequence(
Keyword("item"),
UseLiteral("tag", "item"),
OpenParen(),
Group(
MenuAttribute,
Sequence(UseLiteral("name", "label"), value),
),
Optional(Sequence(
Comma(),
Optional(Sequence(
Group(
MenuAttribute,
Sequence(UseLiteral("name", "action"), value),
),
Optional(Sequence(
Comma(),
Group(
MenuAttribute,
Sequence(UseLiteral("name", "icon"), value),
),
))
))
)),
CloseParen().expected("')'"),
)
)
menu_contents.children = [
OpenBlock(),
Until(AnyOf(
menu_section,
menu_submenu,
menu_item_shorthand,
menu_item,
menu_attribute,
), CloseBlock()),
]
menu = Group(
Menu,
Sequence(
Keyword("menu"),
UseLiteral("tag", "menu"),
Optional(UseIdent("id")),
menu_contents
),
)
@completer(
applies_in=[ast.UI],
matches=new_statement_patterns,
)
def menu_completer(ast_node, match_variables):
yield Completion(
"menu", CompletionItemKind.Snippet,
snippet="menu {\n $0\n}"
)
@completer(
applies_in=[Menu],
matches=new_statement_patterns,
)
def menu_content_completer(ast_node, match_variables):
yield Completion(
"submenu", CompletionItemKind.Snippet,
snippet="submenu {\n $0\n}"
)
yield Completion(
"section", CompletionItemKind.Snippet,
snippet="section {\n $0\n}"
)
yield Completion(
"item", CompletionItemKind.Snippet,
snippet="item {\n $0\n}"
)
yield Completion(
"item (shorthand)", CompletionItemKind.Snippet,
snippet='item (_("${1:Label}"), "${2:action-name}", "${3:icon-name}");'
)
yield Completion(
"label", CompletionItemKind.Snippet,
snippet='label: $0;'
)
yield Completion(
"action", CompletionItemKind.Snippet,
snippet='action: "$0";'
)
yield Completion(
"icon", CompletionItemKind.Snippet,
snippet='icon: "$0";'
)

View file

@ -0,0 +1,84 @@
# gtk_size_group.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from .. import ast
from ..ast_utils import AstNode, validate
from ..completions_utils import *
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Widgets(AstNode):
@validate("widgets")
def container_is_size_group(self):
self.validate_parent_type("Gtk", "SizeGroup", "size group properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("widgets")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Widget(AstNode):
@validate("name")
def obj_widget(self):
object = self.root.objects_by_id.get(self.tokens["name"])
type = self.root.gir.get_type("Widget", "Gtk")
if object is None:
raise CompileError(
f"Could not find object with ID {self.tokens['name']}",
did_you_mean=(self.tokens['name'], self.root.objects_by_id.keys()),
)
elif object.gir_class and not object.gir_class.assignable_to(type):
raise CompileError(
f"Cannot assign {object.gir_class.full_name} to {type.full_name}"
)
def emit_xml(self, xml: XmlEmitter):
xml.put_self_closing("widget", name=self.tokens["name"])
widgets = Group(
Widgets,
Sequence(
Keyword("widgets", True),
OpenBracket(),
Delimited(
Group(
Widget,
UseIdent("name"),
),
Comma(),
),
CloseBracket(),
)
)
@completer(
applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "SizeGroup"),
matches=new_statement_patterns,
)
def size_group_completer(ast_node, match_variables):
yield Completion("widgets", CompletionItemKind.Snippet, snippet="widgets [$0];")

View file

@ -0,0 +1,85 @@
# gtk_combo_box_text.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from ..ast import BaseTypedAttribute, Value, TranslatedStringValue
from ..ast_utils import AstNode, validate
from ..completions_utils import *
from ..gir import StringType
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Items(AstNode):
@validate("items")
def container_is_string_list(self):
self.validate_parent_type("Gtk", "StringList", "StringList items")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("items")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Item(AstNode):
@property
def value_type(self):
return StringType()
def emit_xml(self, xml: XmlEmitter):
value = self.children[Value][0]
attrs = value.attrs if isinstance(value, TranslatedStringValue) else {}
xml.start_tag("item", **attrs)
value.emit_xml(xml)
xml.end_tag()
item = Group(
Item,
value,
)
strings = Group(
Items,
Sequence(
Keyword("strings", True),
OpenBracket(),
Delimited(
item,
Comma()
),
CloseBracket(),
)
)
@completer(
applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "StringList"),
matches=new_statement_patterns,
)
def strings_completer(ast_node, match_variables):
yield Completion(
"strings", CompletionItemKind.Snippet,
snippet="strings [$0];"
)

View file

@ -0,0 +1,71 @@
# gtk_styles.py
#
# Copyright 2021 James Westman <james@jwestman.net>
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from .. import ast
from ..ast_utils import AstNode, validate
from ..completions_utils import *
from ..lsp_utils import Completion, CompletionItemKind
from ..parse_tree import *
from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Styles(AstNode):
@validate("styles")
def container_is_widget(self):
self.validate_parent_type("Gtk", "Widget", "style classes")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("style")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class StyleClass(AstNode):
def emit_xml(self, xml):
xml.put_self_closing("class", name=self.tokens["name"])
styles = Group(
Styles,
Sequence(
Keyword("styles", True),
OpenBracket(),
Delimited(
Group(
StyleClass,
UseQuoted("name")
),
Comma(),
),
CloseBracket(),
)
)
@completer(
applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "Widget"),
matches=new_statement_patterns,
)
def style_completer(ast_node, match_variables):
yield Completion("styles", CompletionItemKind.Keyword, snippet="styles [\"$0\"];")