UNPKG

@composita/source-location

Version:

Composita language source location.

45 lines 1.66 kB
import { CompareValue } from '@composita/ts-utility-types'; export class SourcePosition { constructor(line, character) { this.line = line; this.character = character; } static from(other) { return other instanceof SourcePosition ? other : new SourcePosition(other.line, other.character); } compareTo(other) { if (this.line < other.line || (this.line === other.line && this.character < other.character)) { return CompareValue.LT; } if (this.line > other.line || (this.line === other.line && this.character > other.character)) { return CompareValue.GT; } return CompareValue.EQ; } } export class SourceRange { constructor(start, end) { this.start = start; this.end = end; } static merge(a, b) { const startACompare = SourcePosition.from(a.start).compareTo(b.start); const endACompare = SourcePosition.from(a.end).compareTo(b.end); const startRange = startACompare === CompareValue.LT ? a.start : startACompare === CompareValue.GT ? b.start : a.start; const endRange = endACompare === CompareValue.LT ? b.end : endACompare === CompareValue.GT ? a.end : b.end; return new SourceRange(startRange, endRange); } } export class SourceLocation { constructor(uri, range) { this.uri = uri; this.range = range; } static merge(a, b) { if (a.uri !== b.uri) { throw Error('Source location URI missmatch.'); } return new SourceLocation(a.uri, SourceRange.merge(a.range, b.range)); } } //# sourceMappingURL=location.js.map