Example usage for org.antlr.v4.runtime WritableToken setText

List of usage examples for org.antlr.v4.runtime WritableToken setText

Introduction

In this page you can find the example usage for org.antlr.v4.runtime WritableToken setText.

Prototype

public void setText(String text);

Source Link

Usage

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

License:Open Source License

/**
 * Changes the text of a parse tree/*from   w  w  w . j a  va 2s. c  o m*/
 * @param aParseTree parse tree to modify
 * @param aText new text
 */
public static void setText(final ParseTree aParseTree, final String aText) {
    if (aParseTree == null) {
        ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.setText(): aParseTree == null");
        return;
    }
    if (aParseTree instanceof ParserRuleContext) {
        final ParserRuleContext rule = (ParserRuleContext) aParseTree;
        // in case of a rule we don't want to keep the original sub-tree structure,
        // just delete it and replace the children with an AddedParseTree
        if (rule.children != null) {
            rule.children.clear();
        } else {
            rule.children = new ArrayList<ParseTree>();
        }
        ParseTree newNode = new AddedParseTree(aText);
        addChild(rule, newNode);
    } else if (aParseTree instanceof AddedParseTree) {
        final AddedParseTree node = (AddedParseTree) aParseTree;
        node.setText(aText);
    } else if (aParseTree instanceof TerminalNodeImpl) {
        final TerminalNodeImpl node = (TerminalNodeImpl) aParseTree;
        final Token t = node.symbol;
        if (t instanceof WritableToken) {
            final WritableToken ct = (WritableToken) t;
            ct.setText(aText);
        } else {
            ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.setText(): unhandled token class type");
        }
    } else {
        ErrorReporter.INTERNAL_ERROR("ConfigTreeNodeUtilities.setText(): unhandled ParseTree class type");
    }
}