All Projects → mna → Pigeon

mna / Pigeon

Licence: bsd-3-clause
Command pigeon generates parsers in Go from a PEG grammar.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Pigeon

Pegparser
💡 Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
Stars: ✭ 164 (-72.8%)
Mutual labels:  parser-generator, peg, parser
Npeg
PEGs for Nim, another take
Stars: ✭ 163 (-72.97%)
Mutual labels:  parser-generator, peg, parser
Ts Pegjs
Plugin for pegjs to generate TypeScript parsers.
Stars: ✭ 76 (-87.4%)
Mutual labels:  parser-generator, peg
Php Zephir Parser
The Zephir Parser delivered as a C extension for the PHP language.
Stars: ✭ 129 (-78.61%)
Mutual labels:  parser-generator, parser
Csly
a C# embeddable lexer and parser generator (.Net core)
Stars: ✭ 129 (-78.61%)
Mutual labels:  parser-generator, parser
Pegviz
PEG trace visualizer
Stars: ✭ 18 (-97.01%)
Mutual labels:  peg, parser
Lug
Parsing expression grammar (PEG) embedded domain specific language and parsing machine for C++17
Stars: ✭ 44 (-92.7%)
Mutual labels:  parser-generator, peg
Rust Peg
Parsing Expression Grammar (PEG) parser generator for Rust
Stars: ✭ 836 (+38.64%)
Mutual labels:  parser-generator, peg
kiuatan
A parser library for Pony.
Stars: ✭ 15 (-97.51%)
Mutual labels:  parser-generator, peg
pe
Fastest general-purpose parsing library for Python with a familiar API
Stars: ✭ 21 (-96.52%)
Mutual labels:  parser-generator, peg
peg
Import of Ian Piumarta's peg/leg recursive-descent parser generators for C
Stars: ✭ 41 (-93.2%)
Mutual labels:  parser-generator, peg
Pegjs
PEG.js: Parser generator for JavaScript
Stars: ✭ 4,176 (+592.54%)
Mutual labels:  parser-generator, peg
Tatsu
竜 TatSu generates Python parsers from grammars in a variation of EBNF
Stars: ✭ 198 (-67.16%)
Mutual labels:  parser-generator, parser
Intellij Plugin V4
An IntelliJ plugin for ANTLR v4
Stars: ✭ 318 (-47.26%)
Mutual labels:  parser-generator, parser
Cpp Peglib
A single file C++ header-only PEG (Parsing Expression Grammars) library
Stars: ✭ 435 (-27.86%)
Mutual labels:  parser-generator, peg
Opendirectorydownloader
Indexes open directories
Stars: ✭ 502 (-16.75%)
Mutual labels:  parser
React Markdown Editor Lite
a light-weight Markdown editor based on React. 一款轻量的基于React的markdown编辑器
Stars: ✭ 553 (-8.29%)
Mutual labels:  parser
Textx
Domain-Specific Languages and parsers in Python made easy http://textx.github.io/textX/
Stars: ✭ 496 (-17.74%)
Mutual labels:  parser
Globalize
A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
Stars: ✭ 4,612 (+664.84%)
Mutual labels:  parser
Lol Html
Low output latency streaming HTML parser/rewriter with CSS selector-based API
Stars: ✭ 566 (-6.14%)
Mutual labels:  parser

pigeon - a PEG parser generator for Go

GoDoc build 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.

Breaking Changes since v1.0.0

  • Removed support for Go < v1.11 to support go modules for dependency tracking.

  • Removed support for Go < v1.9 due to the requirement golang.org/x/tools/imports, which was updated to reflect changes in recent versions of Go. This is in compliance with the Go Release Policy respectively the Go Release Maintenance, which states support for each major release until there are two newer major releases.

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 toIfaceSlice(v interface{}) []interface{} {
    if v == nil {
        return nil
    }
    return v.([]interface{})
}

func eval(first, rest interface{}) int {
    l := first.(int)
    restSl := toIfaceSlice(rest)
    for _, v := range restSl {
        restExpr := toIfaceSlice(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 godoc page for detailed usage.

Contributing

See the CONTRIBUTING.md file.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].