forked from craigmjohnston/grunsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
84 lines (69 loc) · 2.38 KB
/
MainWindow.xaml.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace GrunCS
{
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using GrunCS.Graphs;
using QuickGraph.Data;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private MainWindowViewModel viewModel;
public static ITree Tree { get; set; }
public static Parser Parser { get; set; }
public static Lexer Lexer { get; set; }
public static string[] LexerSymbolicNames { get; set; }
public MainWindow()
{
this.viewModel = new MainWindowViewModel();
this.DataContext = this.viewModel;
this.InitializeComponent();
this.TvMain.Items.Add(this.Traverse(MainWindow.Tree, MainWindow.Parser));
}
private TreeViewItem Traverse(ITree tree, Parser parser)
{
string text = null;
if (tree is IRuleNode)
{
text = MainWindow.Parser.RuleNames[((IRuleNode)tree).RuleContext.RuleIndex];
}
else if (tree.Payload is IToken)
{
var token = (IToken) tree.Payload;
if (string.IsNullOrWhiteSpace(token.Text) && (token.Text == null || !token.Text.Contains("\n")))
{
text = $"<{MainWindow.LexerSymbolicNames[token.Type]}>";
}
else
{
text = token.Text.Replace("\n", "\\n");
}
}
var result = new TreeViewItem
{
Header = text,
FontWeight = tree is IRuleNode ? FontWeights.Bold : FontWeights.Normal
};
if (tree is IErrorNode)
{
result.Background = Brushes.IndianRed;
}
for (int i = 0; i < tree.ChildCount; i++)
{
var item = this.Traverse(tree.GetChild(i), parser);
result.Items.Add(item);
}
return result;
}
}
}