Skip to content

Commit

Permalink
Update Interval types
Browse files Browse the repository at this point in the history
  • Loading branch information
pdubroy committed Dec 19, 2022
1 parent f19f820 commit b7dd559
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 24 deletions.
42 changes: 31 additions & 11 deletions packages/ohm-js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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 {
Expand Down
42 changes: 31 additions & 11 deletions packages/ohm-js/scripts/data/index.d.ts.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions packages/ohm-js/src/Interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions packages/ohm-js/test/test-typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit b7dd559

Please sign in to comment.