Comments (4)
It's funny how this project became active exactly at the time I had to write a parser at my job! 😆 I was really happy about the new release.
However, it may be more convenient to have parser
parameter to be polymorphic in its parsed type. Consider an example:
// <Term> ::= <Word> | 'NOT (' <Term> ')'
type Term =
| { readonly tag: 'Word'; readonly value: string; }
| { readonly tag: 'Not'; readonly value: Term; }
const textWithoutSpaces = pipe(
many1(alphanum),
map(chars => chars.join(''))
);
const Word: Parser<string, Term> = pipe(
textWithoutSpaces,
alt(() => doubleQuotedString),
map((value) => ({ tag: 'Word', value }))
);
const braced = between(char('('), char(')')); // => Parser<string, string>
const NotWord = braced(Word); // ❌
// Argument of type 'Parser<string, Term>' is not assignable to parameter of type 'Parser<string, string>'
Better implementation would be the following:
const between = <I, A>(left: Parser<I, A>, right: Parser<I, A>) => <B>(p: Parser<I, B>) => pipe(
left,
chain(() => p),
chainFirst(() => right)
);
@gcanti @IMax153 may I send a PR for this enhancement?
from parser-ts.
@IMax153 thanks for the PR! and sorry for being busy recently and didn't have time to send one.
@YBogomolov Yes! I thinks making it polymorphic should be good! 👀
from parser-ts.
@CYBAI 👍
from parser-ts.
@CYBAI This addition would be excellent in my opinion! I found myself often wanting a between
parser in a different project, so I wrote one as well.
It only uses combinators from parser-ts
, so it would prevent having to include fp-ts-contrib
as a dependency of parser-ts
. I also partially apply both the between
and surroundedBy
parsers to make them more flexible. I figured I would share my implementation in case you found it useful!
import { pipe } from 'fp-ts/lib/function'
import { chain, chainFirst, Parser } from 'parser-ts/lib/Parser'
export const between = <I, A>(left: Parser<I, A>, right: Parser<I, A>) => (
parser: Parser<I, A>
): Parser<I, A> =>
pipe(
left,
chain(() => parser),
chainFirst(() => right)
)
export const surroundedBy = <I, A>(bound: Parser<I, A>): ((parser: Parser<I, A>) => Parser<I, A>) =>
between(bound, bound)
from parser-ts.
Related Issues (20)
- New Feature: notString and notOneOf (string)
- [Feature Request] Expose modules without lib/es6 prefix HOT 5
- export run from test/helpers.ts HOT 1
- Design Question: Stream with Positioned Tokens HOT 2
- Feature Request: manyTill and many1Till HOT 2
- Reci HOT 1
- `doubleQuotedString` hits recursion limit on longer strings HOT 3
- Fatal error from filter HOT 7
- How to skip some parts HOT 5
- Working with Excessively Long Strings HOT 5
- P.either should fail with fatal error, if second parser failed with fatal, or should it? HOT 2
- RangeError: Maximum call stack size exceeded HOT 2
- [feature] repeat – many in terms of semigroup/monoid HOT 1
- Import path problem in using ESM feature typescript@next HOT 1
- How to handle left-recursive grammar with parser-ts HOT 2
- Support for NodeJS Buffer HOT 1
- How to compose and handle Optional types in parsers? HOT 3
- Combination of alt and many runs infinitely
- Add hexDigit combinator
- Can @babel/code-frame be avoided?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parser-ts.