mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Implement array value
Adds ArrayValue docs, and tests for diagnostics.
This commit is contained in:
parent
f1cf70b6eb
commit
14be727777
12 changed files with 115 additions and 4 deletions
|
@ -200,6 +200,10 @@ class ArrayType(GirType):
|
|||
def assignable_to(self, other: GirType) -> bool:
|
||||
return isinstance(other, ArrayType) and self._inner.assignable_to(other._inner)
|
||||
|
||||
@property
|
||||
def inner(self) -> GirType:
|
||||
return self._inner
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._inner.name + "[]"
|
||||
|
|
|
@ -41,6 +41,7 @@ from .imports import GtkDirective, Import
|
|||
from .types import ClassName
|
||||
from .ui import UI
|
||||
from .values import (
|
||||
ArrayValue,
|
||||
Flag,
|
||||
Flags,
|
||||
IdentLiteral,
|
||||
|
|
|
@ -22,18 +22,20 @@ from .binding import Binding
|
|||
from .common import *
|
||||
from .contexts import ValueTypeCtx
|
||||
from .gtkbuilder_template import Template
|
||||
from .values import ObjectValue, Value
|
||||
from .values import ArrayValue, ObjectValue, Value
|
||||
|
||||
|
||||
class Property(AstNode):
|
||||
grammar = Statement(UseIdent("name"), ":", AnyOf(Binding, ObjectValue, Value))
|
||||
grammar = Statement(
|
||||
UseIdent("name"), ":", AnyOf(Binding, ObjectValue, Value, ArrayValue)
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self.tokens["name"]
|
||||
|
||||
@property
|
||||
def value(self) -> T.Union[Binding, ObjectValue, Value]:
|
||||
def value(self) -> T.Union[Binding, ObjectValue, Value, ArrayValue]:
|
||||
return self.children[0]
|
||||
|
||||
@property
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
import typing as T
|
||||
|
||||
from blueprintcompiler.gir import ArrayType
|
||||
|
||||
from .common import *
|
||||
from .contexts import ScopeCtx, ValueTypeCtx
|
||||
from .gobject_object import Object
|
||||
|
@ -395,6 +397,54 @@ class Value(AstNode):
|
|||
return self.children[0]
|
||||
|
||||
|
||||
class ArrayValue(AstNode):
|
||||
grammar = ["[", Delimited(Value, ","), "]"]
|
||||
|
||||
@validate()
|
||||
def validate_for_type(self) -> None:
|
||||
expected_type = self.gir_type
|
||||
if expected_type is not None and not isinstance(expected_type, gir.ArrayType):
|
||||
raise CompileError(f"Cannot assign array to {expected_type.full_name}")
|
||||
|
||||
if expected_type is not None and not isinstance(
|
||||
expected_type.inner, StringType
|
||||
):
|
||||
raise CompileError("Only string arrays are supported")
|
||||
|
||||
@validate()
|
||||
def validate_invalid_newline(self) -> None:
|
||||
expected_type = self.gir_type
|
||||
if expected_type is not None and isinstance(expected_type.inner, StringType):
|
||||
errors = []
|
||||
for value in self.values:
|
||||
if isinstance(value.child, Literal) and isinstance(
|
||||
value.child.value, QuotedLiteral
|
||||
):
|
||||
quoted_literal = value.child.value
|
||||
literal_value = quoted_literal.value
|
||||
if "\n" in literal_value:
|
||||
errors.append(
|
||||
CompileError(
|
||||
"String literals inside arrays can't contain newlines",
|
||||
range=quoted_literal.range,
|
||||
)
|
||||
)
|
||||
if len(errors) > 0:
|
||||
raise MultipleErrors(errors)
|
||||
|
||||
@property
|
||||
def values(self) -> T.List[Value]:
|
||||
return self.children
|
||||
|
||||
@property
|
||||
def gir_type(self):
|
||||
return self.parent.context[ValueTypeCtx].value_type
|
||||
|
||||
@context(ValueTypeCtx)
|
||||
def child_value(self):
|
||||
return ValueTypeCtx(self.gir_type.inner)
|
||||
|
||||
|
||||
class StringValue(AstNode):
|
||||
grammar = AnyOf(Translated, QuotedLiteral)
|
||||
|
||||
|
|
|
@ -139,6 +139,15 @@ class XmlOutput(OutputFormat):
|
|||
self._emit_object(value.object, xml)
|
||||
xml.end_tag()
|
||||
|
||||
elif isinstance(value, ArrayValue):
|
||||
xml.start_tag("property", **props)
|
||||
values = list(value.values)
|
||||
for value in values[:-1]:
|
||||
self._emit_value(value, xml)
|
||||
xml.put_text("\n")
|
||||
self._emit_value(values[-1], xml)
|
||||
xml.end_tag()
|
||||
|
||||
else:
|
||||
raise CompilerBugError()
|
||||
|
||||
|
|
|
@ -145,7 +145,6 @@ Example
|
|||
label: _("This is an advanced feature. Use with caution!");
|
||||
}
|
||||
|
||||
|
||||
.. _Syntax ObjectValue:
|
||||
|
||||
Object Values
|
||||
|
@ -170,3 +169,14 @@ String Values
|
|||
StringValue = :ref:`Translated<Syntax Translated>` | :ref:`QuotedLiteral<Syntax Literal>`
|
||||
|
||||
Menus, as well as some :ref:`extensions<Syntax Extension>`, have properties that can only be string literals or translated strings.
|
||||
|
||||
.. _Syntax ArrayValue:
|
||||
|
||||
Array Values
|
||||
-------------
|
||||
|
||||
.. rst-class:: grammar-block
|
||||
|
||||
ArrayValue = '[' (:ref:`StringValue<Syntax StringValue>`),* ']'
|
||||
|
||||
For now, it only supports :ref:`Strings<Syntax StringValue>`. This is because Gtk.Builder only supports string arrays.
|
||||
|
|
6
tests/sample_errors/newline_in_string_array.blp
Normal file
6
tests/sample_errors/newline_in_string_array.blp
Normal file
|
@ -0,0 +1,6 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
AboutDialog about {
|
||||
valign: center;
|
||||
authors: ["Olimar\n"];
|
||||
}
|
1
tests/sample_errors/newline_in_string_array.err
Normal file
1
tests/sample_errors/newline_in_string_array.err
Normal file
|
@ -0,0 +1 @@
|
|||
5,15,10,String literals inside arrays can't contain newlines
|
6
tests/sample_errors/newline_in_string_array_multi.blp
Normal file
6
tests/sample_errors/newline_in_string_array_multi.blp
Normal file
|
@ -0,0 +1,6 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
AboutDialog about {
|
||||
valign: center;
|
||||
authors: ["Olimar\n", "Luie\nPresident"];
|
||||
}
|
2
tests/sample_errors/newline_in_string_array_multi.err
Normal file
2
tests/sample_errors/newline_in_string_array_multi.err
Normal file
|
@ -0,0 +1,2 @@
|
|||
5,15,10,String literals inside arrays can't contain newlines
|
||||
5,27,17,String literals inside arrays can't contain newlines
|
6
tests/samples/string_array.blp
Normal file
6
tests/samples/string_array.blp
Normal file
|
@ -0,0 +1,6 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
AboutDialog about {
|
||||
valign: center;
|
||||
authors: ["Jane doe <jane-doe@email.com>", "Jhonny D <jd@email.com>"];
|
||||
}
|
14
tests/samples/string_array.ui
Normal file
14
tests/samples/string_array.ui
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
DO NOT EDIT!
|
||||
This file was @generated by blueprint-compiler. Instead, edit the
|
||||
corresponding .blp file and regenerate this file with blueprint-compiler.
|
||||
-->
|
||||
<interface>
|
||||
<requires lib="gtk" version="4.0"/>
|
||||
<object class="GtkAboutDialog" id="about">
|
||||
<property name="valign">3</property>
|
||||
<property name="authors">Jane doe <jane-doe@email.com>
|
||||
Jhonny D <jd@email.com></property>
|
||||
</object>
|
||||
</interface>
|
Loading…
Add table
Add a link
Reference in a new issue