1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

[internal] normalize newlines and bom

This commit is contained in:
Étienne Fildadut 2024-01-19 14:37:06 +01:00
parent 7e29fb87b4
commit cee00eb13d

View file

@ -9,8 +9,16 @@ local function expect_end(exp, rem)
return exp return exp
end end
-- we require UTF-8 but life is full of disapointments
-- remove BOM
-- \r\n and \r -> \n
local function normalize_encoding(str)
return str:gsub("^"..string.char(0xEF, 0xBB, 0xBF), "")
:gsub("\r\n?", "\n")
end
-- parse code (string) with the associated source (Source) -- parse code (string) with the associated source (Source)
-- the returned AST tree is stateless and can be stored/evaluated/etc as you please -- the returned AST tree is stateless and can be stored/evaluated/etc as you please
return function(code, source) return function(code, source)
return expect_end(block(Source:new(source, 1, 1), Options:new(), code)) return expect_end(block(Source:new(source, 1, 1), Options:new(), normalize_encoding(code)))
end end