Skip to content

Commit 9f20627

Browse files
fix parsing of range-like expressions
1 parent 3c4ee3c commit 9f20627

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

assembly/__tests__/range-quantifiers.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ it("handles quantifiers within alternates", () => {
2525
expectMatch("a{2}|b{2}", ["bb", "aa"]);
2626
expectNotMatch("a{2}|b{2}", ["cc"]);
2727
});
28+
29+
it("handles imcomplete quantifier ", () => {
30+
expectMatch("a{2", ["a{2"]);
31+
expectMatch("a{2,", ["a{2,"]);
32+
expectMatch("a{2,3", ["a{2,3"]);
33+
expectMatch("a{2,3a", ["a{2,3a"]);
34+
expectMatch("a{2,3a}", ["a{2,3a}"]);
35+
});

assembly/parser/parser.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class Parser {
136136
return new CharacterNode(this.eatToken());
137137
}
138138

139-
private maybeParseRepetitionRange(): Range {
139+
private maybeParseRepetitionRange(): Range | null {
140140
// snapshot
141141
const iteratorCopy = this.iterator.copy();
142142
this.eatToken(Char.LeftCurlyBrace);
@@ -191,7 +191,7 @@ export class Parser {
191191
// repetition not found - reset state
192192
this.iterator = iteratorCopy;
193193

194-
return range;
194+
return null;
195195
}
196196

197197
// parses a sequence of chars
@@ -213,12 +213,12 @@ export class Parser {
213213
// @ts-ignore
214214
} else if (token == Char.LeftCurlyBrace) {
215215
const range = this.maybeParseRepetitionRange();
216-
if (range.from == -1) {
217-
// this is not the start of a repetition, it's just a char!
218-
nodes.push(this.parseCharacter());
219-
} else {
216+
if (range != null) {
220217
const expression = nodes.pop();
221218
nodes.push(new RangeRepetitionNode(expression, range.from, range.to));
219+
} else {
220+
// this is not the start of a repetition, it's just a char!
221+
nodes.push(this.parseCharacter());
222222
}
223223
} else if (isQuantifier(token)) {
224224
const expression = nodes.pop();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"test": "npm run test:generate && npm run asbuild:untouched && npm run prettier && npm run eslint && npm run asp",
88
"test:generate": "node spec/test-generator.js",
9-
"asp": "asp --verbose",
10-
"asp:ci": "asp",
9+
"asp": "asp --verbose --nologo",
10+
"asp:ci": "asp --nologo",
1111
"prettier": "prettier --check .",
1212
"prettier:write": "prettier --write .",
1313
"eslint:write": "npm run eslint -- --fix ",

ts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ globalAny.log = console.log;
55

66
import { RegExp } from "../assembly/regexp";
77

8-
const regexObj = new RegExp("((a)(b)c)(d)");
9-
const match = regexObj.exec("abcd");
8+
const regexObj = new RegExp("a{3,");
9+
const match = regexObj.exec("a{3,");
1010

1111
console.log(match);

0 commit comments

Comments
 (0)