diff options
author | 2021-04-18 21:11:48 -0700 | |
---|---|---|
committer | 2021-04-18 21:11:48 -0700 | |
commit | c6b137b4add937cb9f771f63b8b189b233c66d78 (patch) | |
tree | 00457020420d0df3b8f7cda075a8eb0793d22415 | |
parent | 97c6c4b036d61ad5a42dff52340a6eb44dd8c855 (diff) | |
download | bun-c6b137b4add937cb9f771f63b8b189b233c66d78.tar.gz bun-c6b137b4add937cb9f771f63b8b189b233c66d78.tar.zst bun-c6b137b4add937cb9f771f63b8b189b233c66d78.zip |
wip
-rw-r--r-- | src/js_lexer.zig | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/src/js_lexer.zig b/src/js_lexer.zig index 4901d8153..b54ff2d81 100644 --- a/src/js_lexer.zig +++ b/src/js_lexer.zig @@ -312,6 +312,241 @@ pub const Lexer = struct { } }, + '&' => { + // '&' or '&=' or '&&' or '&&=' + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_ampersand_equals; + }, + + '&' => { + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_ampersand_ampersand_equals; + }, + + else => { + lexer.token = T.t_ampersand_ampersand; + }, + } + }, + else => { + lexer.token = T.t_ampersand; + }, + } + }, + + '|' => { + + // '|' or '|=' or '||' or '||=' + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_bar_equals; + }, + '|' => { + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_bar_bar_equals; + }, + + else => { + lexer.token = T.t_bar_bar; + }, + } + }, + else => { + lexer.token = T.t_bar; + }, + } + }, + + '^' => { + // '^' or '^=' + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_caret_equals; + }, + + else => { + lexer.token = T.t_caret; + }, + } + }, + + '+' => { + // '+' or '+=' or '++' + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_plus_equals; + }, + + '+' => { + lexer.step(); + lexer.token = T.t_plus_plus; + }, + + else => { + lexer.token = T.t_plus; + }, + } + }, + + '=' => { + // '=' or '=>' or '==' or '===' + lexer.step(); + switch (lexer.code_point) { + '>' => { + lexer.step(); + lexer.token = T.t_equals_greater_than; + }, + + '=' => { + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_equals_equals_equals; + }, + + else => { + lexer.token = T.t_equals_equals; + }, + } + }, + + else => { + lexer.token = T.t_equals; + }, + } + }, + + '<' => { + // '<' or '<<' or '<=' or '<<=' or '<!--' + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_less_than_equals; + }, + + '<' => { + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_less_than_less_than_equals; + }, + + else => { + lexer.token = T.t_less_than_less_than; + }, + } + }, + // Handle legacy HTML-style comments + '!' => { + if (std.mem.eql(u8, lexer.peek("--".len), "--")) { + lexer.addUnsupportedSyntaxError("Legacy HTML comments not implemented yet!"); + return; + } + + lexer.token = T.t_less_than; + }, + + else => { + lexer.token = T.t_less_than; + }, + } + }, + + '>' => { + // '>' or '>>' or '>>>' or '>=' or '>>=' or '>>>=' + lexer.step(); + + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_greater_than_equals; + }, + '>' => { + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_greater_than_greater_than_equals; + }, + '>' => { + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.token = T.t_greater_than_greater_than_greater_than_equals; + }, + else => { + lexer.token = T.t_greater_than_greater_than_greater_than; + }, + } + }, + else => { + lexer.token = T.t_greater_than_greater_than; + }, + } + }, + else => { + lexer.token = T.t_greater_than; + }, + } + }, + + '!' => { + // '!' or '!=' or '!==' + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + switch (lexer.code_point) { + '=' => { + lexer.step(); + lexer.Token = TExclamationEqualsEquals; + }, + + else => { + lexer.Token = TExclamationEquals; + }, + } + }, + else => { + lexer.Token = TExclamation; + }, + } + }, + + '\'', '"', '`' => { + const quote = lexer.code_point; + var needsSlowPath = false; + var suffixLen = 1; + + if (quote != '`') { + lexer.token = T.t_string_literal; + } else if (lexer.rescan_close_brace_as_template_token) { + lexer.token = T.t_template_tail; + } else { + lexer.token = T.t_no_substitution_template_literal; + } + lexer.step(); + }, + else => { // Check for unusual whitespace characters if (isWhitespace(lexer.code_point)) { @@ -446,6 +681,20 @@ fn isWhitespace(codepoint: CodePoint) bool { } } +test "Lexer.next()" { + const msgs = std.ArrayList(logger.Msg).init(std.testing.allocator); + const log = logger.Log{ + .msgs = msgs, + }; + + defer std.testing.allocator.free(msgs.items); + const source = logger.Source.initPathString("index.js", "for (let i = 0; i < 100; i++) { console.log('hi'); }", std.heap.page_allocator); + + var lex = try Lexer.init(log, source, std.testing.allocator); + defer lex.string_literal.shrinkAndFree(0); + lex.next(); +} + test "Lexer.step()" { const msgs = std.ArrayList(logger.Msg).init(std.testing.allocator); const log = logger.Log{ |