Add validation for flag values

This commit is contained in:
James Westman 2022-03-02 14:06:57 -06:00
parent bebe784d1d
commit 8ea06e8a78
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
4 changed files with 25 additions and 0 deletions

View file

@ -109,9 +109,25 @@ class LiteralValue(Value):
class Flag(AstNode):
grammar = UseIdent("value")
@validate()
def validate_for_type(self):
type = self.parent.parent.value_type
if isinstance(type, gir.Bitfield) and self.tokens["value"] not in type.members:
raise CompileError(
f"{self.tokens['value']} is not a member of {type.full_name}",
did_you_mean=(self.tokens['value'], type.members.keys()),
)
class FlagsValue(Value):
grammar = [Flag, "|", Delimited(Flag, "|")]
@validate()
def parent_is_bitfield(self):
type = self.parent.value_type
if not isinstance(type, gir.Bitfield):
raise CompileError(f"{type.full_name} is not a bitfield type")
def emit_xml(self, xml: XmlEmitter):
xml.put_text("|".join([flag.tokens["value"] for flag in self.children[Flag]]))