Correct syntax error ranges

This commit is contained in:
BoykoAlex
2016-09-27 15:37:17 -04:00
parent d50d59c9b6
commit 0ef54104be
2 changed files with 18 additions and 5 deletions

View File

@@ -132,12 +132,22 @@ public class AntlrParser implements Parser {
@Override
public int getOffset() {
return token.getStartIndex();
if (token.getStartIndex() >= token.getStopIndex()) {
// No range? Make error span the whole line then
return token.getStartIndex() - token.getCharPositionInLine();
} else {
return token.getStartIndex();
}
}
@Override
public int getLength() {
return token.getStopIndex() - token.getStartIndex();
if (token.getStartIndex() >= token.getStopIndex()) {
// No range? Make error span the whole line then
return token.getCharPositionInLine();
} else {
return token.getStopIndex() - token.getStartIndex();
}
}
};

View File

@@ -191,7 +191,8 @@ public class PropertiesAntlrParserTest {
assertEquals(1, results.ast.getAllNodes().size());
Problem syntaxError = results.syntaxErrors.get(0);
assertEquals(text.length(), syntaxError.getOffset());
assertEquals(0, syntaxError.getOffset());
assertEquals(text.length(), syntaxError.getLength());
}
@Test
@@ -214,9 +215,11 @@ public class PropertiesAntlrParserTest {
// Test errors
Problem syntaxError1 = results.syntaxErrors.get(0);
assertEquals(11, syntaxError1.getOffset());
assertEquals(0, syntaxError1.getOffset());
assertEquals(11, syntaxError1.getLength());
Problem syntaxError2 = results.syntaxErrors.get(1);
assertEquals(text.length(), syntaxError2.getOffset());
assertEquals(22, syntaxError2.getOffset());
assertEquals(6, syntaxError2.getLength());
}
}