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
|
@ -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):
|
||||
|
|
|
@ -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. """
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue