Header menu logo XParsec.ActiveParsers

ActiveParser Module

Functions that convert between XParsec parsers and active parsers.

Functions and values

Function or value Description

ActiveParser.cursor reader

Full Usage: ActiveParser.cursor reader

Parameters:
    reader : Reader<'T, 'State, 'Input>

Returns: ParseResult<Cursor<'T, 'State, 'Input>, 'T, 'State>
Modifiers: inline
Type parameters: 'T, 'State, 'Input

A parser that returns a cursor at the current position without consuming input. With seek, it lets a parser { } expression match active patterns:

 parser {
     match! ActiveParser.cursor with
     | PString "let" next ->
         do! ActiveParser.seek next
         ...
     | _ -> return! fail (Message "Expected let")
 }

reader : Reader<'T, 'State, 'Input>
Returns: ParseResult<Cursor<'T, 'State, 'Input>, 'T, 'State>

ActiveParser.map mapping p cursor

Full Usage: ActiveParser.map mapping p cursor

Parameters:
    mapping : 'A -> 'B
    p : ActiveParser<'A, 'T, 'State, 'Input>
    cursor : Cursor<'T, 'State, 'Input>

Returns: ('B * Cursor<'T, 'State, 'Input>) voption
Modifiers: inline
Type parameters: 'A, 'B, 'T, 'State, 'Input

Applies to the value of the active parser .

mapping : 'A -> 'B
p : ActiveParser<'A, 'T, 'State, 'Input>
cursor : Cursor<'T, 'State, 'Input>
Returns: ('B * Cursor<'T, 'State, 'Input>) voption

ActiveParser.noMatch

Full Usage: ActiveParser.noMatch

Returns: ErrorType<'T, 'State>

The error of when no pattern matched.

Returns: ErrorType<'T, 'State>

ActiveParser.ofParser p cursor

Full Usage: ActiveParser.ofParser p cursor

Parameters:
    p : Parser<'A, 'T, 'State, 'Input>
    cursor : Cursor<'T, 'State, 'Input>

Returns: ('A * Cursor<'T, 'State, 'Input>) voption
Modifiers: inline
Type parameters: 'A, 'T, 'State, 'Input

Turns an XParsec parser into an active parser: runs p from the cursor and returns its result with the cursor after it, or ValueNone when it fails.

The error of a failed parser is dropped; label the parser built with toParserL instead.

p : Parser<'A, 'T, 'State, 'Input>
cursor : Cursor<'T, 'State, 'Input>
Returns: ('A * Cursor<'T, 'State, 'Input>) voption

ActiveParser.ofReader reader

Full Usage: ActiveParser.ofReader reader

Parameters:
    reader : Reader<'T, 'State, 'Input>

Returns: Cursor<'T, 'State, 'Input>
Modifiers: inline
Type parameters: 'T, 'State, 'Input

Returns a cursor at the current position of the reader.

reader : Reader<'T, 'State, 'Input>
Returns: Cursor<'T, 'State, 'Input>

ActiveParser.seek cursor reader

Full Usage: ActiveParser.seek cursor reader

Parameters:
    cursor : Cursor<'T, 'State, 'Input>
    reader : Reader<'T, 'State, 'Input>

Returns: ParseResult<unit, 'T, 'State>
Modifiers: inline
Type parameters: 'T, 'State, 'Input

A parser that moves the reader to the cursor, which must come from the same reader.

cursor : Cursor<'T, 'State, 'Input>
reader : Reader<'T, 'State, 'Input>
Returns: ParseResult<unit, 'T, 'State>

ActiveParser.toParser p reader

Full Usage: ActiveParser.toParser p reader

Parameters:
Returns: ParseResult<'A, 'T, 'State>
Modifiers: inline
Type parameters: 'A, 'T, 'State, 'Input

Turns an active parser into an XParsec parser that fails with noMatch when p does not match. On failure the reader is left where it started.

The usual source of p is a function that matches its cursor:

 let token = ActiveParser.toParser (fun cursor ->
     match cursor with
     | Number (n, next) -> ValueSome(Token.Number n, next)
     | PChar '+' next -> ValueSome(Token.Plus, next)
     | _ -> ValueNone)
val token: obj
union case ValueOption.ValueSome: 'T -> ValueOption<'T>
union case ValueOption.ValueNone: ValueOption<'T>

p : ActiveParser<'A, 'T, 'State, 'Input>
reader : Reader<'T, 'State, 'Input>
Returns: ParseResult<'A, 'T, 'State>

ActiveParser.toParserL message p

Full Usage: ActiveParser.toParserL message p

Parameters:
Returns: Parser<'A, 'T, 'State, 'Input>
Modifiers: inline
Type parameters: 'A, 'T, 'State, 'Input

Turns an active parser into an XParsec parser that fails with message when p does not match. On failure the reader is left where it started.

message : string
p : ActiveParser<'A, 'T, 'State, 'Input>
Returns: Parser<'A, 'T, 'State, 'Input>

ActiveParser.unfold step cursor

Full Usage: ActiveParser.unfold step cursor

Parameters:
Returns: (ImmutableArray<'A> * Cursor<'T, 'State, 'Input>) voption

Runs step from the cursor, then from the cursor it returns, until it returns Break or BreakWith, and returns the values of the ContinueWith and BreakWith steps. Always matches.

This is the shape of a tokenizer written with active patterns:

 let tokens = ActiveParser.unfold (fun cursor ->
     match cursor with
     | EOF -> Break, cursor
     | Spaces1 next -> Continue, next
     | Number (n, next) -> ContinueWith(Token.Number n), next
     | _ -> Break, cursor)
val tokens: obj

Raises InfiniteLoopException{S} when a Continue or ContinueWith step returns its cursor unchanged, like many.

step : Cursor<'T, 'State, 'Input> -> Unfold<'A> * Cursor<'T, 'State, 'Input>
cursor : Cursor<'T, 'State, 'Input>
Returns: (ImmutableArray<'A> * Cursor<'T, 'State, 'Input>) voption

Type something to start searching.