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

Created as_span method #224

Merged
merged 3 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ fn main() {
for pair in pairs {
// A pair is a combination of the rule which matched and a span of input
println!("Rule: {:?}", pair.as_rule());
println!("Span: {:?}", pair.clone().into_span());
println!("Text: {}", pair.clone().into_span().as_str());
println!("Span: {:?}", pair.as_span());
println!("Text: {}", pair.as_span().as_str());

// A pair can be converted to an iterator of the tokens which make it up:
for inner_pair in pair.into_inner() {
match inner_pair.as_rule() {
Rule::alpha => println!("Letter: {}", inner_pair.into_span().as_str()),
Rule::digit => println!("Digit: {}", inner_pair.into_span().as_str()),
Rule::alpha => println!("Letter: {}", inner_pair.as_span().as_str()),
Rule::digit => println!("Digit: {}", inner_pair.as_span().as_str()),
_ => unreachable!()
};
}
Expand Down
4 changes: 2 additions & 2 deletions grammars/benches/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ fn consume(pair: Pair<Rule>) -> Json {
_ => unreachable!()
},
Rule::number => Json::Number(pair.as_str().parse().unwrap()),
Rule::string => Json::String(pair.into_span()),
Rule::string => Json::String(pair.as_span()),
Rule::array => Json::Array(pair.into_inner().map(value).collect()),
Rule::object => {
let pairs = pair.into_inner().map(|pos| {
let mut pair = pos.into_inner();

let key = pair.next().unwrap().into_span();
let key = pair.next().unwrap().as_span();
let value = value(pair.next().unwrap());

(key, value)
Expand Down
31 changes: 30 additions & 1 deletion pest/src/iterators/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ impl<'i, R: RuleType> Pair<'i, R> {

/// Returns the `Span` defined by the `Pair`, consuming it.
///
/// **Deprecated.** Please use `as_span` instead.
///
/// # Examples
///
/// ```
Expand All @@ -122,7 +124,34 @@ impl<'i, R: RuleType> Pair<'i, R> {
/// assert_eq!(pair.into_span().as_str(), "ab");
/// ```
#[inline]
#[deprecated(since="1.0.7", note="Please use `as_span` instead")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Please change to 2.0.0.

pub fn into_span(self) -> Span<'i> {
self.as_span()
}

/// Returns the `Span` defined by the `Pair`, **without** consuming it.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would keep the documentation the same for both. The only difference is that the deprecated one should point here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure no problem

///
/// # Examples
///
/// ```
/// # use std::rc::Rc;
/// # use pest;
/// # #[allow(non_camel_case_types)]
/// # #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
/// enum Rule {
/// ab
/// }
///
/// let input = "ab";
/// let pair = pest::state(input, |state| {
/// // generating Token pair with Rule::ab ...
/// # state.rule(Rule::ab, |s| s.match_string("ab"))
/// }).unwrap().next().unwrap();
///
/// assert_eq!(pair.as_span().as_str(), "ab");
/// ```
#[inline]
pub fn as_span(&self) -> Span<'i> {
let start = self.pos(self.start);
let end = self.pos(self.pair());

Expand Down Expand Up @@ -207,7 +236,7 @@ impl<'i, R: RuleType> fmt::Debug for Pair<'i, R> {
f,
"Pair {{ rule: {:?}, span: {:?}, inner: {:?} }}",
self.as_rule(),
self.clone().into_span(),
self.clone().as_span(),
self.clone().into_inner()
)
}
Expand Down
4 changes: 2 additions & 2 deletions pest/tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ fn consume(pair: Pair<Rule>) -> Json {
_ => unreachable!()
},
Rule::number => Json::Number(pair.as_str().parse().unwrap()),
Rule::string => Json::String(pair.into_span()),
Rule::string => Json::String(pair.as_span()),
Rule::array => Json::Array(pair.into_inner().map(value).collect()),
Rule::object => {
let pairs = pair.into_inner().map(|pos| {
let mut pair = pos.into_inner();

let key = pair.next().unwrap().into_span();
let key = pair.next().unwrap().as_span();
let value = value(pair.next().unwrap());

(key, value)
Expand Down