Skip to main content

A PEG Parser generator in Go

Project description

⻰OGoPEGo

A PEG parser generator in Go.

⻰OGoPEGo is the Go sibling of 竜TatSu (Python) and 铁修TieXiu (Rust). It is functionally complete and passes the same test suite as its siblings.

Refer to the 竜TatSu documentation for grammar syntax, semantics, and usage. The local SYNTAX.md describes the grammar format.

The CLI tool is a great way to explored the features offered by the library:

$ ogo --help
Usage: ogo <command> [flags]

ogopego: A PEG parser generator in Go

Flags:
  -h, --help             Show context-sensitive help.
  -o, --output=STRING    Output to a file instead of stdout
  -C, --color="auto"     Control colorized output for API results
  -t, --trace            Display a detailed trace of the parsing process
  -v, --version          Print version information

Commands:
  run <grammar> <inputs> ... [flags]
    Execute a grammar against one or more input files

  boot [flags]
    The internal boot grammar

  grammar <grammar> [flags]
    Grammar transformations

Run "ogo <command> --help" for more information on a command.

Installation

go install github.com/neogeny/ogopego/cmd/ogo@latest

Python

There's a Python package that provides an out-of-process integration with ⻰OGoPEGo. The package provides a Python API that allows you to compile grammars and parse input using the Go implementation of the PEG parser generator. The package is available on PyPI as ogopego and can be installed with:

pip install ogopego

After installation the command-line executable becomes available as ogo.

ogo --help

For the Python API please take a look at the documentation in the docs directory.

Library API

import "github.com/neogeny/ogopego/api"

// Compile a grammar string into a Grammar object.
g, err := api.Compile(grammar, cfg)

// Parse input with a compiled Grammar.
tree, err := api.ParseInput(g, input, cfg)

// Compile and parse in one step.
tree, err := api.ParseGrammar(grammar, cfg)

// Compile to JSON-compatible output.
json, err := api.CompileToJSON(grammar, cfg)

// Parse input to JSON-compatible output.
json, err := api.ParseInputToJSON(g, input, cfg)

// JSON roundtrip via peg package.
jsonStr := peg.SerializeGrammar(g)
g2, err := peg.ParseGrammar([]byte(jsonStr))

Grammar object

import "github.com/neogeny/ogopego/peg"

// A compiled grammar. Create one with api.Compile.
type Grammar struct {
    Name       string            // grammar name
    Directives *asjson.OrderedMap // @@directives
    Keywords   []string           // @@keyword declarations
    Rules      []*Rule            // grammar rules
    Analyzed   bool               // true after Initialize()
}

// Parse input text with this grammar (use api.ParseInput).
result, err := api.ParseInput(g, text, cfg)

// Prepare grammar for parsing (link rules, detect left recursion).
err := g.Initialize()

// Serialize.
jsonStr := g.AsJSONStr()             // indented JSON
jsonStr := peg.SerializeGrammar(g)   // clean JSON (recommended)
data, err := peg.ParseGrammar([]byte(jsonStr))  // deserialize

// Display.
fmt.Println(g.PrettyPrint())    // EBNF pretty-print
fmt.Println(g.Railroads())      // railroad diagram

Features

  • Out-of-process integration for Python is complete.
  • Generation of source code with an object model for deifinitions in the grammar is complete. The model generator defines the types specified in the input grammar and creates the transformation from the Tree result of a call to Parse() to the object model.
  • Code generation of a parser recently moved in 竜TatSu to the loading of a model of the Grammar and using it as parser. ⻰OGoPEGo is cabable of generating a model of the Grammar constructor for a grammar that can be compiled by Go for blazing boot times.
  • ⻰OGoPEGo also knows how to load fast a Grammar model from 竜TatSu-format JSON.
  • Semantic actions (transformations) during parse are implemented through a SemanticsFunc(Tree) Tree configuration entry. Transformations are limited to Tree->Tree, so a walker must be used as post-processor if a different AST type is desidred. The AST model generation available through the CLI tool generates such a walker.
  • Interpolation and evaluation of `constant` expressions hasn't had any known use cases with 竜TatSu. They will not be implemented in ⻰OGoPEGo until a use case appears.

License

Licensed under the Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0).

Contribution

Unless explicitly stated otherwise, any contribution intentionally submitted for inclusion in the work, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ogopego-0.1.11.tar.gz (145.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ogopego-0.1.11-py3-none-win_amd64.whl (15.1 kB view details)

Uploaded Python 3Windows x86-64

ogopego-0.1.11-py3-none-manylinux_2_28_x86_64.whl (3.2 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

ogopego-0.1.11-py3-none-macosx_11_0_arm64.whl (3.0 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file ogopego-0.1.11.tar.gz.

File metadata

  • Download URL: ogopego-0.1.11.tar.gz
  • Upload date:
  • Size: 145.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ogopego-0.1.11.tar.gz
Algorithm Hash digest
SHA256 70fbf71a8fccab5d6b1a79684f393a52ac772c805a8b1af30c2148b2344eeaf3
MD5 8f6eee376aa4620ef3599047449b681b
BLAKE2b-256 adbd76be6af8f9698db5de21a161f68c342364fe8a5d01cbcd9fdc45cd840817

See more details on using hashes here.

Provenance

The following attestation bundles were made for ogopego-0.1.11.tar.gz:

Publisher: publish.yml on neogeny/ogopego

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ogopego-0.1.11-py3-none-win_amd64.whl.

File metadata

  • Download URL: ogopego-0.1.11-py3-none-win_amd64.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ogopego-0.1.11-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 a3cafc7b9b74088154a0a8adce0886deae91b4183ce706b7aa0de6fb01476dae
MD5 0ace5aa0826c5e3a4aa228ea115a9841
BLAKE2b-256 1348d98ea70336193ec50d4c0c0d516865ff0035acd3a020bef1c25d4268acd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for ogopego-0.1.11-py3-none-win_amd64.whl:

Publisher: publish.yml on neogeny/ogopego

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ogopego-0.1.11-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ogopego-0.1.11-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b9ac98e33732f1917f7db941a7083de6890f821265be26aab30dae3859fb24ca
MD5 f220cba7e13789f4bfcfce9b959765e1
BLAKE2b-256 2c363248dc82348510befd3eba3af4c8d0185af82d9b988c8733db361a013923

See more details on using hashes here.

Provenance

The following attestation bundles were made for ogopego-0.1.11-py3-none-manylinux_2_28_x86_64.whl:

Publisher: publish.yml on neogeny/ogopego

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ogopego-0.1.11-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ogopego-0.1.11-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a71cea9d7fdd0d4e35d5240898e40e40d0680b830b6e8f27bf22674a94f1478c
MD5 be18e362446be6661fe1087236cda347
BLAKE2b-256 2a91a0ae4981b25ad02f0389f200b2ca7cb89d6e4c6c52824891e29a2925a6ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for ogopego-0.1.11-py3-none-macosx_11_0_arm64.whl:

Publisher: publish.yml on neogeny/ogopego

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page