diff --git a/examples/calculator.rs b/examples/calculator.rs index ebdd4e8a0..43c1f5ae2 100644 --- a/examples/calculator.rs +++ b/examples/calculator.rs @@ -21,18 +21,18 @@ impl_rdp! { } process! { - main(&self) -> i32 { // return an i32 in the end + compute(&self) -> i32 { // return an i32 in the end (&number: number) => { // capture number as &str number.parse::().unwrap() }, - (_: addition, left: main(), sign, right: main()) => { // get left & right by calling - match sign.rule { // main recursively + (_: addition, left: compute(), sign, right: compute()) => { // get left & right by + match sign.rule { // calling compute Rule::plus => left + right, Rule::minus => left - right, _ => unreachable!() } }, - (_: multiplication, left: main(), sign, right: main()) => { + (_: multiplication, left: compute(), sign, right: compute()) => { match sign.rule { Rule::times => left * right, Rule::slash => left / right, @@ -48,5 +48,5 @@ fn main() { parser.expression(); - println!("{}", parser.process()); // prints 44 + println!("{}", parser.compute()); // prints 44 } diff --git a/src/grammar.rs b/src/grammar.rs index 8be3d686e..8293b0dbe 100644 --- a/src/grammar.rs +++ b/src/grammar.rs @@ -132,7 +132,7 @@ macro_rules! grammar { // handle precedence climbing ( @conv_prec $pos:ident ($_prec:expr) $_atomic:tt $slf:ident [] [] [] ) => { { - $slf.set_pos($pos); + $slf.input_mut().set_pos($pos); None } @@ -174,27 +174,27 @@ macro_rules! grammar { { let mut primary = |slf: &mut Self| { - let pos = slf.pos(); + let pos = slf.input().pos(); grammar!(@skip $atomic slf); let result = grammar!(@conv $atomic slf [ $( $primary )* ] [] []); if !result { - slf.set_pos(pos); + slf.input_mut().set_pos(pos); } result }; let mut climb = |slf: &mut Self| { - let pos = slf.pos(); + let pos = slf.input().pos(); grammar!(@skip $atomic slf); grammar!(@conv_prec pos (0u8) $atomic slf [ $( $ts )* ] [] []) }; - let mut pos = $slf.pos(); + let mut pos = $slf.input().pos(); let queue_pos = $slf.queue().len(); let result = primary($slf); @@ -272,8 +272,10 @@ macro_rules! grammar { // match ( @mtc $slf:ident (( $exp:expr )) ) => (($exp)); ( @mtc $slf:ident [ $left:tt .. $right:tt ]) => (grammar!(@mtc $slf [$left, $right])); - ( @mtc $slf:ident [ $left:expr, $right:expr ]) => ($slf.match_range($left, $right)); - ( @mtc $slf:ident [ $str:expr ]) => ($slf.match_string($str)); + ( @mtc $slf:ident [ $left:expr, $right:expr ]) => { + $slf.input_mut().match_range($left, $right) + }; + ( @mtc $slf:ident [ $str:expr ]) => ($slf.input_mut().match_string($str)); ( @mtc $slf:ident $rule:ident) => ($slf.$rule()); // process postfix @@ -282,17 +284,17 @@ macro_rules! grammar { { grammar!(@process false $slf [(( $slf.try(false, |$slf| { if grammar!(@mtc $slf $a) { - let original = $slf.pos(); + let original = $slf.input().pos(); $slf.skip_ws(); - let pos = $slf.pos(); + let pos = $slf.input().pos(); let len = $slf.queue().len(); let result = grammar!(@mtc $slf $b); - if $slf.pos() == pos && !$slf.eoi_matched() { - $slf.set_pos(original); + if $slf.input().pos() == pos && !$slf.eoi_matched() { + $slf.input_mut().set_pos(original); } result @@ -319,16 +321,16 @@ macro_rules! grammar { ( @process false $slf:ident [ $a:tt $( $tail:tt )* ] [ * $( $optail:tt )* ] ) => { { grammar!(@process false $slf [(( { - let mut pos = $slf.pos(); + let mut pos = $slf.input().pos(); loop { if !grammar!(@mtc $slf $a) { - $slf.set_pos(pos); + $slf.input_mut().set_pos(pos); break } - pos = $slf.pos(); + pos = $slf.input().pos(); $slf.skip_ws(); } @@ -354,12 +356,12 @@ macro_rules! grammar { { grammar!(@process false $slf [(( if grammar!(@mtc $slf $a) { loop { - let pos = $slf.pos(); + let pos = $slf.input().pos(); $slf.skip_ws(); if !grammar!(@mtc $slf $a) { - $slf.set_pos(pos); + $slf.input_mut().set_pos(pos); break } @@ -455,14 +457,14 @@ macro_rules! grammar { let slf = self; grammar!(@skip $name slf); - let pos = slf.pos(); + let pos = slf.input().pos(); let len = slf.queue().len(); let tracked_len = slf.tracked_len(); let result = grammar!(@atomic $name false slf [ $( $ts )* ]); if result { - let new_pos = slf.pos(); + let new_pos = slf.input().pos(); let token = Token { rule: Rule::$name, @@ -493,7 +495,7 @@ macro_rules! grammar { let slf = self; grammar!(@skip $name slf); - let pos = slf.pos(); + let pos = slf.input().pos(); let len = slf.queue().len(); let toggled = slf.is_atomic(); @@ -509,7 +511,7 @@ macro_rules! grammar { } if result { - let new_pos = slf.pos(); + let new_pos = slf.input().pos(); let token = Token { rule: Rule::$name, @@ -538,7 +540,7 @@ macro_rules! grammar { let slf = self; grammar!(@skip $name slf); - let pos = slf.pos(); + let pos = slf.input().pos(); let result = grammar!(@atomic $name false slf [ $( $ts )* ]); diff --git a/src/inputs/string_input.rs b/src/inputs/string_input.rs index 67f81dbf7..930cb586d 100644 --- a/src/inputs/string_input.rs +++ b/src/inputs/string_input.rs @@ -25,7 +25,7 @@ use super::super::Input; /// ``` pub struct StringInput<'a> { string: &'a str, - pos: usize + pos: usize, } impl<'a> StringInput<'a> { @@ -43,7 +43,7 @@ impl<'a> StringInput<'a> { pub fn new(string: &'a str) -> StringInput<'a> { StringInput { string: string, - pos : 0 + pos: 0, } } } @@ -76,8 +76,10 @@ impl<'a> Input<'a> for StringInput<'a> { #[inline] fn line_col(&self, pos: usize) -> (usize, usize) { - fn find(chars: &mut Peekable, pos: usize, - current: (usize, usize)) -> (usize, usize) { + fn find(chars: &mut Peekable, + pos: usize, + current: (usize, usize)) + -> (usize, usize) { if pos == 0 { current } else { @@ -94,10 +96,10 @@ impl<'a> Input<'a> for StringInput<'a> { } else { find(chars, pos - 1, (current.0 + 1, 1)) } - }, + } Some('\n') => find(chars, pos - 1, (current.0 + 1, 1)), - Some(_) => find(chars, pos - 1, (current.0, current.1 + 1)), - None => unreachable!() + Some(c) => find(chars, pos - c.len_utf8(), (current.0, current.1 + 1)), + None => unreachable!(), } } } @@ -106,7 +108,9 @@ impl<'a> Input<'a> for StringInput<'a> { panic!("position out of bounds"); } - find(&mut self.string.chars().peekable(), pos, (1, 1)) + let slice = &self.string[..pos]; + + find(&mut slice.chars().peekable(), pos, (1, 1)) } #[inline] @@ -211,7 +215,7 @@ mod tests { #[test] fn line_col() { - let input = StringInput::new("a\rb\nc\r\nd"); + let input = StringInput::new("a\rb\nc\r\ndå—¨"); assert_eq!(input.line_col(0), (1, 1)); assert_eq!(input.line_col(1), (1, 2)); @@ -222,6 +226,7 @@ mod tests { assert_eq!(input.line_col(6), (4, 1)); assert_eq!(input.line_col(7), (4, 1)); assert_eq!(input.line_col(8), (4, 2)); + assert_eq!(input.line_col(11), (4, 3)); } #[test] diff --git a/src/lib.rs b/src/lib.rs index 85df463e5..d1cf4c5d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -460,15 +460,15 @@ //! ``` //! //! To process all these `Token`s we'll use the [`process!`](macro.process!) `macro`. This `macro` -//! defines the `process` method on the `Parser` which works by defining a set of methods, called -//! matchers, that pattern match against a set of `Token`s from the front of the queue, calling -//! themselves recursively until everything matched and returned its result. +//! defines matcher methods on the `Parser` that pattern match against a set of `Token`s from the +//! front of the queue, calling themselves recursively until everything matched and returned its +//! result. //! -//! Let's start by defining the signature of the `main` matcher which gets called by default. We +//! Let's start by defining the signature of the `compute` matcher that will do the computation. We //! need it to return an `i32` in the end. //! //! ```ignore -//! main(&self) -> i32 +//! compute(&self) -> i32 //! ``` //! //! Now all we need to do is to write the three cases of interest, namely `number`, `addition`, and @@ -490,14 +490,14 @@ //! * inside the block match `sign` and return the appropriate result //! //! ```ignore -//! (_: addition, left: main(), sign, right: main()) => { +//! (_: addition, left: compute(), sign, right: compute()) => { //! match sign.rule { //! Rule::plus => left + right, //! Rule::minus => left - right, //! _ => unreachable!() //! } //! }, -//! (_: multiplication, left: main(), sign, right: main()) => { +//! (_: multiplication, left: compute(), sign, right: compute()) => { //! match sign.rule { //! Rule::times => left * right, //! Rule::slash => left / right, @@ -537,18 +537,18 @@ //! } //! //! process! { -//! main(&self) -> i32 { +//! compute(&self) -> i32 { //! (&number: number) => { //! number.parse::().unwrap() //! }, -//! (_: addition, left: main(), sign, right: main()) => { +//! (_: addition, left: compute(), sign, right: compute()) => { //! match sign.rule { //! Rule::plus => left + right, //! Rule::minus => left - right, //! _ => unreachable!() //! } //! }, -//! (_: multiplication, left: main(), sign, right: main()) => { +//! (_: multiplication, left: compute(), sign, right: compute()) => { //! match sign.rule { //! Rule::times => left * right, //! Rule::slash => left / right, @@ -562,7 +562,7 @@ //! let mut parser = Rdp::new(StringInput::new("(3 + (9 + 3 * 4 + (3 + 1) / 2 - 4)) * 2")); //! //! assert!(parser.expression()); -//! assert_eq!(parser.process(), 44); +//! assert_eq!(parser.compute(), 44); //! # } //! ``` diff --git a/src/parser.rs b/src/parser.rs index e1848c268..cbc710a3f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,18 +5,16 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use super::Input; + /// A `trait` that defines a parser. -pub trait Parser<'a> { +pub trait Parser<'a, T: Input<'a>> { type Rule; type Token; - /// Matches `string`, returns whether it matched, and advances a parser with `string.len()` in - /// case it did. - fn match_string(&mut self, string: &str) -> bool; + fn input(&self) -> &T; - /// Matches `char` between `left` and `right`, and advances a parser with one `char` in case - /// it did. - fn match_range(&mut self, left: char, right: char) -> bool; + fn input_mut(&mut self) -> &mut T; /// Tries to match `rule`, returns whether it matched, and advances a parser with in case it /// did. If `revert` is `true`, the parser will not advance. @@ -30,18 +28,17 @@ pub trait Parser<'a> { /// was not silented) along with its precedence and right-associativity, or `None` if no /// operator passes. This operator triplet is also returned by the function when it greedily /// parses an operator useful for a higher precedence. - fn prec_climb(&mut self, pos: usize, left: usize, min_prec: u8, - last_op: Option<(Option, u8, bool)>, primary: &mut F, - climb: &mut G) -> (Option<(Option, u8, bool)>, Option) + fn prec_climb(&mut self, + pos: usize, + left: usize, + min_prec: u8, + last_op: Option<(Option, u8, bool)>, + primary: &mut F, + climb: &mut G) + -> (Option<(Option, u8, bool)>, Option) where F: FnMut(&mut Self) -> bool, G: FnMut(&mut Self) -> Option<(Option, u8, bool)>; - /// Returns the current position of a `Parser`. - fn pos(&self) -> usize; - - /// Sets the position of a `Parser`. - fn set_pos(&mut self, pos: usize); - /// Returns whether a `Parser` has reached its end. fn end(&self) -> bool; @@ -51,9 +48,6 @@ pub trait Parser<'a> { /// Reset a `Parser`. fn reset(&mut self); - /// Slices a `Parser`'s `Input`. - fn slice_input(&self, start: usize, end: usize) -> &'a str; - /// Returns the queue of all matched `Token`s. fn queue(&self) -> &Vec; diff --git a/src/parsers/rdp.rs b/src/parsers/rdp.rs index 523ac1b86..bbde9a018 100644 --- a/src/parsers/rdp.rs +++ b/src/parsers/rdp.rs @@ -114,9 +114,8 @@ macro_rules! impl_rdp { ( grammar! { $( $ts:tt )* } $( $mac:ident! { $( $rest:tt )* } )* ) => { use std::cell::Cell; use std::cmp; - use std::marker::PhantomData; - pub struct Rdp<'n, T: Input<'n>> { + pub struct Rdp { input: T, queue: Vec>, queue_index: Cell, @@ -124,14 +123,13 @@ macro_rules! impl_rdp { fail_pos: usize, atomic: bool, comment: bool, - eoi_matched: bool, - phantom: PhantomData> + eoi_matched: bool } impl_rdp!(@filter [ $( $ts )* ] []); - impl<'n, T: Input<'n>> Rdp<'n, T> { - pub fn new(input: T) -> Rdp<'n, T> { + impl<'a, T: Input<'a>> Rdp { + pub fn new(input: T) -> Rdp { Rdp { input: input, queue: vec![], @@ -140,8 +138,7 @@ macro_rules! impl_rdp { fail_pos: 0, atomic: false, comment: false, - eoi_matched: false, - phantom: PhantomData + eoi_matched: false } } @@ -152,14 +149,14 @@ macro_rules! impl_rdp { #[inline] pub fn any(&mut self) -> bool { if self.end() { - let pos = self.pos(); + let pos = self.input.pos(); self.track(Rule::any, pos); false } else { - let next = self.pos() + 1; - self.set_pos(next); + let next = self.input.pos() + 1; + self.input.set_pos(next); true } @@ -171,7 +168,7 @@ macro_rules! impl_rdp { let result = self.end(); if !result { - let pos = self.pos(); + let pos = self.input.pos(); self.track(Rule::eoi, pos); } else { @@ -192,18 +189,18 @@ macro_rules! impl_rdp { )* } - impl<'n, T: Input<'n>> Parser<'n> for Rdp<'n, T> { + impl<'a, T: Input<'a>> Parser<'a, T> for Rdp { type Rule = Rule; type Token = Token; #[inline] - fn match_string(&mut self, string: &str) -> bool { - self.input.match_string(string) + fn input(&self) -> &T { + &self.input } #[inline] - fn match_range(&mut self, left: char, right: char) -> bool { - self.input.match_range(left, right) + fn input_mut(&mut self) -> &mut T { + &mut self.input } #[inline] @@ -241,8 +238,8 @@ macro_rules! impl_rdp { while let Some((rule, prec, _)) = op { if prec >= min_prec { - let mut new_pos = self.pos(); - let mut right = self.pos(); + let mut new_pos = self.input.pos(); + let mut right = self.input.pos(); let queue_pos = self.queue.len(); primary(self); @@ -290,16 +287,6 @@ macro_rules! impl_rdp { (op, last_right) } - #[inline] - fn pos(&self) -> usize { - self.input.pos() - } - - #[inline] - fn set_pos(&mut self, pos: usize) { - self.input.set_pos(pos); - } - #[inline] fn end(&self) -> bool { self.input.len() == self.input.pos() @@ -318,11 +305,6 @@ macro_rules! impl_rdp { self.fail_pos = 0; } - #[inline] - fn slice_input(&self, start: usize, end: usize) -> &'n str { - self.input.slice(start, end) - } - #[inline] fn queue(&self) -> &Vec>{ &self.queue @@ -361,6 +343,7 @@ macro_rules! impl_rdp { } } + #[inline] fn skip_com(&mut self) { if self.atomic { return @@ -379,14 +362,17 @@ macro_rules! impl_rdp { } } + #[inline] fn is_atomic(&self) -> bool { self.atomic } + #[inline] fn set_atomic(&mut self, value: bool) { self.atomic = value; } + #[inline] fn track(&mut self, failed: Rule, pos: usize) { if self.atomic { return @@ -437,30 +423,19 @@ mod tests { } } - #[test] - fn match_string() { - let input = StringInput::new("asdasdf"); - let mut parser = Rdp::new(input); - - assert!(parser.match_string("asd")); - assert!(parser.match_string("asdf")); - assert!(parser.match_string("")); - assert!(!parser.match_string("a")); - } - #[test] fn try() { let input = StringInput::new("asdasdf"); let mut parser = Rdp::new(input); - assert!(parser.match_string("asd")); + assert!(parser.input_mut().match_string("asd")); assert!(!parser.try(false, |parser| { - parser.match_string("as") && parser.match_string("dd") + parser.input_mut().match_string("as") && parser.input_mut().match_string("dd") })); assert!(parser.try(false, |parser| { - parser.match_string("as") && parser.match_string("df") + parser.input_mut().match_string("as") && parser.input_mut().match_string("df") })); } @@ -469,7 +444,7 @@ mod tests { let input = StringInput::new("asdasdf"); let mut parser = Rdp::new(input); - assert!(parser.match_string("asdasdf")); + assert!(parser.input_mut().match_string("asdasdf")); assert!(parser.end()); } @@ -478,11 +453,11 @@ mod tests { let input = StringInput::new("asdasdf"); let mut parser = Rdp::new(input); - assert!(parser.match_string("asdasdf")); + assert!(parser.input_mut().match_string("asdasdf")); parser.reset(); - assert!(parser.match_string("asdasdf")); + assert!(parser.input_mut().match_string("asdasdf")); } #[test] @@ -492,15 +467,41 @@ mod tests { assert!(parser.expression()); assert!(!parser.end()); - let queue = vec![ - Token { rule: Rule::paren, start: 2, end: 9 }, - Token { rule: Rule::paren, start: 5, end: 8 }, - Token { rule: Rule::paren, start: 9, end: 20 }, - Token { rule: Rule::paren, start: 10, end: 16 }, - Token { rule: Rule::paren, start: 12, end: 14 }, - Token { rule: Rule::paren, start: 16, end: 18 }, - Token { rule: Rule::paren, start: 20, end: 22 } - ]; + let queue = vec![Token { + rule: Rule::paren, + start: 2, + end: 9, + }, + Token { + rule: Rule::paren, + start: 5, + end: 8, + }, + Token { + rule: Rule::paren, + start: 9, + end: 20, + }, + Token { + rule: Rule::paren, + start: 10, + end: 16, + }, + Token { + rule: Rule::paren, + start: 12, + end: 14, + }, + Token { + rule: Rule::paren, + start: 16, + end: 18, + }, + Token { + rule: Rule::paren, + start: 20, + end: 22, + }]; assert_eq!(parser.queue(), &queue); } @@ -512,9 +513,11 @@ mod tests { assert!(parser.zero()); assert!(!parser.end()); - let queue = vec![ - Token { rule: Rule::zero, start: 2, end: 15 } - ]; + let queue = vec![Token { + rule: Rule::zero, + start: 2, + end: 15, + }]; assert_eq!(parser.queue(), &queue); } @@ -526,9 +529,11 @@ mod tests { assert!(parser.one()); assert!(!parser.end()); - let queue = vec![ - Token { rule: Rule::one, start: 2, end: 15 } - ]; + let queue = vec![Token { + rule: Rule::one, + start: 2, + end: 15, + }]; assert_eq!(parser.queue(), &queue); } @@ -540,10 +545,16 @@ mod tests { assert!(parser.expression()); assert!(parser.end()); - let queue = vec![ - Token { rule: Rule::paren, start: 6, end: 10 }, - Token { rule: Rule::paren, start: 7, end: 9 } - ]; + let queue = vec![Token { + rule: Rule::paren, + start: 6, + end: 10, + }, + Token { + rule: Rule::paren, + start: 7, + end: 9, + }]; assert_eq!(parser.queue(), &queue); } @@ -555,10 +566,16 @@ mod tests { assert!(parser.expression()); assert!(parser.end()); - let queue = vec![ - Token { rule: Rule::paren, start: 11, end: 15 }, - Token { rule: Rule::paren, start: 12, end: 14 } - ]; + let queue = vec![Token { + rule: Rule::paren, + start: 11, + end: 15, + }, + Token { + rule: Rule::paren, + start: 12, + end: 14, + }]; assert_eq!(parser.queue(), &queue); } diff --git a/src/process.rs b/src/process.rs index 681e96005..43e2f3474 100644 --- a/src/process.rs +++ b/src/process.rs @@ -9,8 +9,8 @@ /// `process` on `&self` that processes the whole queue of `Token`s, reducing it to one single /// result. /// -/// The `process` is populated with methods, called *matchers*, that match patterns and return -/// results. A pattern is constructed from the following comma-separated items: +/// The `process` is populated with callable methods, called *matchers*, that match patterns and +/// return results. A pattern is constructed from the following comma-separated items: /// /// | Item | What it does | /// |------------------|------------------------------------------------------| @@ -23,8 +23,6 @@ /// | `item: fn()` | call matcher `fn` and store result in `item` | /// | `mut item: fn()` | call matcher `fn` and store mutable result in `item` | /// -/// `process` automatically calls the `main` matcher which is mandatory. -/// /// # Panics /// /// In case all the patterns inside of `process!` won't match, the `process` method will `panic!`. @@ -40,7 +38,7 @@ /// } /// /// process! { -/// main(&self) -> () { +/// ab(&self) -> () { /// (_: a) => {} /// } /// } @@ -49,7 +47,7 @@ /// let mut parser = Rdp::new(StringInput::new("b")); /// /// parser.b(); -/// parser.process(); +/// parser.ab(); /// # } /// ``` /// @@ -96,7 +94,7 @@ /// But before that, it needs to match a `paren` `Token` that gets ignored. /// /// ```ignore -/// (_: paren, expression: main()) +/// (_: paren, expression: nested_letter()) /// ``` /// /// All together now: @@ -119,11 +117,11 @@ /// } /// /// process! { -/// main(&self) -> Expression { +/// nested_letter(&self) -> Expression { /// (&letter: letter) => { /// Expression::Letter(letter.chars().next().unwrap()) /// }, -/// (_: paren, expression: main()) => { +/// (_: paren, expression: nested_letter()) => { /// Expression::Paren(Box::new(expression)) /// } /// } @@ -133,7 +131,7 @@ /// let mut parser = Rdp::new(StringInput::new("((z))")); /// /// assert!(parser.expression()); -/// assert_eq!(parser.process(), +/// assert_eq!(parser.nested_letter(), /// Expression::Paren(Box::new(Expression::Paren(Box::new(Expression::Letter('z')))))); /// # } /// ``` @@ -266,7 +264,7 @@ /// let mut parser = Rdp::new(StringInput::new("abc def")); /// /// assert!(parser.sentence()); -/// parser.process(); +/// parser.main(); /// # } /// ``` #[macro_export] @@ -323,7 +321,7 @@ macro_rules! process { { if let Some(token) = $slf.queue().get($slf.queue_index()) { if token.rule == Rule::$typ { - let $head = $slf.slice_input(token.start, token.end); + let $head = $slf.input().slice(token.start, token.end); $slf.inc_queue_index(); @@ -340,7 +338,7 @@ macro_rules! process { { if let Some(token) = $slf.queue().get($slf.queue_index()) { if token.rule == Rule::$typ { - let $head = $slf.slice_input(token.start, token.end); + let $head = $slf.input().slice(token.start, token.end); $slf.inc_queue_index(); @@ -357,7 +355,7 @@ macro_rules! process { ( @pattern $slf:ident ($block:expr) &$head:ident ) => { { if let Some(token) = $slf.queue().get($slf.queue_index()) { - let $head = $slf.slice_input(token.start, token.end); + let $head = $slf.input().slice(token.start, token.end); $slf.inc_queue_index(); @@ -370,7 +368,7 @@ macro_rules! process { ( @pattern $slf:ident ($block:expr) &$head:ident, $( $tail:tt )* ) => { { if let Some(token) = $slf.queue().get($slf.queue_index()) { - let $head = $slf.slice_input(token.start, token.end); + let $head = $slf.input().slice(token.start, token.end); $slf.inc_queue_index(); @@ -520,23 +518,11 @@ macro_rules! process { } }; - // get main's type - ( @type main $typ:ty ) => { - pub fn process(&self) -> $typ { - self.set_queue_index(0); - - self.main() - } - }; - ( @type $_name:ident $_typ:ty ) => (); - ( $( $name:ident (&$slf:ident) -> $typ:ty { $( $ts:tt )* } )* ) => { $( - fn $name(&$slf) -> $typ { + pub fn $name(&$slf) -> $typ { process!(@branches $slf $name $( $ts )*) } - - process!(@type $name $typ); )* }; } diff --git a/tests/calculator.rs b/tests/calculator.rs index 3933983a1..e012d19c2 100644 --- a/tests/calculator.rs +++ b/tests/calculator.rs @@ -27,18 +27,18 @@ impl_rdp! { } process! { - main(&self) -> i32 { + compute(&self) -> i32 { (&number: number) => { number.parse::().unwrap() }, - (_: addition, left: main(), sign, right: main()) => { + (_: addition, left: compute(), sign, right: compute()) => { match sign.rule { Rule::plus => left + right, Rule::minus => left - right, _ => unreachable!() } }, - (_: multiplication, left: main(), sign, right: main()) => { + (_: multiplication, left: compute(), sign, right: compute()) => { match sign.rule { Rule::times => left * right, Rule::slash => left / right, @@ -54,7 +54,7 @@ fn zero() { let mut parser = Rdp::new(StringInput::new("0")); assert!(parser.expression()); - assert_eq!(parser.process(), 0); + assert_eq!(parser.compute(), 0); } #[test] @@ -62,7 +62,7 @@ fn number() { let mut parser = Rdp::new(StringInput::new("123")); assert!(parser.expression()); - assert_eq!(parser.process(), 123); + assert_eq!(parser.compute(), 123); } #[test] @@ -70,7 +70,7 @@ fn addition() { let mut parser = Rdp::new(StringInput::new("123+321")); assert!(parser.expression()); - assert_eq!(parser.process(), 444); + assert_eq!(parser.compute(), 444); } #[test] @@ -78,7 +78,7 @@ fn subtraction() { let mut parser = Rdp::new(StringInput::new("123-321")); assert!(parser.expression()); - assert_eq!(parser.process(), -198); + assert_eq!(parser.compute(), -198); } #[test] @@ -86,7 +86,7 @@ fn multiplication() { let mut parser = Rdp::new(StringInput::new("16*16")); assert!(parser.expression()); - assert_eq!(parser.process(), 256); + assert_eq!(parser.compute(), 256); } #[test] @@ -94,7 +94,7 @@ fn division() { let mut parser = Rdp::new(StringInput::new("16/16")); assert!(parser.expression()); - assert_eq!(parser.process(), 1); + assert_eq!(parser.compute(), 1); } #[test] @@ -102,7 +102,7 @@ fn precedence() { let mut parser = Rdp::new(StringInput::new("2+3*4")); assert!(parser.expression()); - assert_eq!(parser.process(), 14); + assert_eq!(parser.compute(), 14); } #[test] @@ -110,7 +110,7 @@ fn parens() { let mut parser = Rdp::new(StringInput::new("(2+3)*4")); assert!(parser.expression()); - assert_eq!(parser.process(), 20); + assert_eq!(parser.compute(), 20); } #[test] @@ -118,7 +118,7 @@ fn negative() { let mut parser = Rdp::new(StringInput::new("-2*-2 / -4")); assert!(parser.expression()); - assert_eq!(parser.process(), -1); + assert_eq!(parser.compute(), -1); } #[test] @@ -126,5 +126,5 @@ fn complex() { let mut parser = Rdp::new(StringInput::new("(3 + (9 + 3 * 4 + (3 + 1) / 2 - 4)) * 2")); assert!(parser.expression()); - assert_eq!(parser.process(), 44); + assert_eq!(parser.compute(), 44); } diff --git a/tests/errors.rs b/tests/errors.rs index 7ad208cf1..db8c3a16e 100644 --- a/tests/errors.rs +++ b/tests/errors.rs @@ -18,7 +18,7 @@ impl_rdp! { } process! { - main(&self) -> Result { + primary(&self) -> Result { (_: ab, res: secondary()) => Ok(try!(res) + "b") } @@ -35,7 +35,7 @@ fn a() { assert!(parser.ab()); - assert_eq!(parser.process(), Ok("ab".to_owned())); + assert_eq!(parser.primary(), Ok("ab".to_owned())); } #[test] @@ -44,5 +44,5 @@ fn b() { assert!(parser.ab()); - assert_eq!(parser.process(), Err("error".to_owned())); + assert_eq!(parser.primary(), Err("error".to_owned())); } diff --git a/tests/sentence.rs b/tests/sentence.rs index edfb49d87..98aa8c069 100644 --- a/tests/sentence.rs +++ b/tests/sentence.rs @@ -86,5 +86,5 @@ fn word() { let sentence = Node::Sentence(words); - assert_eq!(parser.process(), sentence); + assert_eq!(parser.main(), sentence); } diff --git a/tests/sentence_lifetime.rs b/tests/sentence_lifetime.rs index 22fc6a5ee..8700c9a1b 100644 --- a/tests/sentence_lifetime.rs +++ b/tests/sentence_lifetime.rs @@ -24,7 +24,7 @@ impl_rdp! { } process! { - main(&self) -> Node<'n> { + _word(&self) -> Node<'a> { (&w: word) => Node::Sentence(w) } } @@ -37,7 +37,7 @@ fn word() { let mut parser = Rdp::new(StringInput::new(&file)); assert!(parser.word()); - parser.process() + parser._word() }; assert_eq!(result, Node::Sentence("abc")); }