Example usage for org.antlr.v4.runtime TokenStream getTokenSource

List of usage examples for org.antlr.v4.runtime TokenStream getTokenSource

Introduction

In this page you can find the example usage for org.antlr.v4.runtime TokenStream getTokenSource.

Prototype

public TokenSource getTokenSource();

Source Link

Document

Gets the underlying TokenSource which provides tokens for this stream.

Usage

From source file:com.github.jknack.handlebars.internal.HbsErrorStrategy.java

License:Apache License

@Override
public void reportNoViableAlternative(final Parser recognizer, final NoViableAltException e) {
    HbsParser parser = (HbsParser) recognizer;
    TokenStream tokens = parser.getTokenStream();
    HbsLexer lexer = (HbsLexer) tokens.getTokenSource();
    String msg = new ErrorStrategyVisitor(lexer.start, lexer.end).visit(e.getCtx());
    if (msg != null) {
        recognizer.notifyErrorListeners(e.getOffendingToken(), msg, e);
    } else {/*from   w  w  w. j ava2  s .  co  m*/
        super.reportNoViableAlternative(recognizer, e);
    }
}

From source file:com.github.jknack.handlebars.internal.HbsErrorStrategy.java

License:Apache License

/**
 * Translate the token's name to name that make more sense to the user.
 *
 * @param recognizer The lexer/parser.//from w  w w. j  av  a 2 s  . c  o  m
 * @return User error messages.
 */
private String[] displayNames(final Parser recognizer) {
    HbsParser parser = (HbsParser) recognizer;
    TokenStream tokens = parser.getTokenStream();
    HbsLexer lexer = (HbsLexer) tokens.getTokenSource();
    String[] tokenNames = recognizer.getTokenNames();
    String[] displayName = new String[tokenNames.length];
    for (int i = 0; i < displayName.length; i++) {
        String[] parts = StringUtils.split(tokenNames[i], "_");
        if (parts[0].equals("START")) {
            String suffix = "";
            if (parts.length > 1) {
                if (parts[1].equals("COMMENT")) {
                    suffix = "!";
                } else if (parts[1].equals("AMP")) {
                    suffix = "&";
                } else if (parts[1].equals("T")) {
                    suffix = "{";
                } else if (parts[1].equals("BLOCK")) {
                    suffix = "#";
                } else if (parts[1].equals("DELIM")) {
                    suffix = "=";
                } else if (parts[1].equals("PARTIAL")) {
                    suffix = ">";
                }
            }
            displayName[i] = lexer.start + suffix;
        } else if (parts[0].equals("END")) {
            String prefix = "";
            if (parts.length > 1) {
                if (parts[1].equals("BLOCK")) {
                    displayName[i] = lexer.start + "/";
                } else if (parts[1].equals("DELIM")) {
                    prefix = "=";
                    displayName[i] = prefix + lexer.end;
                } else if (parts[1].equals("T")) {
                    prefix = "}";
                    displayName[i] = prefix + lexer.end;
                } else {
                    displayName[i] = prefix + lexer.end;
                }
            } else {
                displayName[i] = prefix + lexer.end;
            }
        } else if (parts[0].equals("UNLESS")) {
            displayName[i] = "^";
        } else if (parts[0].equals("NL")) {
            displayName[i] = "\\n";
        } else if (parts[0].equals("WS")) {
            displayName[i] = "space";
        } else if (parts[0].equals("DOUBLE")) {
            displayName[i] = "string";
        } else if (parts[0].equals("SINGLE")) {
            displayName[i] = "string";
        } else if (parts[0].equals("QID")) {
            displayName[i] = "id";
        } else {
            displayName[i] = tokenNames[i];
        }
        displayName[i] = displayName[i].toLowerCase().replace("'", "");
    }
    return displayName;
}

From source file:org.tvl.goworks.editor.go.completion.ParserFactory.java

License:Open Source License

@NonNull
protected CodeCompletionGoParser createParser(@NonNull TokenStream input) {
    CharStream charStream = input.getTokenSource().getInputStream();
    if (!(charStream instanceof DocumentSnapshotCharStream)) {
        throw new UnsupportedOperationException();
    }//from w  w w  .ja v  a  2s.co  m

    CodeCompletionGoParser parser = new CodeCompletionGoParser(input);
    return parser;
}

From source file:org.tvl.goworks.editor.go.parser.GoParser.java

License:Open Source License

public GoParser(TokenStream input) {
    super(input);
    CharStream charStream = input != null ? input.getTokenSource().getInputStream() : null;
    if (charStream instanceof DocumentSnapshotCharStream) {
        DocumentSnapshotCharStream documentSnapshotCharStream = (DocumentSnapshotCharStream) charStream;
        this.snapshot = documentSnapshotCharStream.getSnapshot();
    }//from w ww . j  a va  2 s  .  c o  m
}

From source file:org.tvl.goworks.editor.go.parser.GoParser.java

License:Open Source License

@Override
public void setInputStream(TokenStream input) {
    super.setInputStream(input);

    CharStream charStream = input != null ? input.getTokenSource().getInputStream() : null;
    if (charStream instanceof DocumentSnapshotCharStream) {
        DocumentSnapshotCharStream documentSnapshotCharStream = (DocumentSnapshotCharStream) charStream;
        this.snapshot = documentSnapshotCharStream.getSnapshot();
    } else {//  w  ww.  j av  a  2s. com
        this.snapshot = null;
    }
}