gigaparsec-0.3.0.0: Refreshed parsec-style library for compatibility with Scala parsley
LicenseBSD-3-Clause
MaintainerJamie Willis, Gigaparsec Maintainers
Stabilityexperimental
Safe HaskellSafe
LanguageHaskell2010

Text.Gigaparsec.Expr.Chain

Description

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

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

chainl Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> a

x, the default value to return if no ps can be parsed.

-> Parsec a

a parser that parses alternating p and op, ending in a p, and applies their results left-associatively. If no p was parsed, returns x.

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 ps, x₁ through xₙ, are combined with the results of the ops, 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.

chainl1 Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> Parsec a

a parser that parses alternating p and op, ending in a p, and applies their results left-associatively.

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 ps, x₁ through xₙ, are combined with the results of the ops, 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

chainr1 Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> Parsec a

a parser that parses alternating p and op, ending in a p, and applies their results right-associatively.

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 ps, x₁ through xₙ, are combined with the results of the ops, 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.

chainr Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> a

x, the default value to return if no ps can be parsed.

-> Parsec a

a parser that parses alternating p and op, ending in a p, and applies their results right-associatively. If no p was parsed, returns x.

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 ps, x₁ through xₙ, are combined with the results of the ops, 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

chainn1 Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> Parsec a

a parser that parses p, and possibly an op and then p, and applies the result of op to those of p in the same order.

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 return p.
  • Otherwise, parse this op followed by a single p. Then ensure that this is not followed by a further op, to enforce non-associativity. The results of the ps, x and y, are combined with the result of op, f with the application f 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

prefix Source #

Arguments

:: Parsec (a -> a)

op, the prefix operator to repeatedly parse before p.

-> Parsec a

p, the single value to be parsed

-> Parsec a

a parser that parses many ops, and a final p, and applies all of the results right-associatively.

This combinator handles right-assocative parsing, and application of, zero or more prefix unary operators to a single value.

First parse many repeated ops. When there are no more ops left to parse, parse a single p. The result of p, x, is applied to each of the results of the ops, 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.

prefix1 Source #

Arguments

:: (b -> a)

wrap, a function converting bs into as

-> Parsec (a -> b)

op, the prefix operator to repeatedly parse before p

-> Parsec a

p, the single value to be parsed

-> Parsec b

a parser that parses at least one op, and a final p, and applies their results right-associatively.

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 ops. The result of p, x, is applied first to wrap, and then to each of the results of the ops, 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

postfix Source #

Arguments

:: Parsec a

p, the single value to be parsed

-> Parsec (a -> a)

op, the postfix operator to repeatedly parse after p.

-> Parsec a

a parser that parses many ops, and a final p, and applies all of the results left-associatively.

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 ops. The result of p, x, is applied to each of the results of the ops, 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.

postfix1 Source #

Arguments

:: (b -> a)

a function converting the value type a into the result type b

-> Parsec a

p, the single value to be parsed

-> Parsec (a -> b)

op, the postfix operator to repeatedly parse after p.

-> Parsec b

a parser that parses at least one op, and a final p, and applies all of the results left-associatively.

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 ops. The result of p, x, is applied first to wrap, and then to each of the results of the ops, 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.

chainr Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> a

x, the default value to return if no ps can be parsed.

-> Parsec a

a parser that parses alternating p and op, ending in a p, and applies their results right-associatively. If no p was parsed, returns x.

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 ps, x₁ through xₙ, are combined with the results of the ops, 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.

chainl Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> a

x, the default value to return if no ps can be parsed.

-> Parsec a

a parser that parses alternating p and op, ending in a p, and applies their results left-associatively. If no p was parsed, returns x.

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 ps, x₁ through xₙ, are combined with the results of the ops, 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.

chainr1 Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> Parsec a

a parser that parses alternating p and op, ending in a p, and applies their results right-associatively.

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 ps, x₁ through xₙ, are combined with the results of the ops, 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.

chainl1 Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> Parsec a

a parser that parses alternating p and op, ending in a p, and applies their results left-associatively.

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 ps, x₁ through xₙ, are combined with the results of the ops, 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.

prefix Source #

Arguments

:: Parsec (a -> a)

op, the prefix operator to repeatedly parse before p.

-> Parsec a

p, the single value to be parsed

-> Parsec a

a parser that parses many ops, and a final p, and applies all of the results right-associatively.

This combinator handles right-assocative parsing, and application of, zero or more prefix unary operators to a single value.

First parse many repeated ops. When there are no more ops left to parse, parse a single p. The result of p, x, is applied to each of the results of the ops, 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.

postfix Source #

Arguments

:: Parsec a

p, the single value to be parsed

-> Parsec (a -> a)

op, the postfix operator to repeatedly parse after p.

-> Parsec a

a parser that parses many ops, and a final p, and applies all of the results left-associatively.

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 ops. The result of p, x, is applied to each of the results of the ops, 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.

chainn1 Source #

Arguments

:: Parsec a

p, the value to be parsed

-> Parsec (a -> a -> a)

op, the operator between each value.

-> Parsec a

a parser that parses p, and possibly an op and then p, and applies the result of op to those of p in the same order.

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 return p.
  • Otherwise, parse this op followed by a single p. Then ensure that this is not followed by a further op, to enforce non-associativity. The results of the ps, x and y, are combined with the result of op, f with the application f 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.

prefix1 Source #

Arguments

:: (b -> a)

wrap, a function converting bs into as

-> Parsec (a -> b)

op, the prefix operator to repeatedly parse before p

-> Parsec a

p, the single value to be parsed

-> Parsec b

a parser that parses at least one op, and a final p, and applies their results right-associatively.

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 ops. The result of p, x, is applied first to wrap, and then to each of the results of the ops, 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.

postfix1 Source #

Arguments

:: (b -> a)

a function converting the value type a into the result type b

-> Parsec a

p, the single value to be parsed

-> Parsec (a -> b)

op, the postfix operator to repeatedly parse after p.

-> Parsec b

a parser that parses at least one op, and a final p, and applies all of the results left-associatively.

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 ops. The result of p, x, is applied first to wrap, and then to each of the results of the ops, 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.