Allow parser.lua to be used as an argument parser
This commit is contained in:
parent
c77a2df387
commit
4cff82dd04
17
parser.lua
17
parser.lua
|
@ -95,7 +95,11 @@ end
|
||||||
|
|
||||||
local function get_next_table(state, in_pos)
|
local function get_next_table(state, in_pos)
|
||||||
if state[DATA] == nil or #state[DATA] == 0 then
|
if state[DATA] == nil or #state[DATA] == 0 then
|
||||||
return get_next_common(state, in_pos, EOZ)
|
if state[STATE] == nil then
|
||||||
|
return in_pos, state
|
||||||
|
else
|
||||||
|
return get_next_common(state, in_pos, EOZ)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
in_pos = in_pos + 1
|
in_pos = in_pos + 1
|
||||||
local token = state[DATA][in_pos - state[OFFDATA]]
|
local token = state[DATA][in_pos - state[OFFDATA]]
|
||||||
|
@ -129,15 +133,14 @@ local function stream(defs, data)
|
||||||
local state = {}
|
local state = {}
|
||||||
local fn
|
local fn
|
||||||
state[STATE] = defs
|
state[STATE] = defs
|
||||||
if type(data) == "string" then
|
if type(data) == "function" then
|
||||||
state[DATA] = data
|
|
||||||
state[GEN] = function() end
|
|
||||||
fn = get_next_string
|
|
||||||
else
|
|
||||||
state[DATA] = data()
|
state[DATA] = data()
|
||||||
state[GEN] = data
|
state[GEN] = data
|
||||||
fn = type(state[DATA]) == "string" and get_next_string or get_next_table
|
else
|
||||||
|
state[DATA] = data
|
||||||
|
state[GEN] = function() end
|
||||||
end
|
end
|
||||||
|
fn = type(state[DATA]) == "table" and get_next_table or get_next_string
|
||||||
state[OFFDATA] = 0
|
state[OFFDATA] = 0
|
||||||
return fn, state, state[OFFDATA]
|
return fn, state, state[OFFDATA]
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,18 +18,47 @@
|
||||||
|
|
||||||
local parser = require "parser"
|
local parser = require "parser"
|
||||||
|
|
||||||
do
|
-- CLI argument rules
|
||||||
local filename = arg[1]
|
local defs = parser.selfify({})
|
||||||
local luatokens = require "luatokens"
|
defs['-'] = function(state, token)
|
||||||
local file = io.open(filename, "r")
|
if state.filename then
|
||||||
local tokens = luatokens.defs
|
error("Must specify only one filename")
|
||||||
local state, err, etoken, estate = parser.parse(tokens, function() return file:read(8192) end)
|
end
|
||||||
if state then
|
state.filename = true
|
||||||
for i,v in ipairs(state) do
|
state.file = io.stdin
|
||||||
v = luatokens.reverse_keywords[v] or luatokens.reverse_tokens[v] or v
|
return "self"
|
||||||
print(i, v) -- TODO formatting
|
end
|
||||||
end
|
defs[""] = function(state, token)
|
||||||
else
|
if state.filename then
|
||||||
print("Parse error")
|
error("Must specify only one filename")
|
||||||
|
end
|
||||||
|
state.filename = token
|
||||||
|
state.file, state.err = io.open(state.filename, "r")
|
||||||
|
return "self"
|
||||||
|
end
|
||||||
|
defs[parser.EOZ] = function(state, token)
|
||||||
|
if not state.file then
|
||||||
|
error((state.filename and (state.err or "") or "No file specified") )
|
||||||
|
end
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
defs[-1] = function(state, token, rule)
|
||||||
|
if token ~= parser.EOZ and token:sub(1,1) == "-" and not rule then
|
||||||
|
error("Unknown option: " .. token)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
defs['--'] = parser.selfify({[""] = defs[""]})
|
||||||
|
|
||||||
|
local state = parser.parse(defs, arg)
|
||||||
|
local luatokens = require "luatokens"
|
||||||
|
local file = state.file
|
||||||
|
local tokens = luatokens.defs
|
||||||
|
local state, err, etoken, estate = parser.parse(tokens, function() return file:read(8192) end)
|
||||||
|
if state then
|
||||||
|
for i,v in ipairs(state) do
|
||||||
|
v = luatokens.reverse_keywords[v] or luatokens.reverse_tokens[v] or v
|
||||||
|
print(i, v) -- TODO formatting
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print("Parse error")
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue