nitra
nitra copied to clipboard
NullReferenceException when accessing Dependent Property of decomposed list element
In the below example Foo
is a mandatory element of the Attribute
list. However in the test program it is not present. In the current implementation this causes Foo
to be null
. Consequently setting or retrieving a property on it results in a NullReferenceException
. Instead of null
, Foo
should be AmbiguousOrMissing
.
Test Program:
Bar;
Grammar and AST:
namespace Test
{
/*** Syntax ***/
syntax module TestSyntax
{
using Nitra.Core;
[StartRule]
syntax Test = (Attribute ';' nl)+;
token Attribute
{
| Foo = "Foo";
| Bar = "Bar";
}
}
/*** AST ***/
ast TestAst
{
Foo.Test = "Foo"; // System.NullReferenceException
decompose Attributes
{
Foo : Attribute.Foo;
Bar : Attribute.Bar;
}
Attributes : Attribute*;
}
abstract ast Attribute
{
| Foo { in Test : string; }
| Bar {}
}
/*** Mapping ***/
map syntax TestSyntax.Test -> TestAst
{
Attributes -> Attributes;
}
map syntax TestSyntax.Attribute -> Attribute
{
| Foo -> Foo {}
| Bar -> Bar {}
}
}
The only workaround I've found so far is writing an extension method which does the null
-checks and calling that method on the objet that is potentially null
. However that's really confusing to read. Does anybody have a better idea?