Example usage for org.antlr.v4.runtime Token getText

List of usage examples for org.antlr.v4.runtime Token getText

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Token getText.

Prototype

String getText();

Source Link

Document

Get the text of the token.

Usage

From source file:com.spotify.heroic.grammar.CoreQueryParser.java

License:Apache License

private ParseException toParseException(final RecognitionException e) {
    final Token token = e.getOffendingToken();

    if (token.getType() == HeroicQueryLexer.UnterminatedQutoedString) {
        return new ParseException(String.format("unterminated string: %s", token.getText()), null,
                token.getLine(), token.getCharPositionInLine());
    }/*w  w  w.j  a  va 2 s.  c o  m*/

    return new ParseException("unexpected token: " + token.getText(), null, token.getLine(),
            token.getCharPositionInLine());
}

From source file:com.sri.ai.grinder.demo.ExpressionEditor.java

License:Open Source License

protected boolean isNumber(Token t) {
    boolean result = false;
    if (isSymbol(t)) {
        SyntaxLeaf s = SyntaxTrees.makeSyntaxLeaf(t.getText());
        if (s.getValue() instanceof Number) {
            result = true;/*from w  w  w.ja v a 2s .com*/
        }
    }
    return result;
}

From source file:com.sri.ai.grinder.demo.ExpressionEditor.java

License:Open Source License

protected boolean isVariable(Token t) {
    boolean result = false;
    if (isSymbol(t)) {
        String strValue = t.getText();
        if (Character.isUpperCase(strValue.charAt(0)) || strValue.equals("_")) {
            result = true;/*w  w w . j av  a  2s. co  m*/
        }
    }
    return result;
}

From source file:com.sri.ai.praise.sgsolver.demo.editor.HOGMCodeArea.java

License:Open Source License

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    int lastTokenEnd = 0;
    ANTLRInputStream input = new ANTLRInputStream(text);
    HOGMLexer lexer = new HOGMLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();//from w w  w .j  a v a 2 s .c o  m
    for (int i = 0; i < tokens.size(); i++) {
        Token t = tokens.get(i);
        if (t.getType() == Token.EOF) {
            break;
        }
        String styleClass;
        if (t.getType() == HOGMLexer.COMMENT || t.getType() == HOGMLexer.LINE_COMMENT) {
            styleClass = "hogmCodeComment";
        } else if (HOGMTerminalSymbols.isTerminalSymbol(t.getText())) {
            styleClass = "hogmCodeKeyword";
        } else {
            styleClass = "hogmCodeOther";
        }
        int spacing = t.getStartIndex() - lastTokenEnd;
        if (spacing > 0) {
            spansBuilder.add(Collections.emptyList(), spacing);
        }
        int stylesize = (t.getStopIndex() - t.getStartIndex()) + 1;
        spansBuilder.add(Collections.singleton(styleClass), stylesize);
        lastTokenEnd = t.getStopIndex() + 1;
    }

    return spansBuilder.create();
}

From source file:com.twosigma.beaker.groovy.autocomplete.GrammarPredicates.java

License:Apache License

public static boolean isClassName(TokenStream _input) {
    try {/*from   w  w  w.j av a2 s . c  o m*/
        int i = 1;
        Token token = _input.LT(i);
        while (token != null && i < _input.size() && _input.LT(i + 1).getType() == GroovyParser.DOT) {
            i = i + 2;
            token = _input.LT(i);
        }
        if (token == null)
            return false;
        // TODO here
        return Character.isUpperCase(Character.codePointAt(token.getText(), 0));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

From source file:com.xiaomi.linden.bql.BQLCompilerAnalyzer.java

License:Apache License

public static String unescapeStringLiteral(TerminalNode terminalNode) {
    Token token = terminalNode.getSymbol();
    if (token.getType() != BQLLexer.STRING_LITERAL) {
        throw new IllegalArgumentException();
    }/*from  www .  j  a  v a  2 s . c om*/

    String text = token.getText();
    char initialChar = text.charAt(0);
    if (text.charAt(text.length() - 1) != initialChar) {
        throw new IllegalArgumentException("malformed string literal");
    }

    text = text.substring(1, text.length() - 1);
    if (initialChar == '\'') {
        text = text.replace("''", "'");
    } else if (initialChar == '"') {
        text = text.replace("\"\"", "\"");
    } else {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    return text;
}

From source file:compiladores.Interface.java

public void ExecutarTeste() throws FileNotFoundException, IOException {
    File arq = new File("t.expr");
    PrintWriter writer;/* ww w .  j a  va 2s  .  co m*/

    try {
        writer = new PrintWriter(arq);
        writer.printf(jTextArea2.getText());
        writer.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
    }
    Reader src = new BufferedReader(new FileReader(arq));
    ANTLRInputStream input = new ANTLRInputStream(src);
    LabeledExprLexer lexer = new LabeledExprLexer(input);
    while (true) {
        Token t = lexer.nextToken();
        if (t.getType() == LabeledExprLexer.EOF) {
            break;
        } else {
            jTextArea1.append(t.getType() + " :: " + t.getText() + "\n");
        }
    }
}

From source file:de.adrodoc55.minecraft.mpl.ide.gui.MplSyntaxFilter.java

License:Open Source License

public void colorTokens(String text) {
    MplLexer lexer = new MplLexer(new ANTLRInputStream(text));
    loop: while (true) {
        Token token = lexer.nextToken();
        switch (token.getType()) {
        case MplLexer.EOF:
            break loop;
        case MplLexer.ALWAYS_ACTIVE:
        case MplLexer.UNCONDITIONAL:
            styleToken(token, getLowFocusKeywordStyle());
            break;
        case MplLexer.BREAKPOINT:
        case MplLexer.CONDITIONAL:
        case MplLexer.ELSE:
        case MplLexer.IF:
        case MplLexer.IMPORT:
        case MplLexer.INCLUDE:
        case MplLexer.INSTALL:
        case MplLexer.INTERCEPT:
        case MplLexer.INVERT:
        case MplLexer.NOT:
        case MplLexer.NOTIFY:
        case MplLexer.ORIENTATION:
        case MplLexer.PROCESS:
        case MplLexer.PROJECT:
        case MplLexer.SKIP_TOKEN:
        case MplLexer.START:
        case MplLexer.STOP:
        case MplLexer.THEN:
        case MplLexer.UNINSTALL:
        case MplLexer.WAITFOR:
            styleToken(token, getHighFocusKeywordStyle());
            break;
        case MplLexer.IMPULSE:
            styleToken(token, getImpulseStyle());
            break;
        case MplLexer.CHAIN:
            styleToken(token, getChainStyle());
            break;
        case MplLexer.BREAK:
        case MplLexer.CONTINUE:
        case MplLexer.DO:
        case MplLexer.REPEAT:
        case MplLexer.WHILE:
            styleToken(token, getRepeatStyle());
            break;
        case MplLexer.NEEDS_REDSTONE:
            styleToken(token, getNeedsRedstoneStyle());
            break;
        case MplLexer.IDENTIFIER:
            styleToken(token, getIdentifierStyle());
            break;
        case MplLexer.COMMAND:
            styleToken(token, getDefaultStyle());
            Matcher insert = INSERT_PATTERN.matcher(token.getText());
            while (insert.find()) {
                int tokenStart = token.getStartIndex();
                int start = tokenStart + insert.start();
                int stop = token.getStartIndex() + insert.end();
                styleToken(start, stop, getInsertStyle());
            }/*  w  w w.  j  a v a 2  s  .  com*/
            break;
        case MplLexer.COMMENT:
            styleToken(token, getCommentStyle());
            break;
        default:
            styleToken(token, getDefaultStyle());
        }
    }
}

From source file:de.adrodoc55.minecraft.mpl.interpretation.MplLexerUtils.java

License:Open Source License

@Nonnull
public static String getContainedString(@Nonnull Token stringToken) {
    Preconditions.checkNotNull(stringToken, "stringToken == null!");
    if (stringToken.getType() != MplLexer.STRING) {
        throw new IllegalArgumentException("The Given Token is not of type MplLexer.STRING!");
    }/*from  w w  w. j a v a  2 s .c  o m*/
    String wholeString = stringToken.getText();
    String containedString = wholeString.substring(1, wholeString.length() - 1);
    return containedString;
}

From source file:de.bioviz.parser.BioParser.java

License:Open Source License

/**
 * Parses the annotations in a file./*from  w ww.  j  a  v  a  2 s .com*/
 * @param input an ANTLRInputStream
 * @param channel the channel to parse
 * @return A List of Strings containing the annotations.
 */
private static List<String> parseChannel(final ANTLRInputStream input, final int channel) {
    BioLexerGrammar lexer = new BioLexerGrammar(input);

    lexer.reset();
    CommonTokenStream cts = new CommonTokenStream(lexer);
    List<String> channelTokens = new ArrayList<>();

    // this one gets everything that is in the stream.
    cts.getText();
    // now we can use size() to run over the tokens
    for (int i = 0; i < cts.size(); i++) {
        Token token = cts.get(i);
        // and check here if the token is on the right channel
        if (token.getChannel() == channel) {
            logger.trace("Parsing Comment: " + token.getText());
            channelTokens.add(token.getText());
        }
    }

    return channelTokens;
}