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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! Token types and structures for the Perl lexer.
//!
//! [`TokenType`] classifies every token the lexer can emit, and [`Token`]
//! bundles a type with its source text and byte span.
use std::sync::Arc;
/// Parts of an interpolated string
#[derive(Debug, Clone, PartialEq)]
pub enum StringPart {
/// Literal text
Literal(Arc<str>),
/// Variable interpolation: $var, @array, %hash
Variable(Arc<str>),
/// Expression interpolation: ${expr}, @{expr}
Expression(Arc<str>),
/// Method call: `->method()`
MethodCall(Arc<str>),
/// Array slice: [1..3]
ArraySlice(Arc<str>),
}
/// Token types for Perl
#[derive(Debug, Clone, PartialEq)]
pub enum TokenType {
// Slash-derived tokens
/// Division operator: /
Division,
/// Regex match: m// or //
RegexMatch,
/// Substitution: s///
Substitution,
/// Transliteration: tr/// or y///
Transliteration,
/// Quote regex: qr//
QuoteRegex,
// String and quote tokens
/// String literal: "string" or 'string'
StringLiteral,
/// Single quote: q//
QuoteSingle,
/// Double quote: qq//
QuoteDouble,
/// Quote words: qw//
QuoteWords,
/// Quote command: qx// or `backticks`
QuoteCommand,
// String interpolation tokens
/// String with interpolated parts
InterpolatedString(Vec<StringPart>),
// Heredoc tokens
/// Heredoc start: <<EOF or <<'EOF'
HeredocStart,
/// Heredoc body content
HeredocBody(Arc<str>),
// Format declarations
/// Format body content
FormatBody(Arc<str>),
// Version strings
/// Version string: v5.32.0
Version(Arc<str>),
// POD documentation
/// POD documentation block
Pod,
// Data sections
/// Data section marker: __DATA__ or __END__
DataMarker(Arc<str>),
/// Data section body content
DataBody(Arc<str>),
// Error recovery
/// Unknown rest of input (used when budget exceeded)
UnknownRest,
// Identifiers and literals
/// Identifier or variable name
Identifier(Arc<str>),
/// Numeric literal
Number(Arc<str>),
/// Operator
Operator(Arc<str>),
/// Keyword
Keyword(Arc<str>),
// Delimiters
/// Left parenthesis: (
LeftParen,
/// Right parenthesis: )
RightParen,
/// Left bracket: [
LeftBracket,
/// Right bracket: ]
RightBracket,
/// Left brace: {
LeftBrace,
/// Right brace: }
RightBrace,
// Punctuation
/// Semicolon: ;
Semicolon,
/// Comma: ,
Comma,
/// Colon: :
Colon,
/// Arrow: ->
Arrow,
/// Fat comma: =>
FatComma,
// Whitespace and comments
/// Whitespace (usually not returned)
Whitespace,
/// Newline character
Newline,
/// Comment text
Comment(Arc<str>),
// Special tokens
/// End of file
EOF,
/// Error token for invalid input
Error(Arc<str>),
}
impl TokenType {
/// Return `true` when this token is ignorable trivia.
pub fn is_trivia(&self) -> bool {
matches!(self, Self::Whitespace | Self::Newline | Self::Comment(_))
}
/// Return `true` when lexing could not continue safely.
pub fn is_recovery_token(&self) -> bool {
matches!(self, Self::UnknownRest | Self::Error(_))
}
}
/// A single token produced by [`PerlLexer`](crate::PerlLexer).
///
/// Carries its [`TokenType`], the original source text (as a cheap-to-clone
/// `Arc<str>`), and the byte span within the input.
#[derive(Debug, Clone)]
pub struct Token {
/// Classification of this token (keyword, operator, literal, etc.).
pub token_type: TokenType,
/// Original source text that this token spans.
pub text: Arc<str>,
/// Starting byte offset (inclusive) in the source input.
pub start: usize,
/// Ending byte offset (exclusive) in the source input.
pub end: usize,
}
impl Token {
/// Create a new token with the given type, source text, and byte span.
pub fn new(token_type: TokenType, text: impl Into<Arc<str>>, start: usize, end: usize) -> Self {
Self { token_type, text: text.into(), start, end }
}
/// Return the byte length of this token's span (`end - start`).
pub fn len(&self) -> usize {
self.end - self.start
}
/// Return `true` if the token has a zero-length span.
pub fn is_empty(&self) -> bool {
self.start == self.end
}
}
#[cfg(test)]
mod tests {
use super::{Token, TokenType};
use std::sync::Arc;
#[test]
fn token_type_trivia_classifier_matches_expected_variants()
-> Result<(), Box<dyn std::error::Error>> {
assert!(TokenType::Whitespace.is_trivia());
assert!(TokenType::Newline.is_trivia());
assert!(TokenType::Comment(Arc::from("# note")).is_trivia());
assert!(!TokenType::Identifier(Arc::from("foo")).is_trivia());
Ok(())
}
#[test]
fn token_type_recovery_classifier_matches_expected_variants()
-> Result<(), Box<dyn std::error::Error>> {
assert!(TokenType::UnknownRest.is_recovery_token());
assert!(TokenType::Error(Arc::from("oops")).is_recovery_token());
assert!(!TokenType::EOF.is_recovery_token());
Ok(())
}
// --- Token::new ---
#[test]
fn token_new_stores_type_text_and_span() -> Result<(), Box<dyn std::error::Error>> {
let tok = Token::new(TokenType::Semicolon, ";", 3, 4);
assert_eq!(tok.token_type, TokenType::Semicolon);
assert_eq!(tok.text.as_ref(), ";");
assert_eq!(tok.start, 3);
assert_eq!(tok.end, 4);
Ok(())
}
#[test]
fn token_new_accepts_arc_str_directly() -> Result<(), Box<dyn std::error::Error>> {
let text: Arc<str> = Arc::from("hello");
let tok = Token::new(TokenType::Identifier(Arc::from("hello")), text, 0, 5);
assert_eq!(tok.start, 0);
assert_eq!(tok.end, 5);
Ok(())
}
// --- Token::len ---
#[test]
fn token_len_returns_end_minus_start() -> Result<(), Box<dyn std::error::Error>> {
let tok = Token::new(TokenType::Semicolon, ";", 10, 17);
assert_eq!(tok.len(), 7);
Ok(())
}
#[test]
fn token_len_zero_when_span_is_empty() -> Result<(), Box<dyn std::error::Error>> {
let tok = Token::new(TokenType::EOF, "", 5, 5);
assert_eq!(tok.len(), 0);
Ok(())
}
#[test]
fn token_len_single_byte_span() -> Result<(), Box<dyn std::error::Error>> {
let tok = Token::new(TokenType::Comma, ",", 0, 1);
assert_eq!(tok.len(), 1);
Ok(())
}
// --- Token::is_empty ---
#[test]
fn token_is_empty_true_for_zero_length_span() -> Result<(), Box<dyn std::error::Error>> {
let tok = Token::new(TokenType::EOF, "", 0, 0);
assert!(tok.is_empty());
Ok(())
}
#[test]
fn token_is_empty_true_for_non_zero_start_matching_end()
-> Result<(), Box<dyn std::error::Error>> {
let tok = Token::new(TokenType::EOF, "", 42, 42);
assert!(tok.is_empty());
Ok(())
}
#[test]
fn token_is_empty_false_for_non_zero_length_span() -> Result<(), Box<dyn std::error::Error>> {
let tok = Token::new(TokenType::Semicolon, ";", 0, 1);
assert!(!tok.is_empty());
Ok(())
}
}