@@ -23,6 +23,7 @@ public partial class frmMain : Form
23
23
private Parser parser = null ;
24
24
private Color selectionBackColor = ColorTranslator . FromHtml ( "#0078D7" ) ;
25
25
private Dictionary < TreeNode , TokenInfo > dictTokenInfo = new Dictionary < TreeNode , TokenInfo > ( ) ;
26
+ private SqlSyntaxError error ;
26
27
27
28
private static List < string > ignoreMethods = new List < string > ( )
28
29
{
@@ -50,7 +51,7 @@ private bool IsSqlParser()
50
51
{
51
52
string parserName = this . cboParser . Text ;
52
53
53
- if ( ! string . IsNullOrEmpty ( parserName ) )
54
+ if ( ! string . IsNullOrEmpty ( parserName ) )
54
55
{
55
56
if ( parserName == nameof ( MySqlParser ) ||
56
57
parserName == nameof ( TSqlParser ) ||
@@ -90,7 +91,7 @@ orderby type.Name
90
91
91
92
private void LoadFromFile ( )
92
93
{
93
- string content = this . GetFileContent ( ) ;
94
+ string content = this . GetFileContent ( ) ;
94
95
this . txtText . Text = this . IsSqlParser ( ) ? content . ToUpper ( ) : content ;
95
96
this . LoadTree ( ) ;
96
97
}
@@ -200,7 +201,11 @@ private void LoadTree()
200
201
201
202
CommonTokenStream tokens = new CommonTokenStream ( lexer ) ;
202
203
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 ) ;
204
209
205
210
ParserInfo info = this . GetParserInfo ( ) ;
206
211
@@ -209,9 +214,16 @@ private void LoadTree()
209
214
return ;
210
215
}
211
216
212
- MethodInfo rootMethod = parserType . GetMethod ( info . EntryRuleName ) ;
217
+ var rootMethod = parserType . GetMethod ( info . EntryRuleName ) ;
218
+
219
+ object value = rootMethod . Invoke ( this . parser , new object [ ] { } ) ;
213
220
214
- object value = rootMethod . Invoke ( parser , new object [ ] { } ) ;
221
+ this . error = errorListener . Error ;
222
+
223
+ if ( this . chkHighlightingErrors . Checked )
224
+ {
225
+ this . HighlightingError ( ) ;
226
+ }
215
227
216
228
TreeNode rootNode = this . CreateTreeNode ( info . EntryRuleName ) ;
217
229
rootNode . Tag = value ;
@@ -225,6 +237,27 @@ private void LoadTree()
225
237
rootNode . EnsureVisible ( ) ;
226
238
}
227
239
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
+
228
261
private void AddChildNodes ( TreeNode node , bool expand )
229
262
{
230
263
object value = node . Tag ;
@@ -339,7 +372,7 @@ private void AddChildNodes(TreeNode node, bool expand)
339
372
childNode . Name = childName ;
340
373
childNode . Tag = tn ;
341
374
342
- if ( tn is ErrorNodeImpl )
375
+ if ( tn is ErrorNodeImpl )
343
376
{
344
377
childNode . ForeColor = Color . Red ;
345
378
}
@@ -424,6 +457,7 @@ private void ShowNodeDetails(TreeNode node)
424
457
425
458
this . ClearValues ( ) ;
426
459
this . ClearSelection ( ) ;
460
+ this . HighlightingError ( ) ;
427
461
428
462
object value = node . Tag ;
429
463
@@ -456,7 +490,7 @@ private void ShowNodeDetails(TreeNode node)
456
490
this . txtText . ScrollToCaret ( ) ;
457
491
}
458
492
459
- this . txtMessage . Text = this . GetTreeNodePath ( node ) ;
493
+ this . txtMessage . Text = this . GetTreeNodePath ( node ) ;
460
494
}
461
495
462
496
private TokenInfo GetTokenInfo ( dynamic value )
@@ -681,7 +715,7 @@ private void txtMessage_MouseDoubleClick(object sender, MouseEventArgs e)
681
715
private void txtFile_MouseDoubleClick ( object sender , MouseEventArgs e )
682
716
{
683
717
this . SelectFile ( ) ;
684
- }
718
+ }
685
719
686
720
private void tsmiPaste_Click ( object sender , EventArgs e )
687
721
{
@@ -702,6 +736,7 @@ private void cboParser_SelectedIndexChanged(object sender, EventArgs e)
702
736
private void tsmiClearSelection_Click ( object sender , EventArgs e )
703
737
{
704
738
this . ClearSelection ( ) ;
739
+ this . HighlightingError ( ) ;
705
740
}
706
741
707
742
private void tsmiNavigateToTreeNode_Click ( object sender , EventArgs e )
@@ -758,7 +793,6 @@ private void txtText_MouseUp(object sender, MouseEventArgs e)
758
793
{
759
794
if ( e . Button == MouseButtons . Right )
760
795
{
761
- this . tsmiClearSelection . Visible = this . txtText . SelectionLength > 0 ;
762
796
this . tsmiNavigateToTreeNode . Visible = this . txtText . SelectionLength > 0 ;
763
797
this . tsmiPaste . Visible = this . txtText . Text . Length == 0 || this . txtText . SelectionLength == this . txtText . Text . Length ;
764
798
@@ -779,16 +813,16 @@ private void txtText_KeyDown(object sender, KeyEventArgs e)
779
813
{
780
814
this . txtFile . Text = "" ;
781
815
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 ) )
783
817
{
784
818
e . SuppressKeyPress = true ;
785
819
786
820
this . txtText . Clear ( ) ;
787
821
788
822
string content = Clipboard . GetText ( ) ;
789
823
790
- this . txtText . AppendText ( content . ToUpper ( ) ) ;
791
- }
824
+ this . txtText . AppendText ( content . ToUpper ( ) ) ;
825
+ }
792
826
793
827
this . LoadTree ( ) ;
794
828
}
@@ -835,5 +869,10 @@ private void tsmiClearContent_Click(object sender, EventArgs e)
835
869
{
836
870
this . txtText . Clear ( ) ;
837
871
}
872
+
873
+ private void chkHighlightingErrors_CheckedChanged ( object sender , EventArgs e )
874
+ {
875
+ this . HighlightingError ( ) ;
876
+ }
838
877
}
839
878
}
0 commit comments