- ExprIO
- IndentIO
- indented <TOKEN, A> (int offset, Parser<TOKEN, A> p)
- indented <TOKEN, A> (Parser<TOKEN, A> p)
- indented1 <TOKEN, A> (Parser<TOKEN, A> p)
- indented2 <TOKEN, A> (Parser<TOKEN, A> p)
- indented4 <TOKEN, A> (Parser<TOKEN, A> p)
- ItemIO
- item <A> (A c)
- satisfy <A> (Func<A, bool> pred)
- oneOf <A> (Seq<A> str)
- noneOf <A> (Seq<A> str)
- anyItem <A> ()
- str <A> (Seq<A> s)
- PrimIO
- parse <I, O> (Parser<I, O> p, PString<I> input)
- parse <I, O> (Parser<I, O> p, Seq<I> input, Func<I, Pos> tokenPos)
- lazyp <I, O> (Func<Parser<I, O>> fn)
- unitp <I> ()
- setState <I, S> (S state)
- getState <I, S> ()
- getPos <I> ()
- getIndex <I> ()
- unexpected <I, O> (string msg)
- failure <I, O> (string msg)
- result <I, O> (O value)
- zero <I, O> ()
- either <I, O> (Parser<I, O> p, Parser<I, O> q)
- choice <I, O> (params Parser<I, O>[] ps)
- choice <I, O> (Seq<Parser<I, O>> ps)
- chain <I, O> (params Parser<I, O>[] ps)
- chain <I, O> (Seq<Parser<I, O>> ps)
- attempt <I, O> (Parser<I, O> p)
- lookAhead <I, O> (Parser<I, O> p)
- many <I, O> (Parser<I, O> p)
- many1 <I, O> (Parser<I, O> p)
- skipMany <I, O> (Parser<I, O> p)
- skipMany1 <I, O> (Parser<I, O> p)
- optionOrElse <I, O> (O x, Parser<I, O> p)
- optional <I, O> (Parser<I, O> p)
- optionalSeq <I, O> (Parser<I, O> p)
- optionalList <I, O> (Parser<I, O> p)
- optionalArray <I, O> (Parser<I, O> p)
- between <L, R, I, O> (Parser<I, L> open, Parser<I, R> close, Parser<I, O> inner)
- sepBy1 <S, I, O> (Parser<I, O> p, Parser<I, S> sep)
- sepBy <S, I, O> (Parser<I, O> p, Parser<I, S> sep)
- sepEndBy1 <S, I, O> (Parser<I, O> p, Parser<I, S> sep)
- sepEndBy <S, I, O> (Parser<I, O> p, Parser<I, S> sep)
- endBy1 <S, I, O> (Parser<I, O> p, Parser<I, S> sep)
- endBy <S, I, O> (Parser<I, O> p, Parser<I, S> sep)
- count <S, I, O> (int n, Parser<I, O> p)
- chainr <I, O> (Parser<I, O> p, Parser<I, Func<O, O, O>> op, O x)
- chainl <I, O> (Parser<I, O> p, Parser<I, Func<O, O, O>> op, O x)
- chainr1 <I, O> (Parser<I, O> p, Parser<I, Func<O, O, O>> op)
- chainl1 <I, O> (Parser<I, O> p, Parser<I, Func<O, O, O>> op)
- eof <I> ()
- notFollowedBy <I, O> (Parser<I, O> p)
- asString <I> (Parser<I, Seq<char>> p)
- asString <I, O> (Parser<I, Seq<O>> p)
- asString <I, O> (Parser<I, O> p)
- asInteger <I> (Parser<I, Seq<char>> p)
- asInteger <I> (Parser<I, string> p)
- asInteger <I> (Parser<I, Seq<char>> p, int fromBase)
- asInteger <I> (Parser<I, string> p, int fromBase)
- asDouble <I> (Parser<I, Seq<char>> p)
- asDouble <I> (Parser<I, string> p)
- asFloat <I> (Parser<I, Seq<char>> p)
- asFloat <I> (Parser<I, string> p)
- manyUntil <I, O, U> (Parser<I, O> p, Parser<I, U> end)
- children <TOKEN, A> (Parser<TOKEN, Seq<TOKEN>> children, Parser<TOKEN, A> p)
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.
This is an example of an expression parser that handles prefix signs, postfix increment and basic arithmetic.
Parser
var binary = fun((string name, Func<int,int,int> f, Assoc assoc) =>
Operator.Infix
var prefix = fun((string name, Func<int,int> f) =>
Operator.Prefix
var postfix = fun((string name, Func<int,int> f) =>
Operator.Postfix
Operator
expr = buildExpressionParser(table,term).label("expression")
var res = parse(expr, "(50 + 20) / 2");
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
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
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
Commonly used character parsers.
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'.
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
The primitive parser combinators
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, 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'.
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'.
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.
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.
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.
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.
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.
returns | Enumerable of the returned values of p. |
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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([]).
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
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
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.
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.
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.
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<TOKEN, A> children <TOKEN, A> (Parser<TOKEN, Seq<TOKEN>> children, Parser<TOKEN, A> p) Source #
Parse child tokens
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 |