Implement getLocation
This commit is contained in:
parent
beab541907
commit
9939f6c97f
1 changed files with 35 additions and 5 deletions
40
src/index.ts
40
src/index.ts
|
|
@ -74,11 +74,28 @@ export class SourceText {
|
||||||
return this.source.slice(startOff, endOff);
|
return this.source.slice(startOff, endOff);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts a linear Code Point Index into SourceLocation
|
// Converts a linear Code Point Index into a SourceLocation (line, column, index).
|
||||||
// getLocation(index: CodePointIndex): SourceLocation {
|
getLocation(index: CodePointIndex): SourceLocation {
|
||||||
// // TODO: can be implemented either by a linear or binary search.
|
// Does binary search.
|
||||||
// return (0 as any);
|
let low = 0;
|
||||||
// }
|
let high = this.lineStarts.length - 1;
|
||||||
|
let line = 1;
|
||||||
|
|
||||||
|
while (low <= high) {
|
||||||
|
const mid = Math.floor((low + high) / 2);
|
||||||
|
if (this.lineStarts[mid] <= index) {
|
||||||
|
line = mid + 1;
|
||||||
|
low = mid + 1;
|
||||||
|
} else {
|
||||||
|
high = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const lineStartIndex = this.lineStarts[line - 1];
|
||||||
|
const column = index - lineStartIndex + 1;
|
||||||
|
|
||||||
|
return { index, line, column };
|
||||||
|
}
|
||||||
|
|
||||||
// Returns the full text of a specific line (1-based index)
|
// Returns the full text of a specific line (1-based index)
|
||||||
getLineText(line: number): string {
|
getLineText(line: number): string {
|
||||||
|
|
@ -114,6 +131,19 @@ export function sourceText(s: string): SourceText {
|
||||||
return new SourceText(s);
|
return new SourceText(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class SourceRegion {
|
||||||
|
// TODO
|
||||||
|
readonly sourceText: SourceText;
|
||||||
|
readonly span: Span;
|
||||||
|
|
||||||
|
constructor(sourceText: SourceText, span: Span) {
|
||||||
|
this.sourceText = sourceText;
|
||||||
|
// TODO: What about span validation?
|
||||||
|
this.span = span;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export type Span = {
|
export type Span = {
|
||||||
start: SourceLocation;
|
start: SourceLocation;
|
||||||
end: SourceLocation;
|
end: SourceLocation;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue