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:org.beetl.core.AntlrProgramBuilder.java

License:BSD License

protected Expression parseLiteralExpress(LiteralContext ctx) {

    ParseTree tree = ctx.getChild(0);//from   w w  w . j  a v  a 2 s.  c  o  m
    Object value = null;
    if (tree instanceof TerminalNode) {
        Token node = ((TerminalNode) tree).getSymbol();
        String strValue = node.getText();

        int type = node.getType();
        switch (type) {
        case BeetlParser.StringLiteral:
            value = getStringValue(strValue);
            break;
        case BeetlParser.FloatingPointLiteral:
            char c = strValue.charAt(strValue.length() - 1);
            if (isHighScaleNumber(strValue)) {
                String newValue = strValue.substring(0, strValue.length() - 1);
                value = new BigDecimal(newValue);
            } else {
                value = Double.parseDouble(strValue);
            }

            break;
        case BeetlParser.DecimalLiteral:
            if (isHighScaleNumber(strValue)) {
                String newValue = strValue.substring(0, strValue.length() - 1);
                value = new BigDecimal(newValue);
            } else {
                if (strValue.length() < 10) {
                    value = Integer.parseInt(strValue);
                } else if (strValue.length() > 10) {
                    value = Long.parseLong(strValue);
                } else if (strValue.compareTo("2147483647") > 0) {

                    value = Long.parseLong(strValue);
                } else {
                    value = Integer.parseInt(strValue);
                }

            }

            break;
        case BeetlParser.NULL:
            value = null;
            break;

        }

    } else {
        BooleanLiteralContext blc = (BooleanLiteralContext) tree;
        String strValue = blc.getChild(0).getText();
        value = Boolean.parseBoolean(strValue);
    }
    if (value == null) {
        return Literal.NULLLiteral;
    } else {
        Literal literal = new Literal(value, this.getBTToken(ctx.getStart()));
        return literal;
    }

}

From source file:org.beetl.core.AntlrProgramBuilder.java

License:BSD License

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;
}

From source file:org.beetl.core.parser.BeetlAntlrErrorStrategy.java

License:BSD License

protected GrammarToken getGrammarToken(Token token) {
    return GrammarToken.createToken(token.getText(), token.getLine());
}

From source file:org.btrplace.safeplace.spec.MyCstrSpecVisitor.java

License:Open Source License

private Function resolveFunction(Token t, List<Term> args) {
    Function f = symbols.getFunction(t.getText());
    if (f == null) {
        throw SpecException.unknownSymbol(filename, t);
    }//w  ww.  j  a v a 2 s  .co  m

    Type[] expected = f.signature();
    if (expected.length != args.size()) {
        throw SpecException.badFunctionCall(filename, t, f, args);
    }
    for (int i = 0; i < expected.length; i++) {
        if (!expected[i].equals(args.get(i).type())) {
            throw SpecException.badFunctionCall(filename, t, f, args);
        }
    }

    return f;
}

From source file:org.btrplace.safeplace.spec.SpecException.java

License:Open Source License

public static SpecException unknownSymbol(String name, Token sym) {
    return new SpecException(name, sym.getCharPositionInLine(),
            "Cannot resolve symbol '" + sym.getText() + "'");
}

From source file:org.btrplace.safeplace.spec.SpecException.java

License:Open Source License

public static SpecException unsupportedOperation(String name, Type t1, Token to, Type t2) {
    return new SpecException(name, to.getCharPositionInLine(),
            "Unsupported operation '" + t1 + " " + to.getText() + " " + t2 + "'");
}

From source file:org.cirdles.squid.gui.expressions.ExpressionBuilderController.java

License:Apache License

private void makeTextFlowFromString(String string) {

    List<Node> children = new ArrayList<>();

    //The lexer separates the expression into tokens
    // updated to fix deprecations July 2018
    try {/*from   w  ww.j  ava2s.  c  o m*/
        InputStream stream = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
        ExpressionsForSquid2Lexer lexer = new ExpressionsForSquid2Lexer(
                CharStreams.fromStream(stream, StandardCharsets.UTF_8));
        List<? extends Token> tokens = lexer.getAllTokens();

        //Creates the notes from tokens
        for (int i = 0; i < tokens.size(); i++) {
            Token token = tokens.get(i);
            String nodeText = token.getText();

            ExpressionTextNode etn;

            // Make a node of the corresponding type
            if (ShuntingYard.isNumber(nodeText) || NUMBERSTRING.equals(nodeText)) {
                etn = new NumberTextNode(' ' + nodeText + ' ');
            } else if (listOperators.contains(nodeText)) {
                etn = new OperationTextNode(' ' + nodeText + ' ');
            } else if (nodeText.equals("\n") || nodeText.equals("\r")) {
                if (whiteSpaceVisible.get()) {
                    etn = new PresentationTextNode(VISIBLENEWLINEPLACEHOLDER);
                } else {
                    etn = new PresentationTextNode(INVISIBLENEWLINEPLACEHOLDER);
                }
            } else if (nodeText.equals("\t")) {
                if (whiteSpaceVisible.get()) {
                    etn = new PresentationTextNode(VISIBLETABPLACEHOLDER);
                } else {
                    etn = new PresentationTextNode(INVISIBLETABPLACEHOLDER);
                }
            } else if (nodeText.equals(" ")) {
                if (whiteSpaceVisible.get()) {
                    etn = new PresentationTextNode(VISIBLEWHITESPACEPLACEHOLDER);
                } else {
                    etn = new PresentationTextNode(INVISIBLEWHITESPACEPLACEHOLDER);
                }
            } else {
                etn = new ExpressionTextNode(' ' + nodeText + ' ');
            }

            etn.setOrdinalIndex(i);
            children.add(etn);
        }
    } catch (IOException iOException) {
    }
    expressionTextFlow.getChildren().setAll(children);
}

From source file:org.corpus_tools.annis.ql.parser.JoinListener.java

License:Apache License

private QueryNode nodeByRef(Token ref) {
    return alternativeNodes.get(alternativeIndex).get("" + ref.getText().substring(1));
}

From source file:org.ddi.data.gui.DataImporterGUI.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

    if (!jTextField1.getText().isEmpty()) {
        String w = jTextField1.getText();
        ZemberekLexer lexer = new ZemberekLexer();
        jTextArea1.setText(jTextArea1.getText() + "\n\nSentence: " + w);
        Iterator<Token> tokenIterator = lexer.getTokenIterator(w);
        while (tokenIterator.hasNext()) {
            Token t = tokenIterator.next();
            if (t.getType() == 7) {
                List<MorphParse> parses = parser.parse(t.getText());
                for (MorphParse parse : parses)
                    jTextArea1.setText(jTextArea1.getText() + parse.formatLong() + "\n");
            } else if (t.getType() == 12) {
                jTextArea1.setText(jTextArea1.getText() + "-------------------------\n");
            }//  ww w .  ja  va 2  s.  c  o m

        }

    }

}

From source file:org.eclipse.titan.common.parsers.cfg.CfgParseTreePrinter.java

License:Open Source License

/**
 * Builds token text including hidden tokens before the token
 * @param aToken token to print/*  w  ww . j av  a  2 s.c  o m*/
 * @param aTokens token list from the lexer (all, hidden and not hidden also)
 * @param aPrintHiddenBefore true to print hidden tokens before the token
 * @param aResolveMode mode of resolving
 * @param aFile the parse tree of this file to print
 *                        needed only if aResolveMode != NO_RESOLVING, in case of [ORDERED_INCLUDE]
 */
private void printToken(final Token aToken, final List<Token> aTokens, final boolean aPrintHiddenBefore,
        final ResolveMode aResolveMode, final Path aFile) {
    final int tokenIndex = aToken.getTokenIndex();
    if (tokenIndex > -1 && aPrintHiddenBefore) {
        // Token has no index if tokenIndex == -1.
        // If a token is added to the parse tree after parse time, token start index in unknown (-1),
        // because token has no index in the token list.
        printHiddenTokensBefore(aToken, aTokens);
    }

    // the only non-hidden token
    if (aResolveMode != ResolveMode.NO_RESOLVING) {
        resolveToken(aToken, aResolveMode, aFile);
    } else {
        final String tokenText = aToken.getText();
        mSb.append(tokenText != null ? tokenText : "");
    }
}