Skip to content

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

License

Notifications You must be signed in to change notification settings

pointlander/peg

Repository files navigation

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

Go Reference Go Report Card

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.

Installation

go install github.com/pointlander/peg@latest

Usage

Build executable

go generate && go build

Help

./peg -h

Example

This creates the file peg.peg.go:

./peg -inline -switch peg.peg

Makefile example

This example Makefile will convert any file ending with .peg into a .go file with the same name:

.SUFFIXES: .peg .go

.peg.go:
	peg -noast -switch -inline -strict -output $@ $<

all: grammar.go

PEG File syntax

See peg-file-syntax.md

Development

Requirements

Generate

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

go generate

Files

  • bootstrap/main.go - bootstrap syntax tree of peg
  • tree/peg.go - syntax tree and code generator
  • peg.peg - peg in its own language

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published