Go Parser Combinator with Go Generics

Maybe this post will be old cuz I’m developing peachcomb actively!

This post introduces a Go parser-library that is being developed by me, called peachcomb . this library is aimed to reduce overhead with dynamically dispatching, and notify mismatching among parsers. To achieve them, I use Go Generics in peachcomb. Go Generics was released in Go 1.18.


This post won’t describe the detail of Go Generics. I’ll recommend you this tutorial to understand Go Generics briefly.



To use peachcomb, You should follow just 2-steps. Initialization and Calling it. Before describing in detail, I’ll show you the simplest sample to understand peachcomb’s usage. The below sample parses numbers that they’re separated by |.


package main

import (

func main() {
	element := strparse.Digit1()
	separator := strparse.Rune('|')
	p := combinator.Separated1(element, separator)
	i, o, err := p([]rune("123|456|789Drumato"))
	fmt.Printf("%s\n", o)
$ go run main.go
[123 456 789]

i is the rest input that parser p consumed. o is the p ’s output. In this case, o forms like []string{"123", "456", "789"}. It’s just all of the peachcomb’s usage.


I strongly refered Geal/nom, that is a parser library in Rust. Nom achieves to construct fast/generic parsers by constrainting trait bounds.

Parser Signature

First of all, all parsers in peachcomb implements one signature, type Parser[E comparable, O parser.ParseOutput] . It’s defined such as below.

type Parser[E comparable, O ParseOutput] func(input ParseInput[E]) (ParseInput[E], O, ParseError)

type ParseInput[E comparable] []E

type ParseOutput interface{}

type ParseError interface {

I think there are some merits caused by designing Parser signature. First, if users want to use a certain parser but peachcomb doesn’t suppport it, users can implement in their project, and pass them into generalized function in package combinator (e.g. Map()). Second, almost parsers can be implemented generically. We don’t need to prepare almost parsers by each input type. Last, users only needs to know the interface. Initialization and Calling.

Type Resolving

Now let’s see the type resolving among peachcomb’s parsers. The playable sample code is placed in Go Playground.


package main

import (


func main() {
	var element parser.Parser[rune, string] = strparse.Digit1()
	var separator parser.Parser[rune, rune] = strparse.Rune('|')
	var p parser.Parser[rune, []string] = combinator.Separated1(element, separator)

	var i []rune
	var o []string
	var err error
	i, o, err = p([]rune("123|456|789Drumato"))

	fmt.Printf("%d\n", len(o))
	fmt.Printf("%s %s %s\n", o[0], o[1], o[2])
  • element … A parser that receives []rune and returns string
  • separator … A parser that receives []rune and returns rune
  • p … A parser that receives []rune and returns []string

the actual function signatures are like this.

func Digit1() parser.Parser[rune, string]
func Rune(expected rune) parser.Parser[rune, rune]
func Separated1[
    E comparable, 
    EO parser.ParseOutput, 
    SO parser.ParseOutput,
    element parser.Parser[E, EO], 
    separator parser.Parser[E, SO]) parser.Parser[E, []EO]

Separated1()’s type parameters will be resolved to …

  • Erune
  • EOstring
  • SOrune

So finally we know p implements parser.Parser[rune, []string] at compiliation time.

Next example shows us the peachcomb’s constraints.


package main

import (


func main() {
	sub := byteparse.UInt8()
	p := combinator.Many1(sub)
	i, o, err := p([]rune("aaaabaa"))


parsers in the sample will be resolved to…

  • subparser.Parser[byte, uint8]
  • pparser.Parser[byte, []uint8]

As you know in Playground, this sample will be failed to compile. the actual error message is below.

./prog.go:13:23: cannot use []rune("aaaabaa") (value of type []rune) as type parser.ParseInput[byte] in argument to p

the above sample mismatched btw the actual input and the expected input. peachcomb can also detect inconsistencies among parsers.


package main

import (


func main() {
	sub := strparse.Digit1()
	p := combinator.Map(sub, func(v byte) (bool, error) { return v == 0, nil })
	i, o, err := p([]byte("11112222abc"))


the actual error message is below.

./prog.go:12:27: type func(v byte) (bool, error) of func(v byte) (bool, error) {…} does not match inferred type func(string) (O, error) for func(SO) (O, error)
  • subparser.Parser[rune, string]
  • pparser.Parser[rune, bool]
    • in brief, Map() forms like Map[E, SO, O](sub Parser[E, SO], fn SO -> (O, error))

in this sample, p has E: rune, SO: string type parameters so p requires func (v string) -> (O, error) as the 2nd argument, but the actual argument forms func(v byte) -> (bool, error).

Custom Input Types

Almost parsers can receive any custom input types to parse. If you want to know this mechanism in detail, please read the below example.



Today I described you a Go parser library called peachcomb. If you’re interested in the project, pleace use this and send me feedbacks!