Skip to content

Commit 08b9290

Browse files
author
jingwu1986@hotmail.com
committed
1. make up missing files
2. highlighting parse error for Viewer
1 parent e7491d7 commit 08b9290

36 files changed

+137563
-20
lines changed

CodeParser.Viewer/CodeParser.Viewer.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@
5858
<Compile Include="frmMain.designer.cs">
5959
<DependentUpon>frmMain.cs</DependentUpon>
6060
</Compile>
61+
<Compile Include="Model\SqlSyntaxError.cs" />
6162
<Compile Include="Model\TokenInfo.cs" />
6263
<Compile Include="Program.cs" />
6364
<Compile Include="Properties\AssemblyInfo.cs" />
65+
<Compile Include="SqlSyntaxErrorListener.cs" />
6466
<EmbeddedResource Include="frmMain.resx">
6567
<DependentUpon>frmMain.cs</DependentUpon>
6668
</EmbeddedResource>
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
using System.Text;
3+
4+
namespace CodeParser.Viewer
5+
{
6+
public class SqlSyntaxError
7+
{
8+
public List<SqlSyntaxErrorItem> Items = new List<SqlSyntaxErrorItem>();
9+
10+
public override string ToString()
11+
{
12+
StringBuilder sb = new StringBuilder();
13+
14+
foreach(SqlSyntaxErrorItem item in this.Items)
15+
{
16+
sb.AppendLine($"{item.Text}(Line={item.Line},Column={item.Column},StartIndex={item.StartIndex},StopIndex={item.StopIndex}):{item.Message};");
17+
}
18+
19+
return sb.ToString();
20+
}
21+
}
22+
23+
public class SqlSyntaxErrorItem
24+
{
25+
public int Line { get; set; }
26+
public int Column { get; set; }
27+
public int StartIndex { get; set; }
28+
public int StopIndex { get; set; }
29+
public string Text { get; set; }
30+
public string Message { get; set; }
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Antlr4.Runtime;
2+
using System.IO;
3+
4+
namespace CodeParser.Viewer
5+
{
6+
public class SqlSyntaxErrorListener : BaseErrorListener
7+
{
8+
public bool HasError => this.Error != null && this.Error.Items.Count > 0;
9+
public SqlSyntaxError Error { get; private set; }
10+
11+
public override void SyntaxError(TextWriter output, IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
12+
{
13+
if (this.Error == null)
14+
{
15+
this.Error = new SqlSyntaxError();
16+
}
17+
18+
if (offendingSymbol is CommonToken token)
19+
{
20+
SqlSyntaxErrorItem errorItem = new SqlSyntaxErrorItem();
21+
22+
errorItem.StartIndex = token.StartIndex;
23+
errorItem.StopIndex = token.StopIndex;
24+
errorItem.Line = token.Line;
25+
errorItem.Column = token.Column + 1;
26+
errorItem.Text = token.Text;
27+
errorItem.Message = msg;
28+
29+
this.Error.Items.Add(errorItem);
30+
}
31+
32+
base.SyntaxError(output, recognizer, offendingSymbol, line, charPositionInLine, msg, e);
33+
}
34+
}
35+
}

CodeParser.Viewer/frmMain.cs

+51-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public partial class frmMain : Form
2323
private Parser parser = null;
2424
private Color selectionBackColor = ColorTranslator.FromHtml("#0078D7");
2525
private Dictionary<TreeNode, TokenInfo> dictTokenInfo = new Dictionary<TreeNode, TokenInfo>();
26+
private SqlSyntaxError error;
2627

2728
private static List<string> ignoreMethods = new List<string>()
2829
{
@@ -50,7 +51,7 @@ private bool IsSqlParser()
5051
{
5152
string parserName = this.cboParser.Text;
5253

53-
if(!string.IsNullOrEmpty(parserName))
54+
if (!string.IsNullOrEmpty(parserName))
5455
{
5556
if (parserName == nameof(MySqlParser) ||
5657
parserName == nameof(TSqlParser) ||
@@ -90,7 +91,7 @@ orderby type.Name
9091

9192
private void LoadFromFile()
9293
{
93-
string content= this.GetFileContent();
94+
string content = this.GetFileContent();
9495
this.txtText.Text = this.IsSqlParser() ? content.ToUpper() : content;
9596
this.LoadTree();
9697
}
@@ -200,7 +201,11 @@ private void LoadTree()
200201

201202
CommonTokenStream tokens = new CommonTokenStream(lexer);
202203

203-
this.parser = (Parser)Activator.CreateInstance(parserType, new object[] { tokens });
204+
this.parser = (Parser)Activator.CreateInstance(parserType, new object[] { tokens });
205+
206+
var errorListener = new SqlSyntaxErrorListener();
207+
208+
this.parser.AddErrorListener(errorListener);
204209

205210
ParserInfo info = this.GetParserInfo();
206211

@@ -209,9 +214,16 @@ private void LoadTree()
209214
return;
210215
}
211216

212-
MethodInfo rootMethod = parserType.GetMethod(info.EntryRuleName);
217+
var rootMethod = parserType.GetMethod(info.EntryRuleName);
218+
219+
object value = rootMethod.Invoke(this.parser, new object[] { });
213220

214-
object value = rootMethod.Invoke(parser, new object[] { });
221+
this.error = errorListener.Error;
222+
223+
if (this.chkHighlightingErrors.Checked)
224+
{
225+
this.HighlightingError();
226+
}
215227

216228
TreeNode rootNode = this.CreateTreeNode(info.EntryRuleName);
217229
rootNode.Tag = value;
@@ -225,6 +237,27 @@ private void LoadTree()
225237
rootNode.EnsureVisible();
226238
}
227239

240+
public void HighlightingError()
241+
{
242+
if (this.error != null)
243+
{
244+
bool enabled = this.chkHighlightingErrors.Checked;
245+
246+
foreach (var item in this.error.Items)
247+
{
248+
this.txtText.SelectionStart = item.StartIndex;
249+
this.txtText.SelectionLength = item.StopIndex - item.StartIndex + 1;
250+
251+
this.txtText.SelectionColor = enabled ? Color.Red : Color.Black;
252+
this.txtText.SelectionBackColor = enabled ? Color.Yellow : Color.White;
253+
}
254+
}
255+
256+
this.txtText.SelectionStart = 0;
257+
this.txtText.SelectionLength = 0;
258+
this.txtText.Focus();
259+
}
260+
228261
private void AddChildNodes(TreeNode node, bool expand)
229262
{
230263
object value = node.Tag;
@@ -339,7 +372,7 @@ private void AddChildNodes(TreeNode node, bool expand)
339372
childNode.Name = childName;
340373
childNode.Tag = tn;
341374

342-
if(tn is ErrorNodeImpl)
375+
if (tn is ErrorNodeImpl)
343376
{
344377
childNode.ForeColor = Color.Red;
345378
}
@@ -424,6 +457,7 @@ private void ShowNodeDetails(TreeNode node)
424457

425458
this.ClearValues();
426459
this.ClearSelection();
460+
this.HighlightingError();
427461

428462
object value = node.Tag;
429463

@@ -456,7 +490,7 @@ private void ShowNodeDetails(TreeNode node)
456490
this.txtText.ScrollToCaret();
457491
}
458492

459-
this.txtMessage.Text = this.GetTreeNodePath(node);
493+
this.txtMessage.Text = this.GetTreeNodePath(node);
460494
}
461495

462496
private TokenInfo GetTokenInfo(dynamic value)
@@ -681,7 +715,7 @@ private void txtMessage_MouseDoubleClick(object sender, MouseEventArgs e)
681715
private void txtFile_MouseDoubleClick(object sender, MouseEventArgs e)
682716
{
683717
this.SelectFile();
684-
}
718+
}
685719

686720
private void tsmiPaste_Click(object sender, EventArgs e)
687721
{
@@ -702,6 +736,7 @@ private void cboParser_SelectedIndexChanged(object sender, EventArgs e)
702736
private void tsmiClearSelection_Click(object sender, EventArgs e)
703737
{
704738
this.ClearSelection();
739+
this.HighlightingError();
705740
}
706741

707742
private void tsmiNavigateToTreeNode_Click(object sender, EventArgs e)
@@ -758,7 +793,6 @@ private void txtText_MouseUp(object sender, MouseEventArgs e)
758793
{
759794
if (e.Button == MouseButtons.Right)
760795
{
761-
this.tsmiClearSelection.Visible = this.txtText.SelectionLength > 0;
762796
this.tsmiNavigateToTreeNode.Visible = this.txtText.SelectionLength > 0;
763797
this.tsmiPaste.Visible = this.txtText.Text.Length == 0 || this.txtText.SelectionLength == this.txtText.Text.Length;
764798

@@ -779,16 +813,16 @@ private void txtText_KeyDown(object sender, KeyEventArgs e)
779813
{
780814
this.txtFile.Text = "";
781815

782-
if (this.IsSqlParser() && (this.txtText.Text.Length ==0 || this.txtText.SelectedText.Length == this.txtText.Text.Length))
816+
if (this.IsSqlParser() && (this.txtText.Text.Length == 0 || this.txtText.SelectedText.Length == this.txtText.Text.Length))
783817
{
784818
e.SuppressKeyPress = true;
785819

786820
this.txtText.Clear();
787821

788822
string content = Clipboard.GetText();
789823

790-
this.txtText.AppendText(content.ToUpper());
791-
}
824+
this.txtText.AppendText(content.ToUpper());
825+
}
792826

793827
this.LoadTree();
794828
}
@@ -835,5 +869,10 @@ private void tsmiClearContent_Click(object sender, EventArgs e)
835869
{
836870
this.txtText.Clear();
837871
}
872+
873+
private void chkHighlightingErrors_CheckedChanged(object sender, EventArgs e)
874+
{
875+
this.HighlightingError();
876+
}
838877
}
839878
}

CodeParser.Viewer/frmMain.designer.cs

+23-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodeParser/CodeParser.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
66
<Authors>victor-wiki</Authors>
77
<PackageProjectUrl>https://github.com/victor-wiki/CodeParser</PackageProjectUrl>
8-
<Description>Parser code generated from antlr(https://github.com/antlr/grammars-v4), the library includes C, C++, C#, Java, JavaScript, Python, Php, MySql, TSql, PlSql and SQLite.</Description>
8+
<Description>Parser code generated from antlr(https://github.com/antlr/grammars-v4), the library includes C, C++, C#, Java, JavaScript, Python, Php, MySql, TSql, PlSql, Postgres and SQLite.</Description>
99
<Version>2.0.0</Version>
10+
<PackageReleaseNotes>make up missing files</PackageReleaseNotes>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

0 commit comments

Comments
 (0)