14 using System.Collections.Generic;
17 using System.Threading;
20 namespace Irony.Interpreter {
23 #region Fields and properties
32 private bool _ctrlCPressed;
37 Title = grammar.ConsoleTitle;
38 Greeting = grammar.ConsoleGreeting;
39 Prompt = grammar.ConsolePrompt;
40 PromptMoreInput = grammar.ConsolePromptMoreInput;
43 Interpreter.RethrowExceptions =
false;
44 Interpreter.ParseMode = ParseMode.CommandLine;
45 Interpreter.PrintParseErrors =
false;
52 Console.ForegroundColor = ConsoleColor.Red;
54 Console.WriteLine(ex.ToString());
55 Console.ForegroundColor = ConsoleColor.White;
63 private void RunImpl() {
65 Console.Title = Title;
66 Console.CancelKeyPress += OnCancelKeyPress;
67 Console.WriteLine(Greeting);
71 Console.ForegroundColor = ConsoleColor.White;
72 string prompt = (Interpreter.Status == InterpreterStatus.WaitingMoreInput ? PromptMoreInput : Prompt);
73 Console.Write(prompt);
74 var result = ReadInput(out input);
77 case ReadResult.AbortYes:
return;
78 case ReadResult.AbortNo:
continue;
79 case ReadResult.Script:
break;
81 Interpreter.ClearOutputBuffer();
82 Interpreter.EvaluateAsync(input);
83 while (Interpreter.IsBusy())
85 switch (Interpreter.Status) {
86 case InterpreterStatus.Ready:
87 Console.WriteLine(Interpreter.GetOutput());
89 case InterpreterStatus.SyntaxError:
90 Console.WriteLine(Interpreter.GetOutput());
91 Console.ForegroundColor = ConsoleColor.Red;
92 foreach (var err
in Interpreter.ParsedScript.ParserMessages) {
93 Console.WriteLine(string.Empty.PadRight(prompt.Length + err.Location.Column) +
"^");
94 Console.WriteLine(err.Message);
97 case InterpreterStatus.RuntimeError:
106 private void ReportException() {
107 Console.ForegroundColor = ConsoleColor.Red;
108 var ex = Interpreter.LastException;
109 var runtimeEx = ex as RuntimeException;
110 if (runtimeEx != null)
111 Console.WriteLine(runtimeEx.Message +
" " + Resources.LabelLocation +
" " + runtimeEx.Location.ToUiString());
113 Console.WriteLine(ex.Message);
117 #region Reading input methods
118 private enum ReadResult {
124 private ReadResult ReadInput(out
string input) {
127 input = Console.ReadLine();
128 }
while (input == null);
129 if (!_ctrlCPressed)
return ReadResult.Script;
130 _ctrlCPressed =
false;
131 if (Resources.ConsoleYesChars.Contains(input))
132 return ReadResult.AbortYes;
133 if (Resources.ConsoleNoChars.Contains(input))
134 return ReadResult.AbortNo;
136 return ReadResult.AbortNo;
140 #region Ctrl-C handling
159 _ctrlCPressed =
true;
160 if (Interpreter.IsBusy()) {
164 var result = ReadInput(out input);
166 case ReadResult.AbortYes:
readonly ScriptInterpreter Interpreter
static string MsgPressAnyKeyToExit
Looks up a localized string similar to Press any key to end the program..
static string MsgExitConsoleYN
Looks up a localized string similar to Exit console (y/n)?.
CommandLine(Grammar grammar)
static string ErrConsoleFatalError
Looks up a localized string similar to Fatal error:.
static string MsgAbortScriptYN
Looks up a localized string similar to Abort script(y/n)?.
virtual void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)