Compile

Struct Compile 

Source
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

Source

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.

Source

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.

Source

pub fn destination<T: Into<PathBuf>>(self, filename: T) -> Self

Destination file name.

This option only used if running on a single file.

Source

pub fn format(self) -> Self

Format .rs files with rustfmt after grammar compilation.

Source

pub fn derives(self, derives: Vec<String>) -> Self

Use a specific set of derives when declaring structs.

The default set is #[derive(Debug, Clone)]

Source

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.

Source

pub fn user_context_type(self, user_context_type: &str) -> Self

Set the type of global.user_context. Experimental feature, may change without notice.

Source

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!.

Source

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.

Trait Implementations§

Source§

impl Debug for Compile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.