Java Code Examples for org.antlr.runtime.CommonTokenStream#getTokenSource()

The following examples show how to use org.antlr.runtime.CommonTokenStream#getTokenSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCobolTester.java    From legstar-core2 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Apply a lexer to a source and check that the token stream produced
 * is as expected.
 * @param source the source code
 * @param expected the expected token stream
 */
public void lexAndCheck(final String source, final String expected) {
    try {
        CommonTokenStream ts = lex(source);
        ts.fill();
        StringBuilder sb = new StringBuilder();
        for (Object token : ts.getTokens()) {
            sb.append(toString((Token) token));
        }
        CobolStructureLexerImpl lexer = (CobolStructureLexerImpl) ts.getTokenSource();
        if (lexer.getErrorHandler().getErrorMessages().size() > 0) {
            throw new RecognizerException(
                    lexer.getErrorHandler().getErrorMessages().get(0));
        }
        assertEquals(expected, sb.toString());
    } catch (RecognizerException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
 
Example 2
Source File: AbstractCobolTester.java    From legstar-core2 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * A generic test helper that takes a source fragment and checks the result
 * when it should be an exception.
 * @param source the source fragment
 * @param expected the expected exception
 */
public void lexAndCheck(
        final String source,
        final RecognizerException expected) {
    try {
        CommonTokenStream ts = lex(source);
        ts.fill();
        CobolStructureLexerImpl lexer = (CobolStructureLexerImpl) ts.getTokenSource();
        if (lexer.getErrorHandler().getErrorMessages().size() > 0) {
            throw new RecognizerException(
                    lexer.getErrorHandler().getErrorMessages().get(0));
        }
        fail();
    } catch (RecognizerException e) {
        assertEquals(expected.getMessage(), e.getMessage());
    }
}