Calculemus


Parser Module

Generic functions for parsing.

Note

The general function for parsing in the original code was in the Intro module and has been moved here for isolation purposes.

Functions and values

Function or value Description

make_parser pfn s

Full Usage: make_parser pfn s

Parameters:
    pfn : string list -> 'a * 'b list - The input parser.
    s : string - The input string to be parsed.

Returns: 'a The string parsed based on the input parser logic.

Generic function to impose lexing and exhaustion checking on a parser.

A wrapper function that explodes the input string, lexically analyzes it, parses the sequence of tokens and then internally checks that no input remains unparsed.

pfn : string list -> 'a * 'b list

The input parser.

s : string

The input string to be parsed.

Returns: 'a

The string parsed based on the input parser logic.

Example

 // An oversimplified parser
 let rec parseIntList i = 
     match parseInt i with
     | e1, "," :: i1 ->
         let e2, i2 = parseIntList i1
         e1@e2, i2
     | x -> x
 and parseInt i = 
     match i with
     | [] -> failwith "eof"
     | tok :: i1 -> [int tok], i1
 
 make_parser parseIntList "11,12"
val parseIntList: i: string list -> int list * string list
val i: string list
val parseInt: i: string list -> int list * string list
val e1: int list
val i1: string list
val e2: int list
val i2: string list
val x: int list * string list
val failwith: message: string -> 'T
val tok: string
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Evaluates to [11; 12].

nextin inp tok

Full Usage: nextin inp tok

Parameters:
    inp : 'a list
    tok : 'a

Returns: bool

Checks if the head of a list (typically the list of unparsed input) is some particular item, but also ?rst checks that the list is nonempty before looking at its head.

inp : 'a list
tok : 'a
Returns: bool

papply f (ast, rest)

Full Usage: papply f (ast, rest)

Parameters:
    f : 'a -> 'b
    ast : 'a
    rest : 'c

Returns: 'b * 'c
Modifiers: inline
Type parameters: 'a, 'b, 'c

Applies a function to the first element of a pair, the idea being to modify the returned abstract syntax tree while leaving the 'unparsed input' alone.

f : 'a -> 'b
ast : 'a
rest : 'c
Returns: 'b * 'c

parse_bracketed subparser cbra inp

Full Usage: parse_bracketed subparser cbra inp

Parameters:
    subparser : 'a -> 'b * 'c list
    cbra : 'c
    inp : 'a

Returns: 'b * 'c list

Deals with the common situation of syntactic items enclosed in brackets. It simply calls the subparser and then checks and eliminates the closing bracket. In principle, the terminating character can be anything, so this function could equally be used for other purposes, but we will always use ')' for the cbra ('closing bracket') argument.

subparser : 'a -> 'b * 'c list
cbra : 'c
inp : 'a
Returns: 'b * 'c list

parse_ginfix opsym opupdate sof subparser inp

Full Usage: parse_ginfix opsym opupdate sof subparser inp

Parameters:
    opsym : 'a
    opupdate : ('b -> 'c) -> 'b -> 'b -> 'c - Modifies the function appropriately when a new item is parsed.
    sof : 'b -> 'c - Takes the current input and combines it in some way with the items arrived at so far.
    subparser : 'a list -> 'b * 'a list
    inp : 'a list

Returns: 'c * 'a list

General parsing of iterated infixes.

opsym : 'a
opupdate : ('b -> 'c) -> 'b -> 'b -> 'c

Modifies the function appropriately when a new item is parsed.

sof : 'b -> 'c

Takes the current input and combines it in some way with the items arrived at so far.

subparser : 'a list -> 'b * 'a list
inp : 'a list
Returns: 'c * 'a list

parse_left_infix opsym opcon

Full Usage: parse_left_infix opsym opcon

Parameters:
    opsym : 'a
    opcon : 'b * 'b -> 'b

Returns: ('a list -> 'b * 'a list) -> 'a list -> 'b * 'a list

Parses left infix.

opsym : 'a
opcon : 'b * 'b -> 'b
Returns: ('a list -> 'b * 'a list) -> 'a list -> 'b * 'a list

parse_list opsym

Full Usage: parse_list opsym

Parameters:
    opsym : 'a

Returns: ('a list -> 'b * 'a list) -> 'a list -> 'b list * 'a list

Parses a list: used to parse the list of arguments to a function.

opsym : 'a
Returns: ('a list -> 'b * 'a list) -> 'a list -> 'b list * 'a list

parse_right_infix opsym opcon

Full Usage: parse_right_infix opsym opcon

Parameters:
    opsym : 'a
    opcon : 'b * 'b -> 'b

Returns: ('a list -> 'b * 'a list) -> 'a list -> 'b * 'a list

Parses right infix.

opsym : 'a
opcon : 'b * 'b -> 'b
Returns: ('a list -> 'b * 'a list) -> 'a list -> 'b * 'a list