FsGll


FsGll

A parser combinator library based on the GLL algorithm for F#.

The FsGll library can be installed from NuGet:
PM> Install-Package FsGll

Example

This example demonstrates using this library to construct incremental parser for arithmetical expressions.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
#r "FsGll.dll"
open FsGll.Parsers.Incremental.Pure

// utilities
let isPartial = function Partial(_) -> true 
                       | _          -> false
let getPartial = List.tryFind isPartial
let parse p s = runParser<_,_> p (stringStream s)

// construct parser
let e, er = createParserForwardedToRef<_>()
let t, tr = createParserForwardedToRef<_>()
let chr c = satisfy ((=)c) 
let op = chr '+' <|> chr '-' 
     <|> chr '*' <|> chr '/'
let num = satisfy (Char.IsDigit) |>> Int32.Parse
let f = num <|> (chr '(' >>. e .>> chr')')

let sem l x r = 
    match x with 
    | '+' -> l + r 
    | '-' -> l - r
    | '*' -> l + r 
    | '/' -> l - r
    
er := pipe3 e op t sem <|> t
tr := pipe3 t op f sem <|> f

// run
let f = parse e "1+2*" |> getPartial 
let g = parse f "(3" |> getPartial
let h1 = parse f "3+4"
let h2 = parse g "+4)"

Some more info

Samples & documentation

The library comes with comprehensible documentation. It can include tutorials automatically generated from *.fsx files in the content folder. The API reference is automatically generated from Markdown comments in the library implementation.

  • Tutorial contains a further explanation of this sample library.

  • API Reference contains automatically generated documentation for all types, modules and functions in the library. This includes additional brief samples on using most of the functions.

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into a documentation. You might also want to read the library design notes to understand how it works.

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

val isPartial : (obj -> bool)

Full name: Index.isPartial
val getPartial : (obj list -> obj option)

Full name: Index.getPartial
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val tryFind : predicate:('T -> bool) -> list:'T list -> 'T option

Full name: Microsoft.FSharp.Collections.List.tryFind
val parse : p:'a -> s:'b -> 'c

Full name: Index.parse
val p : 'a
val s : 'a
val e : obj

Full name: Index.e
val er : obj ref

Full name: Index.er
val t : obj

Full name: Index.t
val tr : obj ref

Full name: Index.tr
val chr : c:'a -> 'b

Full name: Index.chr
val c : 'a
val op : obj

Full name: Index.op
val num : obj

Full name: Index.num
val f : obj

Full name: Index.f
val sem : l:int -> x:char -> r:int -> int

Full name: Index.sem
val l : int
val x : char
val r : int
val f : obj option

Full name: Index.f
val g : obj option

Full name: Index.g
val h1 : 'a

Full name: Index.h1
val h2 : 'a

Full name: Index.h2
Fork me on GitHub