• Stars
    star
    1,361
  • Rank 33,203 (Top 0.7 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 10 years ago
  • Updated 7 months ago

Reviews

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

Repository Details

Parsing Expression Grammar (PEG) parser generator for Rust

Parsing Expression Grammars in Rust

Documentation | Release Notes

rust-peg is a simple yet flexible parser generator that makes it easy to write robust parsers. Based on the Parsing Expression Grammar formalism, it provides a Rust macro that builds a recursive descent parser from a concise definition of the grammar.

Features

  • Parse input from &str, &[u8], &[T] or custom types implementing traits
  • Customizable reporting of parse errors
  • Rules can accept arguments to create reusable rule templates
  • Precedence climbing for prefix/postfix/infix expressions
  • Helpful rustc error messages for errors in the grammar definition or the Rust code embedded within it
  • Rule-level tracing to debug grammars

Example

Parse a comma-separated list of numbers surrounded by brackets into a Vec<u32>:

peg::parser!{
  grammar list_parser() for str {
    rule number() -> u32
      = n:$(['0'..='9']+) {? n.parse().or(Err("u32")) }

    pub rule list() -> Vec<u32>
      = "[" l:(number() ** ",") "]" { l }
  }
}

pub fn main() {
    assert_eq!(list_parser::list("[1,1,2,3,5,8]"), Ok(vec![1, 1, 2, 3, 5, 8]));
}

See the tests for more examples
Grammar rule syntax reference in rustdoc

Comparison with similar parser generators

crate parser type action code integration input type precedence climbing parameterized rules streaming input
peg PEG in grammar proc macro (block) &str, &[T], custom Yes Yes No
pest PEG external proc macro (file) &str Yes No No
nom combinators in source library &[u8], custom No Yes Yes
lalrpop LR(1) in grammar build script &str No Yes No

See also

Development

The rust-peg grammar is written in rust-peg: peg-macros/grammar.rustpeg. To avoid the circular dependency, a precompiled grammar is checked in as peg-macros/grammar.rs. To regenerate this, run the ./bootstrap.sh script.

There is a large test suite which uses trybuild to test both functionality (tests/run-pass) and error messages for incorrect grammars (tests/compile-fail). Because rustc error messages change, the compile-fail tests are only run on the minimum supported Rust version to avoid spurious failures.

Use cargo test to run the entire suite, or cargo test -- trybuild trybuild=lifetimes.rs to test just the indicated file. Add --features trace to trace these tests.

More Repositories

1

rtlsdr-433m-sensor

Decoder for Oregon Scientific v1 protocol wireless temperature sensors with RTL-SDR and GNU Radio.
Python
172
star
2

usb

Minimalist portable USB device stack for SAMD21, LPC1800, LPC4300, Xmega
C
98
star
3

node-llvm

LLVM bindings for Node.JS
CoffeeScript
94
star
4

nusb

A new pure-Rust library for cross-platform low-level access to USB devices.
Rust
83
star
5

rust-soapysdr

Rust bindings for SoapySDR, the vendor-neutral software defined radio hardware abstraction layer
Rust
68
star
6

codemap

A data structure for tracking source code positions, inspired by the type in rustc's libsyntax.
Rust
46
star
7

rust-vcd

Read and write VCD (Value Change Dump) files in Rust
Rust
38
star
8

rust-usb

Deprecated: see libusb-rs
Rust
24
star
9

nRF24L01-buspirate

Python code to interface with Nordic nRF24L01+ radio over SPI through BusPirate
Python
19
star
10

codemap-diagnostic

Rust library for displaying compiler diagnostics like rustc
Rust
19
star
11

EquationExplorer

Implicit Equation / Vector Field Graphing in HTML5
JavaScript
13
star
12

nano-dm

Receive Qualcomm DSP debug logs over USB
Rust
12
star
13

OTpad

Operational Transformations Testing (Etherpad in nodejs)
CoffeeScript
12
star
14

eagle2web

Export Eagle PCBs and schematics for interactive display on the web
JavaScript
11
star
15

slideshow

Photo Slideshow Generator written in Python
Python
11
star
16

LUFA-LPC13xx

[DEPRECATED] Port of Dean Camera's LUFA USB framework to NXP LPC1343
C
11
star
17

rust-tessel

Programming Tessel in Rust
Rust
10
star
18

webrender-experiments

GLSL
8
star
19

FiveDollarArduino

ATMega328P + 3 resistors + 2 diodes + USB cable = click the upload button, watch the LED blink
C
7
star
20

openrisc-tools-build

[DEPRECATED] Submodules for OpenRISC toolchain components, and scripts to build them all. Updated from http://openrisc.net/toolchain-build.html
6
star
21

eeref

What would microcontroller datasheets look like if they weren't limited to static PDF?
CoffeeScript
5
star
22

openOCD

openOCD including OpenRISC with adv_debug_sys - patched to build on Ubuntu 12.04
C
4
star
23

external_set

Concurrent Rust collection whose items are owned externally, for implementing pub-sub / observer patterns
Rust
4
star
24

rust-svd

Deprecated, use svd2rust
Rust
3
star
25

calculator

Functional programming language for physics estimation
JavaScript
3
star
26

starfish

USB to Pmod
KiCad Layout
3
star
27

regex-derivs

Analyzing and compiling regular expressions with Brzozowski's regular expression derivatives
CoffeeScript
2
star
28

svgExperiments

Experimenting with drag-and-drop to connect objects in SVG, jQuery, and CoffeeScript
JavaScript
1
star
29

ocw_physics

Source code of notes from MIT OCW 8.01 and 8.02
Python
1
star
30

serenoa

Static site generator framework that isn't afraid of code
Python
1
star
31

Printer

AVR-based printer controller that prints with a sharpie
C++
1
star
32

inlinr

Embed all linked resources into an HTML page
Python
1
star
33

tinysync

Simple SFTP directory upload/sync tool
1
star