Skip to content

Commit 0ace1a3

Browse files
better fix
1 parent 8af2dfb commit 0ace1a3

4 files changed

Lines changed: 393 additions & 143 deletions

File tree

grammar.pegjs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
}
1515
});
1616
}
17-
18-
// https://github.com/estools/esquery/issues/68
19-
// Inside all /regexp/ literals, we replace escaped-backslashes with the \x2F equivalent.
20-
input = input.replaceAll(/\/((?:[^\/\\]|\\.)*?)\//g, (match) => {
21-
return match.replaceAll("\\/", "\\\\x2F");
22-
});
2317
}
2418

2519
start
@@ -103,13 +97,18 @@ attr
10397
path = i:identifierName { return { type: 'literal', value: i }; }
10498
type = "type(" _ t:[^ )]+ _ ")" { return { type: 'type', value: t.join('') }; }
10599
flags = [imsu]+
106-
regex = "/" d:[^/]+ "/" flgs:flags? {
107-
// https://github.com/estools/esquery/issues/68
108-
const text = d.join('').replaceAll("\\\\x2F", "\\/");
100+
regex = "/" pattern:(regex_cc / regex_hex_escape / regex_single_char_escape / regex_chars)+ "/" flgs:flags? {
109101
return {
110-
type: 'regexp', value: new RegExp(text, flgs ? flgs.join('') : '')
102+
type: 'regexp', value: new RegExp(pattern.join(''), flgs ? flgs.join('') : '')
111103
};
112104
}
105+
regex_cc = "[" cs:([^\]\\] / regex_hex_escape / regex_single_char_escape)+ "]" { return '[' + cs.join('') + ']'; }
106+
regex_hex_escape = "\\x" a:[A-Fa-f0-9] b:[A-Fa-f0-9] { return '\\x' + a + b; }
107+
regex_single_char_escape = "\\" a:. {
108+
let hex = a.charCodeAt(0).toString(16);
109+
return '\\x' + (hex.length > 1 ? hex : '0' + hex) ;
110+
}
111+
regex_chars = cs:[^/\\\[]+ { return cs.join(''); }
113112

114113
field = "." i:identifierName is:("." identifierName)* {
115114
return { type: 'field', name: is.reduce(function(memo, p){ return memo + p[0] + p[1]; }, i)};

0 commit comments

Comments
 (0)