use of org.antlr.runtime.CommonTokenStream in project smali by JesusFreke.
the class LexerTest method runTest.
public void runTest(String test, boolean discardHiddenTokens, int apiLevel) {
String smaliFile = String.format("LexerTest%s%s.smali", File.separatorChar, test);
String tokensFile = String.format("LexerTest%s%s.tokens", File.separatorChar, test);
org.jf.smali.expectedTokensTestGrammarLexer expectedTokensLexer = null;
try {
expectedTokensLexer = new org.jf.smali.expectedTokensTestGrammarLexer(new ANTLRInputStream(LexerTest.class.getClassLoader().getResourceAsStream(tokensFile)));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
CommonTokenStream expectedTokensStream = new CommonTokenStream(expectedTokensLexer);
org.jf.smali.expectedTokensTestGrammarParser expectedTokensParser = new org.jf.smali.expectedTokensTestGrammarParser(expectedTokensStream);
try {
expectedTokensParser.top();
} catch (RecognitionException ex) {
throw new RuntimeException(ex);
}
List<ExpectedToken> expectedTokens = expectedTokensParser.getExpectedTokens();
InputStream smaliStream = LexerTest.class.getClassLoader().getResourceAsStream(smaliFile);
if (smaliStream == null) {
Assert.fail("Could not load " + smaliFile);
}
smaliFlexLexer lexer = new smaliFlexLexer(new InputStreamReader(smaliStream), apiLevel);
lexer.setSourceFile(new File(test + ".smali"));
lexer.setSuppressErrors(true);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
tokenStream.fill();
List tokens = tokenStream.getTokens();
int expectedTokenIndex = 0;
CommonToken token;
for (int i = 0; i < tokens.size() - 1; i++) {
token = (CommonToken) tokens.get(i);
if (discardHiddenTokens && token.getChannel() == smaliParser.HIDDEN) {
continue;
}
if (expectedTokenIndex >= expectedTokens.size()) {
Assert.fail("Too many tokens");
}
if (token.getType() == smaliParser.INVALID_TOKEN) {
Assert.assertTrue("Encountered an INVALID_TOKEN not on the error channel", token.getChannel() == smaliParser.ERROR_CHANNEL);
}
ExpectedToken expectedToken = expectedTokens.get(expectedTokenIndex++);
if (!tokenTypesByName.containsKey(expectedToken.tokenName)) {
Assert.fail("Unknown token: " + expectedToken.tokenName);
}
int expectedTokenType = tokenTypesByName.get(expectedToken.tokenName);
if (token.getType() != expectedTokenType) {
Assert.fail(String.format("Invalid token at index %d. Expecting %s, got %s(%s)", expectedTokenIndex - 1, expectedToken.tokenName, getTokenName(token.getType()), token.getText()));
}
if (expectedToken.tokenText != null) {
if (!expectedToken.tokenText.equals(token.getText())) {
Assert.fail(String.format("Invalid token text at index %d. Expecting text \"%s\", got \"%s\"", expectedTokenIndex - 1, expectedToken.tokenText, token.getText()));
}
}
}
if (expectedTokenIndex < expectedTokens.size()) {
Assert.fail(String.format("Not enough tokens. Expecting %d tokens, but got %d", expectedTokens.size(), expectedTokenIndex));
}
}
use of org.antlr.runtime.CommonTokenStream in project ceylon-compiler by ceylon.
the class IssuesTests_1500_1999 method benchmarkParse.
private void benchmarkParse(String file) throws Exception {
String readSource = readFile(new File(getPackagePath(), file));
String source = readSource.toString();
char[] chars = source.toCharArray();
LineMap map = Position.makeLineMap(chars, chars.length, false);
System.err.println(map.hashCode());
ANTLRStringStream input = new ANTLRStringStream(source);
CeylonLexer lexer = new CeylonLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CeylonParser parser = new CeylonParser(tokens);
// CompilationUnit cu = parser.compilationUnit();
// System.err.println(cu.hashCode());
}
use of org.antlr.runtime.CommonTokenStream in project alfresco-remote-api by Alfresco.
the class WhereCompiler method compileWhereClause.
public static CommonTree compileWhereClause(String expression) throws RecognitionException {
// lexer splits input into tokens
ANTLRStringStream input = new ANTLRStringStream(expression);
TokenStream tokens = new CommonTokenStream(new WhereClauseLexer(input));
// parser generates abstract syntax tree
WhereClauseParser parser = new WhereClauseParser(tokens);
WhereClauseParser.whereclause_return ret = parser.whereclause();
// acquire parse result
CommonTree ast = (CommonTree) ret.getTree();
if (logger.isDebugEnabled())
print(ast, 0);
return ast;
}
Aggregations