docs.rs failed to build peg-0.1.2
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build:
peg-0.8.3
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 peg_syntax_ext;
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 any single character"literal"
- match a literal string[a-z]
- match a single character from a set[^a-z]
- match a single character not in a setrule
- match a production defined elsewhere in the grammar and return its resultexpression*
- Match zero or more repetitions ofexpression
and return the results as aVec
expression+
- Match one or more repetitions ofexpression
and return the results as aVec
expression?
- Match one or zero repetitions ofexpression
. Returns anOption
&expression
- Match only ifexpression
matches at this position, without consuming any characters!expression
- Match only ifexpression
does not match at this position, without consuming any charactersexpression ** delim
- Match zero or more repetitions ofexpression
delimited withdelim
and return the results as aVec
expression ++ delim
- Match one or more repetitions ofexpression
delimited withdelim
and return the results as aVec
e1 / e2 / e3
- Try to match e1. If the match succeeds, return its result, otherwise try e2, and so on.e1 e2 e3
- Match expressions in sequencea:e1 b:e2 c:e3 { rust }
- Match e1, e2, e3 in sequence. If they match successfully, run the Rust code in the action and return its result. The variables before the colons in the preceding sequence are bound to the results of the corresponding expressions
Match actions can extract data from the match using these variables:
- match_str - the matched string, as a
&str
slice. Examples:
name -> String
= [a-zA-Z0-9_]+ { match_str.to_string() }
number -> int
= [0-9]+ { from_str::<uint>(match_str).unwrap() }
- start_pos - the index into the string at which the match starts, inclusive
- pos - the index into the string at which the match ends, exclusive
To Do
- Improve parse error reporting
- Caching
- Support passing user-specified objects (e.g. filename for source mapping, string interner) into action code