How to use the parser-ts.parser.sat function in parser-ts

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 / 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 => '0123456789'.indexOf(c) !== -1

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

const identifierFirstLetter = P.sat(c => !isDigit(c) && !isPunctuation(c))

const identifierBody = P.sat(c => !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>