Git Product home page Git Product logo

gobble's Introduction

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"
	"github.com/roblovelock/gobble/pkg/parser/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.

gobble's People

Contributors

roblovelock avatar roblovelock-mpn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.