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

Fix regex termination lexer #623

Merged
Merged
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
16 changes: 15 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,23 @@ const parser = (() => {
var depth = 0;
var pattern;
var flags;

var isClosingSlash = function (position) {
if (path.charAt(position) === '/' && depth === 0) {
var backslashCount = 0;
while (path.charAt(position - (backslashCount + 1)) === '\\') {
backslashCount++;
}
if (backslashCount % 2 === 0) {
return true;
}
}
return false;
};

while (position < length) {
var currentChar = path.charAt(position);
if (currentChar === '/' && path.charAt(position - 1) !== '\\' && depth === 0) {
if (isClosingSlash(position)) {
// end of regex found
pattern = path.substring(start, position);
if (pattern === '') {
Expand Down
31 changes: 31 additions & 0 deletions test/implementation-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,38 @@ describe("Tests that are specific to a Javascript runtime", () => {
});
});

describe("empty regex: Escaped termination", function() {
it("should throw error", function() {
expect(function() {
var expr = jsonata("/\\/");
expr.evaluate();
})
.to.throw()
.to.deep.contain({ position: 3, code: "S0302" });
});
});

describe("empty regex: Escaped termination", function() {
it("should throw error", function() {
expect(function() {
var expr = jsonata("/\\\\\\/");
expr.evaluate();
})
.to.throw()
.to.deep.contain({ position: 5, code: "S0302" });
});
});

describe("Functions - $match", function() {
describe('$match("test escape \\\\", /\\\\/)', function() {
it("should find \\", async function() {
var expr = jsonata('$match("test escape \\\\", /\\\\/)');
var result = await expr.evaluate();
var expected = { match: "\\", index: 12, groups: []};
expect(result).to.deep.equal(expected);
});
});

describe('$match("ababbabbcc",/ab/)', function() {
it("should return result object", async function() {
var expr = jsonata('$match("ababbabbcc",/ab/)');
Expand Down