LanguageExt.Parsec

LanguageExt.Parsec Parsers

Contents

class Char Source #

Commonly used character parsers.

Fields

field Parser<char> space Source #

Parses a white space character (any character which satisfies 'System.Char.IsWhiteSpace') Returns the parsed character.

field Parser<Unit> spaces Source #

Skips zero or more white space characters. See also 'skipMany'.

field Parser<char> control Source #

Parses a control character Returns the parsed character.

field Parser<char> tab Source #

Parses a tab Returns the parsed character.

field Parser<char> newline Source #

Equivalent to LF. Parses a line-feed newline char (\n). Returns the parsed character.

field Parser<char> CR Source #

Parses a carriage-return char (\r) Returns the parsed character.

field Parser<char> LF Source #

Parses a line-feed newline char (\n) Returns the parsed character.

field Parser<char> CRLF Source #

Parses a carriage-return then line-feed Returns the new-line.

field Parser<char> endOfLine Source #

Parses a CRLF (see 'crlf') or LF (see 'newline') end-of-line. Returns a newline character('\n').

field Parser<char> digit Source #

Parses a digit Returns the parsed character.

field Parser<char> letter Source #

Parses a letter Returns the parsed character.

field Parser<char> alphaNum Source #

Parses a letter or digit Returns the parsed character.

field Parser<char> lower Source #

Parses a lowercase letter Returns the parsed character.

field Parser<char> upper Source #

Parses a uppercase letter Returns the parsed character.

field Parser<char> punctuation Source #

Parses a punctuation character Returns the parsed character.

field Parser<char> separator Source #

Parses a separator character Returns the parsed character.

field Parser<char> symbolchar Source #

Parses a symbol character Returns the parsed character.

field Parser<char> octDigit Source #

Parses an octal digit (0-7)

field Parser<char> hexDigit Source #

Parses a hex digit (0-F | 0-f)

field Parser<char> anyChar Source #

The parser anyChar accepts any kind of character.

Methods

method Parser<char> ch (char c) Source #

ch(c) parses a single character c

Parameters

returns

The parsed character

method Parser<char> ch <EQ> (char c) Source #

where EQ : Eq<char>

ch(c) parses a single character c

Parameters

type EQ

Eq trait

returns

The parsed character

method Parser<char> satisfy (Func<char, 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<char> oneOf (string 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<char> oneOf (params char[] 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<char> noneOf (string 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<char> noneOf (params char[] 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<string> str (string s) Source #

Parse a string

method Parser<string> str <EQ> (string s) Source #

where EQ: Eq<char>

class Expr Source #

Methods

method Parser<T> buildExpressionParser <T> ( Operator<T>[][] operators, Parser<T> 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 Indent Source #

Methods

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

Parses only when indented past the level of the reference

method Parser<T> indented <T> (Parser<T> p) Source #

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

method Parser<T> indented1 <T> (Parser<T> p) Source #

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

method Parser<T> indented2 <T> (Parser<T> p) Source #

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

method Parser<T> indented4 <T> (Parser<T> p) Source #

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

class Prim Source #

The primitive parser combinators

Fields

field Parser<Unit> unitp 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

field Parser<Pos> getPos Source #

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

field Parser<int> getIndex Source #

Get the current index into the source

field Parser<Unit> eof Source #

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

Methods

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

Run the parser p with the input provided

method ParserResult<T> parse <T> (Parser<T> p, string input) Source #

Run the parser p with the input provided

method Parser<T> lazyp <T> (Func<Parser<T>> fn) Source #

Lazy parser - useful in recursive scenarios.

method Parser<Unit> setState <T> (T state) Source #

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

method Parser<T> getState <T> () Source #

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

method Parser<T> unexpected <T> (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 'Text.Parsec.Combinator.notFollowedBy'.

Parameters

param msg

Error message to use when parsed

method Parser<T> unexpected <T> (Pos pos, 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 'Text.Parsec.Combinator.notFollowedBy'.

Parameters

param pos

Location of the failure

param msg

Error message to use when parsed

method Parser<T> failure <T> (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 'Text.Parsec.Combinator.notFollowedBy'.

Parameters

param msg

Error message to use when parsed

method Parser<T> result <T> (T value) Source #

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

method Parser<T> zero <T> () Source #

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

method Parser<T> either <T> (Parser<T> p, Parser<T> 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<T> choice <T> (params Parser<T>[] 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<T> choice <T> (Seq<Parser<T>> 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<Seq<T>> chain <T> (params Parser<T>[] 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<Seq<T>> chain <T> (Seq<Parser<T>> 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<Seq<T>> cons <T> (Parser<T> p, Parser<Seq<T>> ps) Source #

Cons for parser results

Parameters

type T
param p
param ps
returns

method Parser<Seq<T>> flatten <T> (Parser<Seq<Seq<T>>> ssp) Source #

Flattens parser result: Seq of Seq of T => Seq of T

Parameters

returns

Parser with flattened result sequence

method Parser<T> attempt <T> (Parser<T> 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<T> lookAhead <T> (Parser<T> 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<Seq<T>> many <T> (Parser<T> 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<Seq<T>> manyn <T> (Parser<T> p, int n) Source #

manyn(p, n) applies the parser p n times.

Parameters

returns

Enumerable of the returned values of p.

Examples

var identifier  = from c in letter
                  from cs in manyn(letterOrDigit, 4)
                  select c.Cons(cs)

method Parser<Seq<T>> manyn0 <T> (Parser<T> p, int n) Source #

manyn0(p) applies the parser p zero or up to a maximum of n times.

Parameters

returns

Enumerable of the returned values of p.

Examples

var identifier  = from c in letter
                  from cs in manyn0(letterOrDigit, 4)
                  select c.Cons(cs)

method Parser<Seq<T>> manyn1 <T> (Parser<T> p, int n) Source #

manyn1(p) applies the parser p one or up to a maximum of n times.

Parameters

returns

Enumerable of the returned values of p.

method Parser<Seq<T>> many1 <T> (Parser<T> p) Source #

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

Parameters

returns

Enumerable of the returned values of p.

method Parser<Unit> skipMany <T> (Parser<T> p) Source #

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

method Parser<Unit> skipMany1 <T> (Parser<T> p) Source #

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

method Parser<T> optionOrElse <T> (T x, Parser<T> 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<Option<T>> optional <T> (Parser<T> 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<Lst<T>> optionalList <T> (Parser<T> 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<Seq<T>> optionalSeq <T> (Parser<T> 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<T[]> optionalArray <T> (Parser<T> 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<T> between <L, R, T> (Parser<L> open, Parser<R> close, Parser<T> inner) Source #

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

Parameters

returns

The value returned by p.

method Parser<Seq<T>> sepBy1 <S, T> (Parser<T> p, Parser<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<Seq<T>> sepBy <S, T> (Parser<T> p, Parser<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<Seq<T>> sepEndBy1 <S, T> (Parser<T> p, Parser<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<Seq<T>> sepEndBy <S, T> (Parser<T> p, Parser<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<Seq<T>> endBy1 <S, T> (Parser<T> p, Parser<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<Seq<T>> endBy <S, T> (Parser<T> p, Parser<S> sep) Source #

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

Parameters

returns

A list of values returned by p.

method Parser<Seq<T>> count <T> (int n, Parser<T> 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<T> chainr <T> (Parser<T> p, Parser<Func<T, T, T>> op, T 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<T> chainl <T> (Parser<T> p, Parser<Func<T, T, T>> op, T 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<T> chainr1 <T> (Parser<T> p, Parser<Func<T, T, T>> 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<T> chainl1 <T> (Parser<T> p, Parser<Func<T, T, T>> 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<Unit> notFollowedBy <T> (Parser<T> p, string? label = null) 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<string> asString (Parser<Seq<char>> p) Source #

Parse a char list and convert into a string

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

Parse a char list and convert into an integer

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

Parse a char list and convert into an integer

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

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

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

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

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

class Token Source #

A helper module to parse lexical elements (tokens)

Methods

method GenTokenParser makeTokenParser (GenLanguageDef def) Source #

Given a LanguageDef, create a token parser.

The expression makeTokenParser(language) creates a 'GenTokenParser' record that contains lexical parsers that are defined using the definitions in the language record.

The use of this function is quite stylised - one imports the appropriate language definition and selects the lexical parsers that are needed from the resulting 'GenTokenParser'.

// The parser ...

var expr = either( parens(expr), identifier, ... )

// The lexer var lexer = makeTokenParser(langDef)

var parens = lexer.Parens(p); var braces = lexer.Braces(p); var identifier = lexer.Identifier; var reserved = lexer.Reserved; ...

class Token2 Source #

A helper module to parse lexical elements (tokens)

Methods

method GenTokenParser2 makeTokenParser (GenLanguageDef def) Source #

Given a LanguageDef, create a token parser.

The expression makeTokenParser(language) creates a 'GenTokenParser' record that contains lexical parsers that are defined using the definitions in the language record.

The use of this function is quite stylised - one imports the appropriate language definition and selects the lexical parsers that are needed from the resulting 'GenTokenParser'.

// The parser ...

var expr = either( parens(expr), identifier, ... )

// The lexer var lexer = makeTokenParser(langDef)

var parens = lexer.Parens(p); var braces = lexer.Braces(p); var identifier = lexer.Identifier; var reserved = lexer.Reserved; ...