]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgrust/libproc_macro/punct.rs
Update copyright years.
[thirdparty/gcc.git] / libgrust / libproc_macro / punct.rs
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::convert::TryInto;
25 use std::fmt;
26 use Span;
27
28 /// Describes the context of a [`Punct`] relatively to the next token.
29 #[repr(C)]
30 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
31 pub enum Spacing {
32 /// A [`Punct`] is not immediately followed by another `Punct`.
33 Alone,
34 /// A [`Punct`] is immediately followed by another `Punct` and can be
35 /// combined into a multi-character operator.
36 Joint,
37 }
38
39 /// Single punctuation character such as `+`, `-` or `#`.
40 ///
41 /// Multi-character operators like `+=` are represented as two instances of
42 /// `Punct` with different forms of `Spacing` returned.
43 #[derive(Clone)]
44 pub struct Punct(pub(crate) bridge::punct::Punct);
45
46 impl Punct {
47 /// Creates a new `Punct` from a given character and spacing.
48 ///
49 /// # Arguments
50 ///
51 /// * `ch` - The punctuation character.
52 /// * `spacing` - The link between this character and the next one.
53 ///
54 /// # Panics
55 ///
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))
60 }
61
62 /// Get the value for this punctuation character as `char`.
63 pub fn as_char(&self) -> char {
64 self.0
65 .ch
66 .try_into()
67 .expect("Cannot convert from u32 to char")
68 }
69
70 /// Get the [`Spacing`] of this punctuation character, indicating whether
71 /// the following character can be combined into a multi-character operator
72 /// or not.
73 pub fn spacing(&self) -> Spacing {
74 self.0.spacing
75 }
76
77 /// Get the [`Span`] for this punctuation character.
78 pub fn span(&self) -> Span {
79 Span(self.0.span())
80 }
81
82 /// Set the span for this punctuation character.
83 ///
84 /// # Arguments
85 ///
86 /// * `span` - The new span value.
87 pub fn set_span(&mut self, span: Span) {
88 self.0.set_span(span.0);
89 }
90 }
91
92 impl fmt::Display for Punct {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 self.0.fmt(f)
95 }
96 }
97
98 impl fmt::Debug for Punct {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 self.0.fmt(f)
101 }
102 }
103
104 impl PartialEq<char> for Punct {
105 fn eq(&self, rhs: &char) -> bool {
106 self.as_char() == *rhs
107 }
108 }
109
110 impl PartialEq<Punct> for char {
111 fn eq(&self, rhs: &Punct) -> bool {
112 *self == rhs.as_char()
113 }
114 }