1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2015 Pierre Talbot (IRCAM)

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Implementation of `Stream` for `&'a str` type. It implements all traits required by `CharStream`.

use stream::*;
use std::cmp::{Ordering, min};
use super::*;
pub use std::ops::Range;
pub use syntex_pos::Span;

impl<'a> Stream for &'a str
{
  type Output = StrStream<'a>;
  fn stream(self) -> StrStream<'a> {
    StrStream::new(self)
  }
}

impl<'a> Stream for &'a String
{
  type Output = StrStream<'a>;
  fn stream(self) -> StrStream<'a> {
    self.as_str().stream()
  }
}

/// Represents a stream from a `&'a str`. It implements all traits required by `CharStream`.
#[derive(Clone, Hash, Debug)]
pub struct StrStream<'a>
{
  raw_data: &'a str,
  bytes_offset: usize
}

impl<'a> StrStream<'a>
{
  fn new(raw_data: &'a str) -> StrStream<'a> {
    StrStream {
      raw_data: raw_data,
      bytes_offset: 0
    }
  }

  #[inline(always)]
  fn assert_same_raw_data(&self, other: &StrStream<'a>) {
    debug_assert!(self.raw_data.as_ptr() == other.raw_data.as_ptr(),
      "Operations between two streams are only defined when they share the same raw data.");
  }

  // Partially taken from https://github.com/kevinmehall/rust-peg/blob/master/src/translate.rs
  pub fn line_column(&self) -> (usize, usize) {
    let mut remaining = self.bytes_offset;
    let mut line_no = 1usize;
    for line in self.raw_data.lines() {
      let line_len = line.len() + 1;
      if remaining < line_len {
        break;
      }
      remaining -= line_len;
      line_no += 1;
    }
    (line_no, remaining + 1)
  }

  pub fn bytes_offset(&self) -> usize {
    self.bytes_offset
  }

  pub fn current_char(&self) -> Option<char> {
    self.raw_data[self.bytes_offset..].chars().next()
  }

  pub fn slice(&self, end: StrStream<'a>) -> &'a str {
    unsafe {
      self.raw_data.get_unchecked(self.bytes_offset..end.bytes_offset)
    }
  }
}

impl<'a> Iterator for StrStream<'a>
{
  type Item = char;
  fn next(&mut self) -> Option<Self::Item> {
    if self.bytes_offset < self.raw_data.len() {
      let current = self.current_char().unwrap();
      self.bytes_offset += current.len_utf8();
      Some(current)
    } else {
      None
    }
  }
}

impl<'a> PartialEq for StrStream<'a>
{
  fn eq(&self, other: &Self) -> bool {
    self.assert_same_raw_data(other);
    self.bytes_offset == other.bytes_offset
  }
}

impl<'a> Eq for StrStream<'a> {}

impl<'a> PartialOrd for StrStream<'a>
{
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    self.assert_same_raw_data(other);
    self.bytes_offset.partial_cmp(&other.bytes_offset)
  }
}

impl<'a> Ord for StrStream<'a>
{
  fn cmp(&self, other: &Self) -> Ordering {
    self.assert_same_raw_data(other);
    self.bytes_offset.cmp(&other.bytes_offset)
  }
}

impl<'a> Location for StrStream<'a>
{
  fn location(&self) -> String {
    let (line, column) = self.line_column();
    format!("{}:{}", line, column)
  }
}

impl<'a> CodeSnippet for StrStream<'a>
{
  fn code_snippet(&self, len_hint: usize) -> String {
    let total_len = self.raw_data.len();
    let current_offset = self.bytes_offset;
    if current_offset == total_len {
      String::from("<end-of-file>")
    }
    else {
      let len = min(total_len - current_offset, len_hint);
      String::from(&self.raw_data[current_offset..][..len])
    }
  }
}

impl<'a> ConsumePrefix<&'static str> for StrStream<'a>
{
  fn consume_prefix(&mut self, prefix: &'static str) -> bool {
    let current_offset = self.bytes_offset;
    let end_offset = current_offset + prefix.len();
    if end_offset <= self.raw_data.len()
     && &self.raw_data.as_bytes()[current_offset..end_offset] == prefix.as_bytes()
    {
      self.bytes_offset = end_offset;
      true
    } else {
      false
    }
  }
}

impl<'a> HasNext for StrStream<'a>
{
  fn has_next(&self) -> bool {
    self.bytes_offset < self.raw_data.len()
  }
}

impl<'a> StreamSpan for Range<StrStream<'a>>
{
  type Output = Span;
  fn stream_span(&self) -> Self::Output {
    make_span(
      self.start.bytes_offset,
      self.end.bytes_offset)
  }
}

#[cfg(test)]
mod test {
  use super::*;

  fn consume_prefix_test<'a>(stream: &StrStream<'a>, prefix: &'static str,
    prefix_match: bool, next_char: Option<char>)
  {
    let mut s2 = stream.clone();
    assert_eq!(s2.consume_prefix(prefix), prefix_match);
    assert!(s2.next() == next_char);
  }

  #[test]
  fn test_consume_prefix() {
    let s1 = &"abc".stream();
    consume_prefix_test(s1, "abc", true, None);
    consume_prefix_test(s1, "ab", true, Some('c'));
    consume_prefix_test(s1, "", true, Some('a'));
    consume_prefix_test(s1, "ac", false, Some('a'));
    consume_prefix_test(s1, "z", false, Some('a'));
  }

  fn test_str_stream<'a, I>(mut s1: StrStream<'a>, chars: I) where
   I: Iterator<Item=char>
  {
    let s1_init = s1.clone();
    let mut s2 = s1_init.clone();
    for c in chars {
      assert!(s1 == s2);
      assert_eq!(s1.next().unwrap(), c);
      assert!(s1 > s1_init);
      assert!(s1 > s2);
      s2 = s1.clone();
    }
    assert_eq!(s1.next(), None);
    assert_eq!(s2.next(), None);
    assert!(s1 > s1_init);
    assert!(s1 == s2);
  }

  #[test]
  fn test_stream() {
    let abc = "abc";
    test_str_stream(abc.stream(), abc.chars());
  }

  #[test]
  fn test_string_stream() {
    let abc = String::from("abc");
    test_str_stream(abc.stream(), abc.chars());
  }

  #[test]
  fn test_empty_stream() {
    let mut empty = "".stream();
    assert_eq!(empty.bytes_offset, 0);
    assert_eq!(empty.next(), None);
    assert_eq!(empty.next(), None);
    assert_eq!(empty.bytes_offset, 0);
    assert!(empty == empty);
    assert!(!(empty > empty));
    let empty2 = empty.clone();
    assert!(empty == empty2);
    assert!(!(empty > empty2));
  }

  fn test_unrelated_streams<R, F>(op: F) where
   F: FnOnce(&StrStream<'static>, &StrStream<'static>) -> R
  {
    let s1 = "abc".stream();
    let s2 = "def".stream();
    op(&s1, &s2);
  }

  #[test]
  #[should_panic]
  fn unrelated_stream_eq() {
    test_unrelated_streams(|a, b| a == b);
  }

  #[test]
  #[should_panic]
  fn unrelated_stream_partial_ord() {
    test_unrelated_streams(|a, b| a.partial_cmp(b));
  }

  #[test]
  #[should_panic]
  fn unrelated_stream_ord() {
    test_unrelated_streams(|a, b| a.cmp(b));
  }
}