State parserState = loadParserStateForBeginningOfLine(line);
if (parserState == null) {
return false;
}
Line previousLine = line.getPreviousLine();
for (int numLinesProcessed = 0; line != null && numLinesProcessed < numLinesToProcess;) {
State stateToSave = parserState;
if (line.getText().length() > LINE_LENGTH_LIMIT) {
// Save the initial state instead of state at the end of line.
stateToSave = parserState.copy(codeMirrorParser);
}
JsonArray<Token> tokens;
try {
tokens = parseLine(parserState, line.getText());
} catch (ParserException e) {
Log.error(getClass(), "Could not parse line:", line, e);
return false;
}
// Restore the initial line state if it was preserved.
parserState = stateToSave;
saveEndOfLineParserState(line, parserState);
tokensRecipient.onTokensParsed(line, lineNumber, tokens);
previousLine = line;
line = line.getNextLine();
numLinesProcessed++;
if (lineNumber != -1) {
lineNumber++;
}
}
if (anchorToUpdate != null) {
if (lineNumber == -1) {
throw new IllegalArgumentException("lineNumber cannot be -1 if anchorToUpdate is given");
}
if (line != null) {
line.getDocument().getAnchorManager()
.moveAnchor(anchorToUpdate, line, lineNumber, AnchorManager.IGNORE_COLUMN);
} else {
previousLine.getDocument().getAnchorManager()
.moveAnchor(anchorToUpdate, previousLine, lineNumber - 1, AnchorManager.IGNORE_COLUMN);
}
}
return line != null;