LanguageExt.Parsec

LanguageExt.Parsec ParserIOs

Contents

class ExprIO Source #

Methods

method Parser<I, O> buildExpressionParser <I, O> ( Operator<I, O>[][] operators, Parser<I, O> simpleExpr) Source #

Convert an OperatorTable and basic term parser into a fully fledged expression parser

buildExpressionParser(table,term) builds an expression parser for terms term with operators from table, taking the associativity and precedence specified in table into account. Prefix and postfix operators of the same precedence can only occur once (i.e. --2 is not allowed if '-' is prefix negate). Prefix and postfix operators of the same precedence associate to the left (i.e. if ++ is postfix increment, than -2++ equals -1, not -3).

The buildExpressionParser function takes care of all the complexity involved in building expression parser.

See remarks.

Examples

This is an example of an expression parser that handles prefix signs, postfix increment and basic arithmetic.

Parser expr = null;

var binary = fun((string name, Func<int,int,int> f, Assoc assoc) => Operator.Infix( assoc, from x in reservedOp(name) select fun );

var prefix = fun((string name, Func<int,int> f) => Operator.Prefix(from x in reservedOp(name) select fun );

var postfix = fun((string name, Func<int,int> f) => Operator.Postfix(from x in reservedOp(name) select fun );

Operator[][] table = { { prefix("-",negate), prefix("+",id) } , { postfix("++", incr) } , { binary("*", mult) Assoc.Left), binary("/", div, Assoc.Left) } , { binary("+", add, Assoc.Left), binary("-", subtr, Assoc.Left) } }; ] var term = either(parens(expr),natural).label("simple expression")

expr = buildExpressionParser(table,term).label("expression")

var res = parse(expr, "(50 + 20) / 2");

class IndentIO Source #

Methods

method Parser<TOKEN, A> indented <TOKEN, A> (int offset, Parser<TOKEN, A> p) Source #

Parses only when indented past the level of the reference

You must have provided a TokenPos to the initial PString that gives accurate column and line values for this function to work.

method Parser<TOKEN, A> indented <TOKEN, A> (Parser<TOKEN, A> p) Source #

Parses only when indented zero or more characters past the level of the reference

You must have provided a TokenPos to the initial PString that gives accurate column and line values for this function to work.

method Parser<TOKEN, A> indented1 <TOKEN, A> (Parser<TOKEN, A> p) Source #

Parses only when indented one or more characters past the level of the reference

You must have provided a TokenPos to the initial PString that gives accurate column and line values for this function to work.

method Parser<TOKEN, A> indented2 <TOKEN, A> (Parser<TOKEN, A> p) Source #

Parses only when indented two or more characters past the level of the reference

You must have provided a TokenPos to the initial PString that gives accurate column and line values for this function to work.

method Parser<TOKEN, A> indented4 <TOKEN, A> (Parser<TOKEN, A> p) Source #

Parses only when indented four or more characters past the level of the reference

You must have provided a TokenPos to the initial PString that gives accurate column and line values for this function to work.

class ItemIO Source #

Commonly used character parsers.

Methods

method Parser<A, A> item <A> (A c) Source #

item(c) parses a single I

Parameters

returns

The parsed character

method Parser<A, A> satisfy <A> (Func<A, bool> pred) Source #

The parser satisfy(pred) succeeds for any character for which the supplied function pred returns 'True'.

Parameters

returns

The character that is actually parsed.

method Parser<A, A> oneOf <A> (Seq<A> str) Source #

oneOf(str) succeeds if the current character is in the supplied list of characters str. Returns the parsed character. See also satisfy

method Parser<A, A> noneOf <A> (Seq<A> str) Source #

As the dual of 'oneOf', noneOf(str) succeeds if the current character not in the supplied list of characters str.

var consonant = noneOf("aeiou")

Parameters

returns

The parsed character.

method Parser<A, A> anyItem <A> () Source #

The parser anyChar accepts any kind of character.

method Parser<A, Seq<A>> str <A> (Seq<A> s) Source #

Parse a string

class PrimIO Source #

The primitive parser combinators

Methods

method ParserResult<I, O> parse <I, O> (Parser<I, O> p, PString<I> input) Source #

Run the parser p with the input provided

method ParserResult<I, O> parse <I, O> (Parser<I, O> p, Seq<I> input, Func<I, Pos> tokenPos) Source #

Run the parser p with the input provided

method Parser<I, O> lazyp <I, O> (Func<Parser<I, O>> fn) Source #

Lazy parser - useful in recursive scenarios.

method Parser<I, Unit> unitp <I> () Source #

This parser is useful to put at the top of LINQ expressions, it makes it easier to put breakpoints on the actual first parser in an expression. It returns unit

method Parser<I, Unit> setState <I, S> (S state) Source #

Special parser for setting user-state that propagates through the computation.

method Parser<I, S> getState <I, S> () Source #

Special parser for getting user-state that was previously set with setState

method Parser<I, Pos> getPos <I> () Source #

Get the current position of the parser in the source as a line and column index (starting at 1 for both)

method Parser<I, int> getIndex <I> () Source #

Get the current index into the source

method Parser<I, O> unexpected <I, O> (string msg) Source #

The parser unexpected(msg) always fails with an Unexpect error message msg without consuming any input.

The parsers 'failure', 'label' and 'unexpected' are the three parsers used to generate error messages. Of these, only 'label' is commonly used. For an example of the use of unexpected, see the definition of 'notFollowedBy'.

Parameters

param msg

Error message to use when parsed

method Parser<I, O> failure <I, O> (string msg) Source #

The parser failure(msg) always fails with a Message error without consuming any input.

The parsers 'failure', 'label' and 'unexpected' are the three parsers used to generate error messages. Of these, only 'label' is commonly used. For an example of the use of unexpected, see the definition of 'notFollowedBy'.

Parameters

param msg

Error message to use when parsed

method Parser<I, O> result <I, O> (O value) Source #

Always success parser. Returns the value provided. This is monad return for the Parser monad

method Parser<I, O> zero <I, O> () Source #

Always fails (with an Unknown error) without consuming any input

method Parser<I, O> either <I, O> (Parser<I, O> p, Parser<I, O> q) Source #

This combinator implements choice. The parser either(p,q) first applies p. If it succeeds, the value of p is returned. If p fails /without consuming any input/, parser q is tried.

This combinator is the mplus behaviour of the Parser monad.

The parser is called /predictive/ since q is only tried when parser p didn't consume any input (i.e.. the look ahead is 1).

This non-backtracking behaviour allows for both an efficient implementation of the parser combinators and the generation of good error messages.

method Parser<I, O> choice <I, O> (params Parser<I, O>[] ps) Source #

choice(ps) tries to apply the parsers in the list ps in order, until one of them succeeds.

Parameters

returns

The value of the succeeding parser.

method Parser<I, O> choice <I, O> (Seq<Parser<I, O>> ps) Source #

choice(ps) tries to apply the parsers in the list ps in order, until one of them succeeds.

Parameters

returns

The value of the succeeding parser.

method Parser<I, Seq<O>> chain <I, O> (params Parser<I, O>[] ps) Source #

Runs a sequence of parsers, if any fail then the failure state is returned immediately and subsequence parsers are not run.

Parameters

returns

The result of each parser as an enumerable.

method Parser<I, Seq<O>> chain <I, O> (Seq<Parser<I, O>> ps) Source #

Runs a sequence of parsers, if any fail then the failure state is returned immediately and subsequence parsers are not run.

Parameters

returns

The result of each parser as an enumerable.

method Parser<I, O> attempt <I, O> (Parser<I, O> p) Source #

The parser attempt(p) behaves like parser p, except that it pretends that it hasn't consumed any input when an error occurs.

This combinator is used whenever arbitrary look ahead is needed. Since it pretends that it hasn't consumed any input when p fails, the either combinator will try its second alternative even when the first parser failed while consuming input.

See remarks.

The attempt combinator can for example be used to distinguish identifiers and reserved words. Both reserved words and identifiers are a sequence of letters. Whenever we expect a certain reserved word where we can also expect an identifier we have to use the attempt combinator. Suppose we write:

var expr = either(letExpr, identifier).label("expression");

var letExpr = from x in str("let") ... select ...;

var identifier = many1(letter);

If the user writes "lexical", the parser fails with: unexpected "x", expecting "t" in "let". Indeed, since the either combinator only tries alternatives when the first alternative hasn't consumed input, the identifier parser is never tried (because the prefix "le" of the str("let") parser is already consumed). The right behaviour can be obtained by adding the attempt combinator:

var expr = either(letExpr, identifier).label("expression");

var letExpr = from x in attempt(str("let")) ... select ...;

var identifier = many1(letter);

method Parser<I, O> lookAhead <I, O> (Parser<I, O> p) Source #

lookAhead(p) parses p without consuming any input.

If p fails and consumes some input, so does lookAhead(p). Combine with 'attempt' if this is undesirable.

method Parser<I, Seq<O>> many <I, O> (Parser<I, O> p) Source #

many(p) applies the parser p zero or more times.

Parameters

returns

Enumerable of the returned values of p.

Examples

var identifier  = from c in letter
                  from cs in many(letterOrDigit)
                  select c.Cons(cs)

method Parser<I, Seq<O>> many1 <I, O> (Parser<I, O> p) Source #

many1(p) applies the parser p one or more times.

Parameters

returns

Enumerable of the returned values of p.

method Parser<I, Unit> skipMany <I, O> (Parser<I, O> p) Source #

skipMany(p) applies the parser p zero or more times, skipping its result.

method Parser<I, Unit> skipMany1 <I, O> (Parser<I, O> p) Source #

skipMany(p) applies the parser p one or more times, skipping its result.

method Parser<I, O> optionOrElse <I, O> (O x, Parser<I, O> p) Source #

optionOrElse(x, p) tries to apply parser p. If p fails without consuming input, it returns the value x, otherwise the value returned by p.

method Parser<I, Option<O>> optional <I, O> (Parser<I, O> p) Source #

optional(p) tries to apply parser p. If p fails without consuming input, it return 'None', otherwise it returns 'Some' the value returned by p.

method Parser<I, Seq<O>> optionalSeq <I, O> (Parser<I, O> p) Source #

optionalSeq(p) tries to apply parser p. If p fails without consuming input, it return an empty IEnumerable, otherwise it returns a one item IEnumerable with the result of p.

Parameters

returns

A list of 0 or 1 parsed items

method Parser<I, Lst<O>> optionalList <I, O> (Parser<I, O> p) Source #

optionalList(p) tries to apply parser p. If p fails without consuming input, it return [], otherwise it returns a one item Lst with the result of p.

Parameters

returns

A list of 0 or 1 parsed items

method Parser<I, O[]> optionalArray <I, O> (Parser<I, O> p) Source #

optionalArray(p) tries to apply parser p. If p fails without consuming input, it return [], otherwise it returns a one item array with the result of p.

Parameters

returns

A list of 0 or 1 parsed items

method Parser<I, O> between <L, R, I, O> (Parser<I, L> open, Parser<I, R> close, Parser<I, O> inner) Source #

between(open,close,p) parses open, followed by p and close.

Parameters

returns

The value returned by p.

method Parser<I, Seq<O>> sepBy1 <S, I, O> (Parser<I, O> p, Parser<I, S> sep) Source #

sepBy1(p,sep) parses one or more occurrences of p, separated by sep.

Parameters

returns

A list of values returned by p.

method Parser<I, Seq<O>> sepBy <S, I, O> (Parser<I, O> p, Parser<I, S> sep) Source #

sepBy(p,sep) parses zero or more occurrences of p, separated by sep.

Parameters

returns

A list of values returned by p.

method Parser<I, Seq<O>> sepEndBy1 <S, I, O> (Parser<I, O> p, Parser<I, S> sep) Source #

sepEndBy1(p,sep) parses one or more occurrences of p, separated and optionally ended by sep.

Parameters

returns

A list of values returned by p.

method Parser<I, Seq<O>> sepEndBy <S, I, O> (Parser<I, O> p, Parser<I, S> sep) Source #

sepEndBy(p,sep) parses zero or more occurrences of p, separated and optionally ended by sep.

Parameters

returns

A list of values returned by p.

method Parser<I, Seq<O>> endBy1 <S, I, O> (Parser<I, O> p, Parser<I, S> sep) Source #

endBy1(p,sep) parses one or more occurrences of p, separated and ended by sep.

Parameters

returns

A list of values returned by p.

method Parser<I, Seq<O>> endBy <S, I, O> (Parser<I, O> p, Parser<I, S> sep) Source #

endBy(p,sep) parses zerp or more occurrences of p, separated and ended by sep.

Parameters

returns

A list of values returned by p.

method Parser<I, Seq<O>> count <S, I, O> (int n, Parser<I, O> p) Source #

count(n,p) parses n occurrences of p. If n is smaller or equal to zero, the parser equals to result([]).

Parameters

returns

A list of values returned by p.

method Parser<I, O> chainr <I, O> (Parser<I, O> p, Parser<I, Func<O, O, O>> op, O x) Source #

chainr(p,op,x) parses zero or more occurrences of p, separated by op

Parameters

returns

a value obtained by a right associative application of all functions returned by op to the values returned by p. If there are no occurrences of p, the value x is returned.

method Parser<I, O> chainl <I, O> (Parser<I, O> p, Parser<I, Func<O, O, O>> op, O x) Source #

chainl(p,op,x) parses zero or more occurrences of p, separated by op

Parameters

returns

a value obtained by a left associative application of all functions returned by op to the values returned by p. If there are no occurrences of p, the value x is returned.

method Parser<I, O> chainr1 <I, O> (Parser<I, O> p, Parser<I, Func<O, O, O>> op) Source #

chainr1(p,op) parses one or more occurrences of p, separated by op.

Parameters

returns

A value obtained by a right associative application of all functions returned by op to the values returned by p

method Parser<I, O> chainl1 <I, O> (Parser<I, O> p, Parser<I, Func<O, O, O>> op) Source #

chainl1(p,op) parses one or more occurrences of p, separated by op.

Parameters

returns

A value obtained by a left associative application of all functions returned by op to the values returned by p

method Parser<I, Unit> eof <I> () Source #

This parser only succeeds at the end of the input. This is not a primitive parser but it is defined using 'notFollowedBy'.

method Parser<I, Unit> notFollowedBy <I, O> (Parser<I, O> p) Source #

notFollowedBy(p) only succeeds when parser p fails. This parser does not consume any input.This parser can be used to implement the 'longest match' rule.

Examples

For example, when recognizing keywords (for example 'let'), we want to make sure that a keyword is not followed by a legal identifier character, in which case the keyword is actually an identifier(for example 'lets'). We can program this behaviour as follows:

var keywordLet  = attempt (from x in str("let")
                           from _ in notFollowedBy letterOrDigit
                           select x);

method Parser<I, string> asString <I> (Parser<I, Seq<char>> p) Source #

Parse a char list and convert into a string

method Parser<I, string> asString <I, O> (Parser<I, Seq<O>> p) Source #

Parse a T list and convert into a string

method Parser<I, string> asString <I, O> (Parser<I, O> p) Source #

Parse a T list and convert into a string

method Parser<I, Option<int>> asInteger <I> (Parser<I, Seq<char>> p) Source #

Parse a char list and convert into an integer

method Parser<I, Option<int>> asInteger <I> (Parser<I, string> p) Source #

Parse a char list and convert into an integer

method Parser<I, Option<int>> asInteger <I> (Parser<I, Seq<char>> p, int fromBase) Source #

Parse a char list and convert into an integer

method Parser<I, Option<int>> asInteger <I> (Parser<I, string> p, int fromBase) Source #

Parse a char list and convert into an integer

method Parser<I, Option<double>> asDouble <I> (Parser<I, Seq<char>> p) Source #

Parse a char list and convert into an double precision floating point value

method Parser<I, Option<double>> asDouble <I> (Parser<I, string> p) Source #

Parse a char list and convert into an double precision floating point value

method Parser<I, Option<float>> asFloat <I> (Parser<I, Seq<char>> p) Source #

Parse a char list and convert into an double precision floating point value

method Parser<I, Option<float>> asFloat <I> (Parser<I, string> p) Source #

Parse a char list and convert into an double precision floating point value

method Parser<I, Seq<O>> manyUntil <I, O, U> (Parser<I, O> p, Parser<I, U> end) Source #

method Parser<TOKEN, A> children <TOKEN, A> (Parser<TOKEN, Seq<TOKEN>> children, Parser<TOKEN, A> p) Source #

Parse child tokens

Parameters

type TOKEN

Token type

type A

Type of the value to parse

param children

Parser that gets the child tokens

param p

Parser to run on the child tokens

returns

Parser that parses a set of tokens then uses them as a new stream to parse