Table of Contents

Property SyntaxTree

Namespace
Alternet.Syntax
Assembly
Alternet.Syntax.v9.dll

SyntaxTree

Gets or sets a hierarchical collection of ISyntaxNode elements representing abstract syntax tree of the text being parsed.

[Browsable(false)]
public virtual ISyntaxTree SyntaxTree { get; set; }

Property Value

ISyntaxTree

Examples

Here is how to use a SyntaxTree in the C# code:

using Alternet.Syntax;
using Alternet.Editor;
using System.Collections.Generic;

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        var parser = new SyntaxParser();
        var edit = new SyntaxEdit();
        edit.Parent = this;
        edit.Lexer = parser;
        parser.Options = SyntaxOptions.SmartIndent | SyntaxOptions.CodeCompletion | SyntaxOptions.Outline | SyntaxOptions.SyntaxErrors;

        string fileName = "myfile.cs";
        if (System.IO.File.Exists(fileName))
        {
            parser.FileName = fileName;
            edit.LoadFile(fileName);
            parser.ReparseText();
            IList<ISyntaxError> errors = new List<ISyntaxError>();
            GetErrors(SyntaxTree.Root, errors);
        }
    }

    private void GetErrors(ISyntaxNode node, IList<ISyntaxError> errors)
    {
        if (node.HasErrors)
        {
            foreach (ISyntaxError error in node.Errors)
                errors.Add(error);
        }

        if (node.HasChildren)
        {
            foreach (ISyntaxNode child in node.ChildList)
            {
                GetErrors(child, errors);
            }
        }
    }
}

Here is how to use a SyntaxTree in the Visual Basic code:

Imports Alternet.Syntax
Imports Alternet.Editor
Imports System.Collections.Generic
Imports System

Partial Public Class Form1
    Inherits Form

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim parser = New SyntaxParser()
        Dim edit = New SyntaxEdit()
        edit.Parent = Me
        edit.Lexer = parser
        parser.Options = SyntaxOptions.SmartIndent Or SyntaxOptions.CodeCompletion Or SyntaxOptions.Outline Or SyntaxOptions.SyntaxErrors
        Dim fileName As String = "myfile.cs"

        If System.IO.File.Exists(fileName) Then
            parser.FileName = fileName
            edit.LoadFile(fileName)
            parser.ReparseText()
            Dim errors As IList(Of ISyntaxError) = New List(Of ISyntaxError)()
            GetErrors(SyntaxTree.Root, errors)
        End If
    End Sub

    Private Sub GetErrors(ByVal node As ISyntaxNode, ByVal errors As IList(Of ISyntaxError))
        If node.HasErrors Then

            For Each error As ISyntaxError In node.Errors
                errors.Add(error)
            Next
        End If

        If node.HasChildren Then

            For Each child As ISyntaxNode In node.ChildList
                GetErrors(child, errors)
            Next
        End If
    End Sub
End Class

Remarks

Analyze the syntax and grammar of the source code using the SyntaxTree property to see if there are any syntax errors.�