mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Merge branch 'main' into formatter-improvements
This commit is contained in:
commit
85a21086f6
20 changed files with 120 additions and 57 deletions
|
@ -23,7 +23,7 @@ from dataclasses import dataclass
|
|||
from enum import Enum
|
||||
|
||||
from .gir import *
|
||||
from .utils import Colors
|
||||
from .utils import Colors, escape_quote
|
||||
from .xml_reader import Element, parse, parse_string
|
||||
|
||||
__all__ = ["decompile"]
|
||||
|
@ -138,7 +138,7 @@ class DecompileCtx:
|
|||
return value.replace("-", "_")
|
||||
|
||||
if type is None:
|
||||
self.print(f'{name}: "{escape_quote(value)}";')
|
||||
self.print(f"{name}: {escape_quote(value)};")
|
||||
elif type.assignable_to(FloatType()):
|
||||
self.print(f"{name}: {value};")
|
||||
elif type.assignable_to(BoolType()):
|
||||
|
@ -157,7 +157,7 @@ class DecompileCtx:
|
|||
self.gir.namespaces["Gtk"].lookup_type("Gtk.ShortcutTrigger")
|
||||
)
|
||||
):
|
||||
self.print(f'{name}: "{escape_quote(value)}";')
|
||||
self.print(f"{name}: {escape_quote(value)};")
|
||||
elif value == self.template_class:
|
||||
self.print(f"{name}: template;")
|
||||
elif type.assignable_to(
|
||||
|
@ -170,7 +170,7 @@ class DecompileCtx:
|
|||
elif isinstance(type, Enumeration):
|
||||
self.print(f"{name}: {get_enum_name(value)};")
|
||||
else:
|
||||
self.print(f'{name}: "{escape_quote(value)}";')
|
||||
self.print(f"{name}: {escape_quote(value)};")
|
||||
|
||||
|
||||
def _decompile_element(
|
||||
|
@ -253,15 +253,6 @@ def decompiler(tag, cdata=False):
|
|||
return decorator
|
||||
|
||||
|
||||
def escape_quote(string: str) -> str:
|
||||
return (
|
||||
string.replace("\\", "\\\\")
|
||||
.replace("'", "\\'")
|
||||
.replace('"', '\\"')
|
||||
.replace("\n", "\\n")
|
||||
)
|
||||
|
||||
|
||||
@decompiler("interface")
|
||||
def decompile_interface(ctx, gir):
|
||||
return gir
|
||||
|
@ -289,11 +280,11 @@ def decompile_translatable(
|
|||
comments = f"/* Translators: {comments} */"
|
||||
|
||||
if context is not None:
|
||||
return comments, f'C_("{escape_quote(context)}", "{escape_quote(string)}")'
|
||||
return comments, f"C_({escape_quote(context)}, {escape_quote(string)})"
|
||||
else:
|
||||
return comments, f'_("{escape_quote(string)}")'
|
||||
return comments, f"_({escape_quote(string)})"
|
||||
else:
|
||||
return comments, f'"{escape_quote(string)}"'
|
||||
return comments, f"{escape_quote(string)}"
|
||||
|
||||
|
||||
@decompiler("property", cdata=True)
|
||||
|
@ -334,7 +325,7 @@ def decompile_property(
|
|||
ctx.print(comments)
|
||||
ctx.print(f"{name}: {translatable};")
|
||||
elif gir is None or gir.properties.get(name) is None:
|
||||
ctx.print(f'{name}: "{escape_quote(cdata)}";')
|
||||
ctx.print(f"{name}: {escape_quote(cdata)};")
|
||||
else:
|
||||
ctx.print_attribute(name, cdata, gir.properties.get(name).type)
|
||||
return gir
|
||||
|
|
|
@ -231,7 +231,7 @@ def decompile_relation(ctx, gir, name, cdata):
|
|||
@decompiler("state", cdata=True)
|
||||
def decompile_state(ctx, gir, name, cdata, translatable="false"):
|
||||
if decompile.truthy(translatable):
|
||||
ctx.print(f'{name}: _("{escape_quote(cdata)}");')
|
||||
ctx.print(f"{name}: _({escape_quote(cdata)});")
|
||||
else:
|
||||
ctx.print_attribute(name, cdata, get_types(ctx.gir).get(name))
|
||||
|
||||
|
|
|
@ -119,7 +119,7 @@ def decompile_mime_types(ctx, gir):
|
|||
|
||||
@decompiler("mime-type", cdata=True)
|
||||
def decompile_mime_type(ctx, gir, cdata):
|
||||
ctx.print(f'"{cdata}",')
|
||||
ctx.print(f"{escape_quote(cdata)},")
|
||||
|
||||
|
||||
@decompiler("patterns")
|
||||
|
@ -129,7 +129,7 @@ def decompile_patterns(ctx, gir):
|
|||
|
||||
@decompiler("pattern", cdata=True)
|
||||
def decompile_pattern(ctx, gir, cdata):
|
||||
ctx.print(f'"{cdata}",')
|
||||
ctx.print(f"{escape_quote(cdata)},")
|
||||
|
||||
|
||||
@decompiler("suffixes")
|
||||
|
@ -139,4 +139,4 @@ def decompile_suffixes(ctx, gir):
|
|||
|
||||
@decompiler("suffix", cdata=True)
|
||||
def decompile_suffix(ctx, gir, cdata):
|
||||
ctx.print(f'"{cdata}",')
|
||||
ctx.print(f"{escape_quote(cdata)},")
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
import typing as T
|
||||
from enum import Enum
|
||||
|
||||
from . import utils
|
||||
from .ast_utils import AstNode
|
||||
from .errors import (
|
||||
CompileError,
|
||||
|
@ -573,14 +574,19 @@ class UseQuoted(ParseNode):
|
|||
if token.type != TokenType.QUOTED:
|
||||
return False
|
||||
|
||||
string = (
|
||||
str(token)[1:-1]
|
||||
.replace("\\n", "\n")
|
||||
.replace('\\"', '"')
|
||||
.replace("\\\\", "\\")
|
||||
.replace("\\'", "'")
|
||||
unescaped = None
|
||||
|
||||
try:
|
||||
unescaped = utils.unescape_quote(str(token))
|
||||
except utils.UnescapeError as e:
|
||||
start = ctx.tokens[ctx.index - 1].start
|
||||
range = Range(start + e.start, start + e.end, ctx.text)
|
||||
ctx.errors.append(
|
||||
CompileError(f"Invalid escape sequence '{range.text}'", range)
|
||||
)
|
||||
ctx.set_group_val(self.key, string, token)
|
||||
|
||||
ctx.set_group_val(self.key, unescaped, token)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
import typing as T
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
class Colors:
|
||||
|
@ -98,3 +99,57 @@ def idxs_to_range(start: int, end: int, text: str):
|
|||
"character": end_c,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class UnescapeError(Exception):
|
||||
start: int
|
||||
end: int
|
||||
|
||||
|
||||
def escape_quote(string: str) -> str:
|
||||
return (
|
||||
"'"
|
||||
+ (
|
||||
string.replace("\\", "\\\\")
|
||||
.replace("'", "\\'")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\t", "\\t")
|
||||
)
|
||||
+ "'"
|
||||
)
|
||||
|
||||
|
||||
def unescape_quote(string: str) -> str:
|
||||
string = string[1:-1]
|
||||
|
||||
REPLACEMENTS = {
|
||||
"\\": "\\",
|
||||
"n": "\n",
|
||||
"t": "\t",
|
||||
'"': '"',
|
||||
"'": "'",
|
||||
}
|
||||
|
||||
result = ""
|
||||
i = 0
|
||||
while i < len(string):
|
||||
c = string[i]
|
||||
if c == "\\":
|
||||
i += 1
|
||||
|
||||
if i >= len(string):
|
||||
from .errors import CompilerBugError
|
||||
|
||||
raise CompilerBugError()
|
||||
|
||||
if r := REPLACEMENTS.get(string[i]):
|
||||
result += r
|
||||
else:
|
||||
raise UnescapeError(i, i + 2)
|
||||
else:
|
||||
result += c
|
||||
|
||||
i += 1
|
||||
|
||||
return result
|
||||
|
|
5
tests/sample_errors/bad_escape_sequence.blp
Normal file
5
tests/sample_errors/bad_escape_sequence.blp
Normal file
|
@ -0,0 +1,5 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
Label {
|
||||
label: '***** \f *****';
|
||||
}
|
1
tests/sample_errors/bad_escape_sequence.err
Normal file
1
tests/sample_errors/bad_escape_sequence.err
Normal file
|
@ -0,0 +1 @@
|
|||
4,17,2,Invalid escape sequence '\f'
|
|
@ -2,7 +2,7 @@ using Gtk 4.0;
|
|||
|
||||
Box {
|
||||
accessibility {
|
||||
label: _("Hello, world!");
|
||||
label: _('Hello, world!');
|
||||
labelled-by: my_label;
|
||||
checked: true;
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
FileFilter {
|
||||
name: "File Filter Name";
|
||||
name: 'File Filter Name';
|
||||
|
||||
mime-types [
|
||||
"text/plain",
|
||||
"image/ *",
|
||||
'text/plain',
|
||||
'image/ *',
|
||||
]
|
||||
|
||||
patterns [
|
||||
"*.txt",
|
||||
'*.txt',
|
||||
]
|
||||
|
||||
suffixes [
|
||||
"png",
|
||||
'png',
|
||||
]
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ using Gtk 4.0;
|
|||
Grid {
|
||||
Label {
|
||||
layout {
|
||||
column: "0";
|
||||
row: "1";
|
||||
column: '0';
|
||||
row: '1';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,26 +3,26 @@ using Gtk 4.0;
|
|||
menu my-menu {
|
||||
submenu {
|
||||
section {
|
||||
label: "test section";
|
||||
label: 'test section';
|
||||
}
|
||||
|
||||
item {
|
||||
label: C_("context", "test translated item");
|
||||
label: C_('context', 'test translated item');
|
||||
}
|
||||
|
||||
item {
|
||||
label: "test item shorthand 1";
|
||||
label: 'test item shorthand 1';
|
||||
}
|
||||
|
||||
item {
|
||||
label: "test item shorthand 2";
|
||||
action: "app.test-action";
|
||||
label: 'test item shorthand 2';
|
||||
action: 'app.test-action';
|
||||
}
|
||||
|
||||
item {
|
||||
label: "test item shorthand 3";
|
||||
action: "app.test-action";
|
||||
icon: "test-symbolic";
|
||||
label: 'test item shorthand 3';
|
||||
action: 'app.test-action';
|
||||
icon: 'test-symbolic';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ using Adw 1;
|
|||
|
||||
Adw.MessageDialog {
|
||||
responses [
|
||||
cancel: _("Cancel"),
|
||||
discard: _("Discard") destructive,
|
||||
save: "Save" suggested disabled,
|
||||
cancel: _('Cancel'),
|
||||
discard: _('Discard') destructive,
|
||||
save: 'Save' suggested disabled,
|
||||
]
|
||||
}
|
|
@ -3,7 +3,7 @@ using Gtk 4.0;
|
|||
Scale {
|
||||
marks [
|
||||
mark (-1, bottom),
|
||||
mark (0, top, _("Hello, world!")),
|
||||
mark (0, top, _('Hello, world!')),
|
||||
mark (2),
|
||||
]
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
Label {
|
||||
label: "Test 1 2 3\n & 4 \"5\' 6";
|
||||
label: "\\\\'Test 1 2 3\n & 4 \"5\' 6 \t";
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
|
|||
<interface>
|
||||
<requires lib="gtk" version="4.0"/>
|
||||
<object class="GtkLabel">
|
||||
<property name="label">Test 1 2 3
|
||||
& 4 "5' 6</property>
|
||||
<property name="label">\\'Test 1 2 3
|
||||
& 4 "5' 6 </property>
|
||||
</object>
|
||||
</interface>
|
||||
|
|
5
tests/samples/strings_dec.blp
Normal file
5
tests/samples/strings_dec.blp
Normal file
|
@ -0,0 +1,5 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
Label {
|
||||
label: '\\\\\'Test 1 2 3\n & 4 "5\' 6 \t';
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
template $TestTemplate : ApplicationWindow {
|
||||
test-property: "Hello, world";
|
||||
test-property: 'Hello, world';
|
||||
test-signal => $on_test_signal();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
Label {
|
||||
label: _("Hello, world!");
|
||||
label: _('Hello, world!');
|
||||
}
|
||||
|
||||
Label {
|
||||
label: C_("translation context", "Hello");
|
||||
label: C_('translation context', 'Hello');
|
||||
}
|
||||
|
|
|
@ -2,6 +2,6 @@ using Gtk 4.0;
|
|||
|
||||
$MyComponent component {
|
||||
$MyComponent2 {
|
||||
flags-value: "a|b";
|
||||
flags-value: 'a|b';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -229,7 +229,7 @@ class TestSamples(unittest.TestCase):
|
|||
self.assert_decompile("responses")
|
||||
self.assert_decompile("scale_marks")
|
||||
self.assert_decompile("signal")
|
||||
self.assert_decompile("strings")
|
||||
self.assert_decompile("strings_dec")
|
||||
self.assert_decompile("style_dec")
|
||||
self.assert_decompile("template")
|
||||
self.assert_decompile("translated")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue