~technomancy/antifennel

ref: c436ed1418acd1cecf63db41326617101688afa1 antifennel/lang/reader.lua -rw-r--r-- 620 bytes
c436ed14Phil Hagelberg Avoid conflicts with reserved fennel names in identifiers. 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
local strsub = string.sub

local function new_string_reader(src)
    local pos = 1
    local function reader()
        local chunk = strsub(src, pos, pos + 4096 - 32)
        pos = pos + #chunk
        return #chunk > 0 and chunk or nil
    end
    return reader
end

local function new_file_reader(filename)
    local f
    if filename then
        f = assert(io.open(filename, 'r'), "cannot open file " .. filename)
    else
        f = io.stdin
    end
    local function reader()
        return f:read(4096 - 32)
    end
    return reader
end

return { 
    string = new_string_reader,
    file = new_file_reader,
}