Closed mbrassesco closed 10 months ago
For the Structorizer project, we had the same problem and solved it indeed by the folllowing patches:
Parser.java
/**
* Consume/remove characters from the front of the lookahead buffer
* and adjust the value of the system Position object.
* @param count the number of characters to consume
*/
private void consumeBuffer(int count) {
if (count > 0 && count <= lookaheadBuffer.length()) {
// Adjust position
// START SSO 2017-06-26 - line counts were wrong
int lines = 0, columns = 0;
for (int i = 0; i < count; i++) {
char c = lookaheadBuffer.charAt(i);
switch (c) {
case 0x0D:
// increment char counter for Windows LF (MacOS would be plain 0x0D)
if (i + 1 != count && lookaheadBuffer.charAt(i + 1) == 0x0A) {
i++;
}
// Fall through
case 0x0A:
lines++;
break;
default:
columns++;
break;
}
}
if (lines != 0) {
sysPosition.incrementLine(lines);
} else {
sysPosition.incrementColumn(columns);
}
// END SSO 2017-06-26
// Remove the characters
lookaheadBuffer.delete(0, count);
}
}
Position.java
// START SSO 2017-06-26 - Added as convenience for Parser.consumeBuffer(int) fix
/** increment current position by given lines, reset column to 1 */
public void incrementLine(int lines) {
this.line += lines;
this.column = 1;
}
/** increment current position by given columns */
public void incrementColumn(int columns) {
this.column += columns;
}
// END SSO 2017-06-26
Thanks for bringing this to my attention and for a solution. I'll write a unit and make the necessary edits and create a release in the next few days.
When I use your engine in Linux the row (line) doesn't increment when the text has only new empty lines.
For example:
Error message: Syntax error at line 2, column 1.
Should be
Syntax error at line 6, column 1.
I suspect the issue is here:
Parser.java (class)
private void consumeBuffer(int count) { if (count > 0 && count <= lookaheadBuffer.length()) { // Adjust position for (int i = 0; i < count; i++) { char c = lookaheadBuffer.charAt(i); if (c == 0x0A) { if (sysPosition.getColumn() > 1) { // Increment row if Unix EOLN (LF) sysPosition.incrementLine(); } } else if (c == 0x0D) { sysPosition.incrementLine(); } else { sysPosition.incrementColumn(); } }