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
use crate::{FormatToken, Span, TerminalIndex, ToSpan};
use std::borrow::Cow;
use std::convert::From;
use std::fmt::{Debug, Display, Error, Formatter};

use super::{Location, TokenNumber};

//
// Special token constants the lexer has to deal with regularly.
// There are some special fix values used for common token types.
//

/// End of input constant
pub const EOI: TerminalIndex = 0;
/// New line token
pub const NEW_LINE: TerminalIndex = 1;
/// Whitespace token
pub const WHITESPACE: TerminalIndex = 2;
/// Line comment token
pub const LINE_COMMENT: TerminalIndex = 3;
/// Block comment token
pub const BLOCK_COMMENT: TerminalIndex = 4;
/// Index of the first user token.
pub const FIRST_USER_TOKEN: TerminalIndex = 5;

const EOI_TOKEN: &str = "$";

///
/// The Token<'t> type represents a scanned token.
/// It has a reference to the scanned text in the text member.
///
/// The lifetime parameter `'t` refers to the lifetime of the scanned text.
///
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct Token<'t> {
    /// The matched string
    pub(crate) text: Cow<'t, str>,

    /// The index of the terminal in the augmented terminal list
    pub token_type: TerminalIndex,

    /// Position information
    pub location: Location,

    /// Unique token number that allows ordering of tokens from different contexts, e.g. comment
    /// tokens can be intermingled with normal tokens.
    pub token_number: TokenNumber,
}

impl<'t> Token<'t> {
    ///
    /// Creates an End-Of-Input token
    ///
    pub fn eoi(token_number: TokenNumber) -> Self {
        Self {
            text: EOI_TOKEN.into(),
            token_type: EOI,
            location: Location::default(),
            token_number,
        }
    }

    ///
    /// Creates a token with given values.
    ///
    pub fn with<T>(
        text: T,
        token_type: TerminalIndex,
        location: Location,
        token_number: TokenNumber,
    ) -> Self
    where
        T: Into<Cow<'t, str>>,
    {
        Self {
            text: text.into(),
            token_type,
            location,
            token_number,
        }
    }

    /// Change the location of the token after it's creation
    pub fn with_location(mut self, location: Location) -> Self {
        self.location = location;
        self
    }

    /// Change the location of the token after it's creation
    pub fn with_type(mut self, token_type: TerminalIndex) -> Self {
        self.token_type = token_type;
        self
    }

    ///
    /// Indicates wether the token is normally skipped by the TokenStream.
    /// The behavior is independent from the language.
    ///
    #[inline]
    pub fn is_skip_token(&self) -> bool {
        self.token_type > EOI && self.token_type < FIRST_USER_TOKEN
    }

    ///
    /// Indicates wether the token is a comment token.
    ///
    #[inline]
    pub fn is_comment_token(&self) -> bool {
        self.token_type == LINE_COMMENT || self.token_type == BLOCK_COMMENT
    }

    ///
    /// Accesses the token's scanned text
    ///
    pub fn text(&self) -> &str {
        self.text.as_ref()
    }

    ///
    /// Creates an owned instance of the token from a shared reference
    ///
    pub fn to_owned(&self) -> Token<'static> {
        Token {
            text: Cow::Owned(self.text.clone().into_owned()),
            token_type: self.token_type,
            location: self.location.clone(),
            token_number: self.token_number,
        }
    }

    ///
    /// Creates an owned instance of the token and consumes self
    ///
    pub fn into_owned(self) -> Token<'static> {
        Token {
            text: Cow::Owned(self.text.into_owned()),
            token_type: self.token_type,
            location: self.location,
            token_number: self.token_number,
        }
    }
}

impl Display for Token<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
        let (c1, c2) = if self.text.starts_with('\'') {
            ('<', '>')
        } else {
            ('\'', '\'')
        };
        write!(
            f,
            "{}{}{}, Ty:{}, at {}",
            c1, self.text, c2, self.token_type, self.location
        )
    }
}

impl FormatToken for Token<'_> {
    fn format(&self, terminal_names: &'static [&'static str]) -> String {
        let name = terminal_names[self.token_type as usize];
        format!(
            "{} ({}) at {}[{}]",
            self.text, name, self.location, self.token_number,
        )
    }
}

impl From<&Token<'_>> for std::ops::Range<usize> {
    fn from(token: &Token<'_>) -> Self {
        (&token.location).into()
    }
}

impl From<&Token<'_>> for Location {
    fn from(token: &Token<'_>) -> Self {
        token.location.clone()
    }
}

impl From<&Token<'_>> for Span {
    fn from(token: &Token<'_>) -> Self {
        (Into::<std::ops::Range<usize>>::into(&token.location)).into()
    }
}

impl ToSpan for Token<'_> {
    fn span(&self) -> Span {
        self.into()
    }
}