diff --git a/src/combinator.js b/src/combinator.js index 8862a95..706d5bd 100644 --- a/src/combinator.js +++ b/src/combinator.js @@ -2,6 +2,7 @@ import { chain, curry } from 'izuna' import { ok } from 'kojima' import { clone, diff, fail, succeed } from './fn.js' import { state as State } from './state.js' +import { skip } from './cond.js' /** @import { ParserState } from './state.js' */ @@ -62,3 +63,9 @@ export const map = curry( }) }) +export const delimited = curry((first, second, third, state) => seq(skip(first), second, skip(third))(state)) +export const preceded = curry((first, second, state) => seq(skip(first), second)(state)) +export const terminated = curry((first, second, state) => seq(first, skip(second))(state)) + +export const separatedPair = curry((first, delimiter, second, state) => seq(first, skip(delimiter), second)(state)) + diff --git a/src/cond.js b/src/cond.js index 9ca4a09..58a6658 100644 --- a/src/cond.js +++ b/src/cond.js @@ -50,5 +50,6 @@ export const verify = curry((parser, predicate, state) => { }) export const skip = curry((parser, state) => { + return chain(_ => succeed([], state), parser(state)) }) diff --git a/tests/units/bytes.js b/tests/units/bytes.js index 4580328..9dff35c 100644 --- a/tests/units/bytes.js +++ b/tests/units/bytes.js @@ -2,18 +2,7 @@ import { it, assert, assertEq } from 'folktest' import { char, charNoCase, noneOf, oneOf, parse, tag, tagNoCase, take, takeWhile } from '../../src/index.js' import { Alphanumeric } from '../../src/const.js' import { chain, curry, map } from 'izuna' - -const assertState = expected => state => { - assertEq(expected, state[0]) -} - -const parseEq = curry((parser, input, expected) => - chain(assertState(expected), parse(parser, input)) -) - -const parseErr = curry((parser, input) => - assert(parse(parser, input).isErr(), `expected an error but "${input}" parsed successfully`) -) +import { assertState, parseEq } from './common.js' export const Byte = [ it('char', () => { diff --git a/tests/units/common.js b/tests/units/common.js new file mode 100644 index 0000000..ca3eea6 --- /dev/null +++ b/tests/units/common.js @@ -0,0 +1,15 @@ +import { chain, curry } from 'izuna' +import { parse } from '../../src/index.js' + +export const assertState = curry((expected, state) => { + assertEq(expected, state[0]) +}) + +export const parseEq = curry((parser, input, expected) => + chain(assertState(expected), parse(parser, input)) +) + +export const parseErr = curry((parser, input) => + assert(parse(parser, input).isErr(), `expected an error but '${input}' parsed successfully`) +) + diff --git a/tests/units/cond.js b/tests/units/cond.js new file mode 100644 index 0000000..726d979 --- /dev/null +++ b/tests/units/cond.js @@ -0,0 +1,11 @@ +import { it, assertEq } from 'folktest' +import { anyChar, parse } from '../../src/index.js' +import { skip } from '../../src/cond.js' +import { parseEq } from './common.js' + +export const Conditionals = [ + it('skip', () => { + parseEq(skip(anyChar), 'test', []) + }) +] + diff --git a/tests/units/index.js b/tests/units/index.js index e7297eb..aad520c 100644 --- a/tests/units/index.js +++ b/tests/units/index.js @@ -1,3 +1,4 @@ export * from './bytes.js' export * from './iter.js' +export * from './cond.js'