parseList = {
const lookupWithDefault = (name, defaultParser) => {
const lookupName = (env) => {
if (!env) return defaultParser
if (env[name]) return env[name]
else {
if (env.parentEnv) return lookupName(env.parentEnv)
else return defaultParser
}
}
return lookupName
}
const deriveEnvironment = (env) => {
let newEnv = { parentEnv: env }
if (env) {
newEnv.listOpener = p.string("[")
newEnv.listCloser = p.string("]")
}
return newEnv
}
const opener = p.from(lookupWithDefault("listOpener", p.string("(")))
const closer = p.from(lookupWithDefault("listCloser", p.string(")")))
let list
const listContent = p.from(() => p.times(list, 0, Infinity))
list = p.subEnv(
p.map(
p.seq(opener, listContent, closer),
([opener, content, closer]) => `list(${content.join(", ")})`),
deriveEnvironment)
return list
}