Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds lookbehind support #2049

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/chevrotain/src/scan/reg_exp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class CharCodeFinder extends BaseRegExpVisitor {
return;
}

// switch lookaheads as they do not actually consume any characters thus
// switch lookaheads / lookbehinds as they do not actually consume any characters thus
// finding a charCode at lookahead context does not mean that regexp can actually contain it in a match.
switch (node.type) {
case "Lookahead":
Expand All @@ -272,7 +272,13 @@ class CharCodeFinder extends BaseRegExpVisitor {
case "NegativeLookahead":
this.visitNegativeLookahead(node);
return;
}
case "Lookbehind":
this.visitLookbehind(node);
return;
case "NegativeLookbehind":
this.visitNegativeLookbehind(node);
return;
}

super.visitChildren(node);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/regexp-to-ast/src/base-regexp-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export class BaseRegExpVisitor {
case "NegativeLookahead":
this.visitNegativeLookahead(node);
break;
case "Lookbehind":
this.visitLookbehind(node);
break;
case "NegativeLookbehind":
this.visitNegativeLookbehind(node);
break;
case "Character":
this.visitCharacter(node);
break;
Expand Down Expand Up @@ -103,6 +109,10 @@ export class BaseRegExpVisitor {

public visitNegativeLookahead(node: Assertion): void {}

public visitLookbehind(node: Assertion): void {}

public visitNegativeLookbehind(node: Assertion): void {}

// atoms
public visitCharacter(node: Character): void {}

Expand Down
91 changes: 91 additions & 0 deletions packages/regexp-to-ast/test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,97 @@ describe("The RegExp to Ast parser", () => {
});
});

it("lookbehind assertion", () => {
const ast = parser.pattern("/a(?<=b)/");
expect(ast.value).to.deep.equal({
type: "Disjunction",
loc: { begin: 1, end: 7 },
value: [
{
type: "Alternative",
loc: { begin: 1, end: 7 },
value: [
{
type: "Character",
loc: { begin: 1, end: 2 },
value: 97,
},
{
type: "Lookbehind",
loc: { begin: 2, end: 7 },
value: {
type: "Disjunction",
loc: { begin: 5, end: 6 },
value: [
{
type: "Alternative",
loc: { begin: 5, end: 6 },
value: [
{
type: "Character",
loc: {
begin: 5,
end: 6,
},
value: 98,
},
],
},
],
},
},
],
},
],
});
});

it("lookbehind assertion", () => {
const ast = parser.pattern("/a(?<!b)/");
expect(ast.value).to.deep.equal({
type: "Disjunction",
loc: { begin: 1, end: 7 },
value: [
{
type: "Alternative",
loc: { begin: 1, end: 7 },
value: [
{
type: "Character",
loc: { begin: 1, end: 2 },
value: 97,
},
{
type: "NegativeLookbehind",
loc: { begin: 2, end: 7 },
value: {
type: "Disjunction",
loc: { begin: 5, end: 6 },
value: [
{
type: "Alternative",
loc: { begin: 5, end: 6 },
value: [
{
type: "Character",
loc: {
begin: 5,
end: 6,
},
value: 98,
},
],
},
],
},
},
],
},
],
});
});
});

context("quantifiers", () => {
it("zero or one", () => {
const ast = parser.pattern("/a?/");
Expand Down
25 changes: 25 additions & 0 deletions packages/regexp-to-ast/test/visitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ describe("The regexp AST visitor", () => {
new NegativeLookaheadVisitor().visit(ast);
});


it("Can visit Lookbehind", () => {
const ast = parser.pattern("/a(?<=a|b)/");
class LookbehindVisitor extends BaseRegExpVisitor {
visitLookbehind(node: Assertion) {
super.visitLookbehind(node);
expect(node.value?.value).to.have.lengthOf(2);
}
}

new LookbehindVisitor().visit(ast);
});

it("Can visit NegativeLookbehind", () => {
const ast = parser.pattern("/a(?<!a|b|c)/");
class NegativeLookbehindVisitor extends BaseRegExpVisitor {
visitNegativeLookbehind(node: Assertion) {
super.visitNegativeLookbehind(node);
expect(node.value!.value).to.have.lengthOf(3);
}
}

new NegativeLookbehindVisitor().visit(ast);
});

it("Can visit Character", () => {
const ast = parser.pattern("/a/");
class CharacterVisitor extends BaseRegExpVisitor {
Expand Down
6 changes: 5 additions & 1 deletion packages/regexp-to-ast/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export interface Assertion extends IRegExpAST {
| "WordBoundary"
| "NonWordBoundary"
| "Lookahead"
| "NegativeLookahead";
| "NegativeLookahead"
| "Lookbehind"
| "NegativeLookbehind";

value?: Disjunction;
}
Expand Down Expand Up @@ -126,6 +128,8 @@ export class BaseRegExpVisitor {
visitNonWordBoundary(node: Assertion): void;
visitLookahead(node: Assertion): void;
visitNegativeLookahead(node: Assertion): void;
visitLookbehind(node: Assertion): void;
visitNegativeLookbehind(node: Assertion): void;
visitCharacter(node: Character): void;
visitSet(node: Set): void;
visitGroup(Node: Group): void;
Expand Down
Loading