Skip to content

Commit

Permalink
getTokenStartLine/StartCharacter
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Mar 21, 2019
1 parent 7479e45 commit 85b5249
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export interface JSONScanner {
/**
* The zero-based start line number of the last read token.
*/
getTokenLine(): number;
getTokenStartLine(): number;
/**
* The zero-based character (start column) of the last read token.
* The zero-based start character (column) of the last read token.
*/
getTokenCharacter(): number;
getTokenStartCharacter(): number;
/**
* An error code of the last scan.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/impl/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,10 @@ export function visit(text: string, visitor: JSONVisitor, options: ParseOptions
let _scanner = createScanner(text, false);

function toNoArgVisit(visitFunction?: (offset: number, length: number, startLine: number, startCharacter: number) => void): () => void {
return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenLine(), _scanner.getTokenCharacter()) : () => true;
return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
}
function toOneArgVisit<T>(visitFunction?: (arg: T, offset: number, length: number, startLine: number, startCharacter: number) => void): (arg: T) => void {
return visitFunction ? (arg: T) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenLine(), _scanner.getTokenCharacter()) : () => true;
return visitFunction ? (arg: T) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
}

let onObjectBegin = toNoArgVisit(visitor.onObjectBegin),
Expand Down
18 changes: 9 additions & 9 deletions src/impl/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
tokenOffset = 0,
token: SyntaxKind = SyntaxKind.Unknown,
lineNumber = 0,
tokenLineNumber = 0,
lineEndOffset = 0,
prevLineEndOffset = 0,
lineStartOffset = 0,
tokenLineStartOffset = 0,
prevTokenLineStartOffset = 0,
scanError: ScanError = ScanError.None;

function scanHexDigits(count: number, exact?: boolean): number {
Expand Down Expand Up @@ -183,8 +183,8 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
scanError = ScanError.None;

tokenOffset = pos;
tokenLineNumber = lineNumber;
prevLineEndOffset = lineEndOffset;
lineStartOffset = lineNumber;
prevTokenLineStartOffset = tokenLineStartOffset;

if (pos >= len) {
// at the end
Expand Down Expand Up @@ -213,7 +213,7 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
value += '\n';
}
lineNumber++;
lineEndOffset = pos;
tokenLineStartOffset = pos;
return token = SyntaxKind.LineBreakTrivia;
}

Expand Down Expand Up @@ -285,7 +285,7 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
}

lineNumber++;
lineEndOffset = pos;
tokenLineStartOffset = pos;
}
}

Expand Down Expand Up @@ -383,8 +383,8 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
getTokenValue: () => value,
getTokenOffset: () => tokenOffset,
getTokenLength: () => pos - tokenOffset,
getTokenLine: () => tokenLineNumber,
getTokenCharacter: () => tokenOffset - prevLineEndOffset,
getTokenStartLine: () => lineStartOffset,
getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset,
getTokenError: () => scanError,
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ export interface JSONScanner {
/**
* The zero-based start line number of the last read token.
*/
getTokenLine(): number;
getTokenStartLine(): number;
/**
* The zero-based character (start column) of the last read token.
* The zero-based start character (column) of the last read token.
*/
getTokenCharacter(): number;
getTokenStartCharacter(): number;
/**
* An error code of the last scan.
*/
Expand Down

0 comments on commit 85b5249

Please sign in to comment.