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:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java

License:Open Source License

@Override
public void enterE20CallFunction(@NotNull RParser.E20CallFunctionContext ctx) {

    /*/*  ww w . j  a  va2s  .c o m*/
     * Interval sourceInterval = ctx.getSourceInterval(); int start =
     * sourceInterval.a; Token assign = tokens.get(start);
     */
    Token start = ctx.start;
    String subExpr = start.getText();
    /* Detect libraries and add them to the outline! */
    if (subExpr.equals("library") || subExpr.equals("require")) {
        Token firstToken = start;
        int lineStart = firstToken.getStartIndex();
        int line = calculateLine(lineStart);
        if (ctx.getParent().getChild(1) != null) {
            String name = ctx.getChild(2).getText();
            // The third token should be a parenthesis!
            String parenthesis = ctx.getChild(3).getText();
            if (parenthesis.equals(")")) {
                if (methods.size() == 0) {
                    new REditorOutlineNode(name, line, "library", editor.baseNode);
                } else {
                    new REditorOutlineNode(name, line, "library", methods.peek());
                }
            }
        }
    }
}

From source file:net.sourceforge.pmd.cpd.SwiftTokenizer.java

License:BSD License

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();

    try {//from   www. j  a va 2 s .c  om
        ANTLRInputStream ais = new ANTLRInputStream(buffer.toString());
        SwiftLexer lexer = new SwiftLexer(ais);

        lexer.removeErrorListeners();
        lexer.addErrorListener(new ErrorHandler());
        Token token = lexer.nextToken();

        while (token.getType() != Token.EOF) {
            if (token.getChannel() != Lexer.HIDDEN) {
                TokenEntry tokenEntry = new TokenEntry(token.getText(), sourceCode.getFileName(),
                        token.getLine());

                tokenEntries.add(tokenEntry);
            }
            token = lexer.nextToken();
        }
    } catch (ANTLRSyntaxError err) {
        // Wrap exceptions of the Swift tokenizer in a TokenMgrError, so
        // they are correctly handled
        // when CPD is executed with the '--skipLexicalErrors' command line
        // option
        throw new TokenMgrError("Lexical error in file " + sourceCode.getFileName() + " at line "
                + err.getLine() + ", column " + err.getColumn() + ".  Encountered: " + err.getMessage(),
                TokenMgrError.LEXICAL_ERROR);
    } finally {
        tokenEntries.add(TokenEntry.getEOF());
    }
}

From source file:nl.knaw.AntlrUtils.java

License:Apache License

public static String makeTokenTable(Lexer lexer) {
    AsciiTable table = new AsciiTable().setTextAlignment(TextAlignment.LEFT);
    CWC_LongestLine cwc = new CWC_LongestLine();
    table.getRenderer().setCWC(cwc);// w  ww  . j av a2  s .c  om
    table.addRule();
    table.addRow("Pos", "Text", "Rule", "Next mode", "Token");
    table.addRule();

    Token token;
    do {
        token = lexer.nextToken();
        //      LOG.info(token.toString());
        if (token.getType() != Token.EOF) {
            String pos = token.getLine() + ":" + token.getCharPositionInLine();
            String text = "'" + token.getText() + "'";
            String rule = lexer.getRuleNames()[token.getType() - 1];
            String mode = lexer.getModeNames()[lexer._mode];
            table.addRow(pos, text, rule, mode, token);
        }
    } while (token.getType() != Token.EOF);
    table.addRule();
    table.getContext().setGrid(A7_Grids.minusBarPlusEquals());
    return table.render();
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporter.java

License:Apache License

private void handleDefaultMode(ImporterContext context) {
    String methodName = "defaultMode";
    Token token;
    do {//from   w  w w . j a v  a 2  s . c  om
        token = context.nextToken();
        if (token.getType() != Token.EOF) {
            String ruleName = context.getRuleName();
            String modeName = context.getModeName();
            log(methodName, ruleName, modeName, token, context);
            switch (token.getType()) {
            case LMNLLexer.BEGIN_OPEN_RANGE:
                handleOpenRange(context);
                break;

            case LMNLLexer.BEGIN_CLOSE_RANGE:
                handleCloseRange(context);
                break;

            case LMNLLexer.TEXT:
                TAGTextNodeDTO textNode = new TAGTextNodeDTO(token.getText());
                update(textNode);
                context.addTextNode(textNode);
                break;

            // case LMNLLexer.TagOpenStartChar:
            // case LMNLLexer.TagOpenEndChar:
            // case LMNLLexer.TagCloseStartChar:
            // case LMNLLexer.TagCloseEndChar:
            // break;

            default:
                handleUnexpectedToken(methodName, token, ruleName, modeName);
                break;
            }
        }
    } while (token.getType() != Token.EOF);
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporter.java

License:Apache License

private void handleOpenRange(ImporterContext context) {
    String methodName = "handleOpenRange";
    boolean goOn = true;
    while (goOn) {
        Token token = context.nextToken();
        String ruleName = context.getRuleName();
        String modeName = context.getModeName();
        log(methodName, ruleName, modeName, token, context);
        switch (token.getType()) {
        case LMNLLexer.Name_Open_Range:
            TAGMarkupDTO markup = context.newMarkup(token.getText());
            context.openMarkup(markup);/*from   www. j  av  a  2 s.co  m*/
            break;
        case LMNLLexer.BEGIN_OPEN_ANNO:
            handleAnnotation(context);
            break;
        case LMNLLexer.END_OPEN_RANGE:
            context.popOpenMarkup();
            goOn = false;
            break;
        case LMNLLexer.END_ANONYMOUS_RANGE:
            TAGTextNodeDTO textNode = new TAGTextNodeDTO("");
            update(textNode);
            context.addTextNode(textNode);
            context.closeMarkup();
            goOn = false;
            break;

        // case LMNLLexer.TagOpenStartChar:
        // case LMNLLexer.TagOpenEndChar:
        // case LMNLLexer.TagCloseStartChar:
        // case LMNLLexer.TagCloseEndChar:
        // break;

        default:
            handleUnexpectedToken(methodName, token, ruleName, modeName);
            break;
        }
        goOn = goOn && token.getType() != Token.EOF;
    }
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporter.java

License:Apache License

private void handleCloseRange(ImporterContext context) {
    String methodName = "handleCloseRange";
    boolean goOn = true;
    while (goOn) {
        Token token = context.nextToken();
        String ruleName = context.getRuleName();
        String modeName = context.getModeName();
        log(methodName, ruleName, modeName, token, context);
        switch (token.getType()) {
        case LMNLLexer.Name_Close_Range:
            String rangeName = token.getText();
            context.pushOpenMarkup(rangeName);
            break;
        case LMNLLexer.BEGIN_OPEN_ANNO_IN_RANGE_CLOSER:
            handleAnnotation(context);/*from   ww  w .  j  a v a2  s. com*/
            break;
        case LMNLLexer.END_CLOSE_RANGE:
            context.closeMarkup();
            goOn = false;
            break;

        // case LMNLLexer.TagOpenStartChar:
        // case LMNLLexer.TagOpenEndChar:
        // case LMNLLexer.TagCloseStartChar:
        // case LMNLLexer.TagCloseEndChar:
        // break;

        default:
            handleUnexpectedToken(methodName, token, ruleName, modeName);
            break;
        }
        goOn = goOn && token.getType() != Token.EOF;
    }
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporterInMemory.java

License:Apache License

private void handleDefaultMode(ImporterContext context) {
    String methodName = "defaultMode";
    Token token;
    do {// ww w  .j  a v  a  2  s.co m
        token = context.nextToken();
        if (token.getType() != Token.EOF) {
            String ruleName = context.getRuleName();
            String modeName = context.getModeName();
            log(methodName, ruleName, modeName, token, context);
            switch (token.getType()) {
            case LMNLLexer.BEGIN_OPEN_RANGE:
                handleOpenRange(context);
                break;

            case LMNLLexer.BEGIN_CLOSE_RANGE:
                handleCloseRange(context);
                break;

            case LMNLLexer.TEXT:
                TextNode textNode = new TextNode(token.getText());
                context.addTextNode(textNode);
                break;

            // case LMNLLexer.TagOpenStartChar:
            // case LMNLLexer.TagOpenEndChar:
            // case LMNLLexer.TagCloseStartChar:
            // case LMNLLexer.TagCloseEndChar:
            // break;

            default:
                handleUnexpectedToken(methodName, token, ruleName, modeName);
                break;
            }
        }
    } while (token.getType() != Token.EOF);
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporterInMemory.java

License:Apache License

private void handleOpenRange(ImporterContext context) {
    String methodName = "handleOpenRange";
    boolean goOn = true;
    while (goOn) {
        Token token = context.nextToken();
        String ruleName = context.getRuleName();
        String modeName = context.getModeName();
        log(methodName, ruleName, modeName, token, context);
        switch (token.getType()) {
        case LMNLLexer.Name_Open_Range:
            Markup markup = context.newMarkup(token.getText());
            context.openMarkup(markup);//from   w  w w. j ava2  s . c o m
            break;
        case LMNLLexer.BEGIN_OPEN_ANNO:
            handleAnnotation(context);
            break;
        case LMNLLexer.END_OPEN_RANGE:
            context.popOpenMarkup();
            goOn = false;
            break;
        case LMNLLexer.END_ANONYMOUS_RANGE:
            TextNode textNode = new TextNode("");
            context.addTextNode(textNode);
            context.closeMarkup();
            goOn = false;
            break;

        // case LMNLLexer.TagOpenStartChar:
        // case LMNLLexer.TagOpenEndChar:
        // case LMNLLexer.TagCloseStartChar:
        // case LMNLLexer.TagCloseEndChar:
        // break;

        default:
            handleUnexpectedToken(methodName, token, ruleName, modeName);
            break;
        }
        goOn = goOn && token.getType() != Token.EOF;
    }
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporterInMemory.java

License:Apache License

private void handleAnnotation(ImporterContext context) {
    String methodName = "handleAnnotation";
    Annotation annotation = new Annotation("");
    context.openAnnotation(annotation);//  w ww . j a  v a  2  s  .c  o  m
    boolean goOn = true;
    while (goOn) {
        Token token = context.nextToken();
        String ruleName = context.getRuleName();
        String modeName = context.getModeName();
        log(methodName, ruleName, modeName, token, context);
        switch (token.getType()) {
        case LMNLLexer.Name_Open_Annotation:
            annotation.setTag(token.getText());
            break;
        case LMNLLexer.OPEN_ANNO_IN_ANNO_OPENER:
        case LMNLLexer.OPEN_ANNO_IN_ANNO_CLOSER:
            handleAnnotation(context);
            break;
        case LMNLLexer.END_OPEN_ANNO:
            context.pushLimenContext(context.currentAnnotationLimen());
            break;

        case LMNLLexer.ANNO_TEXT:
            context.addTextNode(new TextNode(token.getText()));
            break;

        case LMNLLexer.BEGIN_ANNO_OPEN_RANGE:
            handleOpenRange(context);
            break;

        case LMNLLexer.BEGIN_ANNO_CLOSE_RANGE:
            handleCloseRange(context);
            break;

        case LMNLLexer.BEGIN_CLOSE_ANNO:
        case LMNLLexer.Name_Close_Annotation:
            break;
        case LMNLLexer.END_CLOSE_ANNO:
            context.popLimenContext();
        case LMNLLexer.END_EMPTY_ANNO:
            context.closeAnnotation();
            goOn = false;
            break;

        // case LMNLLexer.TagOpenStartChar:
        // case LMNLLexer.TagOpenEndChar:
        // case LMNLLexer.TagCloseStartChar:
        // case LMNLLexer.TagCloseEndChar:
        // break;

        default:
            handleUnexpectedToken(methodName, token, ruleName, modeName);
            break;
        }
        goOn = goOn && token.getType() != Token.EOF;
    }
}

From source file:nl.lxtreme.libtdl.grammar.adv.AdvTdlSemanticAnalyzer.java

License:Apache License

private void validateDeclaredTerm(Token term) {
    String name = normalizeName(term.getText());
    if (!m_declarations.containsKey(name)) {
        int offset = term.getStartIndex();
        int length = term.getStopIndex() - offset;
        String msg = name + " is not declared";

        Marker marker = new MarkerBuilder().setCategory(Category.SEMANTIC).setType(Type.ERROR) //
                .setLocation(offset, length, term.getLine(), term.getCharPositionInLine()) //
                .setDescription(msg).build();

        m_problemReporter.report(marker);
    }//from w  ww.  j  ava  2s  .  c  o m
}