Skip to content
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

Add minimum and maximum repetition rules #171

Merged
merged 5 commits into from
Jan 18, 2018
Merged
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
27 changes: 27 additions & 0 deletions pest_derive/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub enum Expr {
Opt(Box<Expr>),
Rep(Box<Expr>),
RepOnce(Box<Expr>),
RepExact(Box<Expr>, u32),
RepMin(Box<Expr>, u32),
RepMax(Box<Expr>, u32),
RepMinMax(Box<Expr>, u32, u32),
Push(Box<Expr>)
}
Expand Down Expand Up @@ -73,6 +76,18 @@ impl Expr {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepOnce(mapped)
}
Expr::RepExact(expr, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepExact(mapped, max)
}
Expr::RepMin(expr, num) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMin(mapped, num)
}
Expr::RepMax(expr, num) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMax(mapped, num)
}
Expr::RepMinMax(expr, min, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMinMax(mapped, min, max)
Expand Down Expand Up @@ -122,6 +137,18 @@ impl Expr {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepOnce(mapped)
}
Expr::RepExact(expr, num) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepExact(mapped, num)
}
Expr::RepMin(expr, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMin(mapped, max)
}
Expr::RepMax(expr, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMax(mapped, max)
}
Expr::RepMinMax(expr, min, max) => {
let mapped = Box::new(map_internal(*expr, f));
Expr::RepMinMax(mapped, min, max)
Expand Down
6 changes: 6 additions & 0 deletions pest_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@
//! | `e*` | matches `e` zero or more times |
//! | `e+` | matches `e` one or more times |
//! | `e{n}` | matches `e` exactly `n` times |
//! | `e{, n}` | matches `e` at most `n` times |
//! | `e{n,} ` | matches `e` at least `n` times |
//! | `e{m, n}` | matches `e` between `m` and `n` times inclusively |
//! | `e?` | optionally matches `e` |
//! | `&e` | matches `e` without making progress |
Expand Down Expand Up @@ -226,8 +228,12 @@
#![doc(html_root_url = "https://docs.rs/pest_derive")]
#![recursion_limit="256"]

#[cfg(test)]
#[macro_use]
extern crate pest;
#[cfg(not(test))]
extern crate pest;

extern crate proc_macro;
#[macro_use]
extern crate quote;
Expand Down
202 changes: 202 additions & 0 deletions pest_derive/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,70 @@ pub fn optimize(rules: Vec<Rule>) -> Vec<Rule> {
Box::new(Expr::Rep(expr))
)
}
Expr::RepExact(expr, num) => {
(1..num + 1).map(|_| {
*expr.clone()
})
.rev()
.fold(None, |rep, expr| {
match rep {
None => Some(expr),
Some(rep) => {
Some(
Expr::Seq(
Box::new(expr),
Box::new(rep)
)
)
}
}
})
.unwrap()
}
Expr::RepMin(expr, min) => {
(1..min + 2).map(|i| {
if i <= min {
*expr.clone()
} else {
Expr::Rep(expr.clone())
}
})
.rev()
.fold(None, |rep, expr| {
match rep {
None => Some(expr),
Some(rep) => {
Some(
Expr::Seq(
Box::new(expr),
Box::new(rep)
)
)
}
}
})
.unwrap()
}
Expr::RepMax(expr, max) => {
(1..max + 1).map(|_| {
Expr::Opt(expr.clone())
})
.rev()
.fold(None, |rep, expr| {
match rep {
None => Some(expr),
Some(rep) => {
Some(
Expr::Seq(
Box::new(expr),
Box::new(rep)
)
)
}
}
})
.unwrap()
}
Expr::RepMinMax(expr, min, max) => {
(1..max + 1).map(|i| {
if i <= min {
Expand Down Expand Up @@ -170,6 +234,144 @@ mod tests {
assert_eq!(optimize(rules), concatenated);
}

#[test]
fn unroll_loop_exact() {
let rules = vec![
Rule {
name: Ident::new("rule"),
ty: RuleType::Atomic,
expr: Expr::RepExact(
Box::new(Expr::Ident(Ident::new("a"))),
3
)
}
];
let unrolled = vec![
Rule {
name: Ident::new("rule"),
ty: RuleType::Atomic,
expr: Expr::Seq(
Box::new(Expr::Ident(Ident::new("a"))),
Box::new(Expr::Seq(
Box::new(Expr::Ident(Ident::new("a"))),
Box::new(Expr::Ident(Ident::new("a")))
))
)
}
];

assert_eq!(optimize(rules), unrolled);
}


#[test]
fn unroll_loop_max() {
let rules = vec![
Rule {
name: Ident::new("rule"),
ty: RuleType::Atomic,
expr: Expr::RepMax(
Box::new(Expr::Str("a".to_owned())),
3
)
}
];
let unrolled = vec![
Rule {
name: Ident::new("rule"),
ty: RuleType::Atomic,
expr: Expr::Seq(
Box::new(Expr::Opt(
Box::new(Expr::Str("a".to_owned()))
)),
Box::new(Expr::Seq(
Box::new(Expr::Opt(
Box::new(Expr::Str("a".to_owned()))
)),
Box::new(Expr::Opt(
Box::new(Expr::Str("a".to_owned()))
))
))
)
}
];

assert_eq!(optimize(rules), unrolled);
}

#[test]
fn unroll_loop_min() {
let rules = vec![
Rule {
name: Ident::new("rule"),
ty: RuleType::Atomic,
expr: Expr::RepMin(
Box::new(Expr::Str("a".to_owned())),
2
)
}
];
let unrolled = vec![
Rule {
name: Ident::new("rule"),
ty: RuleType::Atomic,
expr: Expr::Seq(
Box::new(Expr::Str("a".to_owned())),
Box::new(Expr::Seq(
Box::new(Expr::Str("a".to_owned())),
Box::new(Expr::Rep(
Box::new(Expr::Str("a".to_owned()))
))
))
)
}
];

assert_eq!(optimize(rules), unrolled);
}

#[test]
fn unroll_loop_min_max() {
let rules = vec![
Rule {
name: Ident::new("rule"),
ty: RuleType::Atomic,
expr: Expr::RepMinMax(
Box::new(Expr::Str("a".to_owned())),
2,
3
)
}
];
let unrolled = vec![
Rule {
name: Ident::new("rule"),
ty: RuleType::Atomic,
expr: Expr::Seq(
/* TODO possible room for improvement here:
* if the sequences were rolled out in the opposite
* order, we could further optimize the strings
* in cases like this.
Box::new(Expr::Str("aa".to_owned())),
Box::new(Expr::Opt(
Box::new(Expr::Str("a".to_owned()))
))
*/
Box::new(Expr::Str("a".to_owned())),
Box::new(Expr::Seq(
Box::new(Expr::Str("a".to_owned())),
Box::new(Expr::Opt(
Box::new(Expr::Str("a".to_owned())),
))
))
)
}
];

assert_eq!(optimize(rules), unrolled);
}


#[test]
fn concat_insensitive_strings() {
let rules = vec![
Expand Down
Loading