Java源码示例:org.antlr.v4.runtime.tree.RuleNode

示例1
@Test
void mapping() {
    TypeScriptParser parser = TypeScriptParserFactory.parse(CharStreams.fromString(mapping_stmt));
    assertThat(parser.getNumberOfSyntaxErrors()).isEqualTo(0);
    CallExpressionContext callExpressionContext = parser.callExpression();
    System.out.println(callExpressionContext.getText());
    callExpressionContext.accept(new AbstractParseTreeVisitor<Object>() {
        @Override
        public Object visitChildren(RuleNode node) {
            if (node instanceof CallExpressionContext) {
                CallExpressionContext call = (CallExpressionContext) node;
                System.out.println(call.getText());
                if (call.identifierName() != null) {
                    System.out.println(call.identifierName().getText());
                }
                if (call.memberExpression() != null) {
                    System.out.println(call.memberExpression().getText());
                }
            }
            return super.visitChildren(node);
        }
    });
}
 
示例2
private void enterParseTree(ParseTree tree){
    int cc = tree.getChildCount();
    for(int i=0;i<cc;i++){
        ParseTree c = tree.getChild(i);
        if(c instanceof RuleNode){
            enterParseTree(c);
        }else if (c instanceof TerminalNode){
            Token t = ((TerminalNode)c).getSymbol();
            index2token.put(tokenCounter, t);
            token2tree.put(t, c);
            tokenCounter ++;
        }else{
            System.err.println("unknown node:" + c);
        }
    }
}
 
示例3
@SuppressWarnings("unchecked")
@Override
public Object visitChildren(final RuleNode node) {

	final int n = node.getChildCount();

	for (int i = 0; i < n; i++) {
		final ParseTree c = node.getChild(i);
		c.accept(this);

	}
	final String textToFind = node.getText();
	if (StringUtils.containsIgnoreCase(textToFind, tempText)
			|| StringUtils.containsIgnoreCase(tempText, textToFind)) {
		nodes.add(new ParsedNode(node));
	}
	return null;

}
 
示例4
@Override
public RuleNode root() throws Exception {
    return parserSetup(
            new ru.smartdec.smartcheck.SolidityParser(
                    new CommonTokenStream(
                            lexerSetup(
                                    new ru.smartdec.smartcheck.SolidityLexer(
                                            this.source.chars()
                                    )
                            )
                    )
            )
    )
            .sourceUnit();
}
 
示例5
@Override
public RuleNode root() throws Exception {
    return parserSetup(
            new ru.smartdec.smartcheck.VyperParser(
                    new CommonTokenStream(
                            lexerSetup(
                                    new ru.smartdec.smartcheck.VyperLexer(
                                            this.source.chars()
                                    )
                            )
                    )
            )
    )
            .file_input();
}
 
示例6
@Override
public RuleNode root() {
    return this.cache.updateAndGet(
            root -> Optional
                    .ofNullable(root)
                    .orElseGet(this.origin::rootUnchecked)
    );
}
 
示例7
/**
 * @return root
 */
default RuleNode rootUnchecked() {
    try {
        return this.root();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
示例8
public static int getParseTreeIndex(ParseTree parseTree) {
    if (parseTree instanceof TerminalNode) {
        return ((TerminalNode) parseTree).getSymbol().getType();
    } else {
        return ((RuleNode) parseTree).getRuleContext().getRuleIndex();
    }
}
 
示例9
public String visitChildren(RuleNode node, List<Integer> withoutNodes) {
    if(node == null) return "";
    String result = this.defaultResult();
    int n = node.getChildCount();

    for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) {
        if(withoutNodes != null && withoutNodes.contains(i)) continue;
        ParseTree c = node.getChild(i);
        String childResult = c instanceof TerminalNode ? printTerminalNode((TerminalNode) c) : c.accept(this);
        result = this.aggregateResult(result, childResult);
    }

    return result;
}
 
示例10
public String visitWithoutTerminals(RuleNode node) {
    String result = this.defaultResult();
    int n = node.getChildCount();

    for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) {
        ParseTree c = node.getChild(i);
        if(c instanceof TerminalNode) continue;
        String childResult = c.accept(this);
        result = this.aggregateResult(result, childResult);
    }

    return result;
}
 
示例11
public String visitWithoutStrings(RuleNode node, String string) {
    String result = this.defaultResult();
    int n = node.getChildCount();

    for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) {
        ParseTree c = node.getChild(i);
        if(string.contains(c.getText())) continue;
        String childResult = c instanceof TerminalNode ? printTerminalNode((TerminalNode) c) : c.accept(this);
        result = this.aggregateResult(result, childResult);
    }

    return result;
}
 
示例12
public String visitWithoutClasses(RuleNode node, Class nodeType) {
    String result = this.defaultResult();
    int n = node.getChildCount();

    for(int i = 0; i < n && this.shouldVisitNextChild(node, result); ++i) {
        ParseTree c = node.getChild(i);
        if(c.getClass() == nodeType) continue;
        String childResult = c instanceof TerminalNode ? printTerminalNode((TerminalNode) c) : c.accept(this);
        result = this.aggregateResult(result, childResult);
    }

    return result;
}
 
示例13
public LogicalPlan parseToPlan() {
    ParseTree root = parseSQL(sql);
    checkNode(root, RuleNode.class);
    if (root.getChildCount() == 0 || checkNode(root.getChild(0), RuleNode.class) == null) {
        return new OneRowRelation();
    }
    StatementContext statementContext = (StatementContext) root.getChild(0);
    return parseToSelectPlan(statementContext.selectStatement());
}
 
示例14
private static void parseTreeWalker(ParseTree node, Consumer<RuleNode> f) {
    RuleNode rule = checkNode(node, RuleNode.class);
    if (rule == null) {
        return;
    }
    f.accept(rule);
    for (int i = 0; i < rule.getChildCount(); i++) {
        parseTreeWalker(rule.getChild(i), f);
    }
}
 
示例15
public static int getParseTreeIndex(ParseTree parseTree) {
    if (parseTree instanceof TerminalNode) {
        return ((TerminalNode)parseTree).getSymbol().getType();
    } else {
        return ((RuleNode)parseTree).getRuleContext().getRuleIndex();
    }
}
 
示例16
@Override
protected void enterRule(ParseTreeListener listener, RuleNode r) {
  ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext();
  try {
    listener.enterEveryRule(ctx);
    ctx.enterRule(listener);
  } catch (Exception e) {
    throw new BatfishParseException(
        String.format("Exception while walking parse tree: %s", e.getMessage()),
        e,
        new ErrorDetails(
            Throwables.getStackTraceAsString(e),
            new ParseExceptionContext(ctx, _parser, _parser.getInput())));
  }
}
 
示例17
protected String text(RuleNode node) {
    return node == null ? "" : node.getText();
}
 
示例18
protected String text(RuleNode node) {
    return node == null ? "" : node.getText();
}
 
示例19
/**
 * @param tree      parse tree to cache
 * @param reference cached tree
 */
public ParseTreeCached(
        final ParseTree tree, final AtomicReference<RuleNode> reference) {
    this.origin = tree;
    this.cache = reference;
}
 
示例20
@Override
public ParserResult visitChildren(RuleNode node) {
    throw new UnsupportedOperationException("No children visitor implemented");
}
 
示例21
/**
 * @return root
 * @throws Exception exception
 */
RuleNode root() throws Exception;
 
示例22
/**
 * @return root as rule node
 */
RuleNode ruleNode();
 
示例23
@Override public String visitChildren(RuleNode node) { return visitChildren(node, null); }