mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-05 16:09:07 -04:00
Reorganize the parser/AST code
The code is now organized by syntax: `menu {}` in one file, `style` in another, etc. This should make it easier to add syntax in the future.
This commit is contained in:
parent
dc7c0cabd8
commit
bfd9daf6a9
10 changed files with 486 additions and 344 deletions
10
gtkblueprinttool/extensions/__init__.py
Normal file
10
gtkblueprinttool/extensions/__init__.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
""" Contains all the syntax beyond basic objects, properties, signal, and
|
||||
templates. """
|
||||
|
||||
from .gtk_menu import menu
|
||||
from .gtk_styles import styles
|
||||
from .gtk_layout import layout
|
||||
|
||||
OBJECT_HOOKS = [menu]
|
||||
|
||||
OBJECT_CONTENT_HOOKS = [styles, layout]
|
56
gtkblueprinttool/extensions/gtk_layout.py
Normal file
56
gtkblueprinttool/extensions/gtk_layout.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
# 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_utils import AstNode, BaseAttribute
|
||||
from ..completions_utils import *
|
||||
from ..parse_tree import *
|
||||
from ..parser_utils import *
|
||||
from ..xml_emitter import XmlEmitter
|
||||
|
||||
|
||||
class Layout(AstNode):
|
||||
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"
|
||||
|
||||
|
||||
layout_prop = Group(
|
||||
LayoutProperty,
|
||||
Statement(
|
||||
UseIdent("name"),
|
||||
Op(":"),
|
||||
value.expected("a value"),
|
||||
)
|
||||
)
|
||||
|
||||
layout = Group(
|
||||
Layout,
|
||||
Sequence(
|
||||
Keyword("layout"),
|
||||
OpenBlock().expected("`{`"),
|
||||
Until(layout_prop, CloseBlock()),
|
||||
)
|
||||
)
|
172
gtkblueprinttool/extensions/gtk_menu.py
Normal file
172
gtkblueprinttool/extensions/gtk_menu.py
Normal file
|
@ -0,0 +1,172 @@
|
|||
# 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_utils import AstNode, BaseAttribute
|
||||
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.end_tag()
|
||||
|
||||
|
||||
class MenuAttribute(BaseAttribute):
|
||||
tag_name = "attribute"
|
||||
|
||||
|
||||
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"),
|
||||
Group(
|
||||
MenuAttribute,
|
||||
Sequence(UseLiteral("name", "label"), value),
|
||||
),
|
||||
Optional(Group(
|
||||
MenuAttribute,
|
||||
Sequence(UseLiteral("name", "action"), value),
|
||||
)),
|
||||
Optional(Group(
|
||||
MenuAttribute,
|
||||
Sequence(UseLiteral("name", "verb-icon-name"), value),
|
||||
)),
|
||||
StmtEnd().expected("`;`"),
|
||||
)
|
||||
)
|
||||
|
||||
menu_contents.children = [
|
||||
OpenBlock().expected("`{`"),
|
||||
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";'
|
||||
)
|
||||
|
64
gtkblueprinttool/extensions/gtk_styles.py
Normal file
64
gtkblueprinttool/extensions/gtk_styles.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
# 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, BaseAttribute
|
||||
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):
|
||||
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,
|
||||
Statement(
|
||||
Keyword("style"),
|
||||
Delimited(
|
||||
Group(
|
||||
StyleClass,
|
||||
UseQuoted("name")
|
||||
),
|
||||
Comma(),
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@completer(
|
||||
applies_in=[ast.ObjectContent],
|
||||
matches=new_statement_patterns,
|
||||
)
|
||||
def style_completer(ast_node, match_variables):
|
||||
yield Completion("style", CompletionItemKind.Keyword, snippet="style \"$0\";")
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue