pub struct Compile { /* private fields */ }Expand description
Compiles peginator grammars into rust code with a builder interface.
It only recompiles files if it detects (based on the generated file header in the .rs file)
change in either the peginator library, or the grammar file.
It is meant to be used as peginator_codegen::Compile, hence the generic name.
Example build.rs for using a single grammar file and putting the result in the target directory:
fn main() {
peginator_codegen::Compile::file("my_grammar.ebnf")
.destination(format!("{}/my_grammar.rs", std::env::var("OUT_DIR").unwrap()))
.format()
.run_exit_on_error();
println!("cargo:rerun-if-changed=my_grammar.ebnf");
}Importing this grammar:
mod grammar { include!(concat!(env!("OUT_DIR"), "/my_grammar.rs")); }Example build.rs for multiple grammar files in the src directory, putting compiled files next to
their grammar definitions:
fn main() {
peginator_codegen::Compile::directory("src")
.format()
.run_exit_on_error()
}Implementations§
Source§impl Compile
impl Compile
Sourcepub fn directory<T: Into<PathBuf>>(filename: T) -> Self
pub fn directory<T: Into<PathBuf>>(filename: T) -> Self
Run compilation on a whole directory run.
The whole directory is recursively searched for files with the .ebnf extension, and
compiled to rust code with the same filename but with .rs extension.
Sourcepub fn file<T: Into<PathBuf>>(filename: T) -> Self
pub fn file<T: Into<PathBuf>>(filename: T) -> Self
Run compilation on a single file.
The file will be compiled. If destination is not given, it will be the same filename in the
same directory, but with .rs extension.
Sourcepub fn destination<T: Into<PathBuf>>(self, filename: T) -> Self
pub fn destination<T: Into<PathBuf>>(self, filename: T) -> Self
Destination file name.
This option only used if running on a single file.
Sourcepub fn derives(self, derives: Vec<String>) -> Self
pub fn derives(self, derives: Vec<String>) -> Self
Use a specific set of derives when declaring structs.
The default set is #[derive(Debug, Clone)]
Sourcepub fn prefix(self, prefix: String) -> Self
pub fn prefix(self, prefix: String) -> Self
Set a prefix (code) that will be pasted into the file between the header and the generated code
Useful for use declarations or maybe custom structs.
Sourcepub fn user_context_type(self, user_context_type: &str) -> Self
pub fn user_context_type(self, user_context_type: &str) -> Self
Set the type of global.user_context. Experimental feature, may change without notice.
Sourcepub fn run(self) -> Result<()>
pub fn run(self) -> Result<()>
Run the compilation, returning an error.
In case of a parse error, PrettyParseError is thrown,
which will print a pretty error with format! or print!.
Sourcepub fn run_exit_on_error(self)
pub fn run_exit_on_error(self)
Run the compilation, and exit with an exit code in case of an error.
It also makes sure to pretty-print the error, should one occur.