public Ust VisitChildren(IRuleNode node) { Ust result; if (node.ChildCount == 0) { result = null; } else if (node.ChildCount == 1) { result = Visit(node.GetChild(0)); } else { var exprs = new List <Expression>(); for (int i = 0; i < node.ChildCount; i++) { Ust child = Visit(node.GetChild(i)); if (child != null) { // Ignore null. if (child is Expression childExpression) { exprs.Add(childExpression); } else { exprs.Add(new WrapperExpression(child)); } } } result = new MultichildExpression(exprs); } return(result); }
public virtual void Walk(IParseTreeListener listener, IParseTree t) { if (t is IErrorNode) { listener.VisitErrorNode((IErrorNode)t); return; } else { if (t is ITerminalNode) { listener.VisitTerminal((ITerminalNode)t); return; } } IRuleNode r = (IRuleNode)t; EnterRule(listener, r); int n = r.ChildCount; for (int i = 0; i < n; i++) { Walk(listener, r.GetChild(i)); } ExitRule(listener, r); }
internal ParseTreeNodeViewModel(IParseTree node) { this.node = node; Name = "not set"; TextColor = Brushes.Black; if (node is IErrorNode) { TextColor = Brushes.Red; } var terminalNode = node as ITerminalNode; if (terminalNode != null) { Name = terminalNode.Symbol.Text; return; } Name = node.GetType().Name; IRuleNode ruleNode = (IRuleNode)node; for (int i = 0; i < ruleNode.ChildCount; i++) { Children.Add(new ParseTreeNodeViewModel(ruleNode.GetChild(i))); } }
public virtual string Walk(IParseTreeListener listener, IParseTree t) { counter++; if (t is IErrorNode) { listener.VisitErrorNode((IErrorNode)t); txt = txt + t.ToString(); return(txt); } else { if (t is ITerminalNode) { listener.VisitTerminal((ITerminalNode)t); return(txt); } } IRuleNode r = (IRuleNode)t; string parentname = r.GetText(); //Console.WriteLine("##### PARENT " + counter + "####" + parentname + "#####"); EnterRule(listener, r); int n = r.ChildCount; for (int i = 0; i < n; i++) { Walk(listener, r.GetChild(i)); Console.WriteLine("##### CHILD " + i + " of " + parentname + "####" + r.GetChild(i).GetText() + "#####"); } ExitRule(listener, r); return(txt); }
/// <summary> /// Is /// <paramref name="t"/> /// /// <c>(expr <expr>)</c> /// subtree? /// </summary> protected internal virtual RuleTagToken GetRuleTagToken(IParseTree t) { if (t is IRuleNode) { IRuleNode r = (IRuleNode)t; if (r.ChildCount == 1 && r.GetChild(0) is ITerminalNode) { ITerminalNode c = (ITerminalNode)r.GetChild(0); if (c.Symbol is RuleTagToken) { // System.out.println("rule tag subtree "+t.toStringTree(parser)); return((RuleTagToken)c.Symbol); } } } return(null); }
public override string VisitChildren([NotNull] IRuleNode node) { string s = string.Empty; for (var i = 0; i < node.ChildCount; i++) { s += Visit(node.GetChild(i)); } return(s); }
private static PathSegment GetModIdent(IRuleNode modNode) { var firstNode = modNode.GetChild(0) as IRuleNode; int currentChild = 0; if (firstNode != null) { currentChild = 1; ITree attrRoot = modNode.GetChild(0); for(int i = 0; i < attrRoot.ChildCount; i++) { IRuleNode attrNode = (IRuleNode)attrRoot.GetChild(i); if(attrNode.GetChild(2).GetText() == "path") { string rawString = EscapeRustString(attrNode.GetChild(4).GetText()); return new PathSegment(rawString.Substring(1, rawString.Length - 2), true); } } } var pubOrMod = (ITerminalNode)modNode.GetChild(currentChild); IParseTree identNode = modNode.GetChild(pubOrMod.Symbol.Type == ModuleParser.PUB ? currentChild + 2 : currentChild + 1); return new PathSegment(identNode.GetText(), false); }
public string VisitChildren(IRuleNode node) { var result = new StringBuilder(); for (int i = 0; i < node.ChildCount; i++) { result.Append(Visit(node.GetChild(i))); if (i != node.ChildCount - 1) { result.Append(Delimeter); } } return(result.ToString()); }
private static PathSegment GetModIdent(IRuleNode modNode) { var firstNode = modNode.GetChild(0) as IRuleNode; int currentChild = 0; if (firstNode != null) { currentChild = 1; ITree attrRoot = modNode.GetChild(0); for (int i = 0; i < attrRoot.ChildCount; i++) { IRuleNode attrNode = (IRuleNode)attrRoot.GetChild(i); if (attrNode.GetChild(2).GetText() == "path") { string rawString = EscapeRustString(attrNode.GetChild(4).GetText()); return(new PathSegment(rawString.Substring(1, rawString.Length - 2), true)); } } } var pubOrMod = (ITerminalNode)modNode.GetChild(currentChild); IParseTree identNode = modNode.GetChild(pubOrMod.Symbol.Type == ModuleParser.PUB ? currentChild + 2 : currentChild + 1); return(new PathSegment(identNode.GetText(), false)); }
/// <summary> /// Visits each children in IRuleNode /// </summary> /// <param name="node"></param> /// <returns></returns> public override AdaptiveCardsTemplateResult VisitChildren([NotNull] IRuleNode node) { if (node == null) { throw new ArgumentNullException(nameof(node)); } AdaptiveCardsTemplateResult result = new AdaptiveCardsTemplateResult(); for (int i = 0; i < node.ChildCount; i++) { result.Append(Visit(node.GetChild(i))); } return(result); }
/// <summary> /// <inheritDoc></inheritDoc> /// <p/> /// The default implementation initializes the aggregate result to /// <see cref="AbstractParseTreeVisitor{Result}.DefaultResult()">defaultResult()</see> /// . Before visiting each child, it /// calls /// <see cref="AbstractParseTreeVisitor{Result}.ShouldVisitNextChild(IRuleNode, Result)">shouldVisitNextChild</see> /// ; if the result /// is /// <code>false</code> /// no more children are visited and the current aggregate /// result is returned. After visiting a child, the aggregate result is /// updated by calling /// <see cref="AbstractParseTreeVisitor{Result}.AggregateResult(Result, Result)">aggregateResult /// </see> /// with the /// previous aggregate result and the result of visiting the child. /// </summary> public virtual Result VisitChildren(IRuleNode node) { Result result = DefaultResult(); int n = node.ChildCount; for (int i = 0; i < n; i++) { if (!ShouldVisitNextChild(node, result)) { break; } IParseTree c = node.GetChild(i); Result childResult = c.Accept(this); result = AggregateResult(result, childResult); } return(result); }
public VbaParseTree VisitChildren(IRuleNode node) { VbaParseTree result = VbaParseTree.Create(node); int childCount = node.ChildCount; for (var i = 0; i < childCount; i++) { VbaParseTree nextResult = node.GetChild(i).Accept(this); if (IsNodeToIgnore(nextResult)) { continue; } nextResult.Parent = result; result.Children.Add(nextResult); } return(result); }
// public override bool Visit(IParseTree tree) // { // throw new NotImplementedException(); // } //public override bool VisitTerminal(ITerminalNode node) //{ // Errors.Add("ERROR: Line 3, Column 4: wtf??"); // return true; //} public override bool VisitChildren(IRuleNode node) { bool result = DefaultResult; int n = node.ChildCount; for (int i = 0; i < n; i++) { if (!ShouldVisitNextChild(node, result)) { break; } IParseTree c = node.GetChild(i); bool childResult = c.Accept(this); //result = AggregateResult(result, childResult); result = result || childResult; } return(result); }
//public override List<GeneralError> Visit(IParseTree tree) //{ // var errs = new List<GeneralError>(); // for (var i = 0; i < tree.ChildCount; i++) // { // var child = tree.GetChild(i); // var childErrs = child.Accept(this); // if (childErrs != null) // { // errs.AddRange(childErrs); // } // //Visit(child); // } // return errs; //} public override List <GeneralError> VisitChildren(IRuleNode node) { var errs = new List <GeneralError>(); for (var i = 0; i < node.ChildCount; i++) { var child = node.GetChild(i); var childErrs = child.Accept(this); if (childErrs != null) { errs.AddRange(childErrs); } //Visit(child); } return(errs); }
public override Node VisitChildren(IRuleNode node) { NodeList result = new NodeList(); int n = node.ChildCount; for (int i = 0; i < n; i++) { if (!ShouldVisitNextChild(node, result)) { break; } IParseTree c = node.GetChild(i); Node childResult = c.Accept(this); if (childResult == null) { continue; } result.Childs.Add(childResult); } return(result); }
public override object VisitChildren([NotNull] IRuleNode node) { object result = null; int n = node.ChildCount; for (int i = 0; i < n; i++) { if (!ShouldVisitNextChild(node, result)) { break; } IParseTree c = node.GetChild(i); if (c is TerminalNodeImpl || c is ErrorNodeImpl) { continue; } object childResult = c.Accept(this); result = AggregateResult(result, childResult); } return(result); }
public override ParseTreeNode VisitChildren(IRuleNode node) { ParseTreeNode result = new ParseTreeNode(); /*if(node.RuleContext is Java8Parser.CompilationUnitContext ||node.RuleContext is Java8Parser.NormalClassDeclarationContext ||node.RuleContext is Java8Parser.FieldDeclarationContext ||node.RuleContext is Java8Parser.MethodDeclarationContext ||node.RuleContext is Java8Parser.InterfaceDeclarationContext ||node.RuleContext is Java8Parser.InterfaceMethodDeclarationContext) * { * result = DefaultResult; * }*/ int n = node.ChildCount; for (int i = 0; i < n; i++) { if (!ShouldVisitNextChild(node, result)) { break; } IParseTree c = node.GetChild(i); ParseTreeNode childResult = c.Accept(this); //result = AggregateResult(result, childResult); if (childResult != null) { if (childResult.ItemType == IClassParser.ItemType.__InternalPlaceHolder || childResult.ItemType == IClassParser.ItemType.Field) //expand sub items { result.Children = result.Children.Concat(childResult.Children).ToList(); } else { result.Children.Add(childResult); } } } return(result); }
public override BaseNode VisitChildren([NotNull] IRuleNode node) { if (node.ChildCount > 1) { ITreeNode[] newChildren = new ITreeNode[node.ChildCount]; for (int i = 0; i < node.ChildCount; i++) { newChildren[i] = new BaseNodeVisitor().Visit(node.GetChild(i)); } BaseNode newNode = new BaseNode() { Name = node.GetType().Name, Children = newChildren }; return(newNode); } else { return(base.VisitChildren(node)); } }
/// <summary> /// this is the default implementation of antlr /// AbstractParseTreeVisitor.VisitChildren /// the differences are the comments /// </summary> /// <param name="node"></param> /// <returns></returns> public override Node VisitChildren(IRuleNode node) { NodeList result = new NodeList(); //Result result = DefaultResult; int n = node.ChildCount; for (int i = 0; i < n; i++) { if (!ShouldVisitNextChild(node, result)) { break; } IParseTree c = node.GetChild(i); Node childResult = c.Accept(this); //result = AggregateResult(result, childResult);/// maybe we should override this method instead if (childResult == null) { continue; } result.Childs.Add(childResult); } return(result); }
public override bool VisitChildren(IRuleNode node) { var childCount = node.ChildCount; var _ = true; for (var i = 0; i < childCount && ShouldVisitNextChild(node, _); ++i) { var child = node.GetChild(i); if (child.ChildCount == 0) { var value = child.GetText(); if (value != "<EOF>") { _output.Append(value); } } else { child.Accept(this); } } return(true); }
public void Parse(string plain_old_input_grammar, string ffn) { Text = plain_old_input_grammar; full_file_name = ffn; // Set up Antlr to parse input grammar. byte[] byteArray = Encoding.UTF8.GetBytes(plain_old_input_grammar); CommonTokenStream cts = new CommonTokenStream( new ANTLRv4Lexer( new AntlrInputStream( new StreamReader( new MemoryStream(byteArray)).ReadToEnd()))); _ant_parser = new ANTLRv4Parser(cts); // Set up another token stream containing comments. This might be // problematic as the parser influences the lexer. CommonTokenStream cts_off_channel = new CommonTokenStream( new ANTLRv4Lexer( new AntlrInputStream( new StreamReader( new MemoryStream(byteArray)).ReadToEnd())), ANTLRv4Lexer.OFF_CHANNEL); // Get all comments. _ant_comments = new List <IToken>(); while (cts_off_channel.LA(1) != ANTLRv4Parser.Eof) { IToken token = cts_off_channel.LT(1); if (token.Type == ANTLRv4Parser.BLOCK_COMMENT || token.Type == ANTLRv4Parser.LINE_COMMENT) { _ant_comments.Add(token); } cts_off_channel.Consume(); } try { _ant_tree = _ant_parser.grammarSpec(); } catch (Exception e) { // Parsing error. } _all_nodes = DFSVisitor.DFS(_ant_tree as ParserRuleContext).ToArray(); { // Get all nonterminal names from the grammar. IEnumerable <IParseTree> nonterm_nodes_iterator = _all_nodes.Where((IParseTree n) => { TerminalNodeImpl nonterm = n as TerminalNodeImpl; return(nonterm?.Symbol.Type == ANTLRv4Parser.RULE_REF); }); _ant_nonterminals_names = nonterm_nodes_iterator.Select <IParseTree, string>( (t) => (t as TerminalNodeImpl).Symbol.Text).ToArray(); } { // Get all terminal names from the grammar. IEnumerable <IParseTree> term_nodes_iterator = _all_nodes.Where((IParseTree n) => { TerminalNodeImpl nonterm = n as TerminalNodeImpl; return(nonterm?.Symbol.Type == ANTLRv4Parser.TOKEN_REF); }); _ant_terminals_names = term_nodes_iterator.Select <IParseTree, string>( (t) => (t as TerminalNodeImpl).Symbol.Text).ToArray(); } { // Get all defining and applied occurences of nonterminal names in grammar. IEnumerable <IParseTree> nonterm_nodes_iterator = _all_nodes.Where((IParseTree n) => { TerminalNodeImpl nonterm = n as TerminalNodeImpl; if (nonterm == null) { return(false); } if (!_ant_nonterminals_names.Contains(nonterm.GetText())) { return(false); } // The token must be part of parserRuleSpec context. for (var p = nonterm.Parent; p != null; p = p.Parent) { if (p is ANTLRv4Parser.ParserRuleSpecContext) { return(true); } } return(false); }); _ant_nonterminals = nonterm_nodes_iterator.Select <IParseTree, IToken>( (t) => (t as TerminalNodeImpl).Symbol).ToArray(); // Get all defining and applied occurences of nonterminal names in grammar. var iterator = nonterm_nodes_iterator.Where((IParseTree n) => { TerminalNodeImpl term = n as TerminalNodeImpl; if (term == null) { return(false); } IRuleNode parent = term.Parent; for (int i = 0; i < parent.ChildCount; ++i) { if (parent.GetChild(i) == term && i + 1 < parent.ChildCount && parent.GetChild(i + 1).GetText() == ":") { return(true); } } return(false); }); _ant_nonterminals_defining = iterator.Select <IParseTree, IToken>( (t) => (t as TerminalNodeImpl).Symbol).ToArray(); } { // Get all defining and applied occurences of nonterminal names in grammar. IEnumerable <IParseTree> term_nodes_iterator = _all_nodes.Where((IParseTree n) => { TerminalNodeImpl term = n as TerminalNodeImpl; if (term == null) { return(false); } if (!_ant_terminals_names.Contains(term.GetText())) { return(false); } // The token must be part of parserRuleSpec context. for (var p = term.Parent; p != null; p = p.Parent) { if (p is ANTLRv4Parser.ParserRuleSpecContext || p is ANTLRv4Parser.LexerRuleSpecContext) { return(true); } } return(false); }); _ant_terminals = term_nodes_iterator.Select <IParseTree, IToken>( (t) => (t as TerminalNodeImpl).Symbol).ToArray(); // Get all defining nonterminal names in grammar. var iterator = term_nodes_iterator.Where((IParseTree n) => { TerminalNodeImpl term = n as TerminalNodeImpl; if (term == null) { return(false); } IRuleNode parent = term.Parent; for (int i = 0; i < parent.ChildCount; ++i) { if (parent.GetChild(i) == term && i + 1 < parent.ChildCount && parent.GetChild(i + 1).GetText() == ":") { return(true); } } return(false); }); _ant_terminals_defining = iterator.Select <IParseTree, IToken>( (t) => (t as TerminalNodeImpl).Symbol).ToArray(); } { // Get all keyword tokens in grammar. IEnumerable <IParseTree> keywords_interator = _all_nodes.Where((IParseTree n) => { TerminalNodeImpl nonterm = n as TerminalNodeImpl; if (nonterm == null) { return(false); } for (var p = nonterm.Parent; p != null; p = p.Parent) { // "parser grammar" "lexer grammar" etc. if (p is ANTLRv4Parser.GrammarTypeContext) { return(true); } if (p is ANTLRv4Parser.OptionsSpecContext) { return(true); } // "options ..." if (p is ANTLRv4Parser.OptionContext) { return(false); } // "import ..." if (p is ANTLRv4Parser.DelegateGrammarsContext) { return(true); } if (p is ANTLRv4Parser.DelegateGrammarContext) { return(false); } // "tokens ..." if (p is ANTLRv4Parser.TokensSpecContext) { return(true); } if (p is ANTLRv4Parser.IdListContext) { return(false); } // "channels ..." if (p is ANTLRv4Parser.ChannelsSpecContext) { return(true); } if (p is ANTLRv4Parser.ModeSpecContext) { return(true); } } return(false); }); _ant_keywords = keywords_interator.Select <IParseTree, IToken>( (t) => (t as TerminalNodeImpl).Symbol).ToArray(); } { // Get all defining and applied occurences of nonterminal names in grammar. IEnumerable <IParseTree> lit_nodes_iterator = _all_nodes.Where((IParseTree n) => { TerminalNodeImpl term = n as TerminalNodeImpl; if (term == null) { return(false); } // Chicken/egg problem. Assume that literals are marked // with the appropriate token type. if (term.Symbol == null) { return(false); } if (!(term.Symbol.Type == ANTLRv4Parser.STRING_LITERAL || term.Symbol.Type == ANTLRv4Parser.INT || term.Symbol.Type == ANTLRv4Parser.LEXER_CHAR_SET)) { return(false); } // The token must be part of parserRuleSpec context. for (var p = term.Parent; p != null; p = p.Parent) { if (p is ANTLRv4Parser.ParserRuleSpecContext || p is ANTLRv4Parser.LexerRuleSpecContext) { return(true); } } return(false); }); _ant_literals = lit_nodes_iterator.Select <IParseTree, IToken>( (t) => (t as TerminalNodeImpl).Symbol).ToArray(); } //pp.ErrorHandler = new MyErrorStrategy(); }
private static int GetSingleChildTokenType(IRuleNode node) { return ((ITerminalNode) node.GetChild(0)).Symbol.Type; }