• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language PureScript
  • License
    BSD 2-Clause "Sim...
  • Created over 10 years ago
  • Updated 27 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A parser combinator library based on Parsec

Parsing

CI Release Pursuit Maintainer: jamesdbrock Maintainer: robertdp Maintainer: chtenb

A monadic parser combinator library based on Haskell’s Parsec.

Installation

Install parsing with Spago:

$ spago install parsing

Quick start

Here is a basic tutorial introduction to monadic parsing with this package.

Parsers

A parser turns a string into a data structure. Parsers in this library have the type Parser s a, where s is the type of the input string, and a is the type of the data which the parser will produce on success. Parser s is a monad. It’s defined in the module Parsing.

Monads can be used to provide context for a computation, and that’s how we use them in monadic parsing. The context provided by the Parser s monad is the parser’s current location in the input string. Parsing starts at the beginning of the input string.

Parsing requires two more capabilities: alternative and failure.

We need alternative to be able to choose what kind of thing we’re parsing depending on the input which we encounter. This is provided by the <|> “alt” operator of the Alt typeclass instance of the Parser s monad. The expression p_left <|> p_right will first try the p_left parser and if that fails and consumes no input then it will try the p_right parser.

We need failure in case the input stream is not parseable. This is provided by the fail function, which calls the throwError function of the MonadThrow typeclass instance of the Parser s monad.

To run a parser, call the function runParser :: s -> Parser s a -> Either ParseError a in the Parsing module, and supply it with an input string and a parser. If the parse succeeds then the result is Right a and if the parse fails then the result is Left ParseError.

Primitive parsers

Each type of input string needs primitive parsers. Primitive parsers for input string type String are in the Parsing.String module. For example, the primitive char :: Char -> Parser String Char parser will exactly match one literal character and then advance by one position in the input string.

We can use these primitive parsers to write other String parsers.

Writing a parser

Here is a parser ayebee :: Parser String Boolean which will accept only two input strings: "ab" or "aB". It will return true if the b character is uppercase. It will return false if the b character is lowercase. It will fail with a ParseError if the input string is anything else.

ayebee :: Parser String Boolean
ayebee = do
  _ <- char 'a'
  b <- char 'b' <|> char 'B'
  pure (b == 'B')

We can run the parser ayebee like so

runParser "aB" ayebee

and then the parser will succeed and return Right true.

Run the ayebee parser in your browser on Try PureScript!

More parsers

There are other String parsers in the module Parsing.String.Basic, for example the parser letter :: Parser String Char which will accept any single alphabetic letter.

Parser combinators

Parser combinators are in this package in the module Parsing.Combinators.

A parser combinator is a function which takes a parser as an argument and returns a new parser. The many combinator, for example, will repeat a parser as many times as it can. So the parser many letter will have type Parser String (Array Char).

Running the parser

runParser "aBabaB" (many ayebee)

will return Right [true, false, true].

Stack-safety

Starting with v9.0.0, all parsers and combinators in this package are always stack-safe.

Recursion

For the most part, we can just write recursive parsers (parsers defined in terms of themselves) and they will work as we expect.

In some cases like this:

aye :: Parser String Char
aye = char 'a' *> aye

we might get a compile-time CycleInDeclaration error which looks like this:

  The value of aye is undefined here, so this reference is not allowed.


See https://github.com/purescript/documentation/blob/master/errors/CycleInDeclaration.md for more information,
or to contribute content related to this error.

This is happening because we tried to call aye recursively “at a point where such a reference would be unavailable because of strict evaluation.”

The best way to solve this is to stick a Control.Lazy.defer in front of the parser to break the cycle.

aye :: Parser String Char
aye = defer \_ -> char 'a' *> aye

Resources

There are lots of other great monadic parsing tutorials on the internet.

Related Packages

Documentation

parsing documentation is stored in a few places:

  1. Module documentation is published on Pursuit.
  2. Written documentation is kept in the docs directory.
  3. Usage examples can be found in the test suite.

If you get stuck, there are several ways to get help:

Contributing

You can contribute to parsing in several ways:

  1. If you encounter a problem or have a question, please open an issue. We'll do our best to work with you to resolve or answer it.

  2. If you would like to contribute code, tests, or documentation, please read the contributor guide. It's a short, helpful introduction to contributing to this library, including development instructions.

  3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the PureScript Discourse! Writing libraries and learning resources are a great way to help this library succeed.

More Repositories

1

pulp

A build tool for PureScript projects
PureScript
444
star
2

purescript-react

React Bindings for PureScript
PureScript
395
star
3

purescript-aff

An asynchronous effect monad for PureScript
PureScript
281
star
4

purescript-vim

Syntax highlighting and indentation for PureScript
Vim Script
154
star
5

purescript-profunctor-lenses

Pure profunctor lenses
PureScript
142
star
6

purescript-affjax

An asynchronous AJAX library built using Aff.
PureScript
121
star
7

purescript-css

A clean, type-safe library for describing, manipulating and rendering CSS
PureScript
105
star
8

purescript-routing

A clean, type-safe routing library for PureScript.
PureScript
104
star
9

purescript-matryoshka

Generalized folds, unfolds, and traversals for fixed point data structures
PureScript
59
star
10

purescript-argonaut-codecs

JSON serialization and deserialization with Argonaut.
PureScript
49
star
11

purescript-argonaut-core

A fast, native representation for JSON, with serialization and folding
PureScript
47
star
12

setup-purescript

Set up a specific PureScript toolchain in your GitHub Actions workflow
JavaScript
46
star
13

purescript-colors

Convert, manipulate, analyze, blend, color scales, color schemes
PureScript
44
star
14

purescript-string-parsers

A parsing library specialized to handling strings
PureScript
43
star
15

purescript-formatters

Formatting and printing for numeric and date/time/interval values
PureScript
41
star
16

purescript-optparse

Applicative option parser
PureScript
40
star
17

purescript-coroutines

Computations which can suspend their execution and return control to their invoker
PureScript
38
star
18

purescript-machines

Finite state machines, including Mealy machines, for modeling computations
PureScript
37
star
19

atom-language-purescript

PureScript language support for the Atom editor
CoffeeScript
34
star
20

purescript-pathy

A type-safe abstraction for platform-independent file system paths.
PureScript
31
star
21

purescript-uri

A type-safe parser, printer, and ADT for URLs and URIs.
PureScript
26
star
22

purescript-bigints

Arbitrary length integers for PureScript
PureScript
26
star
23

purescript-github-actions-toolkit

PureScript wrapper around GitHub's Actions Toolkit
PureScript
25
star
24

purescript-aff-coroutines

Helper functions for creating coroutines with the Aff monad
PureScript
23
star
25

purescript-nullable

A very simple library for dealing with nulls in foreign libraries
PureScript
23
star
26

purescript-argonaut-generic

Generic encoding and decoding functions for data types with a Generic.Rep instance
PureScript
22
star
27

purescript-quickcheck-laws

QuickCheck powered law tests for PureScript's core typeclasses.
PureScript
22
star
28

purescript-freet

Free monad transformers
PureScript
20
star
29

purescript-options

Types and functions for dealing with JavaScript options objects
PureScript
20
star
30

purescript-now

Effect type and functions for accessing the current machine's date and time.
PureScript
19
star
31

purescript-ace

Purescript bindings for the Ace editor
PureScript
19
star
32

purescript-aff-bus

Many-to-many broadcasting
PureScript
16
star
33

purescript-react-dom

Low-level React DOM bindings for PureScript
PureScript
16
star
34

purescript-avar

Low-level interface for asynchronous variables
PureScript
16
star
35

purescript-fixed-points

Types for the least and greatest fixed points of functors.
PureScript
15
star
36

governance

Guidelines and resources for the PureScript Contributors organization
PureScript
15
star
37

purescript-js-date

JavaScript's native date type and corresponding functions.
PureScript
15
star
38

purescript-these

Data type isomorphic to α ∨ β ∨ (α ∧ β)
PureScript
14
star
39

purescript-rationals

Rational numbers for PureScript
PureScript
13
star
40

purescript-argonaut-traversals

Prisms, traversals, and zipper for the Argonaut Json type.
PureScript
11
star
41

purescript-concurrent-queues

An unbounded and bounded queue for concurrent access.
PureScript
10
star
42

purescript-js-promise

PureScript
9
star
43

purescript-http-methods

HTTP method type
PureScript
9
star
44

purescript-js-bigints

PureScript
9
star
45

chnglg

A maintainer and contributor-friendly tool for generating a human-readable CHANGELOG.md
PureScript
8
star
46

purescript-fork

An MTL-style class for monads that support forking
PureScript
8
star
47

purescript-unsafe-reference

Highly unsafe functions for comparing values using the runtime's strict equality.
PureScript
8
star
48

purescript-form-urlencoded

A data type, and encoding / decoding functions for application/x-www-form-urlencoded
PureScript
8
star
49

purescript-js-timers

Low level bindings for JavaScript's timers API
PureScript
7
star
50

purescript-arraybuffer

Bindings and implementation for mutable JavaScript ArrayBuffers.
PureScript
7
star
51

purescript-arraybuffer-types

Type definitions for JavaScript ArrayBuffers
PureScript
7
star
52

purescript-unicode

Unicode character functions.
PureScript
7
star
53

purescript-media-types

Internet media / content / MIME types
PureScript
6
star
54

purescript-uint

32-bit unsigned integer type for PureScript
PureScript
6
star
55

purescript-strings-extra

Additional utilities for the PureScript Strings library.
PureScript
6
star
56

purescript-int64

Signed and unsigned 64-bit integer types for PureScript
JavaScript
5
star
57

purescript-affjax-web

PureScript
5
star
58

purescript-js-promise-aff

PureScript
5
star
59

purescript-js-uri

URI encoding and decoding functions
PureScript
4
star
60

purescript-float32

Float32, single-precision 32-bit floating-point number type.
PureScript
3
star
61

purescript-js-abort-controller

Web/Node bindings to `AbortController`/`AbortSignal`
PureScript
2
star
62

purescript-affjax-node

PureScript
2
star
63

purescript-js-blob

PureScript
1
star