Renaming and source-region issues

This commit is contained in:
Yura Dupyn 2026-04-06 20:34:20 +02:00
parent 909caaf7ac
commit d64c39db82
19 changed files with 415 additions and 36 deletions

View file

@ -2,7 +2,7 @@
import * as readline from 'readline';
import * as fs from 'fs';
import { parseExpr, ParseError } from '../parser/parser';
import { SourceRegion, SourceText, renderSpan, sourceText } from 'source-text';
import { SourceRegion, SourceText, renderSpan, sourceText } from 'source-region';
import { exprToString } from '../debug/expr_show';
import { valueToString } from '../debug/value_show';
import { Program } from '../program';

View file

@ -1,4 +1,4 @@
import { Span } from "source-text"
import { Span } from "source-region"
// === Identifiers ===
export type VariableName = string

View file

@ -1,5 +1,5 @@
// AI GENERATED
import { SourceText, sourceText } from "source-text";
import { SourceText, sourceText } from "source-region";
import { Cursor, scanString, scanNumber } from "./cursor";
import { Result } from "../result";
@ -189,8 +189,8 @@ function test_cursor_tracking() {
// Check location after scan (should be after 123, before \r)
// Actually r1.value.span tells us where the token WAS.
const span1 = (r1 as any).value.span;
assert(span1.line === 1, "Line 1 line# wrong");
assert(span1.column === 1, "Line 1 col# wrong");
assert(span1.start.line === 1, "Line 1 line# wrong");
assert(span1.start.column === 1, "Line 1 col# wrong");
// 2. Skip Whitespace (Scanner logic simulation)
// We need to manually skip \r\n
@ -201,7 +201,7 @@ function test_cursor_tracking() {
const r2 = scanNumber(cursor);
assertOk(r2, 456);
const span2 = (r2 as any).value.span;
assert(span2.line === 2, "Line 2 line# wrong");
assert(span2.start.line === 2, "Line 2 line# wrong");
// 4. Skip \n
cursor.next();
@ -210,7 +210,7 @@ function test_cursor_tracking() {
const r3 = scanString(cursor);
assertOk(r3, "foo");
const span3 = (r3 as any).value.span;
assert(span3.line === 3, "Line 3 line# wrong");
assert(span3.start.line === 3, "Line 3 line# wrong");
console.log(`${GREEN}✔ Cursor tracking passed${RESET}`);
}

View file

@ -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 { SourceRegion, SourceText, Span, SourceLocation, CodePoint, StringIndex, CodePointIndex } from 'source-text';
import { char, NEW_LINE, CARRIAGE_RETURN, DOT, DIGIT_0, DIGIT_9, LOWERCASE_a, LOWERCASE_f, UPPERCASE_A, UPPERCASE_F, SPACE, TAB } from 'source-region';
import type { SourceRegion, SourceText, Span, SourceLocation, CodePoint, StringIndex, CodePointIndex } from 'source-region';
import { Result } from '../result';
export type CursorState = {
@ -43,15 +43,14 @@ export class Cursor {
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;
return this.region.codePointAt(this.index + n);
}
next(): CodePoint | undefined {
if (this.eof()) return undefined;
const ref = this.text.chars[this.index];
if (!ref) return undefined;
const c = this.region.codePointAt(this.index);
if (c === undefined) return undefined;
const c = ref.char;
this.index++;
if (c === NEW_LINE) {
@ -82,7 +81,7 @@ export class Cursor {
// TODO: unicode-index ~> string-offset, make that into a separate function.
currentOffset(): StringIndex {
return this.text.chars[this.index]?.offset ?? this.text.source.length;
return this.text.sliceByCp(0, this.index).length;
}
currentLocation(): SourceLocation {

View file

@ -1,6 +1,6 @@
import { Cursor } from './cursor';
import { ExprScanError, exprStart, ExprStartToken, IdentifierKind, identifierScanner, isNextTokenExprStart, isNextTokenProductPatternStart, patternStart, PatternStartToken, signalExprStart, SignalExprStartToken, skipWhitespaceAndComments } from './scanner';
import { char, CodePoint, SourceRegion, SourceText, Span } from 'source-text';
import { char, CodePoint, SourceRegion, SourceText, Span } from 'source-region';
import { Result } from '../result';
import { Expr, ExprBinding, FieldAssignment, FieldPattern, FunctionName, MatchBranch, Pattern, ProductPattern, SignalExpr, SignalExprBinding } from '../expr';

View file

@ -1,5 +1,5 @@
import { CARRIAGE_RETURN, char, NEW_LINE } from 'source-text';
import type { Span, CodePoint } from 'source-text';
import { CARRIAGE_RETURN, char, NEW_LINE } from 'source-region';
import type { Span, CodePoint } from 'source-region';
import { isDigit, isWhitespace, scanNumber, scanString } from './cursor';
import type { Cursor, GenericScanError, NumberError, StringError } from './cursor';