License | BSD-3-Clause |
---|---|
Maintainer | Jamie Willis, Gigaparsec Maintainers |
Stability | experimental |
Safe Haskell | Safe |
Language | Haskell2010 |
This module contains the very useful chaining family of combinators, which are mostly used to parse operators and expressions of varying fixities.
It is a lower-level API than precedence
.
See Text.Gigaparsec.Expr.Infix for combinators that allow for more freedom in the type of the values and the operators.
Synopsis
- chainl :: Parsec a -> Parsec (a -> a -> a) -> a -> Parsec a
- chainl1 :: Parsec a -> Parsec (a -> a -> a) -> Parsec a
- chainr1 :: Parsec a -> Parsec (a -> a -> a) -> Parsec a
- chainr :: Parsec a -> Parsec (a -> a -> a) -> a -> Parsec a
- chainn1 :: Parsec a -> Parsec (a -> a -> a) -> Parsec a
- prefix :: Parsec (a -> a) -> Parsec a -> Parsec a
- prefix1 :: (b -> a) -> Parsec (a -> b) -> Parsec a -> Parsec b
- postfix :: Parsec a -> Parsec (a -> a) -> Parsec a
- postfix1 :: (b -> a) -> Parsec a -> Parsec (a -> b) -> Parsec b
- chainr :: Parsec a -> Parsec (a -> a -> a) -> a -> Parsec a
- chainl :: Parsec a -> Parsec (a -> a -> a) -> a -> Parsec a
- chainr1 :: Parsec a -> Parsec (a -> a -> a) -> Parsec a
- chainl1 :: Parsec a -> Parsec (a -> a -> a) -> Parsec a
- prefix :: Parsec (a -> a) -> Parsec a -> Parsec a
- postfix :: Parsec a -> Parsec (a -> a) -> Parsec a
- chainn1 :: Parsec a -> Parsec (a -> a -> a) -> Parsec a
- prefix1 :: (b -> a) -> Parsec (a -> b) -> Parsec a -> Parsec b
- postfix1 :: (b -> a) -> Parsec a -> Parsec (a -> b) -> Parsec b
Binary Operator Chains
These combinators allow for the chaining together of values and binary operators in either left-, right- or non-associative application.
Left-Associative Binary Operators
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> a |
|
-> Parsec a | a parser that parses alternating |
This combinator handles left-associative parsing, and the application of, zero or more binary operators between zero or more values.
First parse p
, then parse op
followed by a p
repeatedly.
The results of the p
s, x₁
through xₙ
, are combined with the results of the op
s,
f₁
through fₙ₋₁
, with left-associative application:
fₙ₋₁(fₙ₋₂(..f₁(x₁, x₂).., xₙ₋₁), xₙ).
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
If no p
could be parsed, return a default value x
.
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> Parsec a | a parser that parses alternating |
This combinator handles left-associative parsing, and the application of, zero or more binary operators between one or more values.
First parse p
, then parse op
followed by a p
repeatedly.
The results of the p
s, x₁
through xₙ
, are combined with the results of the op
s,
f₁
through fₙ₋₁
, with left-associative application:
fₙ₋₁(fₙ₋₂(..f₁(x₁, x₂).., xₙ₋₁), xₙ).
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
See also infixl1
for a version where the types can vary,
ensuring that the associativity is enforced type-safely.
Right-Associative Binary Operators
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> Parsec a | a parser that parses alternating |
This combinator handles right-associative parsing, and the application of, zero or more binary operators between one or more values.
First parse p
, then parse op
followed by a p
repeatedly.
The results of the p
s, x₁
through xₙ
, are combined with the results of the op
s,
f₁
through fₙ
, with right-associative application:
f₁(x₁,f₂(x₂,...fₙ₋₁(xₙ₋₁, xₙ)..)).
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
See also infixr1
for a version where the types can vary,
ensuring that the associativity is enforced type-safely.
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> a |
|
-> Parsec a | a parser that parses alternating |
This combinator handles right-associative parsing, and the application of, zero or more binary operators between zero or more values.
First parse p
, then parse op
followed by a p
repeatedly.
The results of the p
s, x₁
through xₙ
, are combined with the results of the op
s,
f₁
through fₙ
, with right-associative application:
f₁ x₁ (f₂ x₂ (...(fₙ₋₁ xₙ₋₁ xₙ)...))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
If no p
could be parsed, return a default value x
.
Non-Associative Binary Operators
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> Parsec a | a parser that parses |
This combinator handles non-associative parsing, and the application of, zero or one binary operators between one or two values.
First parse p
.
Then:
- If this not is followed by an
op
, simply returnp
. - Otherwise, parse this
op
followed by a singlep
. Then ensure that this is not followed by a furtherop
, to enforce non-associativity. The results of thep
s,x
andy
, are combined with the result ofop
,f
with the applicationf x y
. This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
This combinator also fails if the second p
is followed by another op
.
Unary Operator Chains
These combinators allow for the chaining together, and application, of multiple prefix or postfix unary operators to a single value.
Prefix Operators
:: Parsec (a -> a) |
|
-> Parsec a |
|
-> Parsec a | a parser that parses many |
This combinator handles right-assocative parsing, and application of, zero or more prefix unary operators to a single value.
First parse many repeated op
s.
When there are no more op
s left to parse, parse a single p
.
The result of p
, x
, is applied to each of the results of the op
s, f₁
through fₙ
,
such that fₙ
is applied first and f₁
last: f₁ (f₂ (..(fₙ x)..))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
:: (b -> a) |
|
-> Parsec (a -> b) |
|
-> Parsec a |
|
-> Parsec b | a parser that parses at least one |
This combinator handles left-assocative parsing, and application of, one or more postfix unary operators to a single value.
First parse a single p
.
Then, parse at least one repeated op
s.
The result of p
, x
, is applied first to wrap
, and then to each of the results of the op
s, f₁
through fₙ
,
such that f₁
is applied first and fₙ
last: fₙ(fₙ₋₁ (..(f₁ (wrap x))..))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
Postfix Operators
:: Parsec a |
|
-> Parsec (a -> a) |
|
-> Parsec a | a parser that parses many |
This combinator handles left-assocative parsing, and application of, zero or more postfix unary operators to a single value.
First parse a single p
.
Then, parse many repeated op
s.
The result of p
, x
, is applied to each of the results of the op
s, f₁
through fₙ
,
such that f₁
is applied first and fₙ
last: fₙ(fₙ₋₁ (..(f₁ x)..))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
:: (b -> a) | a function converting the value type |
-> Parsec a |
|
-> Parsec (a -> b) |
|
-> Parsec b | a parser that parses at least one |
This combinator handles left-assocative parsing, and application of, one or more postfix unary operators to a single value.
First parse a single p
.
Then, parse at least one repeated op
s.
The result of p
, x
, is applied first to wrap
, and then to each of the results of the op
s, f₁
through fₙ
,
such that f₁
is applied first and fₙ
last: fₙ( fₙ₋₁(..f₁ (wrap x)..))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
Module Re-export
This should be removed.
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> a |
|
-> Parsec a | a parser that parses alternating |
This combinator handles right-associative parsing, and the application of, zero or more binary operators between zero or more values.
First parse p
, then parse op
followed by a p
repeatedly.
The results of the p
s, x₁
through xₙ
, are combined with the results of the op
s,
f₁
through fₙ
, with right-associative application:
f₁ x₁ (f₂ x₂ (...(fₙ₋₁ xₙ₋₁ xₙ)...))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
If no p
could be parsed, return a default value x
.
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> a |
|
-> Parsec a | a parser that parses alternating |
This combinator handles left-associative parsing, and the application of, zero or more binary operators between zero or more values.
First parse p
, then parse op
followed by a p
repeatedly.
The results of the p
s, x₁
through xₙ
, are combined with the results of the op
s,
f₁
through fₙ₋₁
, with left-associative application:
fₙ₋₁(fₙ₋₂(..f₁(x₁, x₂).., xₙ₋₁), xₙ).
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
If no p
could be parsed, return a default value x
.
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> Parsec a | a parser that parses alternating |
This combinator handles right-associative parsing, and the application of, zero or more binary operators between one or more values.
First parse p
, then parse op
followed by a p
repeatedly.
The results of the p
s, x₁
through xₙ
, are combined with the results of the op
s,
f₁
through fₙ
, with right-associative application:
f₁(x₁,f₂(x₂,...fₙ₋₁(xₙ₋₁, xₙ)..)).
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
See also infixr1
for a version where the types can vary,
ensuring that the associativity is enforced type-safely.
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> Parsec a | a parser that parses alternating |
This combinator handles left-associative parsing, and the application of, zero or more binary operators between one or more values.
First parse p
, then parse op
followed by a p
repeatedly.
The results of the p
s, x₁
through xₙ
, are combined with the results of the op
s,
f₁
through fₙ₋₁
, with left-associative application:
fₙ₋₁(fₙ₋₂(..f₁(x₁, x₂).., xₙ₋₁), xₙ).
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
See also infixl1
for a version where the types can vary,
ensuring that the associativity is enforced type-safely.
:: Parsec (a -> a) |
|
-> Parsec a |
|
-> Parsec a | a parser that parses many |
This combinator handles right-assocative parsing, and application of, zero or more prefix unary operators to a single value.
First parse many repeated op
s.
When there are no more op
s left to parse, parse a single p
.
The result of p
, x
, is applied to each of the results of the op
s, f₁
through fₙ
,
such that fₙ
is applied first and f₁
last: f₁ (f₂ (..(fₙ x)..))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
:: Parsec a |
|
-> Parsec (a -> a) |
|
-> Parsec a | a parser that parses many |
This combinator handles left-assocative parsing, and application of, zero or more postfix unary operators to a single value.
First parse a single p
.
Then, parse many repeated op
s.
The result of p
, x
, is applied to each of the results of the op
s, f₁
through fₙ
,
such that f₁
is applied first and fₙ
last: fₙ(fₙ₋₁ (..(f₁ x)..))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
:: Parsec a |
|
-> Parsec (a -> a -> a) |
|
-> Parsec a | a parser that parses |
This combinator handles non-associative parsing, and the application of, zero or one binary operators between one or two values.
First parse p
.
Then:
- If this not is followed by an
op
, simply returnp
. - Otherwise, parse this
op
followed by a singlep
. Then ensure that this is not followed by a furtherop
, to enforce non-associativity. The results of thep
s,x
andy
, are combined with the result ofop
,f
with the applicationf x y
. This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
This combinator also fails if the second p
is followed by another op
.
:: (b -> a) |
|
-> Parsec (a -> b) |
|
-> Parsec a |
|
-> Parsec b | a parser that parses at least one |
This combinator handles left-assocative parsing, and application of, one or more postfix unary operators to a single value.
First parse a single p
.
Then, parse at least one repeated op
s.
The result of p
, x
, is applied first to wrap
, and then to each of the results of the op
s, f₁
through fₙ
,
such that f₁
is applied first and fₙ
last: fₙ(fₙ₋₁ (..(f₁ (wrap x))..))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.
:: (b -> a) | a function converting the value type |
-> Parsec a |
|
-> Parsec (a -> b) |
|
-> Parsec b | a parser that parses at least one |
This combinator handles left-assocative parsing, and application of, one or more postfix unary operators to a single value.
First parse a single p
.
Then, parse at least one repeated op
s.
The result of p
, x
, is applied first to wrap
, and then to each of the results of the op
s, f₁
through fₙ
,
such that f₁
is applied first and fₙ
last: fₙ( fₙ₋₁(..f₁ (wrap x)..))
.
This application is then returned as the result of the combinator.
If p
or op
fails having consumed input at any point, the whole combinator fails.