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.eclipse.titan.common.parsers.cfg.CfgParseTreePrinter.java

License:Open Source License

/**
 * Builds hidden tokens before the token
 * @param aToken the token, this will NOT be printed
 * @param aTokens token list from the lexer (all, hidden and not hidden also)
 *//*from w  w w  .j a  va  2  s. c  o m*/
private void printHiddenTokensBefore(final Token aToken, final List<Token> aTokens) {
    final int tokenIndex = aToken.getTokenIndex();
    if (tokenIndex == -1) {
        // Token has no index.
        // 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.
        return;
    }
    int startHiddenIndex = tokenIndex;
    while (isHiddenToken(startHiddenIndex - 1, aTokens)) {
        startHiddenIndex--;
    }
    for (int i = startHiddenIndex; i < tokenIndex; i++) {
        final Token t = aTokens.get(i);
        final String tokenText = t.getText();
        mSb.append(tokenText != null ? tokenText : "");
    }
}

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

License:Open Source License

/**
 * Resolves token if needed<br>//ww w .j  a  v  a2s .c om
 * Resolving means:
 * <ul>
 *   <li> macros are changed to its actual values
 *   <li> include file name is changed to the content of the included config file (if it's not included yet)
 * </ul>
 * @param aToken token to resolve or print
 * @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 resolveToken(final Token aToken, final ResolveMode aResolveMode, final Path aFile) {
    final int tokenType = aToken.getType();
    if (isMacro(tokenType)) {
        final String macroValue = getMacroValue(aToken);
        mSb.append(macroValue);
    } else if (isTypedMacro(tokenType)) {
        final String macroValue = getTypedMacroValue(aToken);
        mSb.append(macroValue);
    } else if (tokenType == CfgLexer.STRING2) {
        // Quoted string in [INCLUDE] section
        // this is the only non-hidden token in [INCLUDE] section
        if (aResolveMode == ResolveMode.NESTED) {
            resolveTokenNestedInclude(aToken, aResolveMode, aFile);
        }
        // otherwise nothing to do, included files are already collected in mParseTreeRoots
    } else if (tokenType == CfgLexer.STRING4) {
        // Quoted string in [ORDERED_INCLUDE] section
        // this is the only non-hidden token in [ORDERED_INCLUDE] section
        resolveTokenNestedInclude(aToken, aResolveMode, aFile);
    } else {
        final String tokenText = aToken.getText();
        mSb.append(tokenText != null ? tokenText : "");
    }
}

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

License:Open Source License

/**
 * Include file is changed to its content
 * @param aToken token to resolve or print
 * @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]
 *///from  ww  w .  j  av  a  2 s  .c  o m
private void resolveTokenNestedInclude(final Token aToken, final ResolveMode aResolveMode, final Path aFile) {
    // token text is the file name with quotes, which will be included in place
    final String tokenText = aToken.getText();
    // remove quotes
    final String filename = tokenText.replaceAll("^\"|\"$", "");
    // filename is a relative file name to aFile, we need the absolute file name,
    // because absolute file names are used as keys in aParseTreeRoots
    final Path absolutePath = getAbsolutePath(aFile, filename);
    final CfgParseResult cfgParseResult = mCfgParseResults.get(absolutePath);
    if (cfgParseResult != null) {
        printResolved(absolutePath, cfgParseResult.getParseTreeRoot(), cfgParseResult.getTokens(),
                aResolveMode);
    } else {
        // include file is missing from mCfgParseResults, so the included cfg file is not parsed
        // in ConfigFileHandler.readFromFile()
        ErrorReporter.INTERNAL_ERROR("ParseTreePrinter.resolveTokenNestedInclude(): cfgParseResult == null");
    }
}

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

License:Open Source License

/**
 * Gets the macro value string of a macro (without type)
 * @param aMacroToken the macro token/*from w w w  .  ja v  a  2 s  .co  m*/
 * @return the macro value string
 *         or "" if macro is invalid. In this case an error marker is also created
 */
private String getMacroValue(final Token aMacroToken) {
    final String definition = getMacroName(aMacroToken.getText());
    final String value = getDefinitionValue(definition);
    if (value == null) {
        return "";
    }
    return value;
}

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

License:Open Source License

/**
 * Gets the macro value string of a macro (with type)
 * @param aMacroToken the macro token//from   ww w .ja v a2s.  c om
 * @return the macro value string
 *         or "" if macro is invalid. In this case an error marker is also created
 */
private String getTypedMacroValue(Token aMacroToken) {
    final String definition = getTypedMacroName(aMacroToken.getText());
    final String value = getDefinitionValue(definition);
    if (value == null) {
        return "";
    }
    return value;
}

From source file:org.eclipse.titan.common.parsers.ParserLogger.java

License:Open Source License

/**
 * Gets escaped token text/* w  w w .  j a  va  2 s .c  om*/
 * Escaped chars are converted to printable strings.
 * @param aToken input token
 * @return escaped token text
 */
private static String getEscapedTokenText(final Token aToken) {
    return getEscapedText(aToken.getText());
}

From source file:org.eclipse.titan.designer.AST.ASN1.Block.java

License:Open Source License

@Override
public String getText() {
    StringBuilder text = new StringBuilder();
    for (Token t : tokenList) {
        text.append(t.getText());
    }//www.  j  a  va 2 s.c om
    return text.toString();
}

From source file:org.eclipse.titan.designer.AST.ASN1.Object.ObjectClassSyntax_Parser.java

License:Open Source License

@Override
public void visitRoot(final ObjectClassSyntax_root parameter) {
    if (mBlock != null) {
        if (!success || !parameter.getIsBuilded() || (mBlock.getTokenList().isEmpty())) {
            // FATAL ERROR, but now OK
            return;
        }//from   w ww.  j  a va  2s  .  c o m
    }

    previousSuccess = false;
    parameter.getSequence().accept(this);
    if (null != mBlock) {
        if (success && internalIndex < mBlock.getTokenList().size()
                && mBlock.getTokenList().get(internalIndex).getType() != Token.EOF) {
            success = false;
            final Token token = mBlock.getTokenList().get(internalIndex);
            myObject.getLocation()
                    .reportSemanticError("Unexpected `" + token.getText() + "', it is a superfluous part");
        }
    }

    if (!success) {
        myObject.getLocation().reportSemanticError("Check the syntax of objectclass");
        myObject.setIsErroneous(true);
    }
}

From source file:org.eclipse.titan.designer.AST.ASN1.Object.ObjectClassSyntax_Parser.java

License:Open Source License

@Override
public void visitLiteral(final ObjectClassSyntax_literal parameter) {
    previousSuccess = false;/*w  ww . j ava2  s. c o m*/
    if (null != mBlock) {
        if (mBlock.getTokenList().size() <= internalIndex) {
            return;
        }
    }

    if (null != mBlock) {
        final Token token = mBlock.getTokenList().get(internalIndex);
        if (null == token.getText()) {
            // reached the end of the block
            return;
        }
        if (token.getText().equals(parameter.getLiteral())) {
            if (internalIndex < mBlock.getTokenList().size() - 1) {
                internalIndex++;
            }
            previousSuccess = true;
        }
    }
}

From source file:org.eclipse.titan.designer.AST.ASN1.Object.ObjectClassSyntax_Parser.java

License:Open Source License

@Override
public void visitSequence(final ObjectClassSyntax_sequence parameter) {

    if (null != mBlock) {
        if (mBlock.getTokenList().size() <= internalIndex) {
            return;
        }/*from www  .j  a  v  a  2s. com*/
    }

    int i;

    if (null != mBlock) {
        Token token = mBlock.getTokenList().get(internalIndex);
        if (parameter.getOptionalFirstComma() && myObject.getNofFieldSettings() > 0) {
            if (token.getType() == Asn1Lexer.COMMA) {
                if (internalIndex < mBlock.getTokenList().size() - 1) {
                    internalIndex++;
                }
            } else {
                if (parameter.getIsOptional()) {
                    previousSuccess = true;
                } else {
                    success = false;
                    myObject.getLocation()
                            .reportSemanticError("Unexpected `" + token.getText() + "', expecting `,'");
                }
                return;
            }
            i = 0;
        } else {
            if (0 == parameter.getNofNodes()) {
                return;
            }
            parameter.getNthNode(0).accept(this);
            if (!success) {
                return;
            }
            if (!previousSuccess) {
                if (parameter.getIsOptional()) {
                    previousSuccess = true;
                } else {
                    success = false;
                    myObject.getLocation().reportSemanticError("Unexpected `" + token.getText()
                            + "', expecting `" + parameter.getNthNode(0).getDisplayName() + "'");
                }
                return;
            }
            i = 1;
        }

        for (; i < parameter.getNofNodes(); i++) {
            parameter.getNthNode(i).accept(this);
            if (!previousSuccess) {
                if (parameter.getIsOptional()) {
                    previousSuccess = true;
                    internalIndex--;
                    return;
                }
                success = false;
                if (mBlock.getTokenList().size() <= internalIndex) {
                    return;
                }
                token = mBlock.getTokenList().get(internalIndex);
                myObject.getLocation().reportSemanticError("Unexpected `" + token.getText() + "', expecting `"
                        + parameter.getNthNode(i).getDisplayName() + "'");
            }
            if (!success) {
                return;
            }
        }
    }
}