Implement array value

Adds ArrayValue docs, and tests for diagnostics.
This commit is contained in:
Diego Augusto S. C 2023-11-03 19:10:12 -03:00 committed by Sonny Piers
parent f1cf70b6eb
commit 14be727777
12 changed files with 115 additions and 4 deletions

View file

@ -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 + "[]"

View file

@ -41,6 +41,7 @@ from .imports import GtkDirective, Import
from .types import ClassName
from .ui import UI
from .values import (
ArrayValue,
Flag,
Flags,
IdentLiteral,

View file

@ -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

View file

@ -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)

View file

@ -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()

View file

@ -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.

View file

@ -0,0 +1,6 @@
using Gtk 4.0;
AboutDialog about {
valign: center;
authors: ["Olimar\n"];
}

View file

@ -0,0 +1 @@
5,15,10,String literals inside arrays can't contain newlines

View file

@ -0,0 +1,6 @@
using Gtk 4.0;
AboutDialog about {
valign: center;
authors: ["Olimar\n", "Luie\nPresident"];
}

View 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

View 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>"];
}

View 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 &lt;jane-doe@email.com&gt;
Jhonny D &lt;jd@email.com&gt;</property>
</object>
</interface>