From b7dd559a72c390c2c53980ef0695216ac684f3de Mon Sep 17 00:00:00 2001 From: Patrick Dubroy Date: Mon, 19 Dec 2022 13:14:49 +0100 Subject: [PATCH] Update Interval types --- packages/ohm-js/index.d.ts | 42 ++++++++++++++----- .../ohm-js/scripts/data/index.d.ts.template | 42 ++++++++++++++----- packages/ohm-js/src/Interval.js | 3 +- packages/ohm-js/test/test-typings.ts | 23 ++++++++++ 4 files changed, 86 insertions(+), 24 deletions(-) diff --git a/packages/ohm-js/index.d.ts b/packages/ohm-js/index.d.ts index d7167d1d..8155694c 100644 --- a/packages/ohm-js/index.d.ts +++ b/packages/ohm-js/index.d.ts @@ -394,22 +394,24 @@ declare namespace ohm { interface TerminalNode extends Node {} /** - * Interval in input string + * An Interval represents a subset of a string. */ interface Interval { /** - * Input stream of parse + * The string containing the interval. */ - inputStream: any; + sourceString: string; /** - * Starting index in input + * The start index of the interval in `sourceString`. */ startIdx: number; + /** - * Ending index in input + * The end index of the interval in `sourceString`. */ endIdx: number; + /** * Contents of interval */ @@ -425,12 +427,6 @@ declare namespace ohm { */ collapsedRight(): Interval; - /** - * Returns a new Interval which contains the same contents as this one, - * but with whitespace trimmed from both ends. - */ - trimmed(): Interval; - /** * Returns a new Interval that covers this Interval and all the * argument Intervals. The new Interval will start at the lowest start @@ -448,6 +444,30 @@ declare namespace ohm { * interval in the source string, including the line and column number. */ getLineAndColumn(): LineAndColumnInfo; + + /** + * Returns an array of 0, 1, or 2 intervals that represents the result of the + * interval difference operation. + */ + minus(that: Interval): Interval[]; + + /** + * Returns a new Interval that has the same extent as this one, but which is relative + * to `that`, an Interval that fully covers this one. + */ + relativeTo(that: Interval): Interval; + + /** + * Returns a new Interval which contains the same contents as this one, + * but with whitespace trimmed from both ends. + */ + trimmed(): Interval; + + /** + * Returns a new Interval on the same source string, with the given length + * and beginning at `startIdx + offset`. + */ + subInterval(offset: number, len: number): Interval; } interface RuleInfo { diff --git a/packages/ohm-js/scripts/data/index.d.ts.template b/packages/ohm-js/scripts/data/index.d.ts.template index 73a2c945..665af0d7 100644 --- a/packages/ohm-js/scripts/data/index.d.ts.template +++ b/packages/ohm-js/scripts/data/index.d.ts.template @@ -372,22 +372,24 @@ declare namespace ohm { interface TerminalNode extends Node {} /** - * Interval in input string + * An Interval represents a subset of a string. */ interface Interval { /** - * Input stream of parse + * The string containing the interval. */ - inputStream: any; + sourceString: string; /** - * Starting index in input + * The start index of the interval in `sourceString`. */ startIdx: number; + /** - * Ending index in input + * The end index of the interval in `sourceString`. */ endIdx: number; + /** * Contents of interval */ @@ -403,12 +405,6 @@ declare namespace ohm { */ collapsedRight(): Interval; - /** - * Returns a new Interval which contains the same contents as this one, - * but with whitespace trimmed from both ends. - */ - trimmed(): Interval; - /** * Returns a new Interval that covers this Interval and all the * argument Intervals. The new Interval will start at the lowest start @@ -426,6 +422,30 @@ declare namespace ohm { * interval in the source string, including the line and column number. */ getLineAndColumn(): LineAndColumnInfo; + + /** + * Returns an array of 0, 1, or 2 intervals that represents the result of the + * interval difference operation. + */ + minus(that: Interval): Interval[]; + + /** + * Returns a new Interval that has the same extent as this one, but which is relative + * to `that`, an Interval that fully covers this one. + */ + relativeTo(that: Interval): Interval; + + /** + * Returns a new Interval which contains the same contents as this one, + * but with whitespace trimmed from both ends. + */ + trimmed(): Interval; + + /** + * Returns a new Interval on the same source string, with the given length + * and beginning at `startIdx + offset`. + */ + subInterval(offset: number, len: number): Interval; } interface RuleInfo { diff --git a/packages/ohm-js/src/Interval.js b/packages/ohm-js/src/Interval.js index 2f93bf5f..56f8a240 100644 --- a/packages/ohm-js/src/Interval.js +++ b/packages/ohm-js/src/Interval.js @@ -97,8 +97,7 @@ Interval.prototype = { }, // Returns a new Interval which contains the same contents as this one, - // but with whitespace trimmed from both ends. (This only makes sense when - // the input stream is a string.) + // but with whitespace trimmed from both ends. trimmed() { const {contents} = this; const startIdx = this.startIdx + contents.match(/^\s*/)[0].length; diff --git a/packages/ohm-js/test/test-typings.ts b/packages/ohm-js/test/test-typings.ts index c71cb6cd..9a1f2abc 100644 --- a/packages/ohm-js/test/test-typings.ts +++ b/packages/ohm-js/test/test-typings.ts @@ -74,3 +74,26 @@ test('asIteration - #407', t => { }); t.is(s(g.match('abc')).x(), true); }); + +test('Interval typings', t => { + const inputStr = ' Sup friend! '; + const matchFailure = g.match(inputStr); + t.true(matchFailure.failed()); + + const interval = matchFailure.getInterval(); + interval.startIdx = 1; + interval.endIdx = inputStr.length; + t.is(interval.sourceString, ' Sup friend! '); + t.is(interval.contents, 'Sup friend! '); + t.is(interval.trimmed().contents, 'Sup friend!'); + + const left = interval.collapsedLeft(); + const right = interval.collapsedRight(); + t.is(left.startIdx, interval.startIdx); + t.is(right.startIdx, interval.endIdx); + + const fat = interval.minus(interval.trimmed()); + t.is(fat.length, 1); + t.is(fat[0].contents, ' '); + t.is(fat[0].relativeTo(interval).startIdx, 11); +});