cpd-tokenizer 0.1.5

Source code tokenizers for cpd
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// cpd-tokenizer: generic whitespace-and-punctuation tokenizer for non-JS/TS formats.
// Handles comment styles, ignore regions, and per-line token scanning without regex.

use cpd_core::models::{Location, Token, TokenKind};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CommentStyle {
    /// Single-line `//`, block `/* */`
    CStyle,
    /// Single-line `#`
    Hash,
    /// Single-line `--`
    DoubleDash,
    /// Single-line `--`, block `--[[ ]]`
    Lua,
    /// Single-line `;`
    Semicolon,
    /// Single-line `'`
    VisualBasic,
    /// No comments (fallback for unrecognised formats)
    #[allow(dead_code)]
    None,
}

fn comment_style(format: &str) -> CommentStyle {
    match format {
        "c" | "c-header" | "cpp" | "cpp-header" | "csharp" | "java" | "go" | "rust" | "swift"
        | "kotlin" | "scala" | "dart" | "php" | "typescript" | "jsx" | "tsx" | "javascript"
        | "groovy" | "d" | "glsl" | "hlsl" | "wgsl" | "openqasm" | "solidity" | "bicep" | "hcl"
        | "json5" | "less" | "scss" | "css" | "objectivec" | "protobuf" | "apex" | "verilog"
        | "zig" | "odin" | "fsharp" | "actionscript" | "cfscript" => CommentStyle::CStyle,

        "python" | "ruby" | "perl" | "bash" | "sh" | "zsh" | "fish" | "r" | "julia" | "yaml"
        | "toml" | "dockerfile" | "makefile" | "cmake" | "coffeescript" | "crystal" | "nim"
        | "gdscript" | "elixir" | "awk" | "tcl" | "powershell" | "puppet" | "ignore" => {
            CommentStyle::Hash
        }

        "sql" | "haskell" | "elm" | "ada" | "plsql" => CommentStyle::DoubleDash,

        "lua" => CommentStyle::Lua,

        "ini" | "properties" | "asm6502" | "nasm" => CommentStyle::Semicolon,

        "vb" | "vbs" | "basic" | "vbnet" | "visual-basic" => CommentStyle::VisualBasic,

        _ => CommentStyle::CStyle,
    }
}

fn is_ignore_start(text: &str) -> bool {
    text.contains("jscpd:ignore-start")
}

fn is_ignore_end(text: &str) -> bool {
    text.contains("jscpd:ignore-end")
}

fn make_token(kind: TokenKind, value: &str, line: u32, col: u32, offset: u32) -> Token {
    let len = value.len() as u32;
    Token {
        kind,
        value: value.to_string(),
        start: Location {
            line,
            column: col,
            offset,
        },
        end: Location {
            line,
            column: col + len,
            offset: offset + len,
        },
    }
}

fn classify_word(word: &str) -> TokenKind {
    if word.chars().all(|c| c.is_ascii_digit()) {
        return TokenKind::Literal;
    }
    if word.chars().all(|c| c.is_ascii_punctuation()) {
        return TokenKind::Punctuation;
    }
    TokenKind::Identifier
}

fn tokenize_line_content(
    line: &str,
    line_num: u32,
    line_offset: u32,
    style: CommentStyle,
    in_ignore: bool,
    in_block_comment: &mut bool,
) -> Vec<Token> {
    let mut tokens = Vec::new();

    // Collect (byte_offset, char) pairs once — zero heap allocation vs chars().collect().
    // `char_indices()` returns (byte_index, char) which gives us correct UTF-8 byte offsets
    // for column accounting while avoiding a Vec<char> heap allocation per line.
    let chars: Vec<(usize, char)> = line.char_indices().collect();
    let n = chars.len();
    let mut i = 0usize;

    // col is in bytes (UTF-8 units), consistent with char.len_utf8() increments below.
    let mut col = 0u32;

    macro_rules! offset {
        () => {
            line_offset + col
        };
    }

    while i < n {
        let (_, ch) = chars[i];

        // Handle block comment end
        if *in_block_comment {
            if matches!(style, CommentStyle::CStyle)
                && i + 1 < n
                && ch == '*'
                && chars[i + 1].1 == '/'
            {
                let start_col = col;
                let start_off = offset!();
                col += 2;
                i += 2;
                let kind = if in_ignore {
                    TokenKind::Ignore
                } else {
                    TokenKind::Comment
                };
                tokens.push(make_token(kind, "*/", line_num, start_col, start_off));
                *in_block_comment = false;
                continue;
            }
            // Still inside block comment — consume char
            let start_col = col;
            let start_off = offset!();
            let mut s = String::new();
            s.push(ch);
            col += ch.len_utf8() as u32;
            i += 1;
            let kind = if in_ignore {
                TokenKind::Ignore
            } else {
                TokenKind::Comment
            };
            tokens.push(make_token(kind, &s, line_num, start_col, start_off));
            continue;
        }

        // Lua long block comment --[[
        if matches!(style, CommentStyle::Lua)
            && i + 3 < n
            && ch == '-'
            && chars[i + 1].1 == '-'
            && chars[i + 2].1 == '['
            && chars[i + 3].1 == '['
        {
            let rest = &line[chars[i].0..];
            let kind = if in_ignore {
                TokenKind::Ignore
            } else {
                TokenKind::Comment
            };
            tokens.push(make_token(kind, rest, line_num, col, offset!()));
            break;
        }

        // C-style block comment open /*
        if matches!(style, CommentStyle::CStyle) && i + 1 < n && ch == '/' && chars[i + 1].1 == '*'
        {
            *in_block_comment = true;
            let start_col = col;
            let start_off = offset!();
            col += 2;
            i += 2;
            let kind = if in_ignore {
                TokenKind::Ignore
            } else {
                TokenKind::Comment
            };
            tokens.push(make_token(kind, "/*", line_num, start_col, start_off));
            continue;
        }

        // Line comment — check current position directly without allocating
        let is_comment = match style {
            CommentStyle::CStyle => i + 1 < n && ch == '/' && chars[i + 1].1 == '/',
            CommentStyle::Hash => ch == '#',
            CommentStyle::DoubleDash | CommentStyle::Lua => {
                i + 1 < n && ch == '-' && chars[i + 1].1 == '-'
            }
            CommentStyle::Semicolon => ch == ';',
            CommentStyle::VisualBasic => ch == '\'',
            CommentStyle::None => false,
        };

        if is_comment {
            let rest = &line[chars[i].0..];
            let kind = if in_ignore {
                TokenKind::Ignore
            } else {
                TokenKind::Comment
            };
            tokens.push(make_token(kind, rest, line_num, col, offset!()));
            break;
        }

        // String literals (double-quote or single-quote)
        if ch == '"' || ch == '\'' {
            let quote = ch;
            let start_col = col;
            let start_off = offset!();
            let mut j = chars[i].0; // byte start of string in `line`
            let str_start = j;
            col += 1;
            i += 1;
            j += 1;
            while i < n && chars[i].1 != quote {
                if chars[i].1 == '\\' && i + 1 < n {
                    col += chars[i].1.len_utf8() as u32 + chars[i + 1].1.len_utf8() as u32;
                    i += 2;
                } else {
                    col += chars[i].1.len_utf8() as u32;
                    i += 1;
                }
            }
            if i < n {
                col += 1;
                i += 1;
            }
            let str_end = if i < n {
                chars[i - 1].0 + chars[i - 1].1.len_utf8()
            } else {
                line.len()
            };
            let _ = (j, str_start); // byte indices computed above but using slice below
            let s = &line[str_start..str_end];
            let kind = if in_ignore {
                TokenKind::Ignore
            } else {
                TokenKind::Literal
            };
            tokens.push(make_token(kind, s, line_num, start_col, start_off));
            continue;
        }

        // Whitespace
        if ch.is_whitespace() {
            let start_col = col;
            let start_off = offset!();
            let byte_start = chars[i].0;
            while i < n && chars[i].1.is_whitespace() {
                col += chars[i].1.len_utf8() as u32;
                i += 1;
            }
            let byte_end = if i < n { chars[i].0 } else { line.len() };
            let kind = if in_ignore {
                TokenKind::Ignore
            } else {
                TokenKind::Whitespace
            };
            tokens.push(make_token(
                kind,
                &line[byte_start..byte_end],
                line_num,
                start_col,
                start_off,
            ));
            continue;
        }

        // Numbers
        if ch.is_ascii_digit() {
            let start_col = col;
            let start_off = offset!();
            let byte_start = chars[i].0;
            while i < n && (chars[i].1.is_ascii_digit() || chars[i].1 == '.') {
                col += 1;
                i += 1;
            }
            let byte_end = if i < n { chars[i].0 } else { line.len() };
            let kind = if in_ignore {
                TokenKind::Ignore
            } else {
                TokenKind::Literal
            };
            tokens.push(make_token(
                kind,
                &line[byte_start..byte_end],
                line_num,
                start_col,
                start_off,
            ));
            continue;
        }

        // Identifiers / keywords
        if ch.is_alphabetic() || ch == '_' {
            let start_col = col;
            let start_off = offset!();
            let byte_start = chars[i].0;
            while i < n && (chars[i].1.is_alphanumeric() || chars[i].1 == '_') {
                col += chars[i].1.len_utf8() as u32;
                i += 1;
            }
            let byte_end = if i < n { chars[i].0 } else { line.len() };
            let s = &line[byte_start..byte_end];
            let kind = if in_ignore {
                TokenKind::Ignore
            } else {
                classify_word(s)
            };
            tokens.push(make_token(kind, s, line_num, start_col, start_off));
            continue;
        }

        // Operators / punctuation (single char)
        let start_col = col;
        let start_off = offset!();
        let byte_start = chars[i].0;
        col += ch.len_utf8() as u32;
        i += 1;
        let byte_end = if i < n { chars[i].0 } else { line.len() };
        let kind = if in_ignore {
            TokenKind::Ignore
        } else {
            TokenKind::Punctuation
        };
        tokens.push(make_token(
            kind,
            &line[byte_start..byte_end],
            line_num,
            start_col,
            start_off,
        ));
    }

    tokens
}

/// Tokenize source in the given format. Never panics on empty input.
pub fn tokenize_generic(source: &str, format: &str) -> Vec<Token> {
    if source.is_empty() {
        return Vec::new();
    }

    let style = comment_style(format);
    let mut tokens = Vec::new();
    let mut in_ignore = false;
    let mut in_block_comment = false;
    let mut offset = 0u32;

    for (line_idx, line) in source.lines().enumerate() {
        let line_num = line_idx as u32 + 1;
        let trimmed = line.trim();

        if is_ignore_start(trimmed) {
            in_ignore = true;
        }
        if is_ignore_end(trimmed) {
            in_ignore = false;
            // Advance offset past this line and continue
            offset += line.len() as u32 + 1;
            continue;
        }

        let line_tokens = tokenize_line_content(
            line,
            line_num,
            offset,
            style,
            in_ignore,
            &mut in_block_comment,
        );
        tokens.extend(line_tokens);
        offset += line.len() as u32 + 1;
    }

    tokens
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn python_produces_tokens() {
        let tokens = tokenize_generic("def hello():\n    return 42\n", "python");
        assert!(!tokens.is_empty());
    }

    #[test]
    fn python_hash_comment_marked_as_comment() {
        let tokens = tokenize_generic("# this is a comment\nx = 1\n", "python");
        let has_comment = tokens.iter().any(|t| t.kind == TokenKind::Comment);
        assert!(has_comment, "Python # comments must be Comment kind");
    }

    #[test]
    fn go_c_style_comment_recognized() {
        let tokens = tokenize_generic("// hello\nfunc main() {}\n", "go");
        let has_comment = tokens.iter().any(|t| t.kind == TokenKind::Comment);
        assert!(has_comment);
    }

    #[test]
    fn empty_input_returns_empty() {
        let tokens = tokenize_generic("", "python");
        assert!(
            tokens.is_empty(),
            "empty input must return empty vec, not panic"
        );
    }

    #[test]
    fn unknown_format_does_not_panic() {
        let result =
            std::panic::catch_unwind(|| tokenize_generic("hello world", "unknown_format_xyz"));
        assert!(result.is_ok());
    }

    #[test]
    fn ignore_region_tokens_marked_as_ignore() {
        let source = "x = 1\n# jscpd:ignore-start\ny = 2\n# jscpd:ignore-end\nz = 3\n";
        let tokens = tokenize_generic(source, "python");
        let has_ignore = tokens.iter().any(|t| t.kind == TokenKind::Ignore);
        assert!(has_ignore, "tokens in ignore region must be Ignore kind");
    }

    #[test]
    fn sql_double_dash_comment_recognized() {
        let tokens = tokenize_generic("-- a comment\nSELECT * FROM foo;\n", "sql");
        let has_comment = tokens.iter().any(|t| t.kind == TokenKind::Comment);
        assert!(has_comment);
    }

    #[test]
    fn c_block_comment_recognized() {
        let tokens = tokenize_generic("/* block */\nint x = 1;\n", "c");
        let has_comment = tokens.iter().any(|t| t.kind == TokenKind::Comment);
        assert!(has_comment);
    }

    #[test]
    fn location_line_numbers_are_1_based() {
        let tokens = tokenize_generic("x = 1\ny = 2\n", "python");
        let first = tokens.first().expect("at least one token");
        assert_eq!(first.start.line, 1);
    }
}