-- codecs/text/Lexer tests: token kinds, comments, escapes, numbers, errors, and a no-throw pass. local T = require("@tests/T") local Lexer = require("@rosetta/codecs/text/Lexer") local t = T.suite("lexer") local function all(s: string): ({ string }, string?) local L = Lexer.new(s) local out = {} while true do local k, v = L:next() if not k then break end table.insert(out, k .. ":" .. tostring(v)) end return out, L.err end local got = all('Version 2 // c\n{ "a \\"b\\"" ( -0.5 25e-2 0x10 ) /* x\n y */ textures/base_wall/lfwall13f3 a=b; }') t:eq(table.concat(got, " "), 'word:Version number:2 punct:{ string:a "b" punct:( number:-0.5 number:0.25 number:16 punct:) word:textures/base_wall/lfwall13f3 word:a punct:= word:b punct:; punct:}', "tokens") local L = Lexer.new("a // tail") t:eq(select(2, L:peek()), "a", "peek") t:eq(select(2, L:next()), "a", "next after peek") t:eq(L:next(), nil, "end after a trailing comment") t:eq(table.concat((all("x/*y*/z")), " "), "word:x word:z", "a comment ends a word") t:eq(table.concat((all("- inf 1.2.3")), " "), "word:- word:inf word:1.2.3", "not numbers") t:eq(select(2, all('"open')), "unterminated string", "unterminated string") t:eq(select(2, all("/* open")), "unterminated comment", "unterminated comment") local L2 = Lexer.new("a\nb\nc") L2:next() L2:next() t:eq(L2:line(), 2, "line") local rand = T.rng(5) local alphabet = '{}()[];=,"\\/* \n\t0123456789.-+exabc_' t:fuzz("tokens", function(b: buffer) local L3 = Lexer.new(buffer.tostring(b)) for _ = 1, 10000 do if not L3:next() then break end end return true end, (function() local out = {} for _ = 1, 50 do local s = {} for _ = 1, 1 + rand(80) do local i = 1 + rand(#alphabet) table.insert(s, string.sub(alphabet, i, i)) end table.insert(out, buffer.fromstring(table.concat(s))) end return out end)()) t:done()