UNPKG

@composita/source-location

Version:

Composita language source location.

51 lines 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SourceLocation = exports.SourceRange = exports.SourcePosition = void 0; const ts_utility_types_1 = require("@composita/ts-utility-types"); 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 ts_utility_types_1.CompareValue.LT; } if (this.line > other.line || (this.line === other.line && this.character > other.character)) { return ts_utility_types_1.CompareValue.GT; } return ts_utility_types_1.CompareValue.EQ; } } exports.SourcePosition = SourcePosition; 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 === ts_utility_types_1.CompareValue.LT ? a.start : startACompare === ts_utility_types_1.CompareValue.GT ? b.start : a.start; const endRange = endACompare === ts_utility_types_1.CompareValue.LT ? b.end : endACompare === ts_utility_types_1.CompareValue.GT ? a.end : b.end; return new SourceRange(startRange, endRange); } } exports.SourceRange = SourceRange; 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)); } } exports.SourceLocation = SourceLocation; //# sourceMappingURL=location.js.map