Creating squiggles using an abstract syntax tree

Situation:

I developed a scanner, parser, and various AST classes for the small programming language used, my hobby. The analyzer, using a scanner, builds a heterogeneous AST, on which I do some manipulations. In the past, I created plugins / add-ons for some IDEs to highlight syntax and some other elements.

The problem is errors: the parser generates some and has access to the tokens that make up the instruction. However, some errors occur only later, for example, inability to resolve the identifier. I would like to display squiggles under such identifiers or other erroneous tokens. Not only that, I like the ability to manipulate my AST nodes without losing all comments, spaces, etc. In my original document.

When creating a new statement in my AST, I can easily add the tokens that make up this statement as children. But...

Question:

If reasonably doable, I want to enable support for displaying squiggles. This requires the operator to know where the tokens that make up the operator are located. Unfortunately, there are options in the application that sometimes include more tokens, sometimes fewer tokens. If I were doing read-only AST, that would not be a problem. However, I want my AST to be read-written for refactoring! This means that changing the statement in AST essentially means adding tokens (child elements of the statement), and therefore, the Statement class must be able to repair itself.

This would allow AST to be classified using parsing code and no longer support problem separation!

Technical details:

, AssignmentStatement factory, , .

-node , :

:

AssemblyDeclaration
    .. Statement ..
    .. Statement ..
    ClassDeclaration
       .. Token .. // one or more that make up the entire class statement
       .. Token .. // one or more that make up the entire class statement
       Statement(s)
         .. Token .. // Which have their own tokens that make up the statement .. and possibly have sub-nodes of their own such as Expressions which have -their- own Tokens that comprise it.

ast node, , , , .

public abstract class BaseAstNode : IList<BaseAstNode>
{
    ... implementation of IList<BaseAstNode>
    ... implementation of Visitor Pattern
    ... implementation of Clonable
}    

public sealed class AssignmentStatement : BaseAstNode 
{
public Expression Expression { get; set; }; // Setting this will alter the Tokens (children!) of this node, possibly even ADD Tokens!
public TypeReference Target { get; set; }
}

public sealed class PrimitiveNumberExpression : Expression // is a BaseAstNode
{
public int Value { get; set; } // Setting this will alter the Tokens (children!) of this node!
}

public abstract class Token : BaseAstNode
{
    public Layers Layer { get; set; }
    public TokenType TokenType { get; set; }
    public int Column { get; set; }
    public int Line { get; set; }
    public int Position { get; set; }
    public abstract int Length { get; set; }
    public virtual string Value { get; set; }
    public override string ToString(){}
}

? ?

+3
1

, , , , ( ). , , ... , , .

, ( ), , . ?

, , , , , ( ).

: , , , , . :

  • <Token> BaseAstNode.
  • , , .

, , .

2:. , , BaseAstNode, ( , ). , . , , . System.Xml, . , XML . XmlNode, , BaesAstNode. XmlElement, , Token. XmlDocument - XmlNode, LoadXml, .

, Xml, ParseCode ClassDeclaration, . BaseAstNode BaseAstNode, . . , , , ParseCode, , , , , . , , , .

0

All Articles