use of org.apache.groovy.parser.antlr4.GroovyLexer.PRIVATE in project groovy by apache.
the class AstBuilder method transformRecordHeaderToProperties.
private void transformRecordHeaderToProperties(ClassDeclarationContext ctx, ClassNode classNode) {
Parameter[] parameters = this.visitFormalParameters(ctx.formalParameters());
classNode.putNodeMetaData(RECORD_HEADER, parameters);
final int n = parameters.length;
for (int i = 0; i < n; i += 1) {
Parameter parameter = parameters[i];
FormalParameterContext parameterCtx = parameter.getNodeMetaData(PARAMETER_CONTEXT);
ModifierManager parameterModifierManager = parameter.getNodeMetaData(PARAMETER_MODIFIER_MANAGER);
ClassNode originType = parameter.getOriginType();
PropertyNode propertyNode = declareProperty(parameterCtx, parameterModifierManager, originType, classNode, i, parameter, parameter.getName(), parameter.getModifiers(), parameter.getInitialExpression());
propertyNode.getField().putNodeMetaData(IS_RECORD_GENERATED, Boolean.TRUE);
}
}
use of org.apache.groovy.parser.antlr4.GroovyLexer.PRIVATE 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.PRIVATE in project groovy by apache.
the class SmartDocumentFilter method createLexer.
private GroovyLangLexer createLexer(String text) throws IOException {
CharStream charStream = CharStreams.fromReader(new StringReader(text));
GroovyLangLexer lexer = new GroovyLangLexer(charStream);
lexer.setErrorIgnored(true);
lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
return lexer;
}
use of org.apache.groovy.parser.antlr4.GroovyLexer.PRIVATE 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.PRIVATE in project groovy by apache.
the class LexerFrame method scanScript.
private void scanScript(final StringReader reader) throws Exception {
scriptPane.read(reader, null);
reader.reset();
// create lexer
final GroovyLangLexer lexer = new GroovyLangLexer(reader);
tokenPane.setEditable(true);
tokenPane.setText("");
int line = 1;
final ButtonGroup bg = new ButtonGroup();
Token token;
int tokenCnt = 0;
while (true) {
token = lexer.nextToken();
JToggleButton tokenButton = new JToggleButton(tokens.get(token.getType()));
tokenButton.setFont(MONOSPACED_FONT);
bg.add(tokenButton);
tokenButton.addActionListener(this);
tokenButton.setToolTipText(token.getText());
tokenButton.putClientProperty("token", token);
tokenButton.setMargin(new Insets(0, 1, 0, 1));
tokenButton.setFocusPainted(false);
if (token.getLine() > line) {
tokenPane.getDocument().insertString(tokenPane.getDocument().getLength(), "\n", null);
line = token.getLine();
}
insertComponent(tokenButton);
tokenCnt++;
if (eof().equals(token.getType())) {
break;
}
}
tokenStreamLabel.setText(" Token Stream(" + tokenCnt + "):");
tokenPane.setEditable(false);
tokenPane.setCaretPosition(0);
reader.close();
}
Aggregations