I’m getting started with Irony (version Irony_2012_03_15) but I pretty quickly got stuck when trying to generate an AST. Below is a completely strpped language that throws the exception:
[Language("myLang", "0.1", "Bla Bla")]
public class MyLang: Grammar {
public NModel()
: base(false) {
var number = TerminalFactory.CreateCSharpNumber("number");
var binExpr = new NonTerminal("binExpr", typeof(BinaryOperationNode));
var binOp = new NonTerminal("BinOp");
binExpr.Rule = number + binOp + number;
binOp.Rule = ToTerm("+");
RegisterOperators(1, "+");
//MarkTransient(binOp);
this.Root = binExpr;
this.LanguageFlags = Parsing.LanguageFlags.CreateAst; // if I uncomment this line it throws the error
}
}
As soon as I uncomment the last line it throws a NullReferenceException in the grammar explorer or when i want to parse a test. The error is on AstBuilder.cs line 96:
parseNode.AstNode = config.DefaultNodeCreator();
DefaultNodeCreator is a delegate that has not been set.
I’ve tried setting things with MarkTransient etc but no dice.
Can someone help me afloat here? I’m proably missing something obvious. Looked for AST tutorials all over the webs but I can’t seem to find an explanation on how that works.
Thanks in advance,
Gert-Jan
Once you set the
LanguageFlags.CreateAstflag on the grammar, you must provide additional information about how to create the AST.You’re supposed to be able to set
AstContext.Default*Typefor the whole language, but that is currently bugged.Set
TermFlags.NoAstNode. Irony will ignore this node and its children.Set
AstConfig.NodeCreator. This is a delegate that can do the right thing.Set
AstConfig.NodeTypeto the type of the AstNode. This type should be accessible, implementIAstInit, and have a public, no-parameters constructor. Accessible in this case means eitherpublicorinternalwith theInternalsVisibleToattribute.