-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from CAD97/bootstrap
Bootstrap pest_meta with pest:2.0
- Loading branch information
Showing
12 changed files
with
283 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[alias] | ||
bootstrap = "run --package pest_bootstrap" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[workspace] | ||
members = [ | ||
"bootstrap", | ||
"derive", | ||
"generator", | ||
"grammars", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "pest_bootstrap" | ||
description = "pest bootstrap script" | ||
version = "0.0.0" | ||
authors = ["Dragoș Tiselice <dragostiselice@gmail.com>"] | ||
homepage = "https://pest-parser.github.io/" | ||
repository = "https://github.com/pest-parser/pest" | ||
documentation = "https://docs.rs/pest" | ||
publish = false | ||
license = "MIT/Apache-2.0" | ||
|
||
[dependencies] | ||
pest_generator = "2.0" # Use the crates-io version, which (should be) known-good | ||
quote = "0.6.8" | ||
|
||
[badges] | ||
codecov = { repository = "pest-parser/pest" } | ||
maintenance = { status = "actively-developed" } | ||
travis-ci = { repository = "pest-parser/pest" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#[macro_use] | ||
extern crate quote; | ||
extern crate pest_generator; | ||
|
||
use pest_generator::derive_parser; | ||
use std::{fs::File, io::prelude::*, path::Path}; | ||
|
||
fn main() { | ||
let pest = Path::new(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/../meta/src/grammar.pest" | ||
)); | ||
let rs = Path::new(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/../meta/src/grammar.rs" | ||
)); | ||
|
||
let derived = { | ||
let path = pest.to_string_lossy(); | ||
let pest = quote! { | ||
#[grammar = #path] | ||
pub struct PestParser; | ||
}; | ||
derive_parser(pest, false) | ||
}; | ||
|
||
let mut file = File::create(rs).unwrap(); | ||
|
||
writeln!(file, "pub struct PestParser;\n{}", derived,).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/src/grammar.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
extern crate sha1; | ||
|
||
use sha1::{Digest, Sha1}; | ||
use std::env; | ||
use std::fs::{self, File}; | ||
use std::io::prelude::*; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::{Command}; | ||
|
||
fn display_digest(digest: &[u8]) -> String { | ||
digest.iter() | ||
.map(|byte| format!("{:02x}", byte)) | ||
.collect() | ||
} | ||
|
||
fn main() { | ||
println!("rerun-if-changed=src/grammar.pest"); | ||
|
||
// Yes; build.rs is supposed to treat `src` as read-only; however: | ||
// We want to publish `grammar.rs` and not `grammar.pest`, | ||
// so putting it in `src` is the simplest way to do so. | ||
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); | ||
let grammar_pest_path = manifest_dir.join("src/grammar.pest"); | ||
let grammar_rs_path = manifest_dir.join("src/grammar.rs"); | ||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
let hash_path = out_dir.join("pest_hash.sha1"); | ||
|
||
// If `grammar.pest` exists (we're building from git sources) | ||
if grammar_pest_path.exists() { | ||
let mut sha = Sha1::default(); | ||
|
||
let old_hash = File::open(&hash_path).ok().map(|mut file| { | ||
let mut s = String::new(); | ||
file.read_to_string(&mut s).unwrap(); | ||
s | ||
}); | ||
let current_grammar = fs::read_to_string(grammar_pest_path).unwrap(); | ||
sha.input(current_grammar.as_bytes()); | ||
let current_hash = display_digest(&sha.result()); | ||
|
||
// If `grammar.pest` has changed | ||
if !grammar_rs_path.exists() | ||
|| old_hash.as_ref().map(|it| it.trim()) != Some(current_hash.trim()) | ||
{ | ||
println!("Bootstrapping `meta/src/grammar.rs`"); | ||
|
||
let mut hash_file = File::create(hash_path).unwrap(); | ||
writeln!(hash_file, "{}", current_hash).unwrap(); | ||
|
||
// This "dynamic linking" is probably so fragile I don't even want to hear it | ||
let status = Command::new(manifest_dir.join("../target/debug/pest_bootstrap")) | ||
.spawn().unwrap_or_else(|_| { | ||
panic!( | ||
"Bootstrap failed because no bootstrap executable was found. \ | ||
Please run `cargo build --package pest_bootstrap` or `cargo bootstrap` \ | ||
and then try again.", | ||
) | ||
}) | ||
.wait().unwrap(); | ||
if !status.success() { | ||
panic!("Bootstrap failed"); | ||
} | ||
} else { | ||
println!(" Fresh `meta/src/grammar.rs`"); | ||
} | ||
} else { | ||
assert!(grammar_rs_path.exists(), "package is broken; does not contain grammar.rs"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.