-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntaxTreeNode.cs
55 lines (49 loc) · 1.68 KB
/
SyntaxTreeNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using BFParser.Parsers;
using BFParser.SyntaxTreeNodeVisitors;
namespace BFParser
{
public class SyntaxTreeNode
{
public Guid Id { get; }
public string ParsedText { get; }
public string Rest { get; }
public string RuleName => Parser.RuleName;
public CoreParser Parser { get; }
public ReadOnlyCollection<SyntaxTreeNode> Children { get; }
public SyntaxTreeNode(string parsedText, string rest, CoreParser parserWhereWasProducted, IList<SyntaxTreeNode> children)
{
Id = Guid.NewGuid();
ParsedText = parsedText;
Rest = rest;
Parser = parserWhereWasProducted;
Children = (children is null) ? null : new ReadOnlyCollection<SyntaxTreeNode>(children);
}
public void Apply(CoreSyntaxTreeNodeVisitor visitor)
{
visitor.Visit(this);
}
/// <summary>
/// Get visualised AST
/// </summary>
/// <returns>String with graph in dot format</returns>
public string Dot()
{
var visitor = new SyntaxTreeNodeVisualiserVisitor();
Apply(visitor);
return visitor.GetResult() as string;
}
/// <summary>
/// It helps get clear abstract syntax tree
/// </summary>
/// <returns>Returns SyntaxTreeNode graph without trash nodes</returns>
public SyntaxTreeNode Clear()
{
var visitor = new SyntaxTreeNodeClearVisitor();
Apply(visitor);
return visitor.GetResult() as SyntaxTreeNode;
}
}
}