Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bind and bindTo for Parser monad #31

Merged
merged 3 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 0.6.8

- **New Feature**
- Add "do notation" to `Parser` monad (@CYBAI)

# 0.6.7

- **New Feature**
Expand Down
28 changes: 28 additions & 0 deletions docs/modules/Parser.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ Added in v0.6.0
- [parser](#parser)
- [model](#model)
- [Parser (interface)](#parser-interface)
- [utils](#utils)
- [bind](#bind)
- [bindTo](#bindto)

---

Expand Down Expand Up @@ -637,3 +640,28 @@ export interface Parser<I, A> {
```

Added in v0.6.0

# utils

## bind

**Signature**

```ts
export declare const bind: <N extends string, I, A, B>(
name: Exclude<N, keyof A>,
f: (a: A) => Parser<I, B>
) => (fa: Parser<I, A>) => Parser<I, { [K in N | keyof A]: K extends keyof A ? A[K] : B }>
```

Added in v0.6.8

## bindTo

**Signature**

```ts
export declare const bindTo: <N extends string>(name: N) => <I, A>(fa: Parser<I, A>) => Parser<I, { [K in N]: A }>
```

Added in v0.6.8
10 changes: 5 additions & 5 deletions examples/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* For example, one could generalize the parser exemplified below to accept any command. Then, the
* structure of the parsed AST for specific commands can be enforced using instances of `io-ts` `Schema`s.
*/
import { Do } from 'fp-ts-contrib/lib/Do'
import * as A from 'fp-ts/lib/Array'
import { mapLeft, Either } from 'fp-ts/lib/Either'
import { getStructMonoid, Monoid } from 'fp-ts/lib/Monoid'
Expand Down Expand Up @@ -173,10 +172,11 @@ const positional: P.Parser<string, Positional> = pipe(C.many1(C.notSpace), P.map
const argument = P.either<string, Argument>(flag, () => P.either<string, Argument>(named, () => positional))

const statement = (cmd: string) =>
Do(P.parser)
.bind('command', whitespaceSurrounded(S.string(cmd)))
.bind('args', P.many(whitespaceSurrounded(argument)))
.done()
pipe(
whitespaceSurrounded(S.string(cmd)),
P.bindTo('command'),
P.bind('args', () => P.many(whitespaceSurrounded(argument)))
)

const ast = (command: string, source: string): P.Parser<string, Ast> => {
return pipe(
Expand Down
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"docs-ts": "^0.5.1",
"dtslint": "^0.4.2",
"fp-ts": "^2.0.0",
"fp-ts-contrib": "^0.1.17",
"import-path-rewrite": "github:gcanti/import-path-rewrite",
"jest": "^24.8.0",
"mocha": "^5.2.0",
Expand Down
38 changes: 38 additions & 0 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,41 @@ export const parser: Monad2<URI> & Alternative2<URI> = {
alt: alt_,
zero: fail
}

// -------------------------------------------------------------------------------------
// do notation
// -------------------------------------------------------------------------------------

/**
* @internal
*/
const bind_ = <A, N extends string, B>(
a: A,
name: Exclude<N, keyof A>,
b: B
): { [K in keyof A | N]: K extends keyof A ? A[K] : B } => Object.assign({}, a, { [name]: b }) as any

/**
* @since 0.6.8
*/
export const bindTo = <N extends string>(name: N) => <I, A>(fa: Parser<I, A>): Parser<I, { [K in N]: A }> =>
pipe(
fa,
map(a => bind_({}, name, a))
)

/**
* @since 0.6.8
*/
export const bind = <N extends string, I, A, B>(name: Exclude<N, keyof A>, f: (a: A) => Parser<I, B>) => (
fa: Parser<I, A>
): Parser<I, { [K in keyof A | N]: K extends keyof A ? A[K] : B }> =>
pipe(
fa,
chain(a =>
pipe(
f(a),
map(b => bind_(a, name, b))
)
)
)
22 changes: 22 additions & 0 deletions test/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { char as C, parser as P, string as S } from '../src'
import { error, success } from '../src/ParseResult'
import { stream } from '../src/Stream'
import { run } from './helpers'
import { pipe } from 'fp-ts/lib/pipeable'

describe('Parser', () => {
it('eof', () => {
Expand Down Expand Up @@ -120,4 +121,25 @@ describe('Parser', () => {
assert.deepStrictEqual(run(parser, 'ab'), success(['a', 'b'], stream(['a', 'b'], 2), stream(['a', 'b'])))
assert.deepStrictEqual(run(parser, 'abc'), success(['a', 'b'], stream(['a', 'b', 'c'], 2), stream(['a', 'b', 'c'])))
})

it('bind', () => {
const parser = pipe(
C.char('a'),
P.bindTo('a'),
P.bind('b', () => C.char('b')),
P.map(({ a, b }) => [a, b])
)
assert.deepStrictEqual(run(parser, 'ab'), success(['a', 'b'], stream(['a', 'b'], 2), stream(['a', 'b'])))
assert.deepStrictEqual(run(parser, 'bc'), error(stream(['b', 'c'], 0), ['"a"']))
})

it('bindTo', () => {
const parser = pipe(
C.char('a'),
P.bindTo('headingA'),
P.map(({ headingA }) => [headingA])
)
assert.deepStrictEqual(run(parser, 'ab'), success(['a'], stream(['a', 'b'], 1), stream(['a', 'b'])))
assert.deepStrictEqual(run(parser, 'bc'), error(stream(['b', 'c'], 0), ['"a"']))
})
})