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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/// Token types and syntax node kinds for patch files
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[allow(non_camel_case_types)]
#[repr(u16)]
pub enum SyntaxKind {
// Tokens
/// Minus sign token
MINUS = 0,
/// Plus sign token
PLUS,
/// At sign token
AT,
/// Space character at start of line
SPACE,
/// Newline character
NEWLINE,
/// Whitespace characters (spaces and tabs)
WHITESPACE,
/// Numeric token
NUMBER,
/// Comma token
COMMA,
/// Colon token
COLON,
/// Dot token
DOT,
/// Slash token
SLASH,
/// File path token
PATH,
/// Text content token
TEXT,
/// Error token
ERROR,
// Additional tokens for other diff formats
/// Star token (for context diffs)
STAR,
/// Exclamation mark (for context diffs)
EXCLAMATION,
/// Less than sign (for normal/ed diffs)
LESS_THAN,
/// Greater than sign (for normal/ed diffs)
GREATER_THAN,
/// Letter 'a' (for ed diff commands)
LETTER_A,
/// Letter 'c' (for ed diff commands)
LETTER_C,
/// Letter 'd' (for ed diff commands)
LETTER_D,
/// Backslash token
BACKSLASH,
// Composite nodes
/// Root node of the syntax tree
ROOT,
/// A patch file node (generic, format determined by content)
PATCH_FILE,
// Unified diff nodes
/// File header node
FILE_HEADER,
/// Old file header node (unified)
OLD_FILE,
/// New file header node (unified)
NEW_FILE,
/// Hunk node (unified)
HUNK,
/// Hunk header node (unified)
HUNK_HEADER,
/// Hunk range node
HUNK_RANGE,
/// Context line node
CONTEXT_LINE,
/// Add line node
ADD_LINE,
/// Delete line node
DELETE_LINE,
// Context diff nodes
/// Context diff file node
CONTEXT_DIFF_FILE,
/// Context diff old file header
CONTEXT_OLD_FILE,
/// Context diff new file header
CONTEXT_NEW_FILE,
/// Context diff hunk
CONTEXT_HUNK,
/// Context diff hunk header
CONTEXT_HUNK_HEADER,
/// Context diff old section
CONTEXT_OLD_SECTION,
/// Context diff new section
CONTEXT_NEW_SECTION,
/// Context diff change line (!)
CONTEXT_CHANGE_LINE,
// Ed diff nodes
/// Ed diff command node
ED_COMMAND,
/// Ed diff add command
ED_ADD_COMMAND,
/// Ed diff delete command
ED_DELETE_COMMAND,
/// Ed diff change command
ED_CHANGE_COMMAND,
/// Ed diff content line
ED_CONTENT_LINE,
// Normal diff nodes
/// Normal diff hunk
NORMAL_HUNK,
/// Normal diff change command
NORMAL_CHANGE_COMMAND,
/// Normal diff old lines
NORMAL_OLD_LINES,
/// Normal diff new lines
NORMAL_NEW_LINES,
/// Normal diff separator
NORMAL_SEPARATOR,
// Common nodes
/// Hunk line node (generic)
HUNK_LINE,
/// No newline at end of file line
NO_NEWLINE_LINE,
}
impl From<SyntaxKind> for rowan::SyntaxKind {
fn from(kind: SyntaxKind) -> Self {
Self(kind as u16)
}
}
/// Lex a patch file into tokens
pub fn lex(input: &str) -> impl Iterator<Item = (SyntaxKind, &str)> + '_ {
Lexer::new(input)
}
struct Lexer<'a> {
input: &'a str,
cursor: usize,
start_of_line: bool,
}
impl<'a> Lexer<'a> {
fn new(input: &'a str) -> Self {
Self {
input,
cursor: 0,
start_of_line: true,
}
}
fn current_char(&self) -> Option<char> {
self.input[self.cursor..].chars().next()
}
fn advance(&mut self, n: usize) -> &'a str {
let start = self.cursor;
self.cursor = (self.cursor + n).min(self.input.len());
&self.input[start..self.cursor]
}
fn consume_while<F>(&mut self, mut predicate: F) -> &'a str
where
F: FnMut(char) -> bool,
{
let start = self.cursor;
while let Some(c) = self.current_char() {
if !predicate(c) {
break;
}
self.cursor += c.len_utf8();
}
&self.input[start..self.cursor]
}
fn lex_number(&mut self) -> (SyntaxKind, &'a str) {
let text = self.consume_while(|c| c.is_ascii_digit());
(SyntaxKind::NUMBER, text)
}
fn lex_whitespace(&mut self) -> (SyntaxKind, &'a str) {
let text = self.consume_while(|c| c == ' ' || c == '\t');
(SyntaxKind::WHITESPACE, text)
}
fn could_be_ed_command(&self) -> bool {
// Ed commands appear after line numbers (e.g., "5a", "3,7d", "2c")
// Look back to see if we have digits before current position
if self.cursor == 0 {
return false;
}
// Check if previous character was a digit
let prev_char = self.input[..self.cursor].chars().last();
matches!(prev_char, Some('0'..='9'))
}
}
impl<'a> Iterator for Lexer<'a> {
type Item = (SyntaxKind, &'a str);
fn next(&mut self) -> Option<Self::Item> {
if self.cursor >= self.input.len() {
return None;
}
let c = self.current_char()?;
match c {
'\n' => {
self.start_of_line = true;
Some((SyntaxKind::NEWLINE, self.advance(1)))
}
'\r' => {
self.start_of_line = true;
// Check if this is \r\n
if self.cursor + 1 < self.input.len()
&& self.input.as_bytes()[self.cursor + 1] == b'\n'
{
// Consume both \r and \n as a single NEWLINE token
Some((SyntaxKind::NEWLINE, self.advance(2)))
} else {
// Just \r
Some((SyntaxKind::NEWLINE, self.advance(1)))
}
}
' ' => {
if self.start_of_line {
self.start_of_line = false;
Some((SyntaxKind::SPACE, self.advance(1)))
} else {
Some(self.lex_whitespace())
}
}
'\t' => Some(self.lex_whitespace()),
'-' => {
if self.start_of_line {
self.start_of_line = false;
}
Some((SyntaxKind::MINUS, self.advance(1)))
}
'+' => {
if self.start_of_line {
self.start_of_line = false;
}
Some((SyntaxKind::PLUS, self.advance(1)))
}
'@' => {
if self.start_of_line {
self.start_of_line = false;
}
Some((SyntaxKind::AT, self.advance(1)))
}
',' => Some((SyntaxKind::COMMA, self.advance(1))),
':' => Some((SyntaxKind::COLON, self.advance(1))),
'.' => Some((SyntaxKind::DOT, self.advance(1))),
'/' => Some((SyntaxKind::SLASH, self.advance(1))),
'*' => {
if self.start_of_line {
self.start_of_line = false;
}
Some((SyntaxKind::STAR, self.advance(1)))
}
'!' => {
if self.start_of_line {
self.start_of_line = false;
}
Some((SyntaxKind::EXCLAMATION, self.advance(1)))
}
'<' => {
if self.start_of_line {
self.start_of_line = false;
}
Some((SyntaxKind::LESS_THAN, self.advance(1)))
}
'>' => {
if self.start_of_line {
self.start_of_line = false;
}
Some((SyntaxKind::GREATER_THAN, self.advance(1)))
}
'\\' => Some((SyntaxKind::BACKSLASH, self.advance(1))),
'a' if self.could_be_ed_command() => Some((SyntaxKind::LETTER_A, self.advance(1))),
'c' if self.could_be_ed_command() => Some((SyntaxKind::LETTER_C, self.advance(1))),
'd' if self.could_be_ed_command() => Some((SyntaxKind::LETTER_D, self.advance(1))),
'0'..='9' => Some(self.lex_number()),
_ => {
if self.start_of_line {
self.start_of_line = false;
}
// For now, consume everything else as TEXT until special characters
let start = self.cursor;
while let Some(ch) = self.current_char() {
match ch {
'\n' | '\r' | ' ' | '\t' | '-' | '+' | '@' | ',' | ':' | '.' | '/'
| '*' | '!' | '<' | '>' | '\\' => break,
'a' | 'c' | 'd' if self.could_be_ed_command() => break,
_ => self.cursor += ch.len_utf8(),
}
}
if self.cursor > start {
Some((SyntaxKind::TEXT, &self.input[start..self.cursor]))
} else {
// Single character that doesn't match anything
Some((SyntaxKind::TEXT, self.advance(c.len_utf8())))
}
}
}
}
}
#[cfg(test)]
#[path = "lex_tests.rs"]
mod tests;