1 // Copyright (C) 2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
24 use std::convert::TryInto;
28 /// Describes the context of a [`Punct`] relatively to the next token.
30 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
32 /// A [`Punct`] is not immediately followed by another `Punct`.
34 /// A [`Punct`] is immediately followed by another `Punct` and can be
35 /// combined into a multi-character operator.
39 /// Single punctuation character such as `+`, `-` or `#`.
41 /// Multi-character operators like `+=` are represented as two instances of
42 /// `Punct` with different forms of `Spacing` returned.
44 pub struct Punct(pub(crate) bridge::punct::Punct);
47 /// Creates a new `Punct` from a given character and spacing.
51 /// * `ch` - The punctuation character.
52 /// * `spacing` - The link between this character and the next one.
56 /// This function will panic if the `ch` argument is not a valid
57 /// punctuation character allowed by the language.
58 pub fn new(ch: char, spacing: Spacing) -> Self {
59 Punct(bridge::punct::Punct::new(ch, spacing))
62 /// Get the value for this punctuation character as `char`.
63 pub fn as_char(&self) -> char {
67 .expect("Cannot convert from u32 to char")
70 /// Get the [`Spacing`] of this punctuation character, indicating whether
71 /// the following character can be combined into a multi-character operator
73 pub fn spacing(&self) -> Spacing {
77 /// Get the [`Span`] for this punctuation character.
78 pub fn span(&self) -> Span {
82 /// Set the span for this punctuation character.
86 /// * `span` - The new span value.
87 pub fn set_span(&mut self, span: Span) {
88 self.0.set_span(span.0);
92 impl fmt::Display for Punct {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 impl fmt::Debug for Punct {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104 impl PartialEq<char> for Punct {
105 fn eq(&self, rhs: &char) -> bool {
106 self.as_char() == *rhs
110 impl PartialEq<Punct> for char {
111 fn eq(&self, rhs: &Punct) -> bool {
112 *self == rhs.as_char()