Skip to content

Commit ba29267

Browse files
Basic command line (tokens, error readout) and rough WPF GUI
1 parent 9f4b3b5 commit ba29267

20 files changed

+2216
-0
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.idea
2+
3+
*.suo
4+
*.suo
5+
*.user
6+
.vs/
7+
[Bb]in/
8+
[Oo]bj/
9+
[Pp]ackages/
10+
11+
Thumbs.db
12+
Desktop.ini
13+
.DS_Store

App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>

App.xaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="GrunCS.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:GrunCS"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

App.xaml.cs

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
namespace GrunCS
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text.RegularExpressions;
8+
using System.Windows;
9+
using Antlr4.Runtime;
10+
using NDesk.Options;
11+
12+
/// <summary>
13+
/// Interaction logic for App.xaml
14+
/// </summary>
15+
public partial class App
16+
{
17+
protected override void OnStartup(StartupEventArgs e)
18+
{
19+
base.OnStartup(e);
20+
21+
bool showHelp = false;
22+
bool showGui = false;
23+
bool showTokens = false;
24+
bool showTree = false;
25+
string configFileLocation = null;
26+
27+
// parse command line args
28+
var options = new OptionSet
29+
{
30+
{ "to|tokens", "Print the tokens", v => showTokens = true},
31+
{ "tr|tree", "Print the tree", v => showTree = true },
32+
{ "g|gui", "Show the GUI", v => showGui = true },
33+
{ "c|config", "Specify an explicit config file (gruncs.json) location", v => configFileLocation = v },
34+
{ "h|help", "show this message and exit", v => showHelp = true },
35+
};
36+
37+
List<string> extraArgs = null;
38+
try
39+
{
40+
extraArgs = options.Parse(e.Args);
41+
}
42+
catch (OptionException exception)
43+
{
44+
this.PrintInputException(exception);
45+
this.Shutdown();
46+
return;
47+
}
48+
49+
// if help flag present, quit out
50+
if (showHelp)
51+
{
52+
options.WriteOptionDescriptions(Console.Out);
53+
this.Shutdown();
54+
return;
55+
}
56+
57+
if (extraArgs == null || extraArgs.Count < 3)
58+
{
59+
this.PrintInputException(new ArgumentException("Necessary arguments missing."));
60+
this.Shutdown();
61+
return;
62+
}
63+
64+
// load and parse
65+
try
66+
{
67+
string testFilePath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, extraArgs[2]));
68+
69+
Main main = new Main
70+
{
71+
ShowTokens = showTokens,
72+
ShowTree = showTree,
73+
ConfigFileLocation = configFileLocation
74+
};
75+
76+
main.Process(Environment.CurrentDirectory, extraArgs[0], extraArgs[1], testFilePath);
77+
}
78+
catch (Exception exception)
79+
{
80+
Console.WriteLine("There was an error when trying to parse.");
81+
Console.WriteLine(exception.Message);
82+
this.Shutdown();
83+
throw;
84+
}
85+
86+
// don't allow the window to show if the gui flag isn't set
87+
if (!showGui)
88+
{
89+
this.Shutdown();
90+
}
91+
}
92+
93+
94+
private void PrintInputException(Exception exception)
95+
{
96+
Console.Write("gruncs: ");
97+
Console.WriteLine(exception.Message);
98+
Console.WriteLine("Try `gruncs --help for more information.");
99+
}
100+
101+
102+
private void Shutdown()
103+
{
104+
System.Windows.Application.Current.Shutdown();
105+
}
106+
}
107+
}

Config.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace GrunCS
2+
{
3+
using Newtonsoft.Json;
4+
5+
[JsonObject]
6+
public class Config
7+
{
8+
[JsonProperty]
9+
public string[] References { get; private set; } = new string[0];
10+
11+
[JsonProperty]
12+
public string[] Include { get; private set; } = new string[0];
13+
}
14+
}

Graphs/TokenEdge.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace GrunCS.Graphs
2+
{
3+
using QuickGraph;
4+
5+
public class TokenEdge : Edge<PayloadVertex>
6+
{
7+
public TokenEdge(PayloadVertex source, PayloadVertex target) : base(source, target)
8+
{
9+
}
10+
}
11+
}

Graphs/TokenGraph.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace GrunCS.Graphs
2+
{
3+
using QuickGraph;
4+
5+
public class TokenGraph : BidirectionalGraph<PayloadVertex, TokenEdge>
6+
{
7+
}
8+
}

Graphs/TokenGraphLayout.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace GrunCS.Graphs
2+
{
3+
using System.Windows;
4+
using GraphSharp.Controls;
5+
6+
public class TokenGraphLayout : GraphLayout<PayloadVertex, TokenEdge, TokenGraph>
7+
{
8+
protected override void OnDragEnter(DragEventArgs e)
9+
{
10+
}
11+
}
12+
}

Graphs/TokenVertex.cs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace GrunCS.Graphs
2+
{
3+
using Antlr4.Runtime;
4+
using Antlr4.Runtime.Tree;
5+
6+
public class PayloadVertex
7+
{
8+
public virtual string Text { get; }
9+
10+
public virtual bool IsError { get; }
11+
12+
public virtual string Type { get; }
13+
}
14+
15+
public class ErrorVertex : PayloadVertex
16+
{
17+
public ErrorVertex(IErrorNode errorNode)
18+
{
19+
this.ErrorNode = errorNode;
20+
}
21+
22+
public IErrorNode ErrorNode { get; }
23+
24+
public override string Text => this.ErrorNode.Symbol.Text;
25+
26+
public override bool IsError => true;
27+
28+
public override string Type => "Error";
29+
}
30+
31+
public class TokenVertex : PayloadVertex
32+
{
33+
public TokenVertex(IToken token)
34+
{
35+
this.Token = token;
36+
}
37+
38+
public IToken Token { get; }
39+
40+
public override string Text
41+
{
42+
get
43+
{
44+
string text = this.Token.Text;
45+
46+
if (string.IsNullOrWhiteSpace(text) && !text.Contains("\n"))
47+
{
48+
return $"<{MainWindow.LexerSymbolicNames[this.Token.Type]}>";
49+
}
50+
51+
return this.Token.Text.Replace("\n", "\\n");
52+
}
53+
}
54+
55+
public override string Type => "Token";
56+
}
57+
58+
public class RuleVertex : PayloadVertex
59+
{
60+
public RuleVertex(IRuleNode ruleNode)
61+
{
62+
this.RuleNode = ruleNode;
63+
}
64+
65+
public IRuleNode RuleNode { get; }
66+
67+
public override string Text => MainWindow.Parser.RuleNames[this.RuleNode.RuleContext.RuleIndex];
68+
69+
public override string Type => "Rule";
70+
}
71+
}

0 commit comments

Comments
 (0)