Skip to content

Add to support include! for load partial for Pest grammars. #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 93 additions & 23 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern crate quote;
use std::env;
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;
use std::path::{Path, PathBuf};

use proc_macro2::TokenStream;
use syn::{Attribute, DeriveInput, Generics, Ident, Lit, Meta};
Expand All @@ -36,6 +36,42 @@ mod generator;
use pest_meta::parser::{self, rename_meta_rule, Rule};
use pest_meta::{optimizer, unwrap_or_report, validator};

fn join_path(path: &str) -> PathBuf {
let root = env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into());

// Check whether we can find a file at the path relative to the CARGO_MANIFEST_DIR
// first.
//
// If we cannot find the expected file over there, fallback to the
// `CARGO_MANIFEST_DIR/src`, which is the old default and kept for convenience
// reasons.
// TODO: This could be refactored once `std::path::absolute()` get's stabilized.
// https://doc.rust-lang.org/std/path/fn.absolute.html
let path = if Path::new(&root).join(path).exists() {
Path::new(&root).join(path)
} else {
Path::new(&root).join("src/").join(path)
};

path
}

/// Get path relative to `path` dir, or relative to root path
fn partial_path(path: Option<&PathBuf>, filename: &str) -> PathBuf {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should take Option<&Path> instead. That might just work out at the call site, or it might require &* or .as_deref(), I'm not sure.

let root = match path {
Some(path) => path.parent().unwrap().to_path_buf(),
None => join_path("./"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since join_path looks for the presence of a file to choose between the manifest dir or source dir, this is going to always use the src dir.

We should deliberately pick one as the "location" of attribute-inline grammars, or perhaps a better choice, just disallow include!.

Threading up an error from here might be a bigger change, unfortunately.

};

// Add .pest suffix if not exist
let mut filename = filename.to_string();
if !filename.to_lowercase().ends_with(".pest") {
filename.push_str(".pest");
}
Comment on lines +66 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to create file extension less files, so adding an extension on automatically is probably not a great choice. If we do want to add an extension, it should be done via conversion to and then modification via PathBuf rather than string manipulation.


root.join(filename)
}

/// Processes the derive/proc macro input and generates the corresponding parser based
/// on the parsed grammar. If `include_grammar` is set to true, it'll generate an explicit
/// "include_str" statement (done in pest_derive, but turned off in the local bootstrap).
Expand All @@ -45,34 +81,16 @@ pub fn derive_parser(input: TokenStream, include_grammar: bool) -> TokenStream {

let mut data = String::new();
let mut path = None;
let mut has_use = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be has_include now.


for content in contents {
let (_data, _path) = match content {
GrammarSource::File(ref path) => {
let root = env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into());

// Check whether we can find a file at the path relative to the CARGO_MANIFEST_DIR
// first.
//
// If we cannot find the expected file over there, fallback to the
// `CARGO_MANIFEST_DIR/src`, which is the old default and kept for convenience
// reasons.
// TODO: This could be refactored once `std::path::absolute()` get's stabilized.
// https://doc.rust-lang.org/std/path/fn.absolute.html
let path = if Path::new(&root).join(path).exists() {
Path::new(&root).join(path)
} else {
Path::new(&root).join("src/").join(path)
};

let file_name = match path.file_name() {
Some(file_name) => file_name,
None => panic!("grammar attribute should point to a file"),
};
let path = join_path(path);

let data = match read_file(&path) {
Ok(data) => data,
Err(error) => panic!("error opening {:?}: {}", file_name, error),
Err(error) => panic!("error opening {:?}: {}", path, error),
};
(data, Some(path.clone()))
}
Expand All @@ -85,13 +103,43 @@ pub fn derive_parser(input: TokenStream, include_grammar: bool) -> TokenStream {
}
}

let pairs = match parser::parse(Rule::grammar_rules, &data) {
let raw_data = data.clone();
let mut pairs = match parser::parse(Rule::grammar_rules, &raw_data) {
Ok(pairs) => pairs,
Err(error) => panic!("error parsing \n{}", error.renamed_rules(rename_meta_rule)),
};

// parse `include!("file.pest")` to load partial and replace data
// TODO: Try to avoid parse twice as much as possible
let mut partial_pairs = pairs.clone().flatten().peekable();
while let Some(pair) = partial_pairs.next() {
if pair.as_rule() == Rule::include {
if let Some(filename) = partial_pairs.peek() {
let filepath = partial_path(path.as_ref(), filename.as_str());
let partial_data = match read_file(&filepath) {
Ok(data) => data,
Err(error) => panic!("error opening {:?}: {}", filepath, error),
};

let (start, end) = (pair.as_span().start(), pair.as_span().end());

data.replace_range(start..end, &partial_data);
has_use = true;
}
}
}

if has_use {
// Re-parse the data again, after replacing the `use` statement
pairs = match parser::parse(Rule::grammar_rules, &data) {
Ok(pairs) => pairs,
Err(error) => panic!("error parsing \n{}", error.renamed_rules(rename_meta_rule)),
};
}
Comment on lines +132 to +138
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it this way rather than in a loop means that only a single layer of include! will be resolved; a include! of a file with an include! won't work.

At a minimum we need a test for this case. Ideally we'd make it work, but so long as it results in an error rather than silently causing corruption it's not too bad.


let defaults = unwrap_or_report(validator::validate_pairs(pairs.clone()));
let ast = unwrap_or_report(parser::consume_rules(pairs));

let optimized = optimizer::optimize(ast);

generator::generate(name, &generics, path, optimized, defaults, include_grammar)
Expand Down Expand Up @@ -157,6 +205,9 @@ fn get_attribute(attr: &Attribute) -> GrammarSource {
mod tests {
use super::parse_derive;
use super::GrammarSource;
use std::path::PathBuf;

use crate::partial_path;

#[test]
fn derive_inline_file() {
Expand Down Expand Up @@ -225,4 +276,23 @@ mod tests {
let ast = syn::parse_str(definition).unwrap();
parse_derive(ast);
}

#[test]
fn test_partial_path() {
assert_eq!(
PathBuf::from("tests/grammars/base.pest".to_owned()),
partial_path(Some(&PathBuf::from("tests/grammars/foo.pest")), "base")
);

assert_eq!(
PathBuf::from("tests/grammars/base.pest".to_owned()),
partial_path(Some(&PathBuf::from("tests/grammars/foo.pest")), "base.pest")
);

let root = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into());
assert_eq!(
std::path::Path::new(&root).join("base.pest"),
partial_path(None, "base.pest")
);
}
}
1 change: 1 addition & 0 deletions grammars/src/grammars/base.pest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
3 changes: 1 addition & 2 deletions grammars/src/grammars/json.pest
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
include!("base.pest")

json = { SOI ~ (object | array) ~ EOI }

Expand All @@ -28,5 +29,3 @@ exp = @{ ("E" | "e") ~ ("+" | "-")? ~ ASCII_DIGIT+ }
bool = { "true" | "false" }

null = { "null" }

WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
2 changes: 1 addition & 1 deletion grammars/src/grammars/toml.pest
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
include!("./base")

toml = { SOI ~ (table | array_table | pair)* ~ EOI }

Expand Down Expand Up @@ -70,5 +71,4 @@ exp = @{ ("E" | "e") ~ ("+" | "-")? ~ int }

boolean = { "true" | "false" }

WHITESPACE = _{ " " | "\t" | NEWLINE }
COMMENT = _{ "#" ~ (!NEWLINE ~ ANY)* }
7 changes: 5 additions & 2 deletions meta/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

grammar_rules = _{ SOI ~ grammar_rule+ ~ EOI }

grammar_rule = {
identifier ~ assignment_operator ~ modifier? ~
opening_brace ~ expression ~ closing_brace
opening_brace ~ expression ~ closing_brace |
include
}

assignment_operator = { "=" }
Expand Down Expand Up @@ -96,3 +96,6 @@ newline = _{ "\n" | "\r\n" }
WHITESPACE = _{ " " | "\t" | newline }
block_comment = _{ "/*" ~ (block_comment | !"*/" ~ ANY)* ~ "*/" }
COMMENT = _{ block_comment | ("//" ~ (!newline ~ ANY)*) }

include = ${ "include!" ~ WHITESPACE* ~ "(" ~ WHITESPACE* ~ "\"" ~ path ~ "\"" ~ WHITESPACE* ~ ")" }
path = @{ (!(newline | "\"") ~ ANY)* ~ ".pest"? }
39 changes: 38 additions & 1 deletion meta/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub fn rename_meta_rule(rule: &Rule) -> String {
Rule::insensitive_string => "`^`".to_owned(),
Rule::range_operator => "`..`".to_owned(),
Rule::single_quote => "`'`".to_owned(),
Rule::include => "include!".to_owned(),
other_rule => format!("{:?}", other_rule),
}
}
Expand Down Expand Up @@ -1093,13 +1094,49 @@ mod tests {
};
}

#[test]
fn test_include() {
parses_to! {
parser: PestParser,
input: "include!(\"foo\")",
rule: Rule::include,
tokens: [
include(0, 15, [
path(10, 13),
])
]
};

parses_to! {
parser: PestParser,
input: "include! ( \"foo.bar.pest\" )",
rule: Rule::include,
tokens: [
include(0, 28, [
path(13, 25),
])
]
};

parses_to! {
parser: PestParser,
input: "include!(\n\"./foo/bar.pest\"\n)",
rule: Rule::include,
tokens: [
include(0, 28, [
path(11, 25),
])
]
};
}

#[test]
fn wrong_identifier() {
fails_with! {
parser: PestParser,
input: "0",
rule: Rule::grammar_rules,
positives: vec![Rule::identifier],
positives: vec![Rule::grammar_rule],
negatives: vec![],
pos: 0
};
Expand Down