-
Notifications
You must be signed in to change notification settings - Fork 107
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 #371 from A4-Tacks/master
Add where clause for rule fn, allow some rust syntax
- Loading branch information
Showing
6 changed files
with
867 additions
and
260 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
extern crate peg; | ||
|
||
peg::parser!( | ||
grammar parser() for str { | ||
pub rule foo<F: Fn(&str) -> u32 + Copy>(f: F) -> u32 | ||
= s:$(['0'..='9']+) { f(s) } | ||
pub rule bar(f: impl Fn(&str) -> u32 + Copy,) -> u32 | ||
= s:$(['0'..='9']+) { f(s) } | ||
pub rule baz(f: fn(&str) -> u32) -> u32 | ||
= s:$(['0'..='9']+) { f(s) } | ||
} | ||
); | ||
|
||
fn main() { | ||
let n = parser::foo("123", |s| s.parse().unwrap()).unwrap(); | ||
assert_eq!(n, 123); | ||
let n = parser::bar("123", |s| s.parse().unwrap()).unwrap(); | ||
assert_eq!(n, 123); | ||
let n = parser::baz("123", |s| s.parse().unwrap()).unwrap(); | ||
assert_eq!(n, 123); | ||
} |
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,27 @@ | ||
extern crate peg; | ||
use std::{ | ||
str::FromStr, | ||
fmt::Debug, | ||
}; | ||
|
||
peg::parser!( | ||
grammar parser() for str { | ||
use std::cell::Cell; | ||
pub rule nums<C, T>() -> C | ||
where C: Default + Extend<T>, | ||
T: FromStr, | ||
<T as FromStr>::Err: Debug, | ||
= c:({ Cell::new(C::default()) }) | ||
(ch:$(['0'..='9']) { | ||
let mut mutc = c.take(); | ||
mutc.extend(Some(ch.parse::<T>().unwrap())); | ||
c.set(mutc); | ||
})+ | ||
{ c.take() } | ||
} | ||
); | ||
|
||
fn main() { | ||
assert_eq!(parser::nums::<Vec<u8>, u8>("3729"), Ok(vec![3, 7, 2, 9])); | ||
} | ||
|