diff options
Diffstat (limited to 'src/js_lexer.zig')
-rw-r--r-- | src/js_lexer.zig | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/src/js_lexer.zig b/src/js_lexer.zig index 92acc0dd1..5191d7095 100644 --- a/src/js_lexer.zig +++ b/src/js_lexer.zig @@ -4,8 +4,19 @@ const tables = @import("js_lexer_tables.zig"); const build_options = @import("build_options"); const js_ast = @import("js_ast.zig"); -usingnamespace @import("ast/base.zig"); -usingnamespace @import("global.zig"); +const _global = @import("global.zig"); +const string = _global.string; +const Output = _global.Output; +const Global = _global.Global; +const Environment = _global.Environment; +const strings = _global.strings; +const CodePoint = _global.CodePoint; +const MutableString = _global.MutableString; +const stringZ = _global.stringZ; +const default_allocator = _global.default_allocator; +const C = _global.C; +const FeatureFlags = @import("feature_flags.zig"); +const JavascriptString = []const u16; const unicode = std.unicode; @@ -66,7 +77,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { // err: ?LexerType.Error, log: *logger.Log, - source: *const logger.Source, + source: logger.Source, current: usize = 0, start: usize = 0, end: usize = 0, @@ -90,7 +101,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { rescan_close_brace_as_template_token: bool = false, prev_error_loc: logger.Loc = logger.Loc.Empty, regex_flags_start: ?u16 = null, - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, /// In JavaScript, strings are stored as UTF-16, but nearly every string is ascii. /// This means, usually, we can skip UTF8 -> UTF16 conversions. string_literal_buffer: std.ArrayList(u16), @@ -162,7 +173,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { return Error.SyntaxError; } - pub fn addError(self: *LexerType, _loc: usize, comptime format: []const u8, args: anytype, panic: bool) void { + pub fn addError(self: *LexerType, _loc: usize, comptime format: []const u8, args: anytype, _: bool) void { @setCold(true); if (self.is_log_disabled) return; @@ -171,11 +182,11 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { return; } - self.log.addErrorFmt(self.source, __loc, self.allocator, format, args) catch unreachable; + self.log.addErrorFmt(&self.source, __loc, self.allocator, format, args) catch unreachable; self.prev_error_loc = __loc; } - pub fn addRangeError(self: *LexerType, r: logger.Range, comptime format: []const u8, args: anytype, panic: bool) !void { + pub fn addRangeError(self: *LexerType, r: logger.Range, comptime format: []const u8, args: anytype, _: bool) !void { @setCold(true); if (self.is_log_disabled) return; @@ -184,7 +195,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { } const errorMessage = std.fmt.allocPrint(self.allocator, format, args) catch unreachable; - var msg = self.log.addRangeError(self.source, r, errorMessage); + try self.log.addRangeError(&self.source, r, errorMessage); self.prev_error_loc = r.loc; // if (panic) { @@ -213,7 +224,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { return @enumToInt(lexer.token) >= @enumToInt(T.t_identifier); } - pub fn deinit(this: *LexerType) void {} + pub fn deinit(_: *LexerType) void {} fn decodeEscapeSequences(lexer: *LexerType, start: usize, text: string, comptime BufType: type, buf_: *BufType) !void { var buf = buf_.*; @@ -222,7 +233,6 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { const iterator = strings.CodepointIterator{ .bytes = text[start..], .i = 0 }; var iter = strings.CodepointIterator.Cursor{}; - const start_length = buf.items.len; while (iterator.next(&iter)) { const width = iter.width; switch (iter.c) { @@ -309,7 +319,6 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { }; const c3: CodePoint = iter.c; - const width3 = iter.width; switch (c3) { '0'...'7' => { @@ -318,7 +327,6 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { _ = iterator.next(&iter) or return lexer.syntaxError(); const c4 = iter.c; - const width4 = iter.width; switch (c4) { '0'...'7' => { const temp = value * 8 + c4 - '0'; @@ -923,7 +931,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { pub fn expectContextualKeyword(self: *LexerType, comptime keyword: string) !void { if (!self.isContextualKeyword(keyword)) { - if (std.builtin.mode == std.builtin.Mode.Debug) { + if (@import("builtin").mode == std.builtin.Mode.Debug) { self.addError(self.start, "Expected \"{s}\" but found \"{s}\" (token: {s})", .{ keyword, self.raw(), @@ -1289,7 +1297,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { if (lexer.code_point == '>' and lexer.has_newline_before) { lexer.step(); - lexer.log.addRangeWarning(lexer.source, lexer.range(), "Treating \"-->\" as the start of a legacy HTML single-line comment") catch unreachable; + lexer.log.addRangeWarning(&lexer.source, lexer.range(), "Treating \"-->\" as the start of a legacy HTML single-line comment") catch unreachable; singleLineHTMLCloseComment: while (true) { switch (lexer.code_point) { @@ -1692,7 +1700,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { // TODO: implement this // it's too complicated to handle all the edgecases right now given the state of Zig's standard library - pub fn removeMultilineCommentIndent(lexer: *LexerType, _prefix: string, text: string) string { + pub fn removeMultilineCommentIndent(_: *LexerType, _: string, text: string) string { return text; } @@ -1703,7 +1711,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { }; } - pub fn initTSConfig(log: *logger.Log, source: *const logger.Source, allocator: *std.mem.Allocator) !LexerType { + pub fn initTSConfig(log: *logger.Log, source: logger.Source, allocator: std.mem.Allocator) !LexerType { var empty_string_literal: JavascriptString = &emptyJavaScriptString; var lex = LexerType{ .log = log, @@ -1721,7 +1729,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { return lex; } - pub fn initJSON(log: *logger.Log, source: *const logger.Source, allocator: *std.mem.Allocator) !LexerType { + pub fn initJSON(log: *logger.Log, source: logger.Source, allocator: std.mem.Allocator) !LexerType { var empty_string_literal: JavascriptString = &emptyJavaScriptString; var lex = LexerType{ .log = log, @@ -1738,7 +1746,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { return lex; } - pub fn init(log: *logger.Log, source: *const logger.Source, allocator: *std.mem.Allocator) !LexerType { + pub fn init(log: *logger.Log, source: logger.Source, allocator: std.mem.Allocator) !LexerType { try tables.initJSXEntityMap(); var empty_string_literal: JavascriptString = &emptyJavaScriptString; var lex = LexerType{ @@ -1897,7 +1905,6 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { }, '*' => { lexer.step(); - const start_range = lexer.range(); multi_line_comment: { while (true) { switch (lexer.code_point) { @@ -2175,7 +2182,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { return decoded.items; } - inline fn maybeDecodeJSXEntity(lexer: *LexerType, text: string, out: *std.ArrayList(u16), cursor: *strings.CodepointIterator.Cursor) void { + inline fn maybeDecodeJSXEntity(lexer: *LexerType, text: string, cursor: *strings.CodepointIterator.Cursor) void { if (strings.indexOfChar(text[cursor.width + cursor.i ..], ';')) |length| { const end = cursor.width + cursor.i; const entity = text[end .. end + length]; @@ -2214,7 +2221,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { var cursor = strings.CodepointIterator.Cursor{}; while (iterator.next(&cursor)) { - if (cursor.c == '&') lexer.maybeDecodeJSXEntity(text, out, &cursor); + if (cursor.c == '&') lexer.maybeDecodeJSXEntity(text, &cursor); if (cursor.c <= 0xFFFF) { try out.append(@intCast(u16, cursor.c)); @@ -2480,7 +2487,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { } else if (isInvalidLegacyOctalLiteral) { if (std.fmt.parseFloat(f64, text)) |num| { lexer.number = num; - } else |err| { + } else |_| { try lexer.addSyntaxError(lexer.start, "Invalid number {s}", .{text}); } } @@ -2591,7 +2598,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { } } text = bytes; - } else |err| { + } else |_| { try lexer.addSyntaxError(lexer.start, "Out of Memory Wah Wah Wah", .{}); return; } @@ -2616,7 +2623,7 @@ pub fn NewLexer(comptime json_options: JSONOptions) type { // Parse a double-precision floating-point number; if (std.fmt.parseFloat(f64, text)) |num| { lexer.number = num; - } else |err| { + } else |_| { try lexer.addSyntaxError(lexer.start, "Invalid number", .{}); } } |