public AstNode Parse(CompilerContext context, SourceFile source) { Scanner.Prepare(context, source); context.Tokens.Clear(); //If we need to match braces then we need to ensure that we have BraceMatchFilter if (context.OptionIsSet(CompilerOptions.MatchBraces)) EnsureBraceMatchFilter(); IEnumerable<Token> tokenStream = Scanner.BeginScan(); //chain all token filters foreach (TokenFilter filter in Grammar.TokenFilters) { tokenStream = filter.BeginFiltering(context, tokenStream); } //finally, parser takes token stream and produces root Ast node int start = Environment.TickCount; AstNode rootNode = Parser.Parse(context, tokenStream); _compileTime = Environment.TickCount - start; if (context.Errors.Count > 0) context.Errors.Sort(SyntaxErrorList.ByLocation); if (rootNode != null && context.OptionIsSet(CompilerOptions.AnalyzeCode)) AnalyzeCode(rootNode, context); return rootNode; }
public void ReadAndExecuteCommand(String InputString) { // Read and execute commend #region Check if valid command //has to be done via split, because irony doesn't recognize whitespaces, //so "dfgfkgdfgkfd" could be detected as the command "df" with an //strange parameter if (!IsQuit && ValidCommandFromInputString(InputString)) { #endregion #region Prepare Command Execution _Scanner = GraphCLICompiler.Scanner; _CompilerContext = new CompilerContext(GraphCLICompiler); _SourceFile = new SourceFile(InputString, "Source"); _Scanner.Prepare(_CompilerContext, _SourceFile); _CompilerContext.Tokens.Clear(); _TokenStream = _Scanner.BeginNonDetermisticScan(); AstNode ExecutionTree = null; ExecutionTree = GraphCLICompiler.Parser.ParseNonDeterministic(_CompilerContext, _TokenStream); #region Checkt if valid command is complete if (ExecutionTree == null) { MarkWrongOption(InputString, GraphCLICompiler.Parser.GetCorrectElements(_CompilerContext, _TokenStream)); } else { //Carry on, the command is valid and complete #endregion ExtractOptionsFromTree(ExecutionTree); #endregion if (Commands[CurrentCommand].CLI_Output == CLI_Output.Standard) WriteLine(); #region Handle Command Execution //try //{ Stopwatch sw = new Stopwatch(); sw.Start(); // TODO: what's this doing here? //if (Parameters.Count > 0) //{ #region Execute command... if (_GraphDSSharp != null || CurrentCommand.Equals("MKFS") || CurrentCommand.Equals("MOUNT") || CurrentCommand.Equals("QUIT") || CurrentCommand.Equals("EXIT") || CurrentCommand.Equals("USEHISTORY") || CurrentCommand.Equals("SAVEHISTORY")) { Commands[CurrentCommand].Execute(_GraphDSSharp, ref CurrentPath, Parameters, InputString); //if (CommandCategory.Equals(CLICommandCategory.CLIStandardCommand)) //{ #region Handle Quit and History switch (CurrentCommand.ToUpper()) { case "QUIT": IsQuit = true; break; case "EXIT": IsQuit = true; break; case "USEHISTORY": //lets move to the right parameter ParameterEnum = Parameters.GetEnumerator(); ParameterEnum.MoveNext(); ParameterEnum.MoveNext(); switch (ParameterEnum.Current.Key) { case "default": LoadStandardHistory = true; if (!HistoryFileName.Length.Equals(0)) SaveHistory(HistoryFileName, SthMountedList); break; default: LoadStandardHistory = false; HistoryFileName = ParameterEnum.Current.Key; LoadHistoryFrom(HistoryFileName); break; } break; case "SAVEHISTORY": //lets move to the right parameter ParameterEnum = Parameters.GetEnumerator(); ParameterEnum.MoveNext(); ParameterEnum.MoveNext(); if (LoadStandardHistory) SaveHistory(ParameterEnum.Current.Key, NothingMountedList); else SaveHistory(ParameterEnum.Current.Key, SthMountedList); break; } #endregion //} } else WriteLine("Nothing mounted..."); #endregion //}//CommandArray.Length > 0 ? sw.Stop(); if (Parameters.Count > 0 && Commands[CurrentCommand].CLI_Output != CLI_Output.Short) { WriteLine("Command took {0}ms, {1:0.0} MB RAM, {2:0.0}% CPU", sw.ElapsedMilliseconds, _RAMCounter.NextValue() / 1024 / 1024, _CPUCounter.NextValue()); } //} //catch (Exception e) //{ // WriteLine("Uuups... " + e.Message); // WriteLine("StackTrace... " + e.StackTrace); //} Reset(); #endregion } } }
//Note: we don't actually parse in current version, only scan. Will implement full parsing in the future, // to support all intellisense operations private void ParseSource(String newText) { TokenList newTokens = new TokenList(); //Explicitly catch the case when new text is empty if (newText != string.Empty) { SourceFile srcFile = new SourceFile(newText, "source"); _compiler.Scanner.Prepare(_context, srcFile); IEnumerable<Token> tokenStream = _compiler.Scanner.BeginScan(); foreach (Token _token in tokenStream) { newTokens.Add(_token); } //newTokens.AddRange(tokenStream); } //finally create new contents object and replace the existing _contents value _parsedSource = new ParsedSource(newText, newTokens, null); //notify views var views = GetViews(); foreach (var view in views) view.UpdateParsedSource(_parsedSource); }
private List<ParserReturn> GetPossibleTokensViaIrony(String Input) { List<ParserReturn> tempCompletionList = new List<ParserReturn>(); #region set up of autocompletion environment _Scanner = GraphCLICompiler.Scanner; _CompilerContext = new CompilerContext(GraphCLICompiler); #endregion #region get possible tokens _SourceFile = new SourceFile(Input, "Source"); _Scanner.Prepare(_CompilerContext, _SourceFile); _CompilerContext.Tokens.Clear(); _TokenStream = _Scanner.BeginNonDetermisticScan(); tempCompletionList = GraphCLICompiler.Parser.GetPossibleTokens(_CompilerContext, _TokenStream, Input); #endregion return tempCompletionList; }