Giter Club home page Giter Club logo

Comments (4)

YBogomolov avatar YBogomolov commented on September 16, 2024 3

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.

CYBAI avatar CYBAI commented on September 16, 2024 2

@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.

gcanti avatar gcanti commented on September 16, 2024 1

@CYBAI 👍

from parser-ts.

IMax153 avatar IMax153 commented on September 16, 2024 1

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.