List of usage examples for org.antlr.v4.runtime.misc IntervalSet getIntervals
public List<Interval> getIntervals()
From source file:com.bosch.example.rsql.suggestion.RsqlSuggestionHelper.java
License:Open Source License
public static SuggestionContext parse(final String rsql, final int cursorPosition) { CharStream inputCharStream;//ww w .j a v a 2 s . c o m try { inputCharStream = new ANTLRInputStream(new StringReader(rsql)); } catch (final IOException e) { throw Throwables.propagate(e); } final TokenSource tokenSource = new RsqlLexer(inputCharStream); final TokenStream inputTokenStream = new CommonTokenStream(tokenSource); final RsqlParser parser = new RsqlParser(inputTokenStream); final SuggestionErrorListener errorListener = new SuggestionErrorListener(); parser.addErrorListener(errorListener); final SuggestionParseTreeListener parserListener = new SuggestionParseTreeListener(); parser.addParseListener(parserListener); parser.expr(); final SuggestionContext suggestionContext = new SuggestionContext(); suggestionContext.syntaxError = errorListener.isErrorOccurred(); if (errorListener.isErrorOccurred()) { final SyntaxErrorContext errorContext = new SyntaxErrorContext(); final Token errorToken = errorListener.getErrorToken().getToken(); errorContext.tokenStart = errorToken.getStartIndex(); errorContext.tokenEnd = errorToken.getStopIndex(); final IntervalSet expectedTokenType = errorListener.getErrorToken().getExpectedTokenType(); expectedTokenType.getIntervals().forEach(interval -> { final int start = interval.a; final int end = interval.b; for (int index = start; index <= end; index++) { final String symbolicName = parser.getVocabulary().getSymbolicName(index); errorContext.suggestions.addAll(SuggestionMap.getSuggestions(symbolicName)); } }); suggestionContext.syntaxErrorContext = errorContext; } final Optional<Token> tokenAtCursorPosition = parserListener.getTokenAtCursorPosition(cursorPosition); if (tokenAtCursorPosition.isPresent()) { final CursorPositionSuggestionContext cursorPositionSuggestionContext = new CursorPositionSuggestionContext(); cursorPositionSuggestionContext.currentCursor = cursorPosition; cursorPositionSuggestionContext.tokenStart = tokenAtCursorPosition.get().getStartIndex(); cursorPositionSuggestionContext.tokenEnd = tokenAtCursorPosition.get().getStopIndex(); cursorPositionSuggestionContext.suggestions = SuggestionMap .getSuggestions(parser.getVocabulary().getSymbolicName(tokenAtCursorPosition.get().getType())); suggestionContext.cursorPositionContext = cursorPositionSuggestionContext; } return suggestionContext; }
From source file:org.ballerinalang.composer.service.workspace.suggetions.CapturePossibleTokenStrategy.java
License:Open Source License
protected void fetchPossibleTokens(Parser parser, Token currentToken, IntervalSet expectedTokens) { ParserRuleContext currentContext = parser.getContext(); if (isCursorBetweenGivenTokenAndLastNonHiddenToken(currentToken, parser)) { expectedTokens.getIntervals().forEach((interval) -> { int a = interval.a; int b = interval.b; if (a == b) { possibleTokens/*w w w . ja v a2 s .c om*/ .add(new PossibleToken(a, parser.getVocabulary().getDisplayName(a), currentContext)); } else { for (int i = a; i <= b; ++i) { possibleTokens.add( new PossibleToken(i, parser.getVocabulary().getDisplayName(i), currentContext)); } } }); SuggestionsFilterDataModel sfdModel = new SuggestionsFilterDataModel(parser, currentContext, this.possibleTokens); this.setSuggestionsFilterDataModel(sfdModel); } }