Feat/range boundary (#241)

* feat: unify the index, line and column in all APIs

* docs: describe index, line and column
This commit is contained in:
Hayden
2023-12-22 19:12:29 +08:00
committed by GitHub
parent f6bc7594e1
commit 9c52542ec7
9 changed files with 159 additions and 11 deletions

View File

@ -48,11 +48,13 @@ export interface WordRange {
readonly text: string;
/** start at 0 */
readonly startIndex: number;
readonly stopIndex: number;
/** end at ..n-1 */
readonly endIndex: number;
/** start at 1 */
readonly line: number;
/** start at 1 */
readonly startColumn: number;
/** end at ..n + 1 */
readonly stopColumn: number;
}
@ -81,12 +83,15 @@ export interface Suggestions<T = WordRange> {
export interface TextSlice {
/** start at 0 */
readonly startIndex: number;
/** end at ..n-1 */
readonly endIndex: number;
/** start at 1 */
readonly startLine: number;
/** end at ..n */
readonly endLine: number;
/** start at 1 */
readonly startColumn: number;
/** end at ..n + 1 */
readonly endColumn: number;
readonly text: string;
}

View File

@ -244,7 +244,7 @@ export default abstract class BasicParser<
startLine: start.line,
endLine: stop.line,
startColumn: start.charPositionInLine + 1,
endColumn: stop.charPositionInLine + stop.text.length,
endColumn: stop.charPositionInLine + 1 + stop.text.length,
text: this._parsedInput.slice(start.startIndex, stop.stopIndex + 1),
};
});
@ -364,10 +364,10 @@ export default abstract class BasicParser<
return {
text: this._parsedInput.slice(token.startIndex, token.stopIndex + 1),
startIndex: token.startIndex,
stopIndex: token.stopIndex,
endIndex: token.stopIndex,
line: token.line,
startColumn: token.charPositionInLine + 1,
stopColumn: token.charPositionInLine + token.text.length,
stopColumn: token.charPositionInLine + 1 + token.text.length,
};
});
return {

View File

@ -5,10 +5,14 @@ import { ATNSimulator } from 'antlr4ts/atn/ATNSimulator';
* Converted from {@link SyntaxError}.
*/
export interface ParseError {
/** start at 1 */
readonly startLine: number;
/** end at ..n */
readonly endLine: number;
readonly startCol: number;
readonly endCol: number;
/** start at 1 */
readonly startColumn: number;
/** end at ..n + 1 */
readonly endColumn: number;
readonly message: string;
}
@ -31,7 +35,7 @@ export interface SyntaxError<T> {
export type ErrorListener<T> = (parseError: ParseError, originalError: SyntaxError<T>) => void;
export default class ParseErrorListener implements ANTLRErrorListener<Token> {
private _errorListener;
private _errorListener: ErrorListener<Token>;
constructor(errorListener: ErrorListener<Token>) {
this._errorListener = errorListener;
@ -54,8 +58,8 @@ export default class ParseErrorListener implements ANTLRErrorListener<Token> {
{
startLine: line,
endLine: line,
startCol: charPositionInLine,
endCol: endCol,
startColumn: charPositionInLine + 1,
endColumn: endCol + 1,
message: msg,
},
{