gobble

module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2023 License: MIT

README

gobble

LICENSE

gobble is a parser combinator library written in Go. It was inspired by the nom library written for Rust. Its aim is to allow users to build complex parsers, without compromising on speed, while abstracting away most of the parsing complexities.

Example

Hexadecimal color parser:

package main

import (
	"github.com/roblovelock/gobble/pkg/combinator"
	"github.com/roblovelock/gobble/pkg/combinator/branch"
	"github.com/roblovelock/gobble/pkg/combinator/sequence"
	"github.com/roblovelock/gobble/pkg/parser/ascii"
	"github.com/roblovelock/gobble/pkg/parser/bytes"
	"image/color"
	"strconv"
	"strings"
)

var (
	hex       = bytes.TakeWhileMinMax(2, 2, ascii.IsHexDigit)
	rgbValue  = combinator.Map(hex, hexToUint8)
	rgbValues = sequence.Tuple(rgbValue, rgbValue, rgbValue)

	hexByte        = bytes.OneOf([]byte("0123456789abcdefABCDEF")...)
	shortRGBValue  = combinator.Map(hexByte, hexByteToUint8)
	shortRGBValues = sequence.Tuple(shortRGBValue, shortRGBValue, shortRGBValue)

	rgb = branch.Alt(rgbValues, shortRGBValues)

	colorValue = combinator.Map(rgb, func(rgb []uint8) (color.Color, error) {
		return color.RGBA{R: rgb[0], G: rgb[1], B: rgb[2], A: 255}, nil
	})

	hash        = bytes.Byte('#')
	colorParser = sequence.Preceded(hash, colorValue)
)

func hexToUint8(bytes []byte) (uint8, error) {
	i, err := strconv.ParseUint(string(bytes), 16, 8)
	return uint8(i), err
}

func hexByteToUint8(hexByte byte) (uint8, error) {
	return hexToUint8([]byte{hexByte, hexByte})
}

func Parse(in string) (color.Color, error) {
	return colorParser(strings.NewReader(in))
}

WIP

Please not this is in early stage development. This means the API isn't stable and is subject to breaking changes.

Directories

Path Synopsis
examples
expr
Package expr provides a parser to parse basic arithmetic expression based on the following rule.
Package expr provides a parser to parse basic arithmetic expression based on the following rule.
hex
qoi
pkg
parser/ascii
Package ascii provides parsers for recognizing ascii bytes
Package ascii provides parsers for recognizing ascii bytes
parser/bytes
Package bytes provides parsers for recognizing bytes
Package bytes provides parsers for recognizing bytes
parser/numeric
Package numeric provides parsers for recognizing numeric bytes
Package numeric provides parsers for recognizing numeric bytes
parser/runes
Package runes provides parsers for recognizing runes
Package runes provides parsers for recognizing runes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL