Skip to content

Commit e44f46a

Browse files
committed
added rudimentary syntax checking to make life easier for users
1 parent 20108df commit e44f46a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

PythonConsoleControl/PythonConsole.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,17 @@ void ExecuteStatements()
381381
try
382382
{
383383
executing = true;
384-
GetCommandDispatcher()(() => scriptSource.Execute(commandLine.ScriptScope));
384+
var errors = new ErrorReporter();
385+
var command = scriptSource.Compile(errors);
386+
if (command == null)
387+
{
388+
// compilation failed
389+
error = "Syntax Error: " + string.Join("\nSyntax Error: ", errors.Errors) + "\n";
390+
}
391+
else
392+
{
393+
GetCommandDispatcher()(() => scriptSource.Execute(commandLine.ScriptScope));
394+
}
385395
}
386396
catch (ThreadAbortException tae)
387397
{
@@ -715,4 +725,19 @@ void ReplaceCurrentLineTextAfterPrompt(string text)
715725
textEditor.Column = promptLength + text.Length + 1;
716726
}
717727
}
728+
729+
public class ErrorReporter : ErrorListener
730+
{
731+
public List<String> Errors = new List<string>();
732+
733+
public override void ErrorReported(ScriptSource source, string message, SourceSpan span, int errorCode, Severity severity)
734+
{
735+
Errors.Add(string.Format("{0} (line {1})", message, span.Start.Line));
736+
}
737+
738+
public int Count
739+
{
740+
get { return Errors.Count; }
741+
}
742+
}
718743
}

RpsRuntime/ScriptExecutor.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public int ExecuteScript(string source, string sourcePath)
8080
engine.Runtime.IO.SetInput(outputStream, Encoding.UTF8);
8181

8282
var script = engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements);
83+
var errors = new ErrorReporter();
84+
var command = script.Compile(errors);
85+
if (command == null)
86+
{
87+
// compilation failed
88+
_message = string.Join("\n", errors.Errors);
89+
return (int)Result.Failed;
90+
}
91+
92+
8393
try
8494
{
8595
script.Execute(scope);
@@ -195,4 +205,20 @@ private void AddSearchPaths(ScriptEngine engine)
195205
engine.SetSearchPaths(searchPaths);
196206
}
197207
}
208+
209+
210+
public class ErrorReporter : ErrorListener
211+
{
212+
public List<String> Errors = new List<string>();
213+
214+
public override void ErrorReported(ScriptSource source, string message, SourceSpan span, int errorCode, Severity severity)
215+
{
216+
Errors.Add(string.Format("{0} (line {1})", message, span.Start.Line));
217+
}
218+
219+
public int Count
220+
{
221+
get { return Errors.Count; }
222+
}
223+
}
198224
}

0 commit comments

Comments
 (0)