1 // Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 //! `TokenStream`s represent syntactic objects before they are converted into ASTs.
14 //! A `TokenStream` is, roughly speaking, a sequence (eg stream) of `TokenTree`s,
15 //! which are themselves a single `Token` or a `Delimited` subsequence of tokens.
18 //! `TokenStreams` are persistent data structures constructed as ropes with reference
19 //! counted-children. In general, this means that calling an operation on a `TokenStream`
20 //! (such as `slice`) produces an entirely new `TokenStream` from the borrowed reference to
21 //! the original. This essentially coerces `TokenStream`s into 'views' of their subparts,
22 //! and a borrowed `TokenStream` is sufficient to build an owned `TokenStream` without taking
23 //! ownership of the original.
25 use syntax_pos::{BytePos, Span, DUMMY_SP};
27 use ext::tt::{macro_parser, quoted};
29 use parse::token::{self, Token};
31 use serialize::{Decoder, Decodable, Encoder, Encodable};
35 use std::{fmt, iter, mem};
37 /// A delimited sequence of token trees
38 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
39 pub struct Delimited {
40 /// The type of delimiter
41 pub delim: token::DelimToken,
42 /// The delimited sequence of token trees
43 pub tts: ThinTokenStream,
47 /// Returns the opening delimiter as a token.
48 pub fn open_token(&self) -> token::Token {
49 token::OpenDelim(self.delim)
52 /// Returns the closing delimiter as a token.
53 pub fn close_token(&self) -> token::Token {
54 token::CloseDelim(self.delim)
57 /// Returns the opening delimiter as a token tree.
58 pub fn open_tt(&self, span: Span) -> TokenTree {
59 let open_span = if span.is_dummy() {
62 span.with_hi(span.lo() + BytePos(self.delim.len() as u32))
64 TokenTree::Token(open_span, self.open_token())
67 /// Returns the closing delimiter as a token tree.
68 pub fn close_tt(&self, span: Span) -> TokenTree {
69 let close_span = if span.is_dummy() {
72 span.with_lo(span.hi() - BytePos(self.delim.len() as u32))
74 TokenTree::Token(close_span, self.close_token())
77 /// Returns the token trees inside the delimiters.
78 pub fn stream(&self) -> TokenStream {
79 self.tts.clone().into()
83 /// When the main rust parser encounters a syntax-extension invocation, it
84 /// parses the arguments to the invocation as a token-tree. This is a very
85 /// loose structure, such that all sorts of different AST-fragments can
86 /// be passed to syntax extensions using a uniform type.
88 /// If the syntax extension is an MBE macro, it will attempt to match its
89 /// LHS token tree against the provided token tree, and if it finds a
90 /// match, will transcribe the RHS token tree, splicing in any captured
91 /// `macro_parser::matched_nonterminals` into the `SubstNt`s it finds.
93 /// The RHS of an MBE macro is the only place `SubstNt`s are substituted.
94 /// Nothing special happens to misnamed or misplaced `SubstNt`s.
95 #[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
98 Token(Span, token::Token),
99 /// A delimited sequence of token trees
100 Delimited(Span, Delimited),
104 /// Use this token tree as a matcher to parse given tts.
105 pub fn parse(cx: &base::ExtCtxt, mtch: &[quoted::TokenTree], tts: TokenStream)
106 -> macro_parser::NamedParseResult {
107 // `None` is because we're not interpolating
108 let directory = Directory {
109 path: Cow::from(cx.current_expansion.module.directory.as_path()),
110 ownership: cx.current_expansion.directory_ownership,
112 macro_parser::parse(cx.parse_sess(), tts, mtch, Some(directory), true)
115 /// Check if this TokenTree is equal to the other, regardless of span information.
116 pub fn eq_unspanned(&self, other: &TokenTree) -> bool {
117 match (self, other) {
118 (&TokenTree::Token(_, ref tk), &TokenTree::Token(_, ref tk2)) => tk == tk2,
119 (&TokenTree::Delimited(_, ref dl), &TokenTree::Delimited(_, ref dl2)) => {
120 dl.delim == dl2.delim &&
121 dl.stream().eq_unspanned(&dl2.stream())
127 // See comments in `interpolated_to_tokenstream` for why we care about
128 // *probably* equal here rather than actual equality
130 // This is otherwise the same as `eq_unspanned`, only recursing with a
132 pub fn probably_equal_for_proc_macro(&self, other: &TokenTree) -> bool {
133 match (self, other) {
134 (&TokenTree::Token(_, ref tk), &TokenTree::Token(_, ref tk2)) => {
135 tk.probably_equal_for_proc_macro(tk2)
137 (&TokenTree::Delimited(_, ref dl), &TokenTree::Delimited(_, ref dl2)) => {
138 dl.delim == dl2.delim &&
139 dl.stream().probably_equal_for_proc_macro(&dl2.stream())
145 /// Retrieve the TokenTree's span.
146 pub fn span(&self) -> Span {
148 TokenTree::Token(sp, _) | TokenTree::Delimited(sp, _) => sp,
152 /// Modify the `TokenTree`'s span inplace.
153 pub fn set_span(&mut self, span: Span) {
155 TokenTree::Token(ref mut sp, _) | TokenTree::Delimited(ref mut sp, _) => {
161 /// Indicates if the stream is a token that is equal to the provided token.
162 pub fn eq_token(&self, t: Token) -> bool {
164 TokenTree::Token(_, ref tk) => *tk == t,
169 pub fn joint(self) -> TokenStream {
170 TokenStream { kind: TokenStreamKind::JointTree(self) }
176 /// A `TokenStream` is an abstract sequence of tokens, organized into `TokenTree`s.
177 /// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s
178 /// instead of a representation of the abstract syntax tree.
179 /// Today's `TokenTree`s can still contain AST via `Token::Interpolated` for back-compat.
180 #[derive(Clone, Debug)]
181 pub struct TokenStream {
182 kind: TokenStreamKind,
185 #[derive(Clone, Debug)]
186 enum TokenStreamKind {
189 JointTree(TokenTree),
190 Stream(RcSlice<TokenStream>),
193 impl From<TokenTree> for TokenStream {
194 fn from(tt: TokenTree) -> TokenStream {
195 TokenStream { kind: TokenStreamKind::Tree(tt) }
199 impl From<Token> for TokenStream {
200 fn from(token: Token) -> TokenStream {
201 TokenTree::Token(DUMMY_SP, token).into()
205 impl<T: Into<TokenStream>> iter::FromIterator<T> for TokenStream {
206 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
207 TokenStream::concat(iter.into_iter().map(Into::into).collect::<Vec<_>>())
211 impl Eq for TokenStream {}
213 impl PartialEq<TokenStream> for TokenStream {
214 fn eq(&self, other: &TokenStream) -> bool {
215 self.trees().eq(other.trees())
220 pub fn len(&self) -> usize {
221 if let TokenStreamKind::Stream(ref slice) = self.kind {
228 pub fn empty() -> TokenStream {
229 TokenStream { kind: TokenStreamKind::Empty }
232 pub fn is_empty(&self) -> bool {
234 TokenStreamKind::Empty => true,
239 pub fn concat(mut streams: Vec<TokenStream>) -> TokenStream {
240 match streams.len() {
241 0 => TokenStream::empty(),
242 1 => streams.pop().unwrap(),
243 _ => TokenStream::concat_rc_slice(RcSlice::new(streams)),
247 fn concat_rc_slice(streams: RcSlice<TokenStream>) -> TokenStream {
248 TokenStream { kind: TokenStreamKind::Stream(streams) }
251 pub fn trees(&self) -> Cursor {
252 self.clone().into_trees()
255 pub fn into_trees(self) -> Cursor {
259 /// Compares two TokenStreams, checking equality without regarding span information.
260 pub fn eq_unspanned(&self, other: &TokenStream) -> bool {
261 let mut t1 = self.trees();
262 let mut t2 = other.trees();
263 for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
264 if !t1.eq_unspanned(&t2) {
268 t1.next().is_none() && t2.next().is_none()
271 // See comments in `interpolated_to_tokenstream` for why we care about
272 // *probably* equal here rather than actual equality
274 // This is otherwise the same as `eq_unspanned`, only recursing with a
276 pub fn probably_equal_for_proc_macro(&self, other: &TokenStream) -> bool {
277 let mut t1 = self.trees();
278 let mut t2 = other.trees();
279 for (t1, t2) in t1.by_ref().zip(t2.by_ref()) {
280 if !t1.probably_equal_for_proc_macro(&t2) {
284 t1.next().is_none() && t2.next().is_none()
287 /// Precondition: `self` consists of a single token tree.
288 /// Returns true if the token tree is a joint operation w.r.t. `proc_macro::TokenNode`.
289 pub fn as_tree(self) -> (TokenTree, bool /* joint? */) {
291 TokenStreamKind::Tree(tree) => (tree, false),
292 TokenStreamKind::JointTree(tree) => (tree, true),
297 pub fn map_enumerated<F: FnMut(usize, TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
298 let mut trees = self.into_trees();
299 let mut result = Vec::new();
301 while let Some(stream) = trees.next_as_stream() {
302 result.push(match stream.kind {
303 TokenStreamKind::Tree(tree) => f(i, tree).into(),
304 TokenStreamKind::JointTree(tree) => f(i, tree).joint(),
309 TokenStream::concat(result)
312 pub fn map<F: FnMut(TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
313 let mut trees = self.into_trees();
314 let mut result = Vec::new();
315 while let Some(stream) = trees.next_as_stream() {
316 result.push(match stream.kind {
317 TokenStreamKind::Tree(tree) => f(tree).into(),
318 TokenStreamKind::JointTree(tree) => f(tree).joint(),
322 TokenStream::concat(result)
325 fn first_tree_and_joint(&self) -> Option<(TokenTree, bool)> {
327 TokenStreamKind::Empty => None,
328 TokenStreamKind::Tree(ref tree) => Some((tree.clone(), false)),
329 TokenStreamKind::JointTree(ref tree) => Some((tree.clone(), true)),
330 TokenStreamKind::Stream(ref stream) => stream.first().unwrap().first_tree_and_joint(),
334 fn last_tree_if_joint(&self) -> Option<TokenTree> {
336 TokenStreamKind::Empty | TokenStreamKind::Tree(..) => None,
337 TokenStreamKind::JointTree(ref tree) => Some(tree.clone()),
338 TokenStreamKind::Stream(ref stream) => stream.last().unwrap().last_tree_if_joint(),
344 pub struct TokenStreamBuilder(Vec<TokenStream>);
346 impl TokenStreamBuilder {
347 pub fn new() -> TokenStreamBuilder {
348 TokenStreamBuilder(Vec::new())
351 pub fn push<T: Into<TokenStream>>(&mut self, stream: T) {
352 let stream = stream.into();
353 let last_tree_if_joint = self.0.last().and_then(TokenStream::last_tree_if_joint);
354 if let Some(TokenTree::Token(last_span, last_tok)) = last_tree_if_joint {
355 if let Some((TokenTree::Token(span, tok), is_joint)) = stream.first_tree_and_joint() {
356 if let Some(glued_tok) = last_tok.glue(tok) {
357 let last_stream = self.0.pop().unwrap();
358 self.push_all_but_last_tree(&last_stream);
359 let glued_span = last_span.to(span);
360 let glued_tt = TokenTree::Token(glued_span, glued_tok);
361 let glued_tokenstream = if is_joint {
366 self.0.push(glued_tokenstream);
367 self.push_all_but_first_tree(&stream);
375 pub fn add<T: Into<TokenStream>>(mut self, stream: T) -> Self {
380 pub fn build(self) -> TokenStream {
381 TokenStream::concat(self.0)
384 fn push_all_but_last_tree(&mut self, stream: &TokenStream) {
385 if let TokenStreamKind::Stream(ref streams) = stream.kind {
386 let len = streams.len();
389 2 => self.0.push(streams[0].clone().into()),
390 _ => self.0.push(TokenStream::concat_rc_slice(streams.sub_slice(0 .. len - 1))),
392 self.push_all_but_last_tree(&streams[len - 1])
396 fn push_all_but_first_tree(&mut self, stream: &TokenStream) {
397 if let TokenStreamKind::Stream(ref streams) = stream.kind {
398 let len = streams.len();
401 2 => self.0.push(streams[1].clone().into()),
402 _ => self.0.push(TokenStream::concat_rc_slice(streams.sub_slice(1 .. len))),
404 self.push_all_but_first_tree(&streams[0])
410 pub struct Cursor(CursorKind);
415 Tree(TokenTree, bool /* consumed? */),
416 JointTree(TokenTree, bool /* consumed? */),
417 Stream(StreamCursor),
421 struct StreamCursor {
422 stream: RcSlice<TokenStream>,
424 stack: Vec<(RcSlice<TokenStream>, usize)>,
428 fn new(stream: RcSlice<TokenStream>) -> Self {
429 StreamCursor { stream: stream, index: 0, stack: Vec::new() }
432 fn next_as_stream(&mut self) -> Option<TokenStream> {
434 if self.index < self.stream.len() {
436 let next = self.stream[self.index - 1].clone();
438 TokenStreamKind::Tree(..) | TokenStreamKind::JointTree(..) => return Some(next),
439 TokenStreamKind::Stream(stream) => self.insert(stream),
440 TokenStreamKind::Empty => {}
442 } else if let Some((stream, index)) = self.stack.pop() {
443 self.stream = stream;
451 fn insert(&mut self, stream: RcSlice<TokenStream>) {
452 self.stack.push((mem::replace(&mut self.stream, stream),
453 mem::replace(&mut self.index, 0)));
457 impl Iterator for Cursor {
458 type Item = TokenTree;
460 fn next(&mut self) -> Option<TokenTree> {
461 self.next_as_stream().map(|stream| match stream.kind {
462 TokenStreamKind::Tree(tree) | TokenStreamKind::JointTree(tree) => tree,
469 fn new(stream: TokenStream) -> Self {
470 Cursor(match stream.kind {
471 TokenStreamKind::Empty => CursorKind::Empty,
472 TokenStreamKind::Tree(tree) => CursorKind::Tree(tree, false),
473 TokenStreamKind::JointTree(tree) => CursorKind::JointTree(tree, false),
474 TokenStreamKind::Stream(stream) => CursorKind::Stream(StreamCursor::new(stream)),
478 pub fn next_as_stream(&mut self) -> Option<TokenStream> {
479 let (stream, consumed) = match self.0 {
480 CursorKind::Tree(ref tree, ref mut consumed @ false) =>
481 (tree.clone().into(), consumed),
482 CursorKind::JointTree(ref tree, ref mut consumed @ false) =>
483 (tree.clone().joint(), consumed),
484 CursorKind::Stream(ref mut cursor) => return cursor.next_as_stream(),
492 pub fn insert(&mut self, stream: TokenStream) {
494 _ if stream.is_empty() => return,
495 CursorKind::Empty => *self = stream.trees(),
496 CursorKind::Tree(_, consumed) | CursorKind::JointTree(_, consumed) => {
497 *self = TokenStream::concat(vec![self.original_stream(), stream]).trees();
502 CursorKind::Stream(ref mut cursor) => {
503 cursor.insert(ThinTokenStream::from(stream).0.unwrap());
508 pub fn original_stream(&self) -> TokenStream {
510 CursorKind::Empty => TokenStream::empty(),
511 CursorKind::Tree(ref tree, _) => tree.clone().into(),
512 CursorKind::JointTree(ref tree, _) => tree.clone().joint(),
513 CursorKind::Stream(ref cursor) => TokenStream::concat_rc_slice({
514 cursor.stack.get(0).cloned().map(|(stream, _)| stream)
515 .unwrap_or(cursor.stream.clone())
520 pub fn look_ahead(&self, n: usize) -> Option<TokenTree> {
521 fn look_ahead(streams: &[TokenStream], mut n: usize) -> Result<TokenTree, usize> {
522 for stream in streams {
523 n = match stream.kind {
524 TokenStreamKind::Tree(ref tree) | TokenStreamKind::JointTree(ref tree)
525 if n == 0 => return Ok(tree.clone()),
526 TokenStreamKind::Tree(..) | TokenStreamKind::JointTree(..) => n - 1,
527 TokenStreamKind::Stream(ref stream) => match look_ahead(stream, n) {
528 Ok(tree) => return Ok(tree),
539 CursorKind::Tree(_, true) |
540 CursorKind::JointTree(_, true) => Err(n),
541 CursorKind::Tree(ref tree, false) |
542 CursorKind::JointTree(ref tree, false) => look_ahead(&[tree.clone().into()], n),
543 CursorKind::Stream(ref cursor) => {
544 look_ahead(&cursor.stream[cursor.index ..], n).or_else(|mut n| {
545 for &(ref stream, index) in cursor.stack.iter().rev() {
546 n = match look_ahead(&stream[index..], n) {
547 Ok(tree) => return Ok(tree),
559 /// The `TokenStream` type is large enough to represent a single `TokenTree` without allocation.
560 /// `ThinTokenStream` is smaller, but needs to allocate to represent a single `TokenTree`.
561 /// We must use `ThinTokenStream` in `TokenTree::Delimited` to avoid infinite size due to recursion.
562 #[derive(Debug, Clone)]
563 pub struct ThinTokenStream(Option<RcSlice<TokenStream>>);
565 impl From<TokenStream> for ThinTokenStream {
566 fn from(stream: TokenStream) -> ThinTokenStream {
567 ThinTokenStream(match stream.kind {
568 TokenStreamKind::Empty => None,
569 TokenStreamKind::Tree(tree) => Some(RcSlice::new(vec![tree.into()])),
570 TokenStreamKind::JointTree(tree) => Some(RcSlice::new(vec![tree.joint()])),
571 TokenStreamKind::Stream(stream) => Some(stream),
576 impl From<ThinTokenStream> for TokenStream {
577 fn from(stream: ThinTokenStream) -> TokenStream {
578 stream.0.map(TokenStream::concat_rc_slice).unwrap_or_else(TokenStream::empty)
582 impl Eq for ThinTokenStream {}
584 impl PartialEq<ThinTokenStream> for ThinTokenStream {
585 fn eq(&self, other: &ThinTokenStream) -> bool {
586 TokenStream::from(self.clone()) == TokenStream::from(other.clone())
590 impl fmt::Display for TokenStream {
591 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
592 f.write_str(&pprust::tokens_to_string(self.clone()))
596 impl Encodable for TokenStream {
597 fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), E::Error> {
598 self.trees().collect::<Vec<_>>().encode(encoder)
602 impl Decodable for TokenStream {
603 fn decode<D: Decoder>(decoder: &mut D) -> Result<TokenStream, D::Error> {
604 Vec::<TokenTree>::decode(decoder).map(|vec| vec.into_iter().collect())
608 impl Encodable for ThinTokenStream {
609 fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), E::Error> {
610 TokenStream::from(self.clone()).encode(encoder)
614 impl Decodable for ThinTokenStream {
615 fn decode<D: Decoder>(decoder: &mut D) -> Result<ThinTokenStream, D::Error> {
616 TokenStream::decode(decoder).map(Into::into)
623 use syntax::ast::Ident;
625 use syntax_pos::{Span, BytePos, NO_EXPANSION};
626 use parse::token::Token;
627 use util::parser_testing::string_to_stream;
629 fn string_to_ts(string: &str) -> TokenStream {
630 string_to_stream(string.to_owned())
633 fn sp(a: u32, b: u32) -> Span {
634 Span::new(BytePos(a), BytePos(b), NO_EXPANSION)
640 let test_res = string_to_ts("foo::bar::baz");
641 let test_fst = string_to_ts("foo::bar");
642 let test_snd = string_to_ts("::baz");
643 let eq_res = TokenStream::concat(vec![test_fst, test_snd]);
644 assert_eq!(test_res.trees().count(), 5);
645 assert_eq!(eq_res.trees().count(), 5);
646 assert_eq!(test_res.eq_unspanned(&eq_res), true);
651 fn test_to_from_bijection() {
653 let test_start = string_to_ts("foo::bar(baz)");
654 let test_end = test_start.trees().collect();
655 assert_eq!(test_start, test_end)
662 let test_res = string_to_ts("foo");
663 let test_eqs = string_to_ts("foo");
664 assert_eq!(test_res, test_eqs)
671 let test_res = string_to_ts("::bar::baz");
672 let test_eqs = string_to_ts("::bar::baz");
673 assert_eq!(test_res, test_eqs)
680 let test_res = string_to_ts("");
681 let test_eqs = string_to_ts("");
682 assert_eq!(test_res, test_eqs)
689 let test_res = string_to_ts("::bar::baz");
690 let test_eqs = string_to_ts("bar::baz");
691 assert_eq!(test_res == test_eqs, false)
698 let test_res = string_to_ts("(bar,baz)");
699 let test_eqs = string_to_ts("bar,baz");
700 assert_eq!(test_res == test_eqs, false)
707 let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
708 let test1: TokenStream =
709 TokenTree::Token(sp(0, 1), Token::Ident(Ident::from_str("a"), false)).into();
710 let test2 = string_to_ts("foo(bar::baz)");
712 assert_eq!(test0.is_empty(), true);
713 assert_eq!(test1.is_empty(), false);
714 assert_eq!(test2.is_empty(), false);
719 fn test_dotdotdot() {
720 let mut builder = TokenStreamBuilder::new();
721 builder.push(TokenTree::Token(sp(0, 1), Token::Dot).joint());
722 builder.push(TokenTree::Token(sp(1, 2), Token::Dot).joint());
723 builder.push(TokenTree::Token(sp(2, 3), Token::Dot));
724 let stream = builder.build();
725 assert!(stream.eq_unspanned(&string_to_ts("...")));
726 assert_eq!(stream.trees().count(), 1);