
This post continues our series on new features coming to SyntaxEditor 2011.1, primarily centered around our LL(*) Parser Framework.
In our last post, we discussed how our goal for enhancements was to add support for the creation of type-specific AST nodes in our parser grammars. This means that instead of always creating default generic AST node instances, we’d be creating different AST node class instances based on context.
In today’s post, we’re going to look at new features added to our Language Designer application which focus on AST node code generation.
Multiple Parser Support Added
Before we get into the AST node code generation features, one new feature added to the Language Designer is the ability to select which parser to debug when loading an assembly that has more than one parser defined in it. In the past, the first parser found would be used.
Now, if more than one parser is defined in an assembly when going to debug a grammar, it will prompt you for which parser to select.
AST Node Code Generation
As mentioned before, 2011.1 adds support for type-specific AST nodes. We’ll get into how to create them in the grammar in our next post. But before we get to that, let’s first show how to create the AST node classes.
All AST nodes need to implement the IAstNode interface. We’ve added a new AstNodeBase class that implements IAstNode and is intended to be used as the base class for type-specific AST node classes.
Even with that, hand-writing type-specific AST node classes is a tedious task. For instance in our .NET Languages Add-on that is in development, we have over 130 AST node classes. We certainly don’t want to write that many classes by hand. So what we’ve come up with is additions to the Language Designer that make it easy to design your AST node classes and have it code generate them for you.
AST Node Configuration
When you run the 2011.1 version Language Designer, you’ll notice a new AST Nodes button at the top. That opens the pane that allows you to design your AST nodes.
In the screenshot below you can see that the AssignmentExpression node is selected. The property grid control on the right allows us to alter the properties of the selected node(s).

Properties include:
- Description – A text description of the node used in code generated comments.
- Inherits – The base class of the node. It has a dropdown for quick selection of other AST nodes defined in the language project..
- Key – The unique name that identifies the node, and is used as the code generated class name.
AST Node Property Configuration
Now let’s click on one of the AST node properties for the selected node in the bottom list on the left.

Notice how the property grid now shows properties of the selected AST node property. In this case, information for the OperatorType property is displayed.
Properties include:
- Description – A text description of the property used in generated comments.
- Key – The unique name that identifies the node, and is used as the code generated property name.
- PropertyType – The kind of property (described more below).
- Type – The Type returned by the property, which can be an AST node type name, a string, etc.
AST Node Property Types
There are four AST node property types:
- Simple – Gets or sets a simple CLR type object. Supported types are strings, ints, bools, enums, and flags enums.
- SimpleList – Gets an IList<T> containing a collection of strings, ints, bools, or enums.
- AstNode – Gets or sets an AST node object.
- AstNodeList - Gtes an IList<T> containing a collection of AST nodes.
The property types that relate to child AST nodes are separated out since code generation will add special code to generated AST node classes to allow those to be enumerated through as child nodes.
Code Generation
Now lets click the Configuration button.

You’ll note that there are a number of AST node classes listed in the files that we can code generate. In 2011.1, all code generated C# and VB files now have a .g in their filenames to mark them as generated files.
If we double-click a file in the list, we can see what will be code generated:

In the above screenshot, I opened the CastExpression code. While we can’t see the entire list of members that is generated from this screenshot, it has properties that wrap the fields displayed there, along with other members that help implement the IAstNode interface.
An important thing to note is that all AST node classes are generated as partial classes. This allows you to create your own CastExpression.cs in the same project that adds other members to the class. You may wish to override ToString or add other helper methods. The ability to totally customize everything is a key feature of this design.
Summary
Today’s post showed how the 2011.1 Language Designer makes it easy to code generate AST node classes. You simply set a few properties and everything is done for you.
Now that we have a full array of type-specific AST nodes defined, in the next post we’ll examine the grammar enhancements that allow us to create these nodes while parsing and set their properties.