mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
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
This commit is contained in:
parent
78a9481631
commit
b3c28ce3d4
5 changed files with 27 additions and 13 deletions
|
@ -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:
|
looks like ported to this new format:
|
||||||
|
|
||||||
```
|
```
|
||||||
gtk 4.0;
|
using Gtk 4.0;
|
||||||
|
using Shumate 1.0;
|
||||||
import Adw 1.0;
|
|
||||||
import Shumate 1.0;
|
|
||||||
|
|
||||||
template ShumateDemoWindow : Gtk.ApplicationWindow {
|
template ShumateDemoWindow : Gtk.ApplicationWindow {
|
||||||
can-focus: yes;
|
can-focus: yes;
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
# SPDX-License-Identifier: LGPL-3.0-or-later
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
|
||||||
import json, sys
|
import json, sys, traceback
|
||||||
|
|
||||||
from .errors import PrintableError, CompileError, MultipleErrors
|
from .errors import PrintableError, CompileError, MultipleErrors
|
||||||
from .lsp_enums import *
|
from .lsp_enums import *
|
||||||
|
@ -61,7 +61,7 @@ class LanguageServer:
|
||||||
if method in self.commands:
|
if method in self.commands:
|
||||||
self.commands[method](self, id, params)
|
self.commands[method](self, id, params)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._log(e)
|
self._log(traceback.format_exc())
|
||||||
|
|
||||||
|
|
||||||
def _send(self, data):
|
def _send(self, data):
|
||||||
|
|
|
@ -408,6 +408,21 @@ class UseNumber(ParseNode):
|
||||||
return True
|
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):
|
class UseQuoted(ParseNode):
|
||||||
""" ParseNode that matches a quoted string and sets it in a key=value pair
|
""" ParseNode that matches a quoted string and sets it in a key=value pair
|
||||||
on the containing match group. """
|
on the containing match group. """
|
||||||
|
|
|
@ -30,9 +30,9 @@ def parse(tokens) -> ast.UI:
|
||||||
gtk_directive = Group(
|
gtk_directive = Group(
|
||||||
ast.GtkDirective,
|
ast.GtkDirective,
|
||||||
Sequence(
|
Sequence(
|
||||||
Keyword("gtk"),
|
Keyword("using"),
|
||||||
Fail(UseNumber(None), "Version number must be in quotation marks"),
|
Keyword("Gtk"),
|
||||||
UseQuoted("version").expected("a version number for GTK"),
|
UseNumberText("version").expected("a version number for GTK"),
|
||||||
StmtEnd().expected("`;`"),
|
StmtEnd().expected("`;`"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -40,10 +40,9 @@ def parse(tokens) -> ast.UI:
|
||||||
import_statement = Group(
|
import_statement = Group(
|
||||||
ast.Import,
|
ast.Import,
|
||||||
Sequence(
|
Sequence(
|
||||||
Keyword("import"),
|
Keyword("using"),
|
||||||
UseIdent("namespace").expected("a GIR namespace"),
|
UseIdent("namespace").expected("a GIR namespace"),
|
||||||
Fail(UseNumber(None), "Version number must be in quotation marks"),
|
UseNumberText("version").expected("a version number"),
|
||||||
UseQuoted("version").expected("a version number"),
|
|
||||||
StmtEnd().expected("`;`"),
|
StmtEnd().expected("`;`"),
|
||||||
)
|
)
|
||||||
).recover()
|
).recover()
|
||||||
|
@ -173,7 +172,7 @@ def parse(tokens) -> ast.UI:
|
||||||
ui = Group(
|
ui = Group(
|
||||||
ast.UI,
|
ast.UI,
|
||||||
Sequence(
|
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(import_statement),
|
||||||
ZeroOrMore(AnyOf(
|
ZeroOrMore(AnyOf(
|
||||||
template,
|
template,
|
||||||
|
|
|
@ -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):
|
def idx_to_pos(idx: int, text: str) -> (int, int):
|
||||||
|
if idx == 0:
|
||||||
|
return (0, 0)
|
||||||
sp = text[:idx].splitlines(keepends=True)
|
sp = text[:idx].splitlines(keepends=True)
|
||||||
line_num = len(sp)
|
line_num = len(sp)
|
||||||
col_num = len(sp[-1])
|
col_num = len(sp[-1])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue