A Parsec-like parsing library for Go.
Switch branches/tags
Nothing to show
Clone or download
Latest commit ca8122a Sep 25, 2016
Failed to load latest commit information.
example Updated for go1. Mar 31, 2012
README.markdown Initial import. Nov 28, 2009
parsec.go Updated for go1. Mar 31, 2012


Go Parse

A Parsec-like library for Go.


A Vessel is what carries around the input and any user-specified state, as well as internal state such as the position in the input. It should know how to return and set those 3 values, as well as Get from the input, and push/pop (which just adjusts the position).

A Parser takes a Vessel and returns an Output and whether or not the parse was successful.

Parsers can typically be combined in many ways. For example Symbol is just String followed by Whitespace, Many takes a Parser and repeatedly applies it, matching 0 or more times (thus, the parse is always successful), and Any takes any number of Parsers and tries them all in order until one succeeds.


func main() {
    in := new(StringVessel);
    in.SetInput(`< (>)(
)(  >)  < >


    ltgt := Any(Symbol("<"), Symbol(">"));

    parser := Many(Any(ltgt, Parens(ltgt)));
    out, parsed := parser(in);

    fmt.Printf("Matched: %#v\n", parsed);
    fmt.Printf("Matches: %v\n", out);
    fmt.Printf("Vessel: %+v\n", in);


go-parse $ go parsec
Matched: true
Matches: [< > < > < > > >]
Vessel: &{state:<nil> input:< (>)(
)(  >)  < >

    > position:{Name: Line:0 Column:0 Offset:29}}