How do I work with the AST in Irony now?

后端 未结 2 855
[愿得一人]
[愿得一人] 2021-02-15 12:20

I have a grammar that works and parses in the Irony console just fine, but I don\'t get anything in the AST treeview. I was following along with the BASIC->Javascript article f

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-15 12:34

    Check out the aptly named Sarcasm project for a reference implementation of a grammar, parser, and AST built on Irony. I found this blog entry by the author to be helpful in building the AST.

    The following is a general purpose guide to getting the AST up and running.

    1. Define your grammar (example)
    2. Create an abstract base class (MyBaseNode) deriving from AstNode (example). Copy/Paste the methods from the example
    3. For each terminal and non-terminal create a new class derived from MyBaseNode and

      1. Override Accept method (example):

      public override void Accept(IMyNodeVisitor visitor) { visitor.Visit(this); }

      1. Override Init (mostly on terminals) or InitChildren (non-terminals) as appropriate. This is where the AST magic happens.
    4. Add an interface IMyNodeVisitor and add a Visit method for each class defined in the previous step (example):

      void Visit(MyDerivedNode1 node);

    5. Set the ASTNodeType for each of your terminals and non-terminals in your grammar from step 1.

      1. For terminals - (example)

        MyTerminal1.AstConfig.NodeType = typeof(MyDerivedNode1);

      2. For non-terminals - (example)

        var MyNonTerminal2 = new NonTerminal("MyNonTerminal2", typeof(MyDerivedNode2));

    6. In the grammar enable AST creation: (example)

      LanguageFlags = LanguageFlags.CreateAst;

提交回复
热议问题