| 1 |
// Copyright (C) 2023-2024 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU Proc Macro Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// Under Section 7 of GPL version 3, you are granted additional |
| 15 |
// permissions described in the GCC Runtime Library Exception, version |
| 16 |
// 3.1, as published by the Free Software Foundation. |
| 17 |
|
| 18 |
// You should have received a copy of the GNU General Public License and |
| 19 |
// a copy of the GCC Runtime Library Exception along with this program; |
| 20 |
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 21 |
// <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
use bridge; |
| 24 |
use std::fmt; |
| 25 |
use Span; |
| 26 |
use TokenStream; |
| 27 |
|
| 28 |
/// Describes how a sequence of token trees is delimited. |
| 29 |
#[repr(C)] |
| 30 |
#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 31 |
pub enum Delimiter { |
| 32 |
/// The sequence is delimited by a parenthesis `(...)`. |
| 33 |
Parenthesis, |
| 34 |
/// The sequence is delimited by a brace `{...}`. |
| 35 |
Brace, |
| 36 |
/// The sequence is delimited by a bracket `[...]`. |
| 37 |
Bracket, |
| 38 |
/// Invisible delimiter to preserve operator priority. |
| 39 |
None, |
| 40 |
} |
| 41 |
|
| 42 |
/// A delimited token stream. |
| 43 |
#[derive(Clone)] |
| 44 |
pub struct Group(pub(crate) bridge::group::Group); |
| 45 |
|
| 46 |
impl Group { |
| 47 |
/// Creates a new `Group`. |
| 48 |
/// |
| 49 |
/// # Arguments |
| 50 |
/// |
| 51 |
/// * `delimiter` - The delimiter surrounding the inner [`TokenStream`]. |
| 52 |
/// * `stream` - The tokenstream for this `Group`. |
| 53 |
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Self { |
| 54 |
Group(bridge::group::Group::new(delimiter, stream.0)) |
| 55 |
} |
| 56 |
|
| 57 |
/// Get the delimiter of the `Group`. |
| 58 |
pub fn delimiter(&self) -> Delimiter { |
| 59 |
self.0.delimiter() |
| 60 |
} |
| 61 |
|
| 62 |
/// Get the stream of the `Group`. |
| 63 |
/// |
| 64 |
/// # Note |
| 65 |
/// |
| 66 |
/// The returned stream does not include the delimiters of this group. |
| 67 |
pub fn stream(&self) -> TokenStream { |
| 68 |
TokenStream(self.0.stream()) |
| 69 |
} |
| 70 |
|
| 71 |
/// Get the span for the delimiters of this token stream, spanning the |
| 72 |
/// entire group. |
| 73 |
pub fn span(&self) -> Span { |
| 74 |
Span(self.0.span()) |
| 75 |
} |
| 76 |
|
| 77 |
/// Get the span pointing to the opening delimiter of this `Group`. |
| 78 |
pub fn span_open(&self) -> Span { |
| 79 |
Span(self.0.span()) |
| 80 |
} |
| 81 |
|
| 82 |
/// Get the span pointing to the closing delimiter of this `Group`. |
| 83 |
pub fn span_close(&self) -> Span { |
| 84 |
Span(self.0.span()) |
| 85 |
} |
| 86 |
|
| 87 |
/// Change the span for this `Group`'s delimiters, but not its internal |
| 88 |
/// tokens. |
| 89 |
/// |
| 90 |
/// # Note |
| 91 |
/// |
| 92 |
/// This method will **not** set the span of all the internal tokens spanned |
| 93 |
/// by this group, but rather it will only set the span of the delimiter |
| 94 |
/// tokens at the level of the `Group`. |
| 95 |
pub fn set_span(&mut self, span: Span) { |
| 96 |
self.0.set_span(span.0) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
impl fmt::Display for Group { |
| 101 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 102 |
self.0.fmt(f) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl fmt::Debug for Group { |
| 107 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 108 |
self.0.fmt(f) |
| 109 |
} |
| 110 |
} |