Skip to content

decode regex if needed #5167

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

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions src/js_ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,10 @@ pub const E = struct {
};

pub const RegExp = struct {
value: string,
data: union(enum) {
raw: string,
decoded: std.ArrayList(u16),
},

// This exists for JavaScript bindings
// The RegExp constructor expects flags as a second argument.
Expand All @@ -2548,7 +2551,7 @@ pub const E = struct {
// ^
flags_offset: ?u16 = null,

pub var empty = RegExp{ .value = "" };
pub var empty = RegExp{ .data = .{ .raw = "" } };

pub fn pattern(this: RegExp) string {

Expand Down
61 changes: 35 additions & 26 deletions src/js_lexer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn NewLexer_(
this.comments_to_preserve_before.clearAndFree();
}

fn decodeEscapeSequences(lexer: *LexerType, start: usize, text: string, comptime BufType: type, buf_: *BufType) !void {
pub fn decodeEscapeSequences(lexer: *LexerType, start: usize, text: string, comptime BufType: type, buf_: *BufType) !void {
var buf = buf_.*;
defer buf_.* = buf;
if (comptime is_json) lexer.is_ascii_only = false;
Expand Down Expand Up @@ -2075,9 +2075,11 @@ fn NewLexer_(
if (comptime is_json) unreachable;
}

pub fn scanRegExp(lexer: *LexerType) !void {
// returns true of the regex contents need to be decoded
pub fn scanRegExp(lexer: *LexerType) !bool {
lexer.assertNotJSON();
lexer.regex_flags_start = null;
var decode = lexer.code_point >= 0x80;
while (true) {
switch (lexer.code_point) {
'/' => {
Expand Down Expand Up @@ -2121,20 +2123,48 @@ fn NewLexer_(
},
}
}
return;

return decode;
},
'[' => {
lexer.step();
if (lexer.code_point >= 0x80) decode = true;
while (lexer.code_point != ']') {
try lexer.scanRegExpValidateAndStep();
try lexer.scanRegExpValidateAndStep(&decode);
}
lexer.step();
if (lexer.code_point >= 0x80) decode = true;
},
else => {
try lexer.scanRegExpValidateAndStep();
try lexer.scanRegExpValidateAndStep(&decode);
},
}
}

return decode;
}

fn scanRegExpValidateAndStep(lexer: *LexerType, decode: *bool) !void {
lexer.assertNotJSON();

if (lexer.code_point == '\\') {
lexer.step();
if (lexer.code_point >= 0x80) decode.* = true;
}

switch (lexer.code_point) {
'\r', '\n', 0x2028, 0x2029 => {
// Newlines aren't allowed in regular expressions
try lexer.syntaxError();
},
-1 => { // EOF
try lexer.syntaxError();
},
else => {
lexer.step();
if (lexer.code_point >= 0x80) decode.* = true;
},
}
}

// TODO: use wtf-8 encoding.
Expand Down Expand Up @@ -2592,27 +2622,6 @@ fn NewLexer_(
try lexer.nextInsideJSXElement();
}

fn scanRegExpValidateAndStep(lexer: *LexerType) !void {
lexer.assertNotJSON();

if (lexer.code_point == '\\') {
lexer.step();
}

switch (lexer.code_point) {
'\r', '\n', 0x2028, 0x2029 => {
// Newlines aren't allowed in regular expressions
try lexer.syntaxError();
},
-1 => { // EOF
try lexer.syntaxError();
},
else => {
lexer.step();
},
}
}

pub fn rescanCloseBraceAsTemplateToken(lexer: *LexerType) !void {
lexer.assertNotJSON();

Expand Down
32 changes: 29 additions & 3 deletions src/js_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13095,13 +13095,39 @@ fn NewParser_(
return p.newExpr(E.BigInt{ .value = value }, loc);
},
.t_slash, .t_slash_equals => {
try p.lexer.scanRegExp();
const needs_decode = try p.lexer.scanRegExp();
// always set regex_flags_start to null to make sure we don't accidentally use the wrong value later
defer p.lexer.regex_flags_start = null;
const value = p.lexer.raw();

const raw = p.lexer.raw();

if (!needs_decode) {
try p.lexer.next();
return p.newExpr(
E.RegExp{
.data = .{
.raw = raw,
},
.flags_offset = p.lexer.regex_flags_start,
},
loc,
);
}

var buf = std.ArrayList(u16).initCapacity(p.allocator, raw.len) catch unreachable;
try p.lexer.decodeEscapeSequences(p.lexer.start, raw, @TypeOf(buf), &buf);

try p.lexer.next();

return p.newExpr(E.RegExp{ .value = value, .flags_offset = p.lexer.regex_flags_start }, loc);
return p.newExpr(
E.RegExp{
.data = .{
.decoded = buf,
},
.flags_offset = p.lexer.regex_flags_start,
},
loc,
);
},
.t_void => {
try p.lexer.next();
Expand Down
Loading