use of org.antlr.v4.runtime.misc.Nullable in project grakn by graknlabs.
the class QueryParserImpl method parseList.
/**
* @param reader a reader representing several queries
* @return a list of queries
*/
@Override
public <T extends Query<?>> Stream<T> parseList(Reader reader) {
UnbufferedCharStream charStream = new UnbufferedCharStream(reader);
GraqlErrorListener errorListener = GraqlErrorListener.withoutQueryString();
GraqlLexer lexer = createLexer(charStream, errorListener);
/*
We tell the lexer to copy the text into each generated token.
Normally when calling `Token#getText`, it will look into the underlying `TokenStream` and call
`TokenStream#size` to check it is in-bounds. However, `UnbufferedTokenStream#size` is not supported
(because then it would have to read the entire input). To avoid this issue, we set this flag which will
copy over the text into each `Token`, s.t. that `Token#getText` will just look up the copied text field.
*/
lexer.setTokenFactory(new CommonTokenFactory(true));
// Use an unbuffered token stream so we can handle extremely large input strings
UnbufferedTokenStream tokenStream = new UnbufferedTokenStream(ChannelTokenSource.of(lexer));
GraqlParser parser = createParser(tokenStream, errorListener);
/*
The "bail" error strategy prevents us reading all the way to the end of the input, e.g.
```
match $x isa person; insert $x has name "Bob"; match $x isa movie; get;
^
```
In this example, when ANTLR reaches the indicated `match`, it considers two possibilities:
1. this is the end of the query
2. the user has made a mistake. Maybe they accidentally pasted the `match` here.
Because of case 2, ANTLR will parse beyond the `match` in order to produce a more helpful error message.
This causes memory issues for very large queries, so we use the simpler "bail" strategy that will
immediately stop when it hits `match`.
*/
parser.setErrorHandler(new BailErrorStrategy());
// This is a lazy iterator that will only consume a single query at a time, without parsing any further.
// This means it can pass arbitrarily long streams of queries in constant memory!
Iterable<T> queryIterator = () -> new AbstractIterator<T>() {
@Nullable
@Override
protected T computeNext() {
int latestToken = tokenStream.LA(1);
if (latestToken == Token.EOF) {
endOfData();
return null;
} else {
// When we next run it, it will start where it left off in the stream
return (T) QUERY.parse(parser, errorListener);
}
}
};
return StreamSupport.stream(queryIterator.spliterator(), false);
}
use of org.antlr.v4.runtime.misc.Nullable in project java by wavefrontHQ.
the class ReportPointIngesterFormatter method drive.
@Override
public ReportPoint drive(String input, String defaultHostName, String customerId, @Nullable List<String> customSourceTags) {
Queue<Token> queue = getQueue(input);
ReportPoint point = new ReportPoint();
point.setTable(customerId);
// if the point has a timestamp, this would be overriden
point.setTimestamp(Clock.now());
AbstractWrapper wrapper = new ReportPointWrapper(point);
try {
for (FormatterElement element : elements) {
element.consume(queue, wrapper);
}
} catch (Exception ex) {
throw new RuntimeException("Could not parse: " + input, ex);
}
if (!queue.isEmpty()) {
throw new RuntimeException("Could not parse: " + input);
}
// Delta metrics cannot have negative values
if ((point.getMetric().startsWith(DELTA_PREFIX) || point.getMetric().startsWith(DELTA_PREFIX_2)) && point.getValue() instanceof Number) {
double v = ((Number) point.getValue()).doubleValue();
if (v <= 0) {
throw new RuntimeException("Delta metrics cannot be non-positive: " + input);
}
}
String host = null;
Map<String, String> annotations = point.getAnnotations();
if (annotations != null) {
host = annotations.remove("source");
if (host == null) {
host = annotations.remove("host");
} else if (annotations.containsKey("host")) {
// we have to move this elsewhere since during querying,
// host= would be interpreted as host and not a point tag
annotations.put("_host", annotations.remove("host"));
}
if (annotations.containsKey("tag")) {
annotations.put("_tag", annotations.remove("tag"));
}
if (host == null && customSourceTags != null) {
// iterate over the set of custom tags, breaking when one is found
for (String tag : customSourceTags) {
host = annotations.get(tag);
if (host != null) {
break;
}
}
}
}
if (host == null) {
host = defaultHostName;
}
point.setHost(host);
return ReportPoint.newBuilder(point).build();
}
use of org.antlr.v4.runtime.misc.Nullable in project titan.EclipsePlug-ins by eclipse.
the class TitanListener method syntaxError.
@Override
public void syntaxError(@NotNull final Recognizer<?, ?> recognizer, @Nullable final Object offendingSymbol, final int line, final int charPositionInLine, @NotNull final String msg, @Nullable final RecognitionException e) {
SyntacticErrorStorage errorStorage;
if (offendingSymbol instanceof CommonToken) {
final CommonToken token = (CommonToken) offendingSymbol;
errorStorage = new SyntacticErrorStorage(line, token.getStartIndex(), token.getStopIndex() + 1, msg, e);
} else {
errorStorage = new SyntacticErrorStorage(line, charPositionInLine, charPositionInLine + 1, msg, e);
}
errorsStored.add(errorStorage);
}
use of org.antlr.v4.runtime.misc.Nullable in project vespa by vespa-engine.
the class ProgramParser method prepareParser.
private yqlplusParser prepareParser(String programName, CharStream input) {
yqlplusLexer lexer = new yqlplusLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) {
throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg);
}
});
TokenStream tokens = new CommonTokenStream(lexer);
yqlplusParser parser = new yqlplusParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) {
throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg);
}
});
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
return parser;
}
use of org.antlr.v4.runtime.misc.Nullable in project batfish by batfish.
the class Batfish method blacklistInterface.
/**
* Helper function to disable a blacklisted interface and update the given {@link
* ValidateEnvironmentAnswerElement} if the interface does not actually exist.
*/
private static void blacklistInterface(Map<String, Configuration> configurations, ValidateEnvironmentAnswerElement veae, NodeInterfacePair iface) {
String hostname = iface.getHostname();
String ifaceName = iface.getInterface();
@Nullable Configuration node = configurations.get(hostname);
if (node == null) {
veae.setValid(false);
veae.getUndefinedInterfaceBlacklistNodes().add(hostname);
return;
}
@Nullable Interface nodeIface = node.getInterfaces().get(ifaceName);
if (nodeIface == null) {
veae.setValid(false);
veae.getUndefinedInterfaceBlacklistInterfaces().computeIfAbsent(hostname, k -> new TreeSet<>()).add(ifaceName);
return;
}
nodeIface.setActive(false);
nodeIface.setBlacklisted(true);
}
Aggregations