void RefreshFile(object arg) { Irony.Parsing.Parser parser = new Irony.Parsing.Parser(Grammar.LuaGrammar.Instance); var tree = parser.Parse(_cur.TextBuffer.CurrentSnapshot.GetText()); OnFileContentChanged(_cur.TextBuffer.CurrentSnapshot, tree); }
private static ParseTree Parse(string input, bool allowPartial) { ParseTree parseTree = null; IronyParser.Context.Mode = allowPartial ? ParseMode.CommandLine : ParseMode.File; try { parseTree = IronyParser.Parse(input); } catch (Exception) { var msg = "The parser internally crashed and gets reinitialized." + Environment.NewLine + "Although this shouldn't happen, it's likely that it happened because of invalid syntax."; IronyParser = new IronyParser(Grammar); throw new ParseException(msg); } if (parseTree.HasErrors()) // ParseTreeStatus is Error { var logMessage = parseTree.ParserMessages.First(); throw new ParseException(logMessage.Message, logMessage.Location.Line, logMessage.Location.Column, parseTree.SourceText); } return(parseTree); }
void ReParse() { ITextSnapshot newSnapshot = buffer.CurrentSnapshot; List <Region> newRegions = new List <Region>(); LuaLanguage.LuaGrammar grammar = new LuaLanguage.LuaGrammar(); Irony.Parsing.Parser parser = new Irony.Parsing.Parser(grammar); var tree = parser.Parse(newSnapshot.GetText()); if (tree.Root != null) { FindHiddenRegions(newSnapshot, tree.Root, ref newRegions); } //determine the changed span, and send a changed event with the new spans List <Span> oldSpans = new List <Span>(this.regions.Select(r => AsSnapshotSpan(r, this.snapshot) .TranslateTo(newSnapshot, SpanTrackingMode.EdgeExclusive) .Span)); List <Span> newSpans = new List <Span>(newRegions.Select(r => AsSnapshotSpan(r, newSnapshot).Span)); NormalizedSpanCollection oldSpanCollection = new NormalizedSpanCollection(oldSpans); NormalizedSpanCollection newSpanCollection = new NormalizedSpanCollection(newSpans); //the changed regions are regions that appear in one set or the other, but not both. NormalizedSpanCollection removed = NormalizedSpanCollection.Difference(oldSpanCollection, newSpanCollection); int changeStart = int.MaxValue; int changeEnd = -1; if (removed.Count > 0) { changeStart = removed[0].Start; changeEnd = removed[removed.Count - 1].End; } if (newSpans.Count > 0) { changeStart = Math.Min(changeStart, newSpans[0].Start); changeEnd = Math.Max(changeEnd, newSpans[newSpans.Count - 1].End); } this.snapshot = newSnapshot; this.regions = newRegions; if (changeStart <= changeEnd) { ITextSnapshot snap = this.snapshot; if (this.TagsChanged != null) { this.TagsChanged(this, new SnapshotSpanEventArgs( new SnapshotSpan(this.snapshot, Span.FromBounds(changeStart, changeEnd)))); } } }
void RefreshFile(object arg) { Irony.Parsing.Parser parser = new Irony.Parsing.Parser(Grammar.LuaGrammar.Instance); var tree = parser.Parse(_cur.TextBuffer.CurrentSnapshot.GetText()); //降低文本变化的敏感度,选择性刷新? //if(!tree.HasErrors()) OnFileContentChanged(_cur.TextBuffer.CurrentSnapshot, tree); }
public LuaSyntaxErrorTagger(ITextBuffer buffer) { this.buffer = buffer; Babe.Lua.Editor.TextViewCreationListener.FileContentChanged += TextViewCreationListener_FileContentChanged; Irony.Parsing.Parser parser = new Irony.Parsing.Parser(LuaGrammar.Instance); var tree = parser.Parse(buffer.CurrentSnapshot.GetText()); ReParse(buffer.CurrentSnapshot, tree); }
public CILProgram Parse(string sourceCode) { var parseTree = _ironyParser.Parse(sourceCode); if (parseTree.Status == Irony.Parsing.ParseTreeStatus.Parsed) { var programTree = _astBuilder.BuildAST(parseTree); return(programTree); } throw new ArgumentException(parseTree.ParserMessages.First().UserFriendlyMessage()); }
public OutliningTagger(ITextBuffer buffer) { this.buffer = buffer; this.snapshot = buffer.CurrentSnapshot; this.regions = new List <Region>(); Babe.Lua.Editor.TextViewCreationListener.FileContentChanged += TextViewCreationListener_FileContentChanged; Irony.Parsing.Parser parser = new Irony.Parsing.Parser(LuaGrammar.Instance); var tree = parser.Parse(snapshot.GetText()); ReParse(snapshot, tree); }
public CILProgram Parse(string sourceCode) { var parseTree = _parser.Parse(sourceCode); if (parseTree.Status == Irony.Parsing.ParseTreeStatus.Parsed) { var result = _astBuilder.BuildNode(parseTree.Root); return(result); } var error = parseTree.ParserMessages.First(); var errorMessage = BuildErrorMessage(error); throw new ArgumentException(errorMessage); }
private void ReParse() { int previousCount = errorTokens.Count; errorTokens.Clear(); ITextSnapshot newSnapshot = this.buffer.CurrentSnapshot; string text = newSnapshot.GetText(); Irony.Parsing.Parser parser = new Irony.Parsing.Parser(grammar); var newErrors = new List <Irony.Parsing.Token>(); var parseTree = parser.Parse(text); foreach (var token in parseTree.Tokens) { if (token.IsError()) { errorTokens.Add(token); } } if (parseTree.HasErrors()) { var tok = parseTree.Tokens.Last(); errorTokens.Add(tok); //if (tok.Length != 0) // errorTokens.Add(tok); //else //it is EOF error so before the end(use -2) // errorTokens.Add(parseTree.Tokens[parseTree.Tokens.Count - 2]); msgParse = parseTree.ParserMessages[0].ToString(); } else { msgParse = ""; } if (previousCount != 0 || errorTokens.Count != 0) { if (this.TagsChanged != null) { this.TagsChanged(this, new SnapshotSpanEventArgs( new SnapshotSpan(newSnapshot, 0, newSnapshot.Length))); } } }
static void Main(string[] args) { TotemGrammar tg = new TotemGrammar(); Irony.Parsing.Parser parser = new Irony.Parsing.Parser(tg); var tree = parser.Parse(new StreamReader(File.OpenRead("test.pole")).ReadToEnd(), "test.pole"); if (!tree.HasErrors()) { var generator = new Generator("test", Path.Combine(Environment.CurrentDirectory, "test.exe"), tg); var rootNode = tree.Root; generator.GenerateProgram(rootNode); generator.Save(); } else { Console.WriteLine("Error in input"); Console.ReadLine(); } }
static void Main(string[] args) { TotemGrammar tg = new TotemGrammar(); Irony.Parsing.Parser parser = new Irony.Parsing.Parser(tg); var tree = parser.Parse(new StreamReader(File.OpenRead("test.pole")).ReadToEnd(), "test.pole"); if (!tree.HasErrors()) { var generator = new Generator("test", Path.Combine(Environment.CurrentDirectory, "test.exe"), tg); var rootNode = tree.Root; generator.GenerateProgram(rootNode); generator.Save(); } else { Console.WriteLine("Error in input"); Console.ReadLine(); } }
public OutliningTagger(ITextBuffer buffer) { this.buffer = buffer; this.snapshot = buffer.CurrentSnapshot; this.regions = new List <Region>(); Babe.Lua.Editor.TextViewCreationListener.FileContentChanged += TextViewCreationListener_FileContentChanged; Irony.Parsing.Parser parser = new Irony.Parsing.Parser(LuaGrammar.Instance); var tree = parser.Parse(snapshot.GetText()); try { ReParse(snapshot, tree); } catch (Exception ex) { if (UploadError) { UploadError = false; Logger.LogMessage("LuaOutliner.ReParse error.\r\nPosition : " + ex.TargetSite + "\r\nMessage : " + ex.Message); } } }
void RefreshFile(object arg) { Irony.Parsing.Parser parser = new Irony.Parsing.Parser(Grammar.LuaGrammar.Instance); var tree = parser.Parse(_cur.TextBuffer.CurrentSnapshot.GetText()); OnFileContentChanged(_cur.TextBuffer.CurrentSnapshot, tree); }
public static List <MultiValue> FromStringValue(CDataWrapper value) { var parser = new Irony.Parsing.Parser(new Grammars.PCodeGrammar()); var tree = parser.Parse(value); if (tree.Status != Irony.Parsing.ParseTreeStatus.Parsed) { throw new FormatException("failed to parse pcode"); } var list = new List <MultiValue>(); var labels = new Dictionary <string, int>(); foreach (var line in tree.Root.ChildNodes) { var label = line.ChildNodes.SingleOrDefault(c => c.Term.Name == "LABEL"); if (label != null) { labels.Add(label.ChildNodes[0].Token.ValueString, list.Count); } var statement = line.ChildNodes.SingleOrDefault(c => c.Term.Name == "STATEMENT"); if (statement != null) { var code = statement.ChildNodes[0]; MultiValueOpcode op; object arg = null; if (_SimpleOps.ContainsKey(code.Term.Name) == true) { op = _SimpleOps[code.Term.Name]; } else { switch (code.Term.Name) { case "OP_J__": { op = MultiValueOpcode.J__; arg = code.ChildNodes[1].Token.ValueString; break; } case "OP_JZ_": { op = MultiValueOpcode.JZ_; arg = code.ChildNodes[1].Token.ValueString; break; } case "OP_S_V": { op = MultiValueOpcode.S_V; StaticVariableType sv; if (MultiValue.TryParseStaticVariable(code.ChildNodes[1].Token.Text, out sv) == false) { throw new FormatException("invalid static variable type"); } arg = sv; break; } case "OP_STR": { op = MultiValueOpcode.STR; arg = code.ChildNodes[1].Token.ValueString; break; } case "OP_INT": { op = MultiValueOpcode.INT; arg = code.ChildNodes[1].Token.Value; break; } case "OP_FLT": { op = MultiValueOpcode.FLT; arg = code.ChildNodes[1].Token.Value; break; } case "OP_OBJ": { op = MultiValueOpcode.OBJ; arg = code.ChildNodes[1].Token.ValueString; break; } case "OP_RP_": { op = MultiValueOpcode.RP_; arg = code.ChildNodes[1].Token.ValueString; break; } case "OP_IDS": { op = MultiValueOpcode.IDS; arg = code.ChildNodes[1].Token.ValueString; break; } case "OP_FUN": { op = MultiValueOpcode.FUN; arg = code.ChildNodes[1].Token.ValueString; break; } default: { throw new NotImplementedException(); } } } list.Add(new MultiValue() { Op = op, Arg = arg, }); } } foreach (var mv in list.Where( item => item.Op == MultiValueOpcode.J__ || item.Op == MultiValueOpcode.JZ_)) { var label = (string)mv.Arg; if (labels.ContainsKey(label) == false) { throw new InvalidOperationException(); } mv.Arg = (long)labels[label]; } return(list); }