Accept string literals for parseable types

GtkBuilder knows some class-specific parsing methods, so we can use
string literals for these types and pass them straight to the XML.
This commit is contained in:
James Westman 2021-11-24 18:41:14 -06:00
parent 3804d91118
commit 2130c78da9
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
4 changed files with 29 additions and 4 deletions

View file

@ -376,7 +376,7 @@ class LiteralValue(Value):
try:
int(self.tokens["value"])
except:
raise CompileError(f"Cannot convert {self.tokens['value']} to integer")
raise CompileError(f"Cannot convert {self.group.tokens['value']} to integer")
elif isinstance(type, gir.UIntType):
try:
@ -384,19 +384,31 @@ class LiteralValue(Value):
if int(self.tokens["value"]) < 0:
raise Exception()
except:
raise CompileError(f"Cannot convert {self.tokens['value']} to unsigned integer")
raise CompileError(f"Cannot convert {self.group.tokens['value']} to unsigned integer")
elif isinstance(type, gir.FloatType):
try:
float(self.tokens["value"])
except:
raise CompileError(f"Cannot convert {self.tokens['value']} to float")
raise CompileError(f"Cannot convert {self.group.tokens['value']} to float")
elif isinstance(type, gir.StringType):
pass
elif isinstance(type, gir.Class) or isinstance(type, gir.Interface):
parseable_types = [
"Gdk.Paintable",
"Gdk.Texture",
"Gdk.Pixbuf",
"GLib.File",
"Gtk.ShortcutTrigger",
"Gtk.ShortcutAction",
]
if type.full_name not in parseable_types:
raise CompileError(f"Cannot convert {self.group.tokens['value']} to {type.full_name}")
elif type is not None:
raise CompileError(f"Cannot convert {self.tokens['value']} to {type.full_name}")
raise CompileError(f"Cannot convert {self.group.tokens['value']} to {type.full_name}")
class Flag(AstNode):

View file

@ -0,0 +1,5 @@
using Gtk 4.0;
Gtk.Shortcut {
trigger: "Escape";
}

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkShortcut">
<property name="trigger">Escape</property>
</object>
</interface>

View file

@ -104,6 +104,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample("layout")
self.assert_sample("menu")
self.assert_sample("object_prop")
self.assert_sample("parseable")
self.assert_sample("property")
self.assert_sample("signal")
self.assert_sample("size_group")