• Stars
    star
    837
  • Rank 54,466 (Top 2 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created over 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Command pigeon generates parsers in Go from a PEG grammar.

pigeon - a PEG parser generator for Go

Go Reference Test Status GoReportCard Software License

The pigeon command generates parsers based on a parsing expression grammar (PEG). Its grammar and syntax is inspired by the PEG.js project, while the implementation is loosely based on the parsing expression grammar for C# 3.0 article. It parses Unicode text encoded in UTF-8.

See the godoc page for detailed usage. Also have a look at the Pigeon Wiki for additional information about Pigeon and PEG in general.

Releases

  • v1.0.0 is the tagged release of the original implementation.
  • Work has started on v2.0.0 with some planned breaking changes.

Github user @mna created the package in April 2015, and @breml is the package's maintainer as of May 2017.

Release policy

Starting of June 2023, the backwards compatibility support for pigeon is changed to follow the official Go Security Policy.

Over time, the Go ecosystem is evolving.

On one hand side, packages like golang.org/x/tools, which are critical dependencies of pigeon, do follow the official Security Policy and with pigeon not following the same guidelines, it was no longer possible to include recent versions of these dependencies and with this it was no longer possible to include critical bugfixes. On the other hand there are changes to what is considered good practice by the greater community (e.g. change from interface{} to any). For users following (or even enforcing) these good practices, the code generated by pigeon does no longer meet the bar of expectations. Last but not least, following the Go Security Policy over the last years has been a smooth experience and therefore updating Go on a regular bases feels like duty that is reasonable to be put on users of pigeon.

This observations lead to the decision to follow the same Security Policy as Go.

Installation

Provided you have Go correctly installed with the $GOPATH and $GOBIN environment variables set, run:

$ go get -u github.com/mna/pigeon

This will install or update the package, and the pigeon command will be installed in your $GOBIN directory. Neither this package nor the parsers generated by this command require any third-party dependency, unless such a dependency is used in the code blocks of the grammar.

Basic usage

$ pigeon [options] [PEG_GRAMMAR_FILE]

By default, the input grammar is read from stdin and the generated code is printed to stdout. You may save it in a file using the -o flag.

Example

Given the following grammar:

{
// part of the initializer code block omitted for brevity

var ops = map[string]func(int, int) int {
    "+": func(l, r int) int {
        return l + r
    },
    "-": func(l, r int) int {
        return l - r
    },
    "*": func(l, r int) int {
        return l * r
    },
    "/": func(l, r int) int {
        return l / r
    },
}

func toAnySlice(v any) []any {
    if v == nil {
        return nil
    }
    return v.([]any)
}

func eval(first, rest any) int {
    l := first.(int)
    restSl := toAnySlice(rest)
    for _, v := range restSl {
        restExpr := toAnySlice(v)
        r := restExpr[3].(int)
        op := restExpr[1].(string)
        l = ops[op](l, r)
    }
    return l
}
}


Input <- expr:Expr EOF {
    return expr, nil
}

Expr <- _ first:Term rest:( _ AddOp _ Term )* _ {
    return eval(first, rest), nil
}

Term <- first:Factor rest:( _ MulOp _ Factor )* {
    return eval(first, rest), nil
}

Factor <- '(' expr:Expr ')' {
    return expr, nil
} / integer:Integer {
    return integer, nil
}

AddOp <- ( '+' / '-' ) {
    return string(c.text), nil
}

MulOp <- ( '*' / '/' ) {
    return string(c.text), nil
}

Integer <- '-'? [0-9]+ {
    return strconv.Atoi(string(c.text))
}

_ "whitespace" <- [ \n\t\r]*

EOF <- !.

The generated parser can parse simple arithmetic operations, e.g.:

18 + 3 - 27 * (-18 / -3)

=> -141

More examples can be found in the examples/ subdirectory.

See the package documentation for detailed usage.

Contributing

See the CONTRIBUTING.md file.

License

The BSD 3-Clause license. See the LICENSE file.

More Repositories

1

agora

a dynamically typed, garbage collected, embeddable programming language built with Go
Go
326
star
2

martini-api-example

An example RESTful API application built with Martini.
Go
272
star
3

redisc

A Go redis cluster client built on top of redigo.
Go
228
star
4

trofaf

Super simple live static blog generator in Go. Vraiment trofaf.
CSS
130
star
5

express-boilerplate

Basic structure of a node.js web application with Express. Started out as a blog post about how to structure code in an Express web app (in French, see the URL below).
JavaScript
64
star
6

juggler

Juggler: a websocket-based, redis-backed RPC and pub-sub server.
Go
62
star
7

gocostmodel

Benchmarks of common basic operations for the Go language.
Go
61
star
8

specter

a (tiny) VM project built with Go
Go
59
star
9

ghost

Lego blocks for web servers.
Go
52
star
10

lune

A pure Go implementation of the Lua virtual machine.
Go
44
star
11

advice

Advice functional mixin based on Twitter's Angus Croll presentation (How we learned to stop worrying and love Javascript).
JavaScript
39
star
12

upstashdis

A Go client and a compatible server for local testing for the Upstash Redis REST API interface.
Go
22
star
13

gred

A pure Go concurrent implementation of the Redis server
Go
20
star
14

implement.js

Strong type-checking for dependency injection and method arguments.
JavaScript
20
star
15

Networking

Low-level Swift package for POSIX sockets and Epoll/Kqueue.
Swift
19
star
16

luashell

A small Lua module to help write what would be shell scripts in Lua. (mirror)
Lua
12
star
17

gocoro

Lua coroutine implementation in Go
Go
7
star
18

angular-gtd

An example app for Getting Things Done built with AngularJS.
JavaScript
6
star
19

tulip

Lua web framework based on lua-http and PostgreSQL. (mirror)
Lua
6
star
20

algo

Fundamental algorithms and data structures using Go generics.
Go
6
star
21

mainer

Package mainer defines types relevant to flag parsing and command entrypoint implementation.
Go
5
star
22

assnatapi

API pour exploiter les données de l'Assemblée nationale du Québec.
JavaScript
5
star
23

starstruct

Starlark to Go struct converter.
Go
4
star
24

assnat

Extracteur des données du site de l'Assemblée Nationale du Québec, pour nourrir assnatapi.
JavaScript
4
star
25

swiftodo

iOS ToDo list tutorial in Swift
Swift
4
star
26

httpcmw

Package httpcmw supports creating middleware chains for HTTP clients.
Go
3
star
27

runes

Command to print Unicode code point information.
Go
3
star
28

myvim

my vim setup
Vim Script
2
star
29

zzterm

Zero-allocation terminal input key and mouse decoder in Go (mirror).
Go
2
star
30

nitfind

ack-style finder in Nit, mostly to experiment with the language
Nit
2
star
31

setupjs

Personal setup for javascript frontend projects.
JavaScript
2
star
32

see-no-evil

Tiny callback cleaner library for node.js, so that error management gets out of the way.
JavaScript
2
star
33

httpmw

Package httpmw is a collection of bite-sized middleware with chaining support.
Go
2
star
34

bench_go_scripting

Small benchmarks comparing the Tengo and Risor scripting languages
Shell
2
star
35

luafn

Functional programming fundamentals for Lua. (mirror)
Lua
1
star
36

sendkeys

Simulate sending user input in a terminal (mirror).
Go
1
star
37

luasock99

Lua module for the socket99 C library. (mirror)
C
1
star
38

nenuphar

wip
Go
1
star
39

bookify

Make a book out of URLs, basically. Exploring node.js, express and socket.io along the way!
JavaScript
1
star
40

tcheck

Simple Lua module of sanity-checks for types (mirror)
Lua
1
star
41

Provok.in-issues

Issues repository for the Provok.in website.
1
star
42

exp

Go
1
star
43

xpgsql

Lua module providing a straightforward API to the luapgsql library. (mirror)
Lua
1
star
44

feedbuilder

JavaScript
1
star
45

mna.dev

Mirror of git.sr.ht/~mna/mna.dev
Go
1
star
46

vim-trefle

Vim syntax highlighting, filetype detection and indentation for trefle.
Vim Script
1
star
47

0value

files for my 0value.com blog
HTML
1
star
48

zzcsi

Control Sequence Introducer (CSI) functions for xterm-compatible terminals. (mirror)
Go
1
star
49

httpparms

Package httpparms provides helper functions and mechanisms to load the content of an HTTP request into a Go struct.
Go
1
star
50

ungeekenorbite

Blog exploratoire d'un geek qui découvre avec stupeur et tremblements le merveilleux monde des nouvelles technologies.
JavaScript
1
star