Created
July 2, 2023 18:24
-
-
Save Myvar/2684ba4fb86b975274629d6f21eddc7b to your computer and use it in GitHub Desktop.
Zig side of js lexer testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const print = std.debug.print; | |
const MAX_READ_BUFFER = 4294967295; | |
pub fn readFile(alloc: std.mem.Allocator, path: []const u8) ![:0]u8 { | |
const file = try std.fs.cwd().openFile(path, .{}); | |
defer file.close(); | |
const source_code = file.readToEndAllocOptions( | |
alloc, | |
MAX_READ_BUFFER, | |
null, | |
@alignOf(u16), | |
0, | |
); | |
return source_code; | |
} | |
pub fn main() !void { | |
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
const alloc = gpa.allocator(); | |
const args = try std.process.argsAlloc(alloc); | |
const source = try readFile(alloc, args[1]); | |
//\\const std = @import("std"); | |
// ; | |
defer alloc.free(source); | |
//var toks = std.ArrayList(std.zig.Token).init(alloc); | |
//defer toks.deinit(); | |
var tokenizer = std.zig.Tokenizer.init(source); | |
var should_stop = false; | |
const stdout = std.io.getStdOut().writer(); //needed for stdout | |
try stdout.print("[", .{}); | |
//var toks = std.ArrayList(std.zig.Token); | |
while (!should_stop) { | |
const token = tokenizer.next(); | |
// print("{s} \"{s}\"\n", .{ @tagName(token.tag), source[token.loc.start..token.loc.end] }); | |
//try toks.append(token); | |
if (token.tag == .eof) { | |
should_stop = true; | |
try stdout.print("{{\"tag\": \"{s}\", \"loc\": {{\"start\": {}, \"end\": {}}}}}", .{ @tagName(token.tag), token.loc.start, token.loc.end }); | |
} else { | |
try stdout.print("{{\"tag\": \"{s}\", \"loc\": {{\"start\": {}, \"end\": {}}}}},", .{ @tagName(token.tag), token.loc.start, token.loc.end }); | |
} | |
} | |
try stdout.print("]\n", .{}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment