public static DocumentSymbol GetDocumentSymbol(int index, Document doc) { var pd = ParserDetailsFactory.Create(doc); if (pd.ParseTree == null) { return(null); } Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc); var gd = GrammarDescriptionFactory.Create(doc.FullPath); if (pt == null) { return(default(DocumentSymbol)); } Antlr4.Runtime.Tree.IParseTree p = pt; var q = p as Antlr4.Runtime.Tree.TerminalNodeImpl; var found = pd.Tags.TryGetValue(q, out int tag_type); if (!found) { return(null); } if (q.Symbol == null) { return(null); } return(new DocumentSymbol() { name = q.Symbol.Text, range = new Workspaces.Range(q.Symbol.StartIndex, q.Symbol.StopIndex), kind = tag_type }); }
/// <summary> /// Given a parse tree, returns a string containing a textual /// representation of that tree. /// </summary> /// <param name="tree">The parse tree to evaluate.</param> /// <param name="indentPrefix">A string to use for each indent level of /// the parse tree.</param> /// <returns>The text version of <paramref name="tree"/>.</returns> /// <exception cref="InvalidOperationException">Thrown when <paramref /// name="tree"/> contains a parse node that isn't a token or a parse /// rule.</exception> protected static string FormatParseTreeAsText(Antlr4.Runtime.Tree.IParseTree tree, string indentPrefix = "| ") { var stack = new Stack <(int Indent, Antlr4.Runtime.Tree.IParseTree Node)>(); stack.Push((0, tree)); var sb = new System.Text.StringBuilder(); while (stack.Count > 0) { var current = stack.Pop(); sb.Append(string.Concat(Enumerable.Repeat(indentPrefix, current.Indent))); string item; switch (current.Node.Payload) { case Antlr4.Runtime.IToken token: { // Display this token's name and text. Tokens have // no children, so there's nothing else to do here. var tokenName = YarnSpinnerLexer.DefaultVocabulary.GetSymbolicName(token.Type); var tokenText = token.Text.Replace("\n", "\\n"); item = $"{token.Line}:{token.Column} {tokenName} \"{tokenText}\""; break; } case Antlr4.Runtime.ParserRuleContext ruleContext: { // Display this rule's name (not its text, because // that's comprised of all of the child tokens.) var ruleName = YarnSpinnerParser.ruleNames[ruleContext.RuleIndex]; var start = ruleContext.Start; item = $"{start.Line}:{start.Column} {ruleName}"; // Push all children into our stack; do this in // reverse order of child, so that we encounter them // in a reasonable order (i.e. child 0 will be the // next item we see) for (int i = ruleContext.ChildCount - 1; i >= 0; i--) { var child = ruleContext.GetChild(i); stack.Push((current.Indent + 1, child)); } break; } default: throw new InvalidOperationException($"Unexpected parse node type {current.Node.GetType()}"); } sb.AppendLine(item); } var result = sb.ToString(); return(result); }
private static ParserState ParseGrammar(ICharStream stream, IExportHandle?export) { var state = new ParserState() { Exporter = export ?? new MockExportHandle() }; var lexer = new UnitsGrammarLexer(stream); var tokens = new Antlr4.Runtime.CommonTokenStream(lexer); var parser = new UnitsGrammarParser(tokens) { BuildParseTree = true }; parser.AddParseListener(new SemanticUnitsListener(state)); try { Antlr4.Runtime.Tree.IParseTree tree = parser.prog(); } catch (HandleException he) { Console.WriteLine($"Error[#{he.ErrorCode}] on line {parser.CurrentToken.Line}: {he.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Error on line {parser.CurrentToken.Line}: {ex.Message}"); throw; } return(state); }
public static int GetTag(int index, Document doc) { var pd = ParserDetailsFactory.Create(doc); if (pd.ParseTree == null) { return(-1); } Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc); var gd = GrammarDescriptionFactory.Create(doc.FullPath); if (pt == null) { return(-1); } Antlr4.Runtime.Tree.IParseTree p = pt; var q = p as Antlr4.Runtime.Tree.TerminalNodeImpl; var found = pd.Tags.TryGetValue(q, out int tag_type); if (found) { return(tag_type); } if (q.Symbol == null) { return(-1); } var found2 = pd.Comments.TryGetValue(q.Symbol, out int tag2); if (found2) { return(tag2); } return(-1); }
public override string Visit([Antlr4.Runtime.Misc.NotNull] Antlr4.Runtime.Tree.IParseTree tree) { string t = tree.GetText(); System.Console.WriteLine(t); return(base.Visit(tree)); }
public override void ExitRhs(BisonParser.RhsContext context) { if (context.ChildCount >= 2) { Antlr4.Runtime.Tree.IParseTree c = context.GetChild(1); if (c is BisonParser.SymbolContext) { string sym = c.GetText(); rhs.Last().Add(sym); } } }
/// <summary> /// Обойдём дерево сгенерированное antlr dfs чтобы найти всё что надо подсвечивать(пока находит только атрибуты) /// </summary> /// <param name="parser"> Корень дерева</param> static void GetAttributes(Antlr4.Runtime.Tree.IParseTree parser) { if (parser is XAMLParser.AttributeContext attribute) { Coordinates.Add(Tuple.Create(attribute.Name().Symbol.StartIndex, attribute.Name().Symbol.StopIndex)); } File.AppendAllText("ans.txt", parser.Payload.ToString()); for (int i = 0; i < parser.ChildCount; i++) { GetAttributes(parser.GetChild(i)); } }
override public bool Visit(Antlr4.Runtime.Tree.IParseTree tree) { Warnings = new List <string>(); Errors = new List <string>(); Pass = 1; base.Visit(tree); Pass = 2; base.Visit(tree); Pass = 3; base.Visit(tree); RunAdditionalVerifications(); return(true); }
public override IValue Visit(Antlr4.Runtime.Tree.IParseTree tree) { if (environment["DEBUG"].Value.Equals(ValueFactory.make(true))) { Console.WriteLine("Interpreting: " + tree.GetText()); Console.WriteLine("========================================================"); Console.WriteLine("In environment: " + environment); Console.WriteLine("========================================================"); var result = base.Visit(tree); Console.WriteLine("Resulting in: " + result); Console.WriteLine("========================================================"); return(result); } return(base.Visit(tree)); }
/* * // https://github.com/c0oil/Antlr * public override string VisitSql_clauses(TSqlParser.Sql_clausesContext ctx) * { * System.Console.WriteLine("VisitSql_clauses"); * return VisitChildren(ctx).ToString(); * } * * public override string VisitSql_clause(TSqlParser.Sql_clauseContext ctx) * { * System.Console.WriteLine("VisitSql_clause"); * try * { * return VisitDml_clause(ctx.dml_clause()).ToString(); * } * catch (System.Exception e) * { * return ""; * } * } * * public override string VisitDml_clause(TSqlParser.Dml_clauseContext ctx) * { * System.Console.WriteLine("VisitDml_clause"); * return VisitChildren(ctx).ToString(); * } */ public override string Visit([Antlr4.Runtime.Misc.NotNull] Antlr4.Runtime.Tree.IParseTree tree) { return(base.Visit(tree)); // return tree.GetText(); /* * int n = tree.ChildCount; * string s = ""; * for (int i = 0; i < n; i++) * { * Antlr4.Runtime.Tree.IParseTree c = tree.GetChild(i); * * TSqlParser.BatchContext bc = c as TSqlParser.BatchContext; * if (bc != null) * { * s += bc.start.Text + " " + bc.stop.Text; * } * * } * * return s; */ }
public MathFuncNode Visit(Antlr4.Runtime.Tree.IParseTree tree) { return(tree?.Accept(this)); }
public double Visit(Antlr4.Runtime.Tree.IParseTree tree) { return(tree.Accept(this)); }
/// <summary> /// Get the NodeProperty /// </summary> /// <param name="node">the node</param> /// <returns>NodeProperty</returns> public NodeProperty GetNodePropertyValue(Antlr4.Runtime.Tree.IParseTree node) { return(nodeProp.Get(node)); }
/// <summary> /// Set NodeProperty /// </summary> /// <param name="node">the node</param> /// <param name="value">value</param> public void SetNodePropertyValue(Antlr4.Runtime.Tree.IParseTree node, NodeProperty value) { nodeProp.Put(node, value); }
/// <summary> /// Visits the specified tree. /// </summary> /// <param name="tree">The tree.</param> /// <returns></returns> public override string Visit(Antlr4.Runtime.Tree.IParseTree tree) { base.Visit(tree); return(ReturnType); }
public static QuickInfo GetQuickInfo(int index, Document doc) { var pd = ParserDetailsFactory.Create(doc); if (pd.ParseTree == null) { return(null); } Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc); var gd = GrammarDescriptionFactory.Create(doc.FullPath); if (pt == null) { return(null); } Antlr4.Runtime.Tree.IParseTree p = pt; pd.Attributes.TryGetValue(p, out IList <CombinedScopeSymbol> list_value); var q = p as Antlr4.Runtime.Tree.TerminalNodeImpl; var range = new Workspaces.Range(new Workspaces.Index(q.Symbol.StartIndex), new Workspaces.Index(q.Symbol.StopIndex + 1)); var found = pd.Tags.TryGetValue(q, out int tag_type); if (!found) { return(null); } if (list_value == null || list_value.Count == 0) { return(new QuickInfo() { Display = gd.Map[tag_type], Range = range }); } if (list_value.Count == 1) { var value = list_value.First(); var name = value as Symtab.ISymbol; string show = name?.Name; if (value is Symtab.Literal) { show = ((Symtab.Literal)value).Cleaned; } if (gd.PopUpDefinition[tag_type] != null) { var fun = gd.PopUpDefinition[tag_type]; var mess = fun(pd, p); if (mess != null) { return(new QuickInfo() { Display = mess, Range = range }); } } var display = gd.Map[tag_type] + "\n" + show; return(new QuickInfo() { Display = display, Range = range }); } { var display = "Ambiguous -- "; foreach (var value in list_value) { var name = value as Symtab.ISymbol; string show = name?.Name; if (value is Symtab.Literal) { show = ((Symtab.Literal)value).Cleaned; } if (gd.PopUpDefinition[tag_type] != null) { var fun = gd.PopUpDefinition[tag_type]; var mess = fun(pd, p); if (mess != null) { display = display + mess; } } else { display = display + gd.Map[tag_type] + "\n" + show; } } return(new QuickInfo() { Display = display, Range = range }); } }