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

The following examples show how to use org.antlr.v4.runtime.Token#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: NodeFactory.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public ArithmeticNode createBinary(Token opToken, ExpressionNode leftNode, ExpressionNode rightNode) {
    final ArithmeticNode result;
    switch (opToken.getText()) {
        case "+":
            result = new ArithmeticNode(ArithmeticNode.Operation.ADD, leftNode, rightNode);
            break;
        case "-":
            result = new ArithmeticNode(ArithmeticNode.Operation.SUBTRACT, leftNode, rightNode);
            break;
        case "*":
            result = new ArithmeticNode(ArithmeticNode.Operation.MULTIPLY, leftNode, rightNode);
            break;
        case "/":
            result = new ArithmeticNode(ArithmeticNode.Operation.DIVIDE, leftNode, rightNode);
            break;
        case "%":
            result = new ArithmeticNode(ArithmeticNode.Operation.MODULO, leftNode, rightNode);
            break;
        default:
            // should not happen due to lexer
            throw new GrCUDAInternalException("unexpected operation: " + opToken.getText());
    }
    return result;
}
 
Example 2
Source File: AstBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
private static ArithmeticBinaryExpression.Operator getArithmeticBinaryOperator(Token operator)
{
    switch (operator.getType()) {
        case SqlBaseLexer.PLUS:
            return ArithmeticBinaryExpression.Operator.ADD;
        case SqlBaseLexer.MINUS:
            return ArithmeticBinaryExpression.Operator.SUBTRACT;
        case SqlBaseLexer.ASTERISK:
            return ArithmeticBinaryExpression.Operator.MULTIPLY;
        case SqlBaseLexer.SLASH:
            return ArithmeticBinaryExpression.Operator.DIVIDE;
        case SqlBaseLexer.PERCENT:
            return ArithmeticBinaryExpression.Operator.MODULUS;
    }

    throw new UnsupportedOperationException("Unsupported operator: " + operator.getText());
}
 
Example 3
Source File: AstBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
private static ComparisonExpression.Operator getComparisonOperator(Token symbol)
{
    switch (symbol.getType()) {
        case SqlBaseLexer.EQ:
            return ComparisonExpression.Operator.EQUAL;
        case SqlBaseLexer.NEQ:
            return ComparisonExpression.Operator.NOT_EQUAL;
        case SqlBaseLexer.LT:
            return ComparisonExpression.Operator.LESS_THAN;
        case SqlBaseLexer.LTE:
            return ComparisonExpression.Operator.LESS_THAN_OR_EQUAL;
        case SqlBaseLexer.GT:
            return ComparisonExpression.Operator.GREATER_THAN;
        case SqlBaseLexer.GTE:
            return ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL;
    }

    throw new IllegalArgumentException("Unsupported operator: " + symbol.getText());
}
 
Example 4
Source File: AstBuilder.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
private static IntervalLiteral.IntervalField getIntervalFieldType(Token token) {
  switch (token.getType()) {
    case SqlBaseLexer.YEAR:
      return IntervalLiteral.IntervalField.YEAR;
    case SqlBaseLexer.MONTH:
      return IntervalLiteral.IntervalField.MONTH;
    case SqlBaseLexer.DAY:
      return IntervalLiteral.IntervalField.DAY;
    case SqlBaseLexer.HOUR:
      return IntervalLiteral.IntervalField.HOUR;
    case SqlBaseLexer.MINUTE:
      return IntervalLiteral.IntervalField.MINUTE;
    case SqlBaseLexer.SECOND:
      return IntervalLiteral.IntervalField.SECOND;
    default:
      throw new IllegalArgumentException("Unsupported interval field: " + token.getText());
  }
}
 
Example 5
Source File: AltLabelTextProvider.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String getLabelForToken(Token token) {
	String text = token.getText();
	if (text.equals("<EOF>")) {
		return text;
	}

	return parser.getVocabulary().getSymbolicName(token.getType()) + ": \"" + text + "\"";
}
 
Example 6
Source File: ChapterDef.java    From bookish with MIT License 5 votes vote down vote up
public ChapterDef(int chapNumber, Token titleToken, EntityWithScope enclosingScope) {
	super(chapNumber, titleToken, enclosingScope);
	String title = titleToken.getText();
	title = title.substring(title.indexOf(' ')+1).trim();
	Pair<String, String> results = splitSectionTitle(title);
	this.title = results.a;
	this.label = results.b;
	if ( label==null ) {
		label = "chp:"+getContainerNumber();
	}
}
 
Example 7
Source File: AstBuilder.java    From rainbow with Apache License 2.0 5 votes vote down vote up
private static SortItem.NullOrdering getNullOrderingType(Token token)
{
    switch (token.getType()) {
        case SqlBaseLexer.FIRST:
            return SortItem.NullOrdering.FIRST;
        case SqlBaseLexer.LAST:
            return SortItem.NullOrdering.LAST;
    }

    throw new IllegalArgumentException("Unsupported ordering: " + token.getText());
}
 
Example 8
Source File: AstBuilder.java    From presto with Apache License 2.0 5 votes vote down vote up
private static SortItem.Ordering getOrderingType(Token token)
{
    switch (token.getType()) {
        case SqlBaseLexer.ASC:
            return SortItem.Ordering.ASCENDING;
        case SqlBaseLexer.DESC:
            return SortItem.Ordering.DESCENDING;
    }

    throw new IllegalArgumentException("Unsupported ordering: " + token.getText());
}
 
Example 9
Source File: AstBuilder.java    From presto with Apache License 2.0 5 votes vote down vote up
private static SortItem.NullOrdering getNullOrderingType(Token token)
{
    switch (token.getType()) {
        case SqlBaseLexer.FIRST:
            return SortItem.NullOrdering.FIRST;
        case SqlBaseLexer.LAST:
            return SortItem.NullOrdering.LAST;
    }

    throw new IllegalArgumentException("Unsupported ordering: " + token.getText());
}
 
Example 10
Source File: ZserioAstBuilder.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public FixedBitFieldType visitFixedBitFieldType(ZserioParser.FixedBitFieldTypeContext ctx)
{
    final Token token = ctx.getStart();
    final AstLocation location = new AstLocation(token);
    final boolean isSigned = (ctx.INT_FIELD() != null);

    return new FixedBitFieldType(location, token.getText(), isSigned, ctx.DECIMAL_LITERAL().getText());
}
 
Example 11
Source File: BatfishParserErrorListener.java    From batfish with Apache License 2.0 5 votes vote down vote up
private String printToken(Token token) {
  int modeAsInt = _combinedParser.getTokenMode(token);
  String mode = _combinedParser.getLexer().getModeNames()[modeAsInt];
  String rawTokenText = token.getText();
  String tokenText = BatfishCombinedParser.escape(rawTokenText);
  int tokenType = token.getType();
  String channel = token.getChannel() == Lexer.HIDDEN ? "(HIDDEN) " : "";
  String tokenName;
  int line = token.getLine();
  int col = token.getCharPositionInLine();
  if (tokenType == -1) {
    tokenName = "EOF";
  } else {
    tokenName = _combinedParser.getParser().getVocabulary().getSymbolicName(tokenType);
    tokenText = "'" + tokenText + "'";
  }
  return " line "
      + line
      + ":"
      + col
      + " "
      + channel
      + " "
      + tokenName
      + ":"
      + tokenText
      + (!"DEFAULT_MODE".equals(mode) ? "  <== mode:" + mode : "");
}
 
Example 12
Source File: AstBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
private static CurrentTime.Type getDateTimeFunctionType(Token token) {
    switch (token.getType()) {
        case SqlBaseLexer.CURRENT_DATE:
            return CurrentTime.Type.DATE;
        case SqlBaseLexer.CURRENT_TIME:
            return CurrentTime.Type.TIME;
        case SqlBaseLexer.CURRENT_TIMESTAMP:
            return CurrentTime.Type.TIMESTAMP;
        default:
            throw new UnsupportedOperationException("Unsupported special function: " + token.getText());
    }
}
 
Example 13
Source File: AstBuilder.java    From presto with Apache License 2.0 5 votes vote down vote up
private static FrameBound.Type getBoundedFrameBoundType(Token token)
{
    switch (token.getType()) {
        case SqlBaseLexer.PRECEDING:
            return FrameBound.Type.PRECEDING;
        case SqlBaseLexer.FOLLOWING:
            return FrameBound.Type.FOLLOWING;
    }

    throw new IllegalArgumentException("Unsupported bound type: " + token.getText());
}
 
Example 14
Source File: AstBuilder.java    From presto with Apache License 2.0 5 votes vote down vote up
private static WindowFrame.Type getFrameType(Token type)
{
    switch (type.getType()) {
        case SqlBaseLexer.RANGE:
            return WindowFrame.Type.RANGE;
        case SqlBaseLexer.ROWS:
            return WindowFrame.Type.ROWS;
    }

    throw new IllegalArgumentException("Unsupported frame type: " + type.getText());
}
 
Example 15
Source File: AstBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
private static SortItem.NullOrdering getNullOrderingType(Token token) {
    switch (token.getType()) {
        case SqlBaseLexer.FIRST:
            return SortItem.NullOrdering.FIRST;
        case SqlBaseLexer.LAST:
            return SortItem.NullOrdering.LAST;
        default:
            throw new IllegalArgumentException("Unsupported ordering: " + token.getText());
    }
}
 
Example 16
Source File: AstBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
private static String getClazz(Token token) {
    switch (token.getType()) {
        case SqlBaseLexer.SCHEMA:
            return SCHEMA;
        case SqlBaseLexer.TABLE:
            return TABLE;
        case SqlBaseLexer.VIEW:
            return VIEW;
        default:
            throw new IllegalArgumentException("Unsupported privilege class: " + token.getText());

    }
}
 
Example 17
Source File: Python3MethodParser.java    From Siamese with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a Method given the class node (null for global methods) and the list of TerminalNodeImpl for that
 * function. Loops through terminalNodes to find function name, function parameters, function headers.
 * @param classNode TerminalNodeImpl of the class name
 * @param terminalNodes List of TerminalNodeImpl of a method
 * @return {@link crest.siamese.document.Method}
 */
private Method createMethod(TerminalNodeImpl classNode, List<TerminalNodeImpl> terminalNodes) {
    String className = classNode == null ? StringUtils.EMPTY : classNode.getText();
    String functionName = null;
    int startLine = terminalNodes.get(0).getSymbol().getLine();
    int endLine = terminalNodes.get(terminalNodes.size() - 1).getSymbol().getLine();
    List<Parameter> params = new ArrayList<>();
    StringBuilder header = new StringBuilder();
    for (TerminalNodeImpl terminalNodeImpl: terminalNodes) {
        Token token = terminalNodeImpl.getSymbol();
        RuleContext parent = (RuleContext) terminalNodeImpl.getParent();

        // Function name
        if (functionName == null && token.getType() == Python3Parser.NAME) {
            functionName = token.getText();
        }

        // Function parameters
        if (token.getType() == Python3Parser.NAME && parent.getRuleIndex() == Python3Parser.RULE_tfpdef) {
            params.add(new Parameter(StringUtils.EMPTY, token.getText()));
        }

        // Function header, append space after "def" keyword
        header.append(header.length() == 0 ? token.getText() + WHITESPACE : token.getText());

        // Outside of function header, no further iteration required
        if (token.getType() == Python3Parser.COLON && parent.getRuleIndex() == Python3Parser.RULE_funcdef) {
            break;
        }
    }
    String src = reformat(terminalNodes);
    return new Method(FILE_PATH, StringUtils.EMPTY, className, functionName,
            StringUtils.EMPTY, src, startLine, endLine, params, header.toString());
}
 
Example 18
Source File: HashemNodeFactory.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
/**
 * Returns the corresponding subclass of {@link HashemExpressionNode} for binary expressions. </br>
 * These nodes are currently not instrumented.
 *
 * @param opToken The operator of the binary expression
 * @param leftNode The left node of the expression
 * @param rightNode The right node of the expression
 * @return A subclass of SLExpressionNode using the given parameters based on the given opToken.
 *         null if either leftNode or rightNode is null.
 */
public HashemExpressionNode createBinary(Token opToken, HashemExpressionNode leftNode, HashemExpressionNode rightNode) {
    if (leftNode == null || rightNode == null) {
        return null;
    }
    final HashemExpressionNode leftUnboxed = HashemUnboxNodeGen.create(leftNode);
    final HashemExpressionNode rightUnboxed = HashemUnboxNodeGen.create(rightNode);

    final HashemExpressionNode result;
    switch (opToken.getText()) {
        case "+":
            result = HashemAddNodeGen.create(leftUnboxed, rightUnboxed);
            break;
        case "*":
            result = HashemMulNodeGen.create(leftUnboxed, rightUnboxed);
            break;
        case "/":
            result = HashemDivNodeGen.create(leftUnboxed, rightUnboxed);
            break;
        case "%":
            result = HashemModNodeGen.create(leftUnboxed,rightUnboxed);
            break;
        case "-":
            result = HashemSubNodeGen.create(leftUnboxed, rightUnboxed);
            break;
        case "<":
            result = HashemLessThanNodeGen.create(leftUnboxed, rightUnboxed);
            break;
        case "<=":
            result = HashemLessOrEqualNodeGen.create(leftUnboxed, rightUnboxed);
            break;
        case ">":
            result = HashemLogicalNotNodeGen.create(HashemLessOrEqualNodeGen.create(leftUnboxed, rightUnboxed));
            break;
        case ">=":
            result = HashemLogicalNotNodeGen.create(HashemLessThanNodeGen.create(leftUnboxed, rightUnboxed));
            break;
        case "==":
            result = HashemEqualNodeGen.create(leftUnboxed, rightUnboxed);
            break;
        case "!=":
            result = HashemLogicalNotNodeGen.create(HashemEqualNodeGen.create(leftUnboxed, rightUnboxed));
            break;
        case "&&":
            result = new HashemLogicalAndNode(leftUnboxed, rightUnboxed);
            break;
        case "||":
            result = new HashemLogicalOrNode(leftUnboxed, rightUnboxed);
            break;
        default:
            throw new RuntimeException("unexpected operation: " + opToken.getText());
    }

    int start = leftNode.getSourceCharIndex();
    int length = rightNode.getSourceEndIndex() - start;
    result.setSourceSection(start, length);
    result.addExpressionTag();

    return result;
}
 
Example 19
Source File: AntlrProgramBuilder.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public GrammarToken getBTToken(Token t)
{
	org.beetl.core.statement.GrammarToken token = new org.beetl.core.statement.GrammarToken(t.getText(),
			t.getLine(), t.getCharPositionInLine());
	return token;
}
 
Example 20
Source File: SpecException.java    From scheduler with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static SpecException unknownSymbol(String name, Token sym) {
    return new SpecException(name, sym.getCharPositionInLine(), "Cannot resolve symbol '" + sym.getText() + "'");
}