Question

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

Was it helpful?

Solution

Once you set the LanguageFlags.CreateAst flag on the grammar, you must provide additional information about how to create the AST.

You're supposed to be able to set AstContext.Default*Type for 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.NodeType to the type of the AstNode. This type should be accessible, implement IAstInit, and have a public, no-parameters constructor. Accessible in this case means either public or internal with the InternalsVisibleTo attribute.

OTHER TIPS

To be honest, I was facing the same problem and did not understand Jay Bazuzi answer, though it looks like valid one(maybe it's outdated).

If there's anyone like me;

I just inherited my Grammar from Irony.Interpreter.InterpretedLanguageGrammar class, and it works. Also, anyone trying to get AST working, make sure your nodes are "public" :- )

On top of Jay's and Erti-Chris's responses, this thread is also useful:

https://irony.codeplex.com/discussions/361018

The creator of Irony points out the relevant configuration code in InterpretedLanguageGrammar.BuildAst.

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top