- Char
- ch (char c)
- ch <EQ> (char c)
- satisfy (Func<char, bool> pred)
- oneOf (string str)
- oneOf (params char[] str)
- noneOf (string str)
- noneOf (params char[] str)
- space
- spaces
- control
- tab
- newline
- CR
- LF
- CRLF
- endOfLine
- digit
- letter
- alphaNum
- lower
- upper
- punctuation
- separator
- symbolchar
- octDigit
- hexDigit
- anyChar
- str (string s)
- str <EQ> (string s)
- Expr
- Indent
- indented <T> (int offset, Parser<T> p)
- indented <T> (Parser<T> p)
- indented1 <T> (Parser<T> p)
- indented2 <T> (Parser<T> p)
- indented4 <T> (Parser<T> p)
- Prim
- parse <T> (Parser<T> p, PString input)
- parse <T> (Parser<T> p, string input)
- lazyp <T> (Func<Parser<T>> fn)
- unitp
- setState <T> (T state)
- getState <T> ()
- getPos
- getIndex
- unexpected <T> (string msg)
- unexpected <T> (Pos pos, string msg)
- failure <T> (string msg)
- result <T> (T value)
- zero <T> ()
- either <T> (Parser<T> p, Parser<T> q)
- choice <T> (params Parser<T>[] ps)
- choice <T> (Seq<Parser<T>> ps)
- chain <T> (params Parser<T>[] ps)
- chain <T> (Seq<Parser<T>> ps)
- cons <T> (Parser<T> p, Parser<Seq<T>> ps)
- flatten <T> (Parser<Seq<Seq<T>>> ssp)
- attempt <T> (Parser<T> p)
- lookAhead <T> (Parser<T> p)
- many <T> (Parser<T> p)
- manyn <T> (Parser<T> p, int n)
- manyn0 <T> (Parser<T> p, int n)
- manyn1 <T> (Parser<T> p, int n)
- many1 <T> (Parser<T> p)
- skipMany <T> (Parser<T> p)
- skipMany1 <T> (Parser<T> p)
- optionOrElse <T> (T x, Parser<T> p)
- optional <T> (Parser<T> p)
- optionalList <T> (Parser<T> p)
- optionalSeq <T> (Parser<T> p)
- optionalArray <T> (Parser<T> p)
- between <L, R, T> (Parser<L> open, Parser<R> close, Parser<T> inner)
- sepBy1 <S, T> (Parser<T> p, Parser<S> sep)
- sepBy <S, T> (Parser<T> p, Parser<S> sep)
- sepEndBy1 <S, T> (Parser<T> p, Parser<S> sep)
- sepEndBy <S, T> (Parser<T> p, Parser<S> sep)
- endBy1 <S, T> (Parser<T> p, Parser<S> sep)
- endBy <S, T> (Parser<T> p, Parser<S> sep)
- count <T> (int n, Parser<T> p)
- chainr <T> (Parser<T> p, Parser<Func<T, T, T>> op, T x)
- chainl <T> (Parser<T> p, Parser<Func<T, T, T>> op, T x)
- chainr1 <T> (Parser<T> p, Parser<Func<T, T, T>> op)
- chainl1 <T> (Parser<T> p, Parser<Func<T, T, T>> op)
- eof
- notFollowedBy <T> (Parser<T> p, string? label = null)
- asString (Parser<Seq<char>> p)
- asInteger (Parser<Seq<char>> p)
- asInteger (Parser<Seq<char>> p, int fromBase)
- asDouble (Parser<Seq<char>> p)
- asFloat (Parser<Seq<char>> p)
- manyUntil <T, U> (Parser<T> p, Parser<U> end)
- Token
- Token2
Commonly used character parsers.
field Parser<char> space Source #
Parses a white space character (any character which satisfies 'System.Char.IsWhiteSpace') 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> endOfLine Source #
Parses a CRLF (see 'crlf') or LF (see 'newline') end-of-line. Returns a newline character('\n').
field Parser<char> punctuation Source #
Parses a punctuation character Returns the parsed character.
field Parser<char> symbolchar Source #
Parses a symbol character Returns the parsed character.
method Parser<char> ch (char c) Source #
ch(c) parses a single character c
returns | The parsed character |
method Parser<char> ch <EQ> (char c) Source #
ch(c) parses a single character c
type | EQ | Eq〈char〉 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'.
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")
returns | The parsed character. |
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.
This is an example of an expression parser that handles prefix signs, postfix increment and basic arithmetic.
Parser〈int〉 expr = null;
var binary = fun((string name, Func〈int, int, int〉 f, Assoc assoc) =〉 Operator.Infix〈int〉( assoc, from x in reservedOp(name) select fun );
var prefix = fun((string name, Func〈int, int〉 f) =〉 Operator.Prefix〈int〉(from x in reservedOp(name) select fun );
var postfix = fun((string name, Func〈int, int〉 f) =〉 Operator.Postfix〈int〉(from x in reservedOp(name) select fun );
Operator〈int〉[][] 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");
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
The primitive parser combinators
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
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'.
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'.
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'.
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.
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.
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.
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.
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
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
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.
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<Seq<T>> manyn <T> (Parser<T> p, int n) Source #
manyn(p, n) applies the parser p n times.
returns | Enumerable of the returned values of p. |
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.
returns | Enumerable of the returned values of p. |
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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([]).
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
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
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.
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.
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.
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
A helper module to parse lexical elements (tokens)
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; ...
A helper module to parse lexical elements (tokens)
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; ...