public static bool IsCompleteOrInvalid(/*this*/ ScriptCodeParseResult props, bool allowIncompleteStatement) { return (props == ScriptCodeParseResult.Invalid || props != ScriptCodeParseResult.IncompleteToken && (allowIncompleteStatement || props != ScriptCodeParseResult.IncompleteStatement)); }
public static bool IsCompleteOrInvalid(/*this*/ ScriptCodeParseResult props, bool allowIncompleteStatement) { return props == ScriptCodeParseResult.Invalid || props != ScriptCodeParseResult.IncompleteToken && (allowIncompleteStatement || props != ScriptCodeParseResult.IncompleteStatement); }
private bool CanExecute(string text) { CompilerOptions options = this.scriptEngine.GetCompilerOptions(this.scope); ScriptCodeParseResult result = this.scriptEngine.CreateScriptSourceFromString(text, SourceCodeKind.InteractiveCode).GetCodeProperties(options); return(result == ScriptCodeParseResult.Complete || result == ScriptCodeParseResult.Empty || result == ScriptCodeParseResult.Invalid); }
private ScriptCodeParseResult GetCommandPropertiesAndParse(string code, out ScriptSource source) { ScriptCodeParseResult result = ScriptCodeParseResult.IncompleteStatement; try { ScriptSource command = this.Engine.CreateScriptSourceFromString(code, SourceCodeKind.InteractiveCode); result = command.GetCodeProperties(this.Engine.GetCompilerOptions(this.ScriptScope)); source = command; } catch (ParseException error) { if (error.CanContinue) { result = ScriptCodeParseResult.IncompleteStatement; } else { result = ScriptCodeParseResult.Invalid; Console.ErrorOutput.WriteLine("//[parse]: {0}", error.Message); } source = null; } //catch (Exception e) //{ // source = null; // Console.ErrorOutput.WriteLine("**->{0}", e); //} return(result); }
internal void ValidateGetCodeProperties(string InputCode, ScriptCodeParseResult ExpectedValue) { ScriptSource ss = CreateStringBasedScriptSource(InputCode); Assert.AreEqual(ExpectedValue, ss.GetCodeProperties()); //test the same method on a file based ScriptSource object ss = CreateFileBasedScriptSource(InputCode); Assert.AreEqual(ExpectedValue, ss.GetCodeProperties()); }
private bool IsIncompleteCode(string code, string lastLine, ScriptCodeParseResult parseResult) { if (lastLine.EndsWith("\\")) // todo: handle backslash in comments { return(true); } if (string.IsNullOrEmpty(lastLine)) { return(false); } return(parseResult == ScriptCodeParseResult.IncompleteStatement); }
public void CreateScriptSourceFromFile_JunkStringsTest() { int cnt = 0; ScriptSource sSrc; ScriptCodeParseResult sCodeProp = new ScriptCodeParseResult(); foreach (string junk in StandardTestStrings.AllStrings) { // Count the loop sanity check! cnt++; string msg = string.Format("junk string index {0}", cnt); Assert.IsNotNull(sSrc = CreateScriptSourceFromTempFile(junk, Encoding.ASCII), msg); Assert.IsNotNull(sCodeProp = sSrc.GetCodeProperties(), msg); Assert.IsTrue(sCodeProp.Equals(ScriptCodeParseResult.Invalid), msg); } }
/* * struct s1 * { * public string f1; * public int f2; * } */ static void try1(string[] args) { string code = @" outputdata.PutData = inputdata.GetNextData; print inputdata.GetNextData; print worker.DoWork(); def function1(val): print (val) def function2(val1, val2): print (val1, val2) "; ScriptEngine engine = IronPython.Hosting.Python.CreateEngine(); ScriptScope scope = engine.CreateScope(); scope.SetVariable("application", new Application()); scope.SetVariable("inputdata", new SourceData()); DestinationData dd = new DestinationData(); scope.SetVariable("outputdata", dd); scope.SetVariable("worker", new DoWorkClass()); //ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements); dynamic source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements); CompiledCode compiled = source.Compile(); ScriptCodeParseResult codeProps = source.GetCodeProperties(); Console.WriteLine("codeProps: {0}", codeProps); source.Execute(scope); Console.WriteLine("got it -- {0}", dd.PutData); Console.WriteLine("-----------"); source.Function1("function1"); source.Function2("function2", "parameter2"); Console.ReadKey(); }
public void CreateScriptSourceFromFile_ValidPathToJunkFile() { //Create a temp source with some invalid code string testSrc = "Moby-Dick[1] is an 1851 novel by Herman Melville. The story" + " tells the adventures of the wandering sailor Ishmael and his" + " voyage on the whaling ship Pequod, commanded by Captain " + " Ahab. Ishmael soon learns that Ahab does not mean to use the" + " Pequod and her crew to hunt whales for market trade, as whaling"; // Much of this code is tested in the helper function CreateScriptSourceFromTempFile!!! ScriptSource sSrc = CreateScriptSourceFromTempFile(testSrc, Encoding.ASCII); ScriptSource sSrcTest = _testEng.CreateScriptSourceFromFile(sSrc.Path); ScriptCodeParseResult SrcCodeProp = sSrcTest.GetCodeProperties(); bool result = SrcCodeProp.Equals(ScriptCodeParseResult.Invalid); Assert.IsTrue(result, String.Format("Is this code invalid {0}", result)); }
/// <summary> /// Read a statement, which can potentially be a multiple-line statement suite (like a class declaration). /// </summary> /// <param name="continueInteraction">Should the console session continue, or did the user indicate /// that it should be terminated?</param> /// <returns>Expression to evaluate. null for empty input</returns> protected string ReadStatement(out bool continueInteraction) { StringBuilder b = new StringBuilder(); int autoIndentSize = 0; _console.Write(Prompt, Style.Prompt); while (true) { string line = ReadLine(autoIndentSize); continueInteraction = true; if (line == null || (_terminatingExitCode != null)) { continueInteraction = false; return(null); } bool allowIncompleteStatement = TreatAsBlankLine(line, autoIndentSize); b.Append(line); b.Append("\n"); string code = b.ToString(); ScriptSource command = _engine.CreateScriptSourceFromString(code, (code.Contains(Environment.NewLine))? SourceCodeKind.Statements : SourceCodeKind.InteractiveCode); ScriptCodeParseResult props = command.GetCodeProperties(_engine.GetCompilerOptions(_scope)); if (SourceCodePropertiesUtils.IsCompleteOrInvalid(props, allowIncompleteStatement)) { return(props != ScriptCodeParseResult.Empty ? code : null); } if (_options.AutoIndent && _options.AutoIndentSize != 0) { autoIndentSize = GetNextAutoIndentSize(code); } // Keep on reading input _console.Write(PromptContinuation, Style.Prompt); } }
internal object ParseInteractiveStatement(out ScriptCodeParseResult result) { result = ScriptCodeParseResult.Complete; object s = null; try { s = LispReader.read(new PushbackTextReader(new StringReader(_text)), false, _eof, false); } catch (Exception) { result = ScriptCodeParseResult.Invalid; s = null; } if (s == _eof) { result = ScriptCodeParseResult.IncompleteToken; s = null; } return(s); }
internal object ParseInteractiveStatement(out ScriptCodeParseResult result) { result = ScriptCodeParseResult.Complete; object s = null; try { s = LispReader.read(new PushbackTextReader(new StringReader(_text)), false, _eof, false); } catch (Exception) { result = ScriptCodeParseResult.Invalid; s = null; } if (s == _eof) { result = ScriptCodeParseResult.IncompleteToken; s = null; } return s; }
internal static InteractiveShellResult ExecuteInteractiveShellInput(string Command) { InteractiveShellResult ISR = new InteractiveShellResult(); if (MoreExpected) { CommandBuffer += Command; } else { CommandBuffer = Command; } try { ScriptSource Source = IronScripting.Engine.CreateScriptSourceFromString(CommandBuffer, SourceCodeKind.InteractiveCode); ScriptCodeParseResult Result = Source.GetCodeProperties(); if (Result == ScriptCodeParseResult.Complete || Result == ScriptCodeParseResult.Invalid || CanExecute(Command)) { Source.Execute(IronScripting.Scope); Reset(); return(ISR); } else { ISR.MoreExpected = true; Set(Command); return(ISR); } } catch (Exception exp) { ISR.ResultString = "Exception: " + exp.Message + Environment.NewLine; Reset(); return(ISR); } }
public TotemAst ParseInteractiveCode(out ScriptCodeParseResult properties) { bool parsingMultiLineCmpdStmt; bool isEmptyStmt = false; properties = ScriptCodeParseResult.Complete; StartParsing(); _globalParent = new TotemAst(false, true, _context); Statement ret = InternalParseInteractiveInput(out parsingMultiLineCmpdStmt, out isEmptyStmt); if (_errorCode == 0) { if (isEmptyStmt) properties = ScriptCodeParseResult.Empty; else if (parsingMultiLineCmpdStmt) properties = ScriptCodeParseResult.IncompleteStatement; if (isEmptyStmt) return null; return FinishParsing(ret); } else { if ((_errorCode & ErrorCodes.IncompleteStatement) != 0) { if ((_errorCode & ErrorCodes.IncompleteToken) != 0) { properties = ScriptCodeParseResult.IncompleteToken; return null; } properties = ScriptCodeParseResult.IncompleteStatement; return null; } properties = ScriptCodeParseResult.Invalid; return null; } }
//[stmt_list] Newline | compound_stmt Newline //stmt_list ::= simple_stmt (";" simple_stmt)* [";"] //compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef //Returns a simple or coumpound_stmt or null if input is incomplete /// <summary> /// Parse one or more lines of interactive input /// </summary> /// <returns>null if input is not yet valid but could be with more lines</returns> public PythonAst ParseInteractiveCode(out ScriptCodeParseResult properties) { bool parsingMultiLineCmpdStmt; bool isEmptyStmt = false; // ugly but it's difficult to flow this in at creation time. _tokenizer._dontImplyDedent = true; properties = ScriptCodeParseResult.Complete; StartParsing(); Statement ret = InternalParseInteractiveInput(out parsingMultiLineCmpdStmt, out isEmptyStmt); if (_errorCode == 0) { if (isEmptyStmt) { properties = ScriptCodeParseResult.Empty; } else if (parsingMultiLineCmpdStmt) { properties = ScriptCodeParseResult.IncompleteStatement; } if (isEmptyStmt) { return null; } return new PythonAst(ret, false, _languageFeatures, true); } else { if ((_errorCode & ErrorCodes.IncompleteMask) != 0) { if ((_errorCode & ErrorCodes.IncompleteToken) != 0) { properties = ScriptCodeParseResult.IncompleteToken; return null; } if ((_errorCode & ErrorCodes.IncompleteStatement) != 0) { if (parsingMultiLineCmpdStmt) { properties = ScriptCodeParseResult.IncompleteStatement; } else { properties = ScriptCodeParseResult.IncompleteToken; } return null; } } properties = ScriptCodeParseResult.Invalid; return null; } }
public void CreateScriptSourceFromString_JunkStringsTest() { int cnt = 0; ScriptSource sSrc; ScriptCodeParseResult sCodeProp = new ScriptCodeParseResult(); foreach (string junk in StandardTestStrings.AllStrings) { // Count the loop sanity check! cnt++; string msg = string.Format("junk string index {0}", cnt); Assert.IsNotNull(sSrc = _testEng.CreateScriptSourceFromString(junk)); Assert.IsNotNull(sCodeProp = sSrc.GetCodeProperties(), msg); Assert.IsTrue(sCodeProp.Equals(ScriptCodeParseResult.Invalid), msg); } }
internal void ValidateGetCodeProperties(string InputCode, ScriptCodeParseResult ExpectedValue) { ScriptSource ss = CreateStringBasedScriptSource(InputCode); Assert.AreEqual( ExpectedValue, ss.GetCodeProperties()); //test the same method on a file based ScriptSource object ss = CreateFileBasedScriptSource(InputCode); Assert.AreEqual( ExpectedValue, ss.GetCodeProperties()); }
private bool IsComplete(ScriptCodeParseResult result) { if (result == ScriptCodeParseResult.IncompleteStatement || result == ScriptCodeParseResult.IncompleteToken || result == ScriptCodeParseResult.Invalid) return false; else return true; }
//[stmt_list] Newline | compound_stmt Newline //stmt_list ::= simple_stmt (";" simple_stmt)* [";"] //compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef //Returns a simple or coumpound_stmt or null if input is incomplete /// <summary> /// Parse one or more lines of interactive input /// </summary> /// <returns>null if input is not yet valid but could be with more lines</returns> public PythonAst ParseInteractiveCode(out ScriptCodeParseResult properties) { bool parsingMultiLineCmpdStmt; bool isEmptyStmt = false; properties = ScriptCodeParseResult.Complete; StartParsing(); Statement ret = InternalParseInteractiveInput(out parsingMultiLineCmpdStmt, out isEmptyStmt); if (_errorCode == 0) { if (isEmptyStmt) { properties = ScriptCodeParseResult.Empty; } else if (parsingMultiLineCmpdStmt) { properties = ScriptCodeParseResult.IncompleteStatement; } if (isEmptyStmt) { return null; } return new PythonAst(ret, false, _languageFeatures, true, _context); } else { if ((_errorCode & ErrorCodes.IncompleteMask) != 0) { if ((_errorCode & ErrorCodes.IncompleteToken) != 0) { properties = ScriptCodeParseResult.IncompleteToken; return null; } if ((_errorCode & ErrorCodes.IncompleteStatement) != 0) { if (parsingMultiLineCmpdStmt) { properties = ScriptCodeParseResult.IncompleteStatement; } else { properties = ScriptCodeParseResult.IncompleteToken; } return null; } } properties = ScriptCodeParseResult.Invalid; return null; } }