Skip to content

Commit da4dab3

Browse files
committed
Added support for displaying the parse tree graph in GrunWin as well as making the graph interactive for source code selection.
1 parent 1ef5bbc commit da4dab3

File tree

10 files changed

+109
-13
lines changed

10 files changed

+109
-13
lines changed

Grun.Net.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</s:String>
379379
<s:Boolean x:Key="/Default/UserDictionary/Words/=Diesl/@EntryIndexedValue">True</s:Boolean>
380380
<s:Boolean x:Key="/Default/UserDictionary/Words/=Edgerunner/@EntryIndexedValue">True</s:Boolean>
381381
<s:Boolean x:Key="/Default/UserDictionary/Words/=EMDK/@EntryIndexedValue">True</s:Boolean>
382+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Grapher/@EntryIndexedValue">True</s:Boolean>
382383
<s:Boolean x:Key="/Default/UserDictionary/Words/=Grun/@EntryIndexedValue">True</s:Boolean>
383384
<s:Boolean x:Key="/Default/UserDictionary/Words/=handheld/@EntryIndexedValue">True</s:Boolean>
384385
<s:Boolean x:Key="/Default/UserDictionary/Words/=keyguard/@EntryIndexedValue">True</s:Boolean>

Grun/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ private static void LoadGui(string data, GrammarReference grammar, string parser
162162
visualAnalyzer.SetSourceCode(data);
163163
visualAnalyzer.SetGrammar(grammar);
164164
visualAnalyzer.SetDefaultParserRule(parserRule);
165-
visualAnalyzer.ParseSource();
166165
Application.Run(visualAnalyzer);
167166
}
168167
}

Grun/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.20064.8")]
36-
[assembly: AssemblyFileVersion("1.0.20064.8")]
35+
[assembly: AssemblyVersion("1.0.20065.0")]
36+
[assembly: AssemblyFileVersion("1.0.20065.0")]

GunWin/GrammarSelector.Designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GunWin/GunWin.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@
119119
<None Include="App.config" />
120120
</ItemGroup>
121121
<ItemGroup>
122+
<ProjectReference Include="..\Code Grapher\Code Grapher.csproj">
123+
<Project>{ec6f674c-4d48-40c4-b28b-85627c1ea3ce}</Project>
124+
<Name>Code Grapher</Name>
125+
</ProjectReference>
122126
<ProjectReference Include="..\TestRig\TestRig.csproj">
123127
<Project>{4e020f1d-27bf-4853-8573-474d7edb4e72}</Project>
124128
<Name>TestRig</Name>

GunWin/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ static void Main(string[] args)
6363
if (!string.IsNullOrEmpty(grammarRule))
6464
visualAnalyzer.SetDefaultParserRule(grammarRule);
6565

66-
visualAnalyzer.ParseSource();
67-
6866
Application.Run(visualAnalyzer);
6967
}
7068
}

GunWin/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.20064.17")]
36-
[assembly: AssemblyFileVersion("1.0.20064.17")]
35+
[assembly: AssemblyVersion("1.0.20065.23")]
36+
[assembly: AssemblyFileVersion("1.0.20065.23")]

GunWin/VisualAnalyzer.Designer.cs

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GunWin/VisualAnalyzer.cs

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@
4141
using System.Linq;
4242
using System.Windows.Forms;
4343

44+
using Antlr4.Runtime;
45+
using Antlr4.Runtime.Tree;
46+
47+
using FastColoredTextBoxNS;
48+
49+
using Microsoft.Msagl.Drawing;
50+
using Microsoft.Msagl.GraphViewerGdi;
51+
using Microsoft.Msagl.Layout.Layered;
52+
4453
using Org.Edgerunner.ANTLR4.Tools.Testing.Grammar;
4554

4655
namespace Org.Edgerunner.ANTLR4.Tools.Testing.GrunWin
@@ -56,6 +65,8 @@ public partial class VisualAnalyzer : Form
5665

5766
private List<string> _ParserRules;
5867

68+
private GViewer _Viewer;
69+
5970
private string _DefaultRule;
6071

6172
#region Constructors And Finalizers
@@ -116,10 +127,58 @@ public bool ParseWithTracing
116127

117128
private void LoadParserRules()
118129
{
119-
cmbRules.DataSource = _ParserRules;
130+
cmbRules.DataSource = _ParserRules.OrderBy(x => x).Distinct().ToList();
120131
cmbRules.Refresh();
121132
}
122133

134+
private void InitializeGraphCanvas()
135+
{
136+
SuspendLayout();
137+
_Viewer = new GViewer();
138+
pnlGraph.Controls.Add(_Viewer);
139+
_Viewer.Dock = DockStyle.Fill;
140+
ResumeLayout();
141+
_Viewer.LayoutAlgorithmSettingsButtonVisible = false;
142+
_Viewer.Click += _Viewer_Click;
143+
}
144+
145+
private void ShowSourcePosition(ParserRuleContext context)
146+
{
147+
if (context == null)
148+
return;
149+
150+
var startingPlace = new Place(context.Start.Column, context.start.Line - 1);
151+
var stoppingPlace = new Place(context.Stop.Column + context.stop.Text.Length, context.stop.Line - 1);
152+
153+
CodeEditor.Selection = new Range(CodeEditor, startingPlace, stoppingPlace);
154+
CodeEditor.DoCaretVisible();
155+
CodeEditor.Focus();
156+
}
157+
158+
private void ShowSourcePosition(TerminalNodeImpl node)
159+
{
160+
if (node == null)
161+
return;
162+
163+
var startingPlace = new Place(node.Symbol.Column, node.Symbol.Line - 1);
164+
var stoppingPlace = new Place(node.Symbol.Column + node.Symbol.Text.Length, node.Symbol.Line - 1);
165+
166+
CodeEditor.Selection = new Range(CodeEditor, startingPlace, stoppingPlace);
167+
CodeEditor.DoCaretVisible();
168+
CodeEditor.Focus();
169+
}
170+
171+
private void _Viewer_Click(object sender, EventArgs e)
172+
{
173+
if (_Viewer.SelectedObject is Node node)
174+
{
175+
if (node.UserData is ParserRuleContext context)
176+
ShowSourcePosition(context);
177+
else if (node.UserData is TerminalNodeImpl terminal)
178+
ShowSourcePosition(terminal);
179+
}
180+
}
181+
123182
/// <summary>
124183
/// Parses the source code.
125184
/// </summary>
@@ -131,7 +190,7 @@ public void ParseSource()
131190
if (_ParserRules.Count == 0)
132191
return;
133192

134-
if (string.IsNullOrEmpty(cmbRules.SelectedItem.ToString()))
193+
if (string.IsNullOrEmpty(cmbRules.SelectedItem?.ToString()))
135194
return;
136195

137196
var analyzer = new Grammar.Analyzer(_Grammar, CodeEditor.Text);
@@ -141,13 +200,31 @@ public void ParseSource()
141200
if (ParseWithTracing) options |= ParseOption.Trace;
142201
analyzer.Parse(cmbRules.SelectedItem.ToString(), options);
143202
PopulateTokens(analyzer.DisplayTokens);
203+
BuildParseTreeGraph(analyzer.ParseContext);
144204
}
145205

146206
private void PopulateTokens(IList<TokenViewModel> tokens)
147207
{
148208
tokenListView.SetObjects(tokens);
149209
}
150210

211+
private void BuildParseTreeGraph(ITree tree)
212+
{
213+
if (_Viewer == null)
214+
return;
215+
216+
if (tree == null)
217+
return;
218+
219+
var grapher = new Graphing.ParseTreeGrapher(tree, _ParserRules)
220+
{
221+
BackgroundColor = Color.LightBlue, BorderColor = Color.Black, TextColor = Color.Black
222+
};
223+
var graph = grapher.CreateGraph();
224+
graph.LayoutAlgorithmSettings = new SugiyamaLayoutSettings();
225+
_Viewer.Graph = graph;
226+
}
227+
151228
/// <summary>
152229
/// Sets the source code to be analyzed.
153230
/// </summary>
@@ -166,7 +243,6 @@ public void SetGrammar(GrammarReference grammar)
166243
var scanner = new Scanner();
167244
_Grammar = grammar;
168245
_ParserRules = scanner.GetParserRulesForGrammar(grammar).ToList();
169-
_ParserRules.Sort();
170246
LoadParserRules();
171247
}
172248

@@ -231,5 +307,19 @@ private void loadGrammarToolStripMenuItem_Click(object sender, EventArgs e)
231307

232308
SetGrammar(grammarToLoad);
233309
}
310+
311+
private void VisualAnalyzer_Load(object sender, EventArgs e)
312+
{
313+
InitializeGraphCanvas();
314+
315+
if (!string.IsNullOrEmpty(CodeEditor.Text))
316+
ParseSource();
317+
}
318+
319+
private void cmbRules_SelectedIndexChanged(object sender, EventArgs e)
320+
{
321+
if (cmbRules.Items.Count > 0)
322+
ParseSource();
323+
}
234324
}
235325
}

TestRig/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.20064.8")]
36-
[assembly: AssemblyFileVersion("1.0.20064.8")]
35+
[assembly: AssemblyVersion("1.0.20065.1")]
36+
[assembly: AssemblyFileVersion("1.0.20065.1")]

0 commit comments

Comments
 (0)