Skip to content

Commit

Permalink
Merge pull request pest-parser#62 from sunng87/v0.3.0
Browse files Browse the repository at this point in the history
Adding some lifetime back
  • Loading branch information
dragostis committed Jun 22, 2016
2 parents edcc38c + 7dbebda commit be2e12f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

/// A `trait` that defines an input for a `Parser`.
pub trait Input {
pub trait Input<'a> {
/// Returns length of an `Input`.
fn len(&self) -> usize;

Expand All @@ -20,7 +20,7 @@ pub trait Input {
fn set_pos(&mut self, pos: usize);

/// Slices an `Input`.
fn slice(&self, start: usize, end: usize) -> &str;
fn slice(&self, start: usize, end: usize) -> &'a str;

/// Returns the line and column of a position for an `Input`.
fn line_col(&self, pos: usize) -> (usize, usize);
Expand Down
20 changes: 11 additions & 9 deletions src/inputs/string_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use super::super::Input;
/// ```
pub struct StringInput<'a> {
string: &'a str,
pos: usize
pos: usize,
}

impl<'a> StringInput<'a> {
Expand All @@ -43,12 +43,12 @@ impl<'a> StringInput<'a> {
pub fn new(string: &'a str) -> StringInput<'a> {
StringInput {
string: string,
pos : 0
pos: 0,
}
}
}

impl<'a> Input for StringInput<'a> {
impl<'a> Input<'a> for StringInput<'a> {
#[inline]
fn len(&self) -> usize {
self.string.len()
Expand All @@ -70,14 +70,16 @@ impl<'a> Input for StringInput<'a> {
}

#[inline]
fn slice(&self, start: usize, end: usize) -> &str {
fn slice(&self, start: usize, end: usize) -> &'a str {
&self.string[start..end]
}

#[inline]
fn line_col(&self, pos: usize) -> (usize, usize) {
fn find(chars: &mut Peekable<Chars>, pos: usize,
current: (usize, usize)) -> (usize, usize) {
fn find(chars: &mut Peekable<Chars>,
pos: usize,
current: (usize, usize))
-> (usize, usize) {
if pos == 0 {
current
} else {
Expand All @@ -94,10 +96,10 @@ impl<'a> Input for StringInput<'a> {
} else {
find(chars, pos - 1, (current.0 + 1, 1))
}
},
}
Some('\n') => find(chars, pos - 1, (current.0 + 1, 1)),
Some(c) => find(chars, pos - c.len_utf8(), (current.0, current.1 + 1)),
None => unreachable!()
Some(c) => find(chars, pos - c.len_utf8(), (current.0, current.1 + 1)),
None => unreachable!(),
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use super::Input;

/// A `trait` that defines a parser.
pub trait Parser {
pub trait Parser<'a, T: Input<'a>> {
type Rule;
type Token;

fn input(&self) -> &Input;
fn input(&self) -> &T;

fn input_mut(&mut self) -> &mut Input;
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.
Expand All @@ -28,9 +28,14 @@ pub trait Parser {
/// 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<F, G>(&mut self, pos: usize, left: usize, min_prec: u8,
last_op: Option<(Option<Self::Rule>, u8, bool)>, primary: &mut F,
climb: &mut G) -> (Option<(Option<Self::Rule>, u8, bool)>, Option<usize>)
fn prec_climb<F, G>(&mut self,
pos: usize,
left: usize,
min_prec: u8,
last_op: Option<(Option<Self::Rule>, u8, bool)>,
primary: &mut F,
climb: &mut G)
-> (Option<(Option<Self::Rule>, u8, bool)>, Option<usize>)
where F: FnMut(&mut Self) -> bool,
G: FnMut(&mut Self) -> Option<(Option<Self::Rule>, u8, bool)>;

Expand Down
98 changes: 70 additions & 28 deletions src/parsers/rdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ macro_rules! impl_rdp {
use std::cell::Cell;
use std::cmp;

pub struct Rdp<T: Input> {
pub struct Rdp<T> {
input: T,
queue: Vec<Token<Rule>>,
queue_index: Cell<usize>,
Expand All @@ -128,7 +128,7 @@ macro_rules! impl_rdp {

impl_rdp!(@filter [ $( $ts )* ] []);

impl<T: Input> Rdp<T> {
impl<'a, T: Input<'a>> Rdp<T> {
pub fn new(input: T) -> Rdp<T> {
Rdp {
input: input,
Expand Down Expand Up @@ -189,17 +189,17 @@ macro_rules! impl_rdp {
)*
}

impl<T: Input> Parser for Rdp<T> {
impl<'a, T: Input<'a>> Parser<'a, T> for Rdp<T> {
type Rule = Rule;
type Token = Token<Rule>;

#[inline]
fn input(&self) -> &Input {
fn input(&self) -> &T {
&self.input
}

#[inline]
fn input_mut(&mut self) -> &mut Input {
fn input_mut(&mut self) -> &mut T {
&mut self.input
}

Expand Down Expand Up @@ -467,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);
}
Expand All @@ -487,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);
}
Expand All @@ -501,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);
}
Expand All @@ -515,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);
}
Expand All @@ -530,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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/sentence_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl_rdp! {
}

process! {
_word(&self) -> Node {
_word(&self) -> Node<'a> {
(&w: word) => Node::Sentence(w)
}
}
Expand Down

0 comments on commit be2e12f

Please sign in to comment.