dedup many
This commit is contained in:
parent
74bac48730
commit
628d0ef80b
3 changed files with 23 additions and 16 deletions
|
@ -32,3 +32,24 @@ export const anyOf = curry(
|
|||
any(...mapStr(char, str))(state)
|
||||
))
|
||||
|
||||
export const map = curry(
|
||||
/**
|
||||
* @param {(...args: any[]) => any} fn
|
||||
* @param {Parser} parser
|
||||
* @param {ParserState} state
|
||||
*/
|
||||
(fn, parser, state) => {
|
||||
return parser(state).chain(result => {
|
||||
try {
|
||||
/** @type {Result<ParserState, ParseError>} */
|
||||
const parsed = fn(diff(state[0], result[0]))
|
||||
const backtrack = Tuple(state[0], result[1])
|
||||
|
||||
|
||||
return succeed(parsed, backtrack)
|
||||
} catch (e) {
|
||||
return fail('failed to map', state, e)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
|
15
src/cond.js
15
src/cond.js
|
@ -44,19 +44,6 @@ export const until = parser => state => {
|
|||
return result
|
||||
}
|
||||
|
||||
export const many = curry((parser, state) => {
|
||||
let result = ok(state)
|
||||
|
||||
while (true) {
|
||||
const [original, clone] = fork(result)
|
||||
const res = result.chain(x => parser(clone))
|
||||
if (res.isOk()) {
|
||||
result = res
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
export const skip = curry((parser, state) => {
|
||||
})
|
||||
|
||||
|
|
|
@ -4,8 +4,7 @@ import { mapStr, join } from '../../src/fn.js'
|
|||
|
||||
export const Tests = [
|
||||
it('whatever', () => {
|
||||
const a = 'abcdef!!!!'.split('')
|
||||
parse(str('abc'), 'abc').map(console.log)
|
||||
|
||||
})
|
||||
]
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue