From b3c28ce3d411369f69cd90c48c22536886d26139 Mon Sep 17 00:00:00 2001 From: James Westman Date: Fri, 22 Oct 2021 22:26:00 -0500 Subject: [PATCH] Use "using" instead of "gtk" and "import" - Having one keyword for both is less syntax to remember - I might use "include" as a keyword in the future, which would make "import" confusing, so use "using" instead --- README.md | 6 ++---- gtkblueprinttool/lsp.py | 4 ++-- gtkblueprinttool/parse_tree.py | 15 +++++++++++++++ gtkblueprinttool/parser.py | 13 ++++++------- gtkblueprinttool/utils.py | 2 ++ 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2d6c2d6..5c1f18d 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,8 @@ Here is what [the libshumate demo's UI definition](https://gitlab.gnome.org/GNOM looks like ported to this new format: ``` -gtk 4.0; - -import Adw 1.0; -import Shumate 1.0; +using Gtk 4.0; +using Shumate 1.0; template ShumateDemoWindow : Gtk.ApplicationWindow { can-focus: yes; diff --git a/gtkblueprinttool/lsp.py b/gtkblueprinttool/lsp.py index 9eafd24..ccf154b 100644 --- a/gtkblueprinttool/lsp.py +++ b/gtkblueprinttool/lsp.py @@ -18,7 +18,7 @@ # SPDX-License-Identifier: LGPL-3.0-or-later -import json, sys +import json, sys, traceback from .errors import PrintableError, CompileError, MultipleErrors from .lsp_enums import * @@ -61,7 +61,7 @@ class LanguageServer: if method in self.commands: self.commands[method](self, id, params) except Exception as e: - self._log(e) + self._log(traceback.format_exc()) def _send(self, data): diff --git a/gtkblueprinttool/parse_tree.py b/gtkblueprinttool/parse_tree.py index 32b2777..571d8c3 100644 --- a/gtkblueprinttool/parse_tree.py +++ b/gtkblueprinttool/parse_tree.py @@ -408,6 +408,21 @@ class UseNumber(ParseNode): return True +class UseNumberText(ParseNode): + """ ParseNode that matches a number, but sets its *original text* it in a + key=value pair on the containing match group. """ + def __init__(self, key): + self.key = key + + def _parse(self, ctx: ParseContext): + token = ctx.next_token() + if token.type != TokenType.NUMBER: + return False + + ctx.set_group_val(self.key, str(token), token) + return True + + class UseQuoted(ParseNode): """ ParseNode that matches a quoted string and sets it in a key=value pair on the containing match group. """ diff --git a/gtkblueprinttool/parser.py b/gtkblueprinttool/parser.py index dc5966d..f61d15e 100644 --- a/gtkblueprinttool/parser.py +++ b/gtkblueprinttool/parser.py @@ -30,9 +30,9 @@ def parse(tokens) -> ast.UI: gtk_directive = Group( ast.GtkDirective, Sequence( - Keyword("gtk"), - Fail(UseNumber(None), "Version number must be in quotation marks"), - UseQuoted("version").expected("a version number for GTK"), + Keyword("using"), + Keyword("Gtk"), + UseNumberText("version").expected("a version number for GTK"), StmtEnd().expected("`;`"), ) ) @@ -40,10 +40,9 @@ def parse(tokens) -> ast.UI: import_statement = Group( ast.Import, Sequence( - Keyword("import"), + Keyword("using"), UseIdent("namespace").expected("a GIR namespace"), - Fail(UseNumber(None), "Version number must be in quotation marks"), - UseQuoted("version").expected("a version number"), + UseNumberText("version").expected("a version number"), StmtEnd().expected("`;`"), ) ).recover() @@ -173,7 +172,7 @@ def parse(tokens) -> ast.UI: ui = Group( ast.UI, Sequence( - gtk_directive.err("File must start with a gtk directive (e.g. `gtk 4.0;`)"), + gtk_directive.err("File must start with a \"using gtk\" directive (e.g. `using Gtk 4.0;`)"), ZeroOrMore(import_statement), ZeroOrMore(AnyOf( template, diff --git a/gtkblueprinttool/utils.py b/gtkblueprinttool/utils.py index 387badb..eb632cc 100644 --- a/gtkblueprinttool/utils.py +++ b/gtkblueprinttool/utils.py @@ -68,6 +68,8 @@ def did_you_mean(word: str, options: [str]) -> T.Optional[str]: def idx_to_pos(idx: int, text: str) -> (int, int): + if idx == 0: + return (0, 0) sp = text[:idx].splitlines(keepends=True) line_num = len(sp) col_num = len(sp[-1])