4141using System . IO ;
4242using System . Linq ;
4343using System . Reflection ;
44+ using System . Runtime . InteropServices ;
4445using System . Text ;
4546using System . Windows . Forms ;
4647
4748using Antlr4 . Runtime . Misc ;
48-
49+ using Colorful ;
4950using CommandLine ;
5051using CommandLine . Text ;
5152
@@ -84,16 +85,23 @@ internal class Program
8485
8586 #region Static
8687
88+ [ DllImport ( "kernel32.dll" , EntryPoint = "SetConsoleMode" , SetLastError = true ,
89+ CharSet = CharSet . Auto , CallingConvention = CallingConvention . StdCall ) ]
90+ private static extern bool SetConsoleMode ( int hConsoleHandle , int dwMode ) ;
91+
8792 [ STAThread ]
8893 // ReSharper disable once MethodTooLong
8994 private static void Main ( string [ ] args )
9095 {
9196 try
9297 {
9398 LoadApplicationSettings ( ) ;
94- //Console.BackgroundColor = _Settings.EditorBackgroundColor;
95- //Console.ForegroundColor = _Settings.EditorTextColor;
96- //FillCurrentLineBackground();
99+ //if (_Settings.EnableConsoleSyntaxHighlighting)
100+ //{
101+ // Console.BackgroundColor = _Settings.EditorBackgroundColor;
102+ // Console.ForegroundColor = _Settings.EditorTextColor;
103+ // Console.Clear();
104+ //}
97105
98106 var parser = new Parser ( with => with . HelpWriter = null ) ;
99107 var parserResult = parser . ParseArguments < Options > ( args ) ;
@@ -138,11 +146,12 @@ private static void Main(string[] args)
138146 return ;
139147 }
140148
141- // To be used later once syntax highlighting for the console is enabled.
149+ //// To be used later once syntax highlighting for the console is enabled.
142150 //var guideResult = grammar.LoadSyntaxHighlightingGuide();
143151 //guide = guideResult != null ? guideResult.Item2 : new HeuristicSyntaxHighlightingGuide(_Settings);
144152
145153 string data ;
154+ var analyzer = new Analyzer ( ) ;
146155
147156 if ( ! string . IsNullOrEmpty ( o . FileName ) )
148157 {
@@ -158,7 +167,6 @@ private static void Main(string[] args)
158167 }
159168 else
160169 {
161- //var analyzer = new Analyzer();
162170 var builder = new StringBuilder ( ) ;
163171 Console . WriteLine ( Resources . ReadingFromStandardInputPromptMessage ) ;
164172 var currentLine = Console . CursorTop ;
@@ -195,14 +203,14 @@ private static void Main(string[] args)
195203 }
196204 else if ( typed . Key == ConsoleKey . Backspace )
197205 {
198- if ( Console . CursorLeft > 0 )
199- {
200- Console . Write ( typed . KeyChar ) ;
201- Console . Write ( ' ' ) ;
202- Console . Write ( typed . KeyChar ) ;
203- builder . Remove ( builder . Length - 1 , 1 ) ;
204- _Cache . FlushTokensForLine ( currentLine - ( _ScrollFadeCount + 1 ) ) ;
205- }
206+ if ( Console . CursorLeft <= 0 )
207+ continue ;
208+
209+ Console . Write ( typed . KeyChar ) ;
210+ Console . Write ( ' ' ) ;
211+ Console . Write ( typed . KeyChar ) ;
212+ builder . Remove ( builder . Length - 1 , 1 ) ;
213+ _Cache . FlushTokensForLine ( currentLine - ( _ScrollFadeCount + 1 ) ) ;
206214 }
207215 else
208216 {
@@ -211,8 +219,11 @@ private static void Main(string[] args)
211219 }
212220 }
213221
214- //analyzer.Tokenize(grammar, builder.ToString());
215- //HighlightSyntaxInConsole(currentLine - (_ScrollFadeCount + 1), analyzer, guide);
222+ //if (_Settings.EnableConsoleSyntaxHighlighting)
223+ //{
224+ // analyzer.Tokenize(grammar, builder.ToString(), null);
225+ // HighlightSyntaxInConsole(currentLine - (_ScrollFadeCount + 1), analyzer, guide);
226+ //}
216227 }
217228 }
218229
@@ -223,15 +234,14 @@ private static void Main(string[] args)
223234 // If tokens are the only option we've received, we don't need to parse
224235 if ( options == Grammar . ParseOption . Tokens )
225236 {
226- DisplayTokens ( grammar , data ) ;
237+ DisplayTokens ( grammar , analyzer , data ) ;
227238 return ;
228239 }
229240
230241 // Now we attempt to parse, but still handle a lexer-only grammar.
231242 if ( grammar . Parser != null )
232243 {
233- var analyzer = new Analyzer ( ) ;
234- var grammarParser = analyzer . BuildParserWithOptions ( grammar , data , options ) ;
244+ var grammarParser = analyzer . BuildParserWithOptions ( grammar , data , options , null ) ;
235245 analyzer . ExecuteParsing ( grammarParser , o . RuleName ) ;
236246
237247 if ( showParseTree )
@@ -257,7 +267,7 @@ private static void Main(string[] args)
257267 else
258268 {
259269 if ( options . HasFlag ( ParseOption . Tokens ) )
260- DisplayTokens ( grammar , data ) ;
270+ DisplayTokens ( grammar , analyzer , data ) ;
261271
262272 if ( showParseTree || writeSvg )
263273 Console . WriteLine ( Resources . GrammarHasNoParserErrorMessage , grammar . GrammarName ) ;
@@ -351,26 +361,23 @@ private static void FillCurrentLineBackground()
351361 Console . SetCursorPosition ( cursorColumn , cursorRow ) ;
352362 }
353363
354- private static void DisplayTokens ( GrammarReference grammar , string data )
364+ private static void DisplayTokens ( GrammarReference grammar , Analyzer analyzer , string data )
355365 {
356- var analyzer = new Grammar . Analyzer ( ) ;
357- var tokens = analyzer . Tokenize ( grammar , data ) ;
366+ var tokens = analyzer . Tokenize ( grammar , data , null ) ;
358367 foreach ( var token in tokens )
359368 Console . WriteLine ( token . ToString ( ) ) ;
360369 }
361370
362371 private static void LoadGui ( string data , GrammarReference grammar , string parserRule )
363372 {
364- {
365- Application . EnableVisualStyles ( ) ;
366- Application . SetCompatibleTextRenderingDefault ( false ) ;
367- var visualAnalyzer = new VisualAnalyzer ( ) ;
368- visualAnalyzer . SetSourceCode ( data ) ;
369- visualAnalyzer . SetGrammar ( grammar ) ;
370- if ( grammar . Parser != null || ! parserRule . Equals ( "tokens" , StringComparison . InvariantCultureIgnoreCase ) )
371- visualAnalyzer . SetDefaultParserRule ( parserRule ) ;
372- Application . Run ( visualAnalyzer ) ;
373- }
373+ Application . EnableVisualStyles ( ) ;
374+ Application . SetCompatibleTextRenderingDefault ( false ) ;
375+ var visualAnalyzer = new VisualAnalyzer ( ) ;
376+ visualAnalyzer . SetSourceCode ( data ) ;
377+ visualAnalyzer . SetGrammar ( grammar ) ;
378+ if ( grammar . Parser != null || ! parserRule . Equals ( "tokens" , StringComparison . InvariantCultureIgnoreCase ) )
379+ visualAnalyzer . SetDefaultParserRule ( parserRule ) ;
380+ Application . Run ( visualAnalyzer ) ;
374381 }
375382
376383 /// <summary>
0 commit comments