1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00
anselme/parser/Source.lua
Étienne Reuh Fildadut 809613ef8b LuaJIT compatibility
Spoiler alert: Anselme run ~1.5x slower in LuaJIT than Lua 5.3/5.4. I didn't expected LuaJIT to be able to optimize anything with my super performant and cache friendly AST walker interpreter, but being this much slower is kinda impressive.
2023-12-28 17:37:57 +01:00

39 lines
856 B
Lua

local class = require("class")
local utf8 = utf8 or require("lua-utf8")
local Source
Source = class {
name = "?",
line = -1,
position = -1,
init = function(self, name, line, position)
self.name = name
self.line = line
self.position = position
end,
increment = function(self, n, ...)
self.position = self.position + n
end,
count = function(self, capture, ...)
self:increment(utf8.len(capture))
return capture, ...
end,
consume = function(self, capture, ...)
self:increment(utf8.len(capture))
return ...
end,
clone = function(self)
return Source:new(self.name, self.line, self.position)
end,
set = function(self, other)
self.name, self.line, self.position = other.name, other.line, other.position
end,
__tostring = function(self)
return ("%s:%s:%s"):format(self.name, self.line, self.position)
end
}
return Source