use of org.antlr.runtime.ANTLRStringStream in project drill by axbaretto.
the class SchemaPath method parseFromString.
/**
* Parses input string using the same rules which are used for the field in the query.
* If a string contains dot outside back-ticks, or there are no backticks in the string,
* will be created {@link SchemaPath} with the {@link NameSegment}
* which contains one else {@link NameSegment}, etc.
* If a string contains [] then {@link ArraySegment} will be created.
*
* @param expr input string to be parsed
* @return {@link SchemaPath} instance
*/
public static SchemaPath parseFromString(String expr) {
if (expr == null || expr.isEmpty()) {
return null;
}
try {
ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
CommonTokenStream tokens = new CommonTokenStream(lexer);
ExprParser parser = new ExprParser(tokens);
parse_return ret = parser.parse();
if (ret.e instanceof SchemaPath) {
return (SchemaPath) ret.e;
} else {
throw new IllegalStateException("Schema path is not a valid format.");
}
} catch (RecognitionException e) {
throw new RuntimeException(e);
}
}
use of org.antlr.runtime.ANTLRStringStream in project n4js by eclipse.
the class NodeModelTokenSource method toPrefixToken.
/**
* Produce an Antlr token for the prefix of the given leaf that overlaps the requested region
*
* @see #endOffset
*/
private Token toPrefixToken(ILeafNode leaf) {
Lexer lexer = new InternalN4JSLexer();
String text = leaf.getText();
String prefix = text.substring(0, endOffset - leaf.getTotalOffset());
ANTLRStringStream stream = new ANTLRStringStream(prefix);
lexer.setCharStream(stream);
Token nextToken = lexer.nextToken();
// copy to get rid of the reference to the stream again
return new CommonToken(nextToken.getType(), nextToken.getText());
}
use of org.antlr.runtime.ANTLRStringStream in project vespa by vespa-engine.
the class Predicate method fromString.
public static Predicate fromString(String str) {
ANTLRStringStream input = new ANTLRStringStream(str);
PredicateLexer lexer = new PredicateLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PredicateParser parser = new PredicateParser(tokens);
try {
return parser.predicate();
} catch (RecognitionException e) {
throw new IllegalArgumentException(e);
}
}
use of org.antlr.runtime.ANTLRStringStream in project n4js by eclipse.
the class AbstractSmokeTester method skipTokensInBetween.
private void skipTokensInBetween(CharSequence input) throws Exception {
String string = input.toString();
List<CommonToken> tokenList = Lists.newArrayList();
{
Lexer lexer = lexerProvider.get();
lexer.setCharStream(new ANTLRStringStream(string));
Token token = lexer.nextToken();
while (token != Token.EOF_TOKEN) {
tokenList.add((CommonToken) token);
token = lexer.nextToken();
}
}
for (CommonToken token : tokenList) {
int start = token.getStartIndex();
int length = token.getText().length();
processFile(string.substring(0, start) + string.substring(start + length));
}
}
use of org.antlr.runtime.ANTLRStringStream in project drools by kiegroup.
the class DrlExprParser method parse.
/**
* Parse an expression from text
*/
public ConstraintConnectiveDescr parse(final String text) {
ConstraintConnectiveDescr constraint = null;
try {
DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
CommonTokenStream input = new CommonTokenStream(lexer);
RecognizerSharedState state = new RecognizerSharedState();
helper = new ParserHelper(input, state, languageLevel);
DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
parser.setBuildDescr(true);
// setting initial value just in case
parser.setLeftMostExpr(null);
BaseDescr expr = parser.conditionalOrExpression();
if (expr != null && !parser.hasErrors()) {
constraint = ConstraintConnectiveDescr.newAnd();
constraint.addOrMerge(expr);
}
} catch (RecognitionException e) {
helper.reportError(e);
}
return constraint;
}
Aggregations