Update to new source-region SourceRegion abstraction
This commit is contained in:
parent
38b147c3e7
commit
909caaf7ac
12 changed files with 63 additions and 52 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { char, NEW_LINE, CARRIAGE_RETURN, DOT, DIGIT_0, DIGIT_9, LOWERCASE_a, LOWERCASE_f, UPPERCASE_A, UPPERCASE_F, SPACE, TAB } from 'source-text';
|
||||
import type { SourceText, Span, SourceLocation, CodePoint, StringIndex, CodePointIndex } from 'source-text';
|
||||
import type { SourceRegion, SourceText, Span, SourceLocation, CodePoint, StringIndex, CodePointIndex } from 'source-text';
|
||||
import { Result } from '../result';
|
||||
|
||||
export type CursorState = {
|
||||
|
|
@ -10,13 +10,21 @@ export type CursorState = {
|
|||
}
|
||||
|
||||
export class Cursor {
|
||||
private index: CodePointIndex = 0;
|
||||
private line: number = 1;
|
||||
private column: number = 1;
|
||||
private index: CodePointIndex;
|
||||
private line: number;
|
||||
private column: number;
|
||||
// Track previous char to handle \r\n correctly
|
||||
private lastCharWasCR: boolean = false;
|
||||
|
||||
constructor(readonly text: SourceText) {}
|
||||
constructor(readonly region: SourceRegion) {
|
||||
this.index = region.span.start.index;
|
||||
this.line = region.span.start.line;
|
||||
this.column = region.span.start.column;
|
||||
}
|
||||
|
||||
get text(): SourceText {
|
||||
return this.region.source;
|
||||
}
|
||||
|
||||
save(): CursorState {
|
||||
return { index: this.index, line: this.line, column: this.column, lastCharWasCR: this.lastCharWasCR };
|
||||
|
|
@ -30,14 +38,16 @@ export class Cursor {
|
|||
}
|
||||
|
||||
eof(): boolean {
|
||||
return this.index >= this.text.length;
|
||||
return this.index >= this.region.span.end.index;
|
||||
}
|
||||
|
||||
peek(n: number = 0): CodePoint | undefined {
|
||||
if (this.index + n >= this.region.span.end.index) return undefined;
|
||||
return this.text.chars[this.index + n]?.char;
|
||||
}
|
||||
|
||||
next(): CodePoint | undefined {
|
||||
if (this.eof()) return undefined;
|
||||
const ref = this.text.chars[this.index];
|
||||
if (!ref) return undefined;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue