fix str
This commit is contained in:
parent
5152336bdc
commit
74bac48730
11 changed files with 70 additions and 28 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
node_modules/
|
||||
|
22
package-lock.json
generated
Normal file
22
package-lock.json
generated
Normal file
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"name": "kuebiko",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "kuebiko",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"folktest": "git+https://git.kitsu.cafe/rowan/folktest.git"
|
||||
}
|
||||
},
|
||||
"node_modules/folktest": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "git+https://git.kitsu.cafe/rowan/folktest.git#708d44f1215be33fcceba426029f44b4f963dbe5",
|
||||
"dev": true,
|
||||
"license": "GPL-3.0-or-later"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,9 +5,12 @@
|
|||
"main": "index.js",
|
||||
"author": "Rowan <rowan@kitsu.cafe> (https://kitsu.cafe)",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"test": "./tests/index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"license": "ISC",
|
||||
"description": ""
|
||||
"description": "",
|
||||
"devDependencies": {
|
||||
"folktest": "git+https://git.kitsu.cafe/rowan/folktest.git"
|
||||
}
|
||||
}
|
||||
|
|
11
src/char.js
11
src/char.js
|
@ -12,14 +12,17 @@ export const char = curry(
|
|||
* @param {string} ch
|
||||
* @param {ParserState} state
|
||||
*/
|
||||
(ch, state) => (
|
||||
next(state) === ch ? succeed(ch, state) : fail(`could not parse ${ch} `, state)
|
||||
))
|
||||
//(ch, state) => (
|
||||
// next(state) === ch ? succeed(ch, state) : fail(`could not parse ${ch} `, state)
|
||||
//))
|
||||
(ch, state) => {
|
||||
return next(state) === ch ? succeed(ch, state) : fail(`could not parse ${ch} `, state)
|
||||
})
|
||||
|
||||
export const str = curry(
|
||||
/**
|
||||
* @param {string} str
|
||||
* @param {State} state
|
||||
* @param {ParserState} state
|
||||
*/
|
||||
(str, state) => (
|
||||
map(
|
||||
|
|
14
src/cond.js
14
src/cond.js
|
@ -1,7 +1,8 @@
|
|||
import { ParseError } from './state.js'
|
||||
import { fork, succeed } from './fn.js'
|
||||
import { curry } from '../vendor/izuna/src/index.js'
|
||||
import { fail, fork, succeed } from './fn.js'
|
||||
import { anyChar } from './char.js'
|
||||
import { ok } from '../vendor/kojima/src/index.js'
|
||||
import { curry } from '../vendor/izuna/src/index.js'
|
||||
|
||||
/** @import { Result } from '../vendor/kojima/src/index.js' */
|
||||
/** @import { ParserState } from './state.js' */
|
||||
|
@ -19,6 +20,7 @@ export const maybe = curry(
|
|||
|
||||
export const not = parser => state => {
|
||||
const result = parser(state)
|
||||
|
||||
if (result.isOk()) {
|
||||
return fail('"not" parser failed', state)
|
||||
} else {
|
||||
|
@ -27,19 +29,19 @@ export const not = parser => state => {
|
|||
}
|
||||
|
||||
export const until = parser => state => {
|
||||
let acc = ok(state)
|
||||
let result = ok(state)
|
||||
|
||||
while (result.isOk()) {
|
||||
const [original, clone] = fork(state)
|
||||
const result = acc.chain(x => parser(clone))
|
||||
result = result.chain(x => parser(clone))
|
||||
if (result.isOk()) {
|
||||
break
|
||||
} else {
|
||||
acc = anyChar(original)
|
||||
result = anyChar(original)
|
||||
}
|
||||
}
|
||||
|
||||
return acc
|
||||
return result
|
||||
}
|
||||
|
||||
export const many = curry((parser, state) => {
|
||||
|
|
|
@ -97,3 +97,4 @@ export const mapStr = curry(
|
|||
(fn, str) => Array.from(str).map(v => fn(v))
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,3 @@ export * from './cond.js'
|
|||
export * from './seq.js'
|
||||
export * from './state.js'
|
||||
|
||||
console.log(
|
||||
parse(until(not(char('!'))), '!!!!!!!a!!!!')
|
||||
)
|
||||
|
||||
|
|
|
@ -19,8 +19,7 @@ export class Iter {
|
|||
* @returns {value is Iterable<T>}
|
||||
*/
|
||||
static _isIterable(value) {
|
||||
return Object.hasOwn(value, Symbol.iterator)
|
||||
&& typeof value[Symbol.iterator] === 'function'
|
||||
return typeof value[Symbol.iterator] === 'function'
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,7 +48,8 @@ export class Iter {
|
|||
* @param {any} [value]
|
||||
*/
|
||||
next(value) {
|
||||
return this._iterator.next(value)
|
||||
const n = this._iterator.next(value)
|
||||
return n
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
13
src/seq.js
13
src/seq.js
|
@ -40,10 +40,9 @@ export const seq = (...parsers) =>
|
|||
/** @param {ParserState} state */
|
||||
state => {
|
||||
let acc = ok(state)
|
||||
|
||||
for (const parser of parsers) {
|
||||
if (acc.isOk()) {
|
||||
acc = acc.bind(parser)
|
||||
acc = acc.chain(parser)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
@ -59,16 +58,12 @@ export const map = curry(
|
|||
* @param {ParserState} state
|
||||
*/
|
||||
(fn, parser, state) => {
|
||||
return parser(state).map(result => {
|
||||
return parser(state).chain(result => {
|
||||
try {
|
||||
/** @type {Result<ParserState, ParseError>} */
|
||||
const parsed = result.chain(otherState =>
|
||||
fn(diff(state[0], otherState[0]))
|
||||
)
|
||||
const parsed = fn(diff(state[0], result[0]))
|
||||
const backtrack = Tuple(state[0], result[1])
|
||||
|
||||
const backtrack = result.chain(otherState =>
|
||||
Tuple(state[0], otherState[1])
|
||||
)
|
||||
|
||||
return succeed(parsed, backtrack)
|
||||
} catch (e) {
|
||||
|
|
7
tests/index.js
Executable file
7
tests/index.js
Executable file
|
@ -0,0 +1,7 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import { TerminalRunner } from 'folktest'
|
||||
import * as Tests from './units/index.js'
|
||||
|
||||
console.log(TerminalRunner(Tests).toString())
|
||||
|
11
tests/units/index.js
Normal file
11
tests/units/index.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { it, assert } from 'folktest'
|
||||
import { map, not, until, char, parse, State, seq, str } from '../../src/index.js'
|
||||
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