Parsing Expression Grammars in Rust

This is a simple parser generator based on the Parsing Expression Grammar formalism.

Usage

As a syntax extension

Add to your Cargo.toml:

[dependencies] peg = "~0.1.0"

Add to your crate root: ```

![feature(phase)]

[phase(plugin)] extern crate pegsyntaxext;

```

Use peg_file! modname("mygrammarfile.rustpeg"); to include the grammar from an external file. The macro expands into a module called modname with functions corresponding to the #[pub] rules in your grammar.

Or, use peg! modname(r#" // grammar rules here "#);`

to embed a short PEG grammar inline in your Rust source file. Example.

As a standalone code generator

Run peg input_file.rustpeg to compile a grammar and generate Rust code on stdout.

Grammar Syntax

```

[pub]

rule_name -> type = expression ```

If a rule is marked with #[pub], the generated module has a public function that begins parsing at that rule.

Match actions can extract data from the match using these variables:

name -> String = [a-zA-Z0-9_]+ { match_str.to_string() }

number -> int = [0-9]+ { from_str::<uint>(match_str).unwrap() }

To Do