Java Code Examples for org.antlr.v4.runtime.ParserRuleContext#getText()

The following examples show how to use org.antlr.v4.runtime.ParserRuleContext#getText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQL92Visitor.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
private ASTNode createExpressionSegment(final ASTNode astNode, final ParserRuleContext context) {
    if (astNode instanceof StringLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((StringLiteralValue) astNode).getValue());
    }
    if (astNode instanceof NumberLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((NumberLiteralValue) astNode).getValue());
    }
    if (astNode instanceof BooleanLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((BooleanLiteralValue) astNode).getValue());
    }
    if (astNode instanceof ParameterMarkerValue) {
        return new ParameterMarkerExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
    }
    if (astNode instanceof SubquerySegment) {
        return new SubqueryExpressionSegment((SubquerySegment) astNode);
    }
    if (astNode instanceof OtherLiteralValue) {
        return new CommonExpressionSegment(context.getStart().getStartIndex(), context.getStop().getStopIndex(), context.getText());
    }
    return astNode;
}
 
Example 2
Source File: OracleVisitor.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
private ASTNode createExpressionSegment(final ASTNode astNode, final ParserRuleContext context) {
    if (astNode instanceof StringLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((StringLiteralValue) astNode).getValue());
    }
    if (astNode instanceof NumberLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((NumberLiteralValue) astNode).getValue());
    }
    if (astNode instanceof BooleanLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((BooleanLiteralValue) astNode).getValue());
    }
    if (astNode instanceof ParameterMarkerValue) {
        return new ParameterMarkerExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
    }
    if (astNode instanceof SubquerySegment) {
        return new SubqueryExpressionSegment((SubquerySegment) astNode);
    }
    if (astNode instanceof OtherLiteralValue) {
        return new CommonExpressionSegment(context.getStart().getStartIndex(), context.getStop().getStopIndex(), context.getText());
    }
    return astNode;
}
 
Example 3
Source File: MySQLVisitor.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
private ASTNode createExpressionSegment(final ASTNode astNode, final ParserRuleContext context) {
    if (astNode instanceof StringLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((StringLiteralValue) astNode).getValue());
    }
    if (astNode instanceof NumberLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((NumberLiteralValue) astNode).getValue());
    }
    if (astNode instanceof BooleanLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((BooleanLiteralValue) astNode).getValue());
    }
    if (astNode instanceof ParameterMarkerValue) {
        return new ParameterMarkerExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
    }
    if (astNode instanceof SubquerySegment) {
        return new SubqueryExpressionSegment((SubquerySegment) astNode);
    }
    if (astNode instanceof OtherLiteralValue) {
        return new CommonExpressionSegment(context.getStart().getStartIndex(), context.getStop().getStopIndex(), context.getText());
    }
    return astNode;
}
 
Example 4
Source File: SQLServerVisitor.java    From shardingsphere with Apache License 2.0 6 votes vote down vote up
private ASTNode createExpressionSegment(final ASTNode astNode, final ParserRuleContext context) {
    if (astNode instanceof StringLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((StringLiteralValue) astNode).getValue());
    }
    if (astNode instanceof NumberLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((NumberLiteralValue) astNode).getValue());
    }
    if (astNode instanceof BooleanLiteralValue) {
        return new LiteralExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((BooleanLiteralValue) astNode).getValue());
    }
    if (astNode instanceof ParameterMarkerValue) {
        return new ParameterMarkerExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
    }
    if (astNode instanceof SubquerySegment) {
        return new SubqueryExpressionSegment((SubquerySegment) astNode);
    }
    if (astNode instanceof OtherLiteralValue) {
        return new CommonExpressionSegment(context.getStart().getStartIndex(), context.getStop().getStopIndex(), context.getText());
    }
    return astNode;
}
 
Example 5
Source File: AntlrUtils.java    From yauaa with Apache License 2.0 5 votes vote down vote up
public static String getSourceText(ParserRuleContext ctx){
    if (ctx == null) {
        return null;
    }
    if (ctx.start == null || ctx.stop == null) {
        return ctx.getText();
    }
    int startIndex = ctx.start.getStartIndex();
    int stopIndex = ctx.stop.getStopIndex();
    if (stopIndex < startIndex) {
        return ""; // Just return the empty string.
    }
    CharStream inputStream = ctx.start.getInputStream();
    return inputStream.getText(new Interval(startIndex, stopIndex));
}
 
Example 6
Source File: TreeExpressionEvaluator.java    From yauaa with Apache License 2.0 5 votes vote down vote up
public TreeExpressionEvaluator(ParserRuleContext requiredPattern,
                               Matcher matcher,
                               boolean verbose) {
    this.requiredPatternText = requiredPattern.getText();
    this.matcher = matcher;
    this.verbose = verbose;
    this.fixedValue = calculateFixedValue(requiredPattern);
    walkList = new WalkList(requiredPattern, matcher.getLookups(), matcher.getLookupSets(), verbose);
}
 
Example 7
Source File: PaloAltoConfigurationBuilder.java    From batfish with Apache License 2.0 5 votes vote down vote up
/**
 * Return the text of the provided {@code ctx} if its length is within the provided {@link
 * IntegerSpace lengthSpace}, or else {@link Optional#empty}.
 */
private @Nonnull Optional<String> toStringWithLengthInSpace(
    ParserRuleContext messageCtx, ParserRuleContext ctx, IntegerSpace lengthSpace, String name) {
  String text = ctx.getText();
  if (!lengthSpace.contains(text.length())) {
    _w.addWarning(
        messageCtx,
        getFullText(messageCtx),
        _parser,
        String.format(
            "Expected %s with length in range %s, but got '%s'", text, lengthSpace, name));
    return Optional.empty();
  }
  return Optional.of(text);
}
 
Example 8
Source File: BinaryExpression.java    From swift-js-transpiler with MIT License 4 votes vote down vote up
static public String operatorAlias(ParserRuleContext operator) {
    if(operator instanceof SwiftParser.Conditional_operatorContext) return "?:";
    if(operator instanceof SwiftParser.Type_casting_operatorContext) return operator.getChild(0).getText();
    return operator.getText();
}
 
Example 9
Source File: GroovydocManager.java    From groovy with Apache License 2.0 4 votes vote down vote up
private String findDocCommentByNode(ParserRuleContext node) {
    if (!asBoolean(node)) {
        return null;
    }

    if (node instanceof GroovyParser.ClassBodyContext) {
        return null;
    }

    ParserRuleContext parentContext = node.getParent();

    if (!asBoolean(parentContext)) {
        return null;
    }

    String docCommentNodeText = null;
    boolean sameTypeNodeBefore = false;
    for (ParseTree child : parentContext.children) {

        if (node == child) {
            // if no doc comment node found and no siblings of same type before the node,
            // try to find doc comment node of its parent
            if (!asBoolean((Object) docCommentNodeText) && !sameTypeNodeBefore) {
                return findDocCommentByNode(parentContext);
            }

            return docCommentNodeText;
        }

        if (node.getClass() == child.getClass()) { // e.g. ClassBodyDeclarationContext == ClassBodyDeclarationContext
            docCommentNodeText = null;
            sameTypeNodeBefore = true;
            continue;
        }

        if (!(child instanceof GroovyParser.NlsContext || child instanceof GroovyParser.SepContext)) {
            continue;
        }

        // doc comments are treated as NL
        List<? extends TerminalNode> nlList =
                child instanceof GroovyParser.NlsContext
                        ? ((GroovyParser.NlsContext) child).NL()
                        : ((GroovyParser.SepContext) child).NL();

        int nlListSize = nlList.size();
        if (0 == nlListSize) {
            continue;
        }

        for (int i = nlListSize - 1; i >= 0; i--) {
            String text = nlList.get(i).getText();

            if (matches(text, SPACES_PATTERN)) {
                continue;
            }

            if (text.startsWith(GROOVYDOC_PREFIX)) {
                docCommentNodeText = text;
            } else {
                docCommentNodeText = null;
            }

            break;
        }
    }

    throw new GroovyBugError("node can not be found: " + node.getText()); // The exception should never be thrown!
}
 
Example 10
Source File: QuickstartCqlVisitor.java    From clinical_quality_language with Apache License 2.0 4 votes vote down vote up
private String nullsafeText(ParserRuleContext ctx, String defaultValue) {
    return ctx != null ? ctx.getText() : defaultValue;
}
 
Example 11
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public SiddhiParserException newSiddhiParserException(ParserRuleContext context) {
    return new SiddhiParserException("Syntax error in SiddhiQL, near '" + context.getText() + "'.",
            new int[]{context.getStart().getLine(), context.getStart().getCharPositionInLine()},
            new int[]{context.getStop().getLine(), context.getStop().getCharPositionInLine()});
}
 
Example 12
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public SiddhiParserException newSiddhiParserException(ParserRuleContext context, String message) {
    return new SiddhiParserException("Syntax error in SiddhiQL, near '" + context.getText() + "', " + message + ".",
            new int[]{context.getStart().getLine(), context.getStart().getCharPositionInLine()},
            new int[]{context.getStop().getLine(), context.getStop().getCharPositionInLine()});
}
 
Example 13
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public SiddhiParserException newSiddhiParserException(ParserRuleContext context, String message, Throwable t) {
    return new SiddhiParserException("Syntax error in SiddhiQL, near '" + context.getText() + "', " + message + ".",
            t, new int[]{context.getStart().getLine(), context.getStart().getCharPositionInLine()},
            new int[]{context.getStop().getLine(), context.getStop().getCharPositionInLine()});
}