use of org.antlr.runtime.CharStream in project binnavi by google.
the class CFilterRuleParser method parse.
/**
* Parses a filter string.
*
* @param filterString The filter string to parse.
*
* @return The root node of the parsed AST.
*
* @throws RecognitionException Thrown if parsing the filter string failed.
*/
public static IAbstractNode parse(final String filterString) throws RecognitionException {
final CharStream charStream = new ANTLRStringStream(filterString);
final FilterLexer lexer = new FilterLexer(charStream);
final CommonTokenStream tokens = new CommonTokenStream();
tokens.setTokenSource(lexer);
final FilterParser parser = new FilterParser(tokens);
parser.setTreeAdaptor(adaptor);
final FilterParser.prog_return parserResult = parser.prog();
final CommonTree ast = (CommonTree) parserResult.getTree();
return convert(ast);
}
use of org.antlr.runtime.CharStream in project cassandra by apache.
the class CqlParserTest method testAddErrorListener.
@Test
public void testAddErrorListener() throws Exception {
SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();
CharStream stream = new ANTLRStringStream("SELECT * FORM FROM test");
CqlLexer lexer = new CqlLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
CqlParser parser = new CqlParser(tokenStream);
parser.addErrorListener(firstCounter);
parser.addErrorListener(secondCounter);
// By default CqlParser should recover from the syntax error by removing FORM
// but as recoverFromMismatchedToken and recover have been overloaded, it will not
// and the returned ParsedStatement will be null.
assertNull(parser.query());
// Only one error must be reported (mismatched: FORM).
assertEquals(1, firstCounter.count);
assertEquals(1, secondCounter.count);
}
use of org.antlr.runtime.CharStream in project binnavi by google.
the class DebuggerMemoryExpressionParser method parse.
/**
* Parses a single memory expression string.
*
* @param memoryExpression The memory expression string to parse.
*
* @return The parsed memory expression tree.
*
* @throws RecognitionException Thrown if parsing failed.
*/
public static MemoryExpressionElement parse(final String memoryExpression) throws RecognitionException {
final CharStream charStream = new ANTLRStringStream(memoryExpression);
final MemoryExpressionLexer lexer = new MemoryExpressionLexer(charStream);
final CommonTokenStream tokens = new CommonTokenStream();
tokens.setTokenSource(lexer);
final MemoryExpressionParser parser = new MemoryExpressionParser(tokens) {
@Override
public void recover(final IntStream input, final RecognitionException exception) {
// Nothing to do
}
@Override
public Object recoverFromMismatchedToken(final IntStream input, final int ttype, final org.antlr.runtime.BitSet follow) throws RecognitionException {
throw new MismatchedTokenException(ttype, input);
}
@Override
public void reportError(final RecognitionException exception) {
// Nothing to do
}
};
parser.setTreeAdaptor(adaptor);
final MemoryExpressionParser.prog_return parserResult = parser.prog();
final CommonTree ast = (CommonTree) parserResult.getTree();
return convert(ast);
}
use of org.antlr.runtime.CharStream in project antlr4 by antlr.
the class GrammarAST method toTokenString.
public String toTokenString() {
CharStream input = this.token.getInputStream();
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, this);
StringBuilder buf = new StringBuilder();
GrammarAST o = (GrammarAST) nodes.LT(1);
int type = adaptor.getType(o);
while (type != Token.EOF) {
buf.append(" ");
buf.append(o.getText());
nodes.consume();
o = (GrammarAST) nodes.LT(1);
type = adaptor.getType(o);
}
return buf.toString();
}
Aggregations