forked from craigmjohnston/grunsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindowViewModel.cs
65 lines (54 loc) · 1.85 KB
/
MainWindowViewModel.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
56
57
58
59
60
61
62
63
64
65
namespace GrunCS
{
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using GrunCS.Annotations;
using GrunCS.Graphs;
public class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
this.Graph = new TokenGraph();
this.DrawTraverse(this.Graph, MainWindow.Tree, MainWindow.Parser);
}
public event PropertyChangedEventHandler PropertyChanged;
public TokenGraph Graph { get; private set; }
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private PayloadVertex DrawTraverse(TokenGraph graph, ITree tree, Parser parser)
{
PayloadVertex vertex;
if (tree is IErrorNode)
{
vertex = new ErrorVertex((IErrorNode)tree);
}
else if (tree is IRuleNode)
{
vertex = new RuleVertex((IRuleNode)tree.Payload);
}
else if (tree.Payload is IToken)
{
vertex = new TokenVertex((IToken)tree.Payload);
}
else
{
throw new ArgumentException();
}
graph.AddVertex(vertex);
for (int i = 0; i < tree.ChildCount; i++)
{
var childVertex = this.DrawTraverse(graph, tree.GetChild(i), parser);
var edge = new TokenEdge(vertex, childVertex);
graph.AddEdge(edge);
}
return vertex;
}
}
}