Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

peg

> 编程语言
Open source

Peg, Parsing Expression Grammar, is an implementation of a Packrat parser generator.

1.1K stars0 likes2 views
WebsiteGitHub

About

Peg, Parsing Expression Grammar, is an implementation of a Packrat parser generator.

PEG, an Implementation of a Packrat Parsing Expression Grammar in Go

A Parsing Expression Grammar ( hence peg) is a way to create grammars similar in principle to regular expressions but which allow better code integration. Specifically, peg is an implementation of the Packrat parser generator originally implemented as peg/leg by Ian Piumarta in C. A Packrat parser is a "descent recursive parser" capable of backtracking and negative look-ahead assertions which are problematic for regular expression engines.

PEG file syntax

See peg-file-syntax.md for creating your own *.peg file.

Usage

  1. Add peg as a tool dependency

    go get -tool github.com/pointlander/peg@main
  2. Create your mygrammar.peg

  3. Add a //go:generate line in a Go source of your package

    go
    //go:generate go tool peg mygrammar.peg

    Widespread community design pattern: Add lines like this in doc.go.

  4. Generate mygrammar.peg.go from mygrammar.peg

    go generate

Example

This creates the file peg.peg.go

go tool peg -inline -switch peg.peg

Help

go tool peg -h

Development

Requirements

  • Golang, see go.mod for version
  • golangci-lint latest version (v2 or later)
  • Bash 3.2.x or higher

Generate

Bootstrap and generate grammar *.peg.go. This commands should initially be executed once before other commands.

go generate

Build

go build

(go generate required once beforehand)

Set version

Use the version from the tag if the current commit has a tag. If not use the current commit hash.

go build -ldflags "-X main.Version=$(git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD)"

Additionally, since Go 1.18 the go command embeds version control information. Read the information:

go version -m peg

Test

go test -short ./...

(go generate required once beforehand)

Lint

golangci-lint run

(go generate required once beforehand)

Format

golangci-lint fmt

Benchmark

go test -benchmem -bench .

(go generate required once beforehand)

Author

Andrew Snodgrass

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Go

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言