How to use parser-ts - 10 common examples

To help you get started, we’ve selected a few parser-ts examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gcanti / fp-ts-codegen / test / haskell.ts View on Github external
const assertSuccess = <a>(parser: parser.Parser, input: string, expected: A) =&gt; {
  const result = parser(stream.stream(input.split('')))
  if (isRight(result)) {
    assert.deepStrictEqual(result.right.value, expected)
  } else {
    throw new Error(`${result} is not a right`)
  }
}
</a>
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
const identifierFirstLetter = P.sat(c =&gt; !isDigit(c) &amp;&amp; !isPunctuation(c))

const identifierBody = P.sat(c =&gt; !isPunctuation(c))

/**
 * @since 0.4.0
 */
export const identifier: P.Parser = P.expected(
  S.fold([identifierFirstLetter, C.many(identifierBody)]),
  'an identifier'
)

const leftParens: P.Parser = S.fold([C.char('('), S.spaces])

const rightParens: P.Parser = S.fold([S.spaces, C.char(')')])

const withParens = <a>(parser: P.Parser): P.Parser =&gt; {
  return pipe(
    leftParens,
    P.apSecond(parser),
    P.apFirst(rightParens)
  )
}

const unparametrizedRef: P.Parser = pipe(
  identifier,
  P.map(name =&gt; M.ref(name))
)

/**
 * @since 0.4.0</a>
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
const isPunctuation = (c: string): boolean =&gt; '| =\n():,{};[]-&gt;'.indexOf(c) !== -1

const identifierFirstLetter = P.sat(c =&gt; !isDigit(c) &amp;&amp; !isPunctuation(c))

const identifierBody = P.sat(c =&gt; !isPunctuation(c))

/**
 * @since 0.4.0
 */
export const identifier: P.Parser = P.expected(
  S.fold([identifierFirstLetter, C.many(identifierBody)]),
  'an identifier'
)

const leftParens: P.Parser = S.fold([C.char('('), S.spaces])

const rightParens: P.Parser = S.fold([S.spaces, C.char(')')])

const withParens = <a>(parser: P.Parser): P.Parser =&gt; {
  return pipe(
    leftParens,
    P.apSecond(parser),
    P.apFirst(rightParens)
  )
}

const unparametrizedRef: P.Parser = pipe(
  identifier,
  P.map(name =&gt; M.ref(name))
)
</a>
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
import { pipe } from 'fp-ts/lib/pipeable'
import { run } from 'parser-ts/lib/code-frame'

const isDigit = (c: string): boolean =&gt; '0123456789'.indexOf(c) !== -1

const isPunctuation = (c: string): boolean =&gt; '| =\n():,{};[]-&gt;'.indexOf(c) !== -1

const identifierFirstLetter = P.sat(c =&gt; !isDigit(c) &amp;&amp; !isPunctuation(c))

const identifierBody = P.sat(c =&gt; !isPunctuation(c))

/**
 * @since 0.4.0
 */
export const identifier: P.Parser = P.expected(
  S.fold([identifierFirstLetter, C.many(identifierBody)]),
  'an identifier'
)

const leftParens: P.Parser = S.fold([C.char('('), S.spaces])

const rightParens: P.Parser = S.fold([S.spaces, C.char(')')])

const withParens = <a>(parser: P.Parser): P.Parser =&gt; {
  return pipe(
    leftParens,
    P.apSecond(parser),
    P.apFirst(rightParens)
  )
}

const unparametrizedRef: P.Parser = pipe(</a>
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
import { some } from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/pipeable'
import { run } from 'parser-ts/lib/code-frame'

const isDigit = (c: string): boolean =&gt; '0123456789'.indexOf(c) !== -1

const isPunctuation = (c: string): boolean =&gt; '| =\n():,{};[]-&gt;'.indexOf(c) !== -1

const identifierFirstLetter = P.sat(c =&gt; !isDigit(c) &amp;&amp; !isPunctuation(c))

const identifierBody = P.sat(c =&gt; !isPunctuation(c))

/**
 * @since 0.4.0
 */
export const identifier: P.Parser = P.expected(
  S.fold([identifierFirstLetter, C.many(identifierBody)]),
  'an identifier'
)

const leftParens: P.Parser = S.fold([C.char('('), S.spaces])

const rightParens: P.Parser = S.fold([S.spaces, C.char(')')])

const withParens = <a>(parser: P.Parser): P.Parser =&gt; {
  return pipe(
    leftParens,
    P.apSecond(parser),
    P.apFirst(rightParens)
  )
}
</a>
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
import { parser as P, string as S, char as C } from 'parser-ts'
import * as M from './model'
import { Either } from 'fp-ts/lib/Either'
import { some } from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/pipeable'
import { run } from 'parser-ts/lib/code-frame'

const isDigit = (c: string): boolean =&gt; '0123456789'.indexOf(c) !== -1

const isPunctuation = (c: string): boolean =&gt; '| =\n():,{};[]-&gt;'.indexOf(c) !== -1

const identifierFirstLetter = P.sat(c =&gt; !isDigit(c) &amp;&amp; !isPunctuation(c))

const identifierBody = P.sat(c =&gt; !isPunctuation(c))

/**
 * @since 0.4.0
 */
export const identifier: P.Parser = P.expected(
  S.fold([identifierFirstLetter, C.many(identifierBody)]),
  'an identifier'
)

const leftParens: P.Parser = S.fold([C.char('('), S.spaces])

const rightParens: P.Parser = S.fold([S.spaces, C.char(')')])

const withParens = <a>(parser: P.Parser): P.Parser =&gt; {</a>
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
import { parser as P, string as S, char as C } from 'parser-ts'
import * as M from './model'
import { Either } from 'fp-ts/lib/Either'
import { some } from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/pipeable'
import { run } from 'parser-ts/lib/code-frame'

const isDigit = (c: string): boolean =&gt; '0123456789'.indexOf(c) !== -1

const isPunctuation = (c: string): boolean =&gt; '| =\n():,{};[]-&gt;'.indexOf(c) !== -1

const identifierFirstLetter = P.sat(c =&gt; !isDigit(c) &amp;&amp; !isPunctuation(c))

const identifierBody = P.sat(c =&gt; !isPunctuation(c))

/**
 * @since 0.4.0
 */
export const identifier: P.Parser = P.expected(
  S.fold([identifierFirstLetter, C.many(identifierBody)]),
  'an identifier'
)

const leftParens: P.Parser = S.fold([C.char('('), S.spaces])

const rightParens: P.Parser = S.fold([S.spaces, C.char(')')])

const withParens = <a>(parser: P.Parser): P.Parser =&gt; {
  return pipe(
    leftParens,</a>
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
case 0:
              return M.unit
            case 1:
              return types[0]
            default:
              return M.tuple(types)
          }
        })
      )
    ),
    P.apFirst(rightParens)
  ),
  'a tuple'
)

const arrow = S.fold([S.spaces, S.string('-&gt;'), S.spaces])

/**
 * @since 0.4.0
 */
export const fun: P.Parser = P.expected(
  pipe(
    S.spaces,
    P.chain(() =&gt;
      pipe(
        ref,
        P.alt(() =&gt; tuple),
        P.chain(domain =&gt;
          pipe(
            arrow,
            P.apSecond(type),
            P.map(codomain =&gt; M.fun(domain, codomain))
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
P.map(types =&gt; M.constructor(name, types.map(type =&gt; M.member(type))))
        )
      )
    )
  )
)

/**
 * @since 0.4.0
 */
export const constructor: P.Parser = pipe(
  recordConstructor,
  P.alt(() =&gt; positionalConstructor)
)

const equal = S.fold([S.spaces, C.char('='), S.spaces])

const unconstrainedParameterDeclaration: P.Parser = pipe(
  identifier,
  P.map(name =&gt; M.parameterDeclaration(name))
)

const constrainedParameterDeclaration: P.Parser = pipe(
  S.fold([C.char('('), S.spaces]),
  P.apSecond(
    pipe(
      pair,
      P.map(({ name, type }) =&gt; M.parameterDeclaration(name, some(type))),
      P.apFirst(S.fold([S.spaces, C.char(')')]))
    )
  )
)
github gcanti / fp-ts-codegen / src / haskell.ts View on Github external
export const ref: P.Parser = pipe(
  identifier,
  P.chain(name =&gt;
    pipe(
      S.spaces,
      P.apSecond(
        pipe(
          types,
          P.map(parameters =&gt; M.ref(name, parameters))
        )
      )
    )
  )
)

const comma = S.fold([S.spaces, C.char(','), S.spaces])

/**
 * @since 0.4.0
 */
export const tuple: P.Parser = P.expected(
  pipe(
    leftParens,
    P.chain(() =&gt;
      pipe(
        P.sepBy(comma, type),
        P.map(types =&gt; {
          switch (types.length) {
            case 0:
              return M.unit
            case 1:
              return types[0]