use of org.apache.groovy.parser.antlr4.GroovyLexer.CONTINUE in project groovy by apache.
the class SmartDocumentFilter method findTokensToRender.
private List<Token> findTokensToRender(List<Token> tokenList) {
final Tuple2<Integer, Integer> renderRange = getRenderRange();
if (null != renderRange) {
long startLine = lineNumber(renderRange.getV1());
long stopLine = lineNumber(renderRange.getV2());
// should never happen
if (startLine < 0 || stopLine < 0)
return tokenList;
return tokenList.stream().filter(e -> e.getLine() >= startLine && PositionConfigureUtils.endPosition(e).getV1() <= stopLine).collect(Collectors.toList());
}
List<Token> tmpLatestTokenList = filterNewlines(this.latestTokenList);
int latestTokenListSize = tmpLatestTokenList.size();
if (0 == latestTokenListSize) {
return tokenList;
}
List<Token> tmpTokenList = filterNewlines(tokenList);
int tmpTokenListSize = tmpTokenList.size();
if (0 == tmpTokenListSize) {
return tokenList;
}
int startTokenIndex = 0;
int minSize = Math.min(tmpTokenListSize, latestTokenListSize);
for (int i = 0; i < minSize; i++) {
Token token = tmpTokenList.get(i);
Token latestToken = tmpLatestTokenList.get(i);
if (token.getType() == latestToken.getType() && token.getStartIndex() == latestToken.getStartIndex() && token.getStopIndex() == latestToken.getStopIndex()) {
continue;
}
startTokenIndex = i;
break;
}
List<Token> newTokenList = new ReversedList<>(tmpTokenList);
List<Token> newLatestTokenList = new ReversedList<>(tmpLatestTokenList);
int stopTokenIndex = tmpTokenListSize;
Token lastToken = newTokenList.get(0);
Token lastLatestToken = newLatestTokenList.get(0);
for (int i = 0; i < minSize; i++) {
Token token = newTokenList.get(i);
Token latestToken = newLatestTokenList.get(i);
if ((token.getType() == latestToken.getType()) && (token.getStartIndex() - lastToken.getStartIndex()) == (latestToken.getStartIndex() - lastLatestToken.getStartIndex()) && ((token.getStopIndex() - lastToken.getStopIndex()) == (latestToken.getStopIndex() - lastLatestToken.getStopIndex()))) {
continue;
}
stopTokenIndex = tmpTokenListSize - i;
break;
}
if (startTokenIndex == stopTokenIndex) {
return tmpTokenListSize != latestTokenListSize ? tokenList : Collections.emptyList();
} else if (startTokenIndex < stopTokenIndex) {
return tmpTokenList.subList(startTokenIndex, stopTokenIndex);
}
// should never reach here. If unexpected error occurred, it's better to render all tokens
return tokenList;
}
use of org.apache.groovy.parser.antlr4.GroovyLexer.CONTINUE in project groovy by apache.
the class AstBuilder method visitContinueStatement.
@Override
public ContinueStatement visitContinueStatement(final ContinueStatementContext ctx) {
if (visitingLoopStatementCount == 0) {
throw createParsingFailedException("continue statement is only allowed inside loops", ctx);
}
GroovyParserRuleContext gprc = switchExpressionRuleContextStack.peek();
if (gprc instanceof SwitchExpressionContext) {
throw createParsingFailedException("switch expression does not support `continue`", ctx);
}
String label = asBoolean(ctx.identifier()) ? this.visitIdentifier(ctx.identifier()) : null;
return configureAST(new ContinueStatement(label), ctx);
}
use of org.apache.groovy.parser.antlr4.GroovyLexer.CONTINUE in project groovy by apache.
the class SmartDocumentFilter method parseDocument.
private void parseDocument() throws BadLocationException {
GroovyLangLexer lexer;
try {
lexer = createLexer(styledDocument.getText(0, styledDocument.getLength()));
} catch (IOException e) {
e.printStackTrace();
this.latest = false;
return;
}
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
try {
tokenStream.fill();
} catch (LexerNoViableAltException | GroovySyntaxError e) {
// ignore
this.latest = false;
return;
} catch (Exception e) {
e.printStackTrace();
this.latest = false;
return;
}
List<Token> tokenList = tokenStream.getTokens();
List<Token> tokenListToRender;
try {
tokenListToRender = findTokensToRender(tokenList);
} finally {
this.setRenderRange(null);
}
for (Token token : tokenListToRender) {
int tokenType = token.getType();
if (EOF == tokenType) {
continue;
}
int tokenStartIndex = token.getStartIndex();
int tokenStopIndex = token.getStopIndex();
int tokenLength = tokenStopIndex - tokenStartIndex + 1;
styledDocument.setCharacterAttributes(tokenStartIndex, tokenLength, findStyleByTokenType(tokenType), true);
if (GStringBegin == tokenType || GStringPart == tokenType) {
styledDocument.setCharacterAttributes(tokenStartIndex + tokenLength - 1, 1, defaultStyle, true);
}
}
this.latestTokenList = tokenList;
this.latest = true;
}
use of org.apache.groovy.parser.antlr4.GroovyLexer.CONTINUE in project groovy by apache.
the class AstBuilder method visitGstring.
// } literal ---------------------------------------------------------------
// gstring { ---------------------------------------------------------------
@Override
public GStringExpression visitGstring(final GstringContext ctx) {
final List<ConstantExpression> stringLiteralList = new LinkedList<>();
final String begin = ctx.GStringBegin().getText();
final String beginQuotation = beginQuotation(begin);
stringLiteralList.add(configureAST(new ConstantExpression(parseGStringBegin(ctx, beginQuotation)), ctx.GStringBegin()));
List<ConstantExpression> partStrings = ctx.GStringPart().stream().map(e -> configureAST(new ConstantExpression(parseGStringPart(e, beginQuotation)), e)).collect(Collectors.toList());
stringLiteralList.addAll(partStrings);
stringLiteralList.add(configureAST(new ConstantExpression(parseGStringEnd(ctx, beginQuotation)), ctx.GStringEnd()));
List<Expression> values = ctx.gstringValue().stream().map(this::visitGstringValue).collect(Collectors.toList());
StringBuilder verbatimText = new StringBuilder(ctx.getText().length());
for (int i = 0, n = stringLiteralList.size(), s = values.size(); i < n; i += 1) {
verbatimText.append(stringLiteralList.get(i).getValue());
if (i == s) {
continue;
}
Expression value = values.get(i);
if (!asBoolean(value)) {
continue;
}
boolean isVariableExpression = value instanceof VariableExpression;
verbatimText.append(DOLLAR_STR);
if (!isVariableExpression)
verbatimText.append('{');
verbatimText.append(value.getText());
if (!isVariableExpression)
verbatimText.append('}');
}
return configureAST(new GStringExpression(verbatimText.toString(), stringLiteralList, values), ctx);
}
Aggregations