hyperlit-extractor 0.1.0

A software documentation tool documentation embedded in source files - extractor crate
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/* 📖 DR-0002 Use `syntect` to extract doc comments from code #decision #extractor

Status: Approved\
Date: 2025-06-19

### Decision

To extract doc comments from source code, we will use the [syntect](https://crates.io/crates/syntect) crate.

### Context

To extract doc comments from code, we need to find all the comments in the code, for various languages.

The requirements for this extractor were:

1. Wide support for various programming language formats
2. Robustness against invalid code/syntax
3. Good performance

### Consequences

syntect is used to extract doc comments from the code.

To support as many languages as possible, the [two-face](https://crates.io/crates/two-face) crate is used.

### Considered Alternatives

#### Custom lexer

A custom lexer could be implemented to find comments.
Due to the number of languages and the complexity of handling different syntaxes, this might not be a good idea.
Especially handling "comment-like" syntax in strings would potentially mean having a custom lexer for each language.

#### tree-sitter

tree-sitter parsers could be used to extract the comments from source files.

The drawback is that these parsers need to be curated, are platform-specific and are relatively heavyweight.

#### inkjet

[inkjet](https://crates.io/crates/inkjet) bundles ~70 tree-sitter parsers for various languages.

The downside of this approach is that all these parsers need to be compiled (making the compilation much slower) and bundled in the binary (making the binary much larger)
*/

use hyperlit_base::result::HyperlitResult;
use hyperlit_base::shared_string::SharedString;
use hyperlit_base::{bail, err};
use hyperlit_model::file_source::FileSource;
use hyperlit_model::location::Location;
use hyperlit_model::segment::Segment;
use std::collections::HashSet;
use std::io::{BufRead, BufReader, Read};
use std::str::{FromStr, from_utf8};
use syntect::easy::ScopeRegionIterator;
use syntect::highlighting::ScopeSelectors;
use syntect::parsing::{ParseState, ScopeStack, SyntaxSet};

pub struct Extractor {
    syntax_set: SyntaxSet,
    doc_comment_markers: HashSet<String>,
    root_path: String,
}

impl Extractor {
    pub fn new(doc_comment_markers: &[&str], root_path: String) -> Self {
        Self {
            doc_comment_markers: doc_comment_markers.iter().map(|s| s.to_string()).collect(),
            syntax_set: two_face::syntax::extra_newlines(),
            root_path,
        }
    }
}

enum ExtractorState {
    Code,
    Comment,
    DocComment,
}
const NEWLINE: char = '\n';
const MAXIMUM_LINE_LENGTH: usize = 4096;

#[derive(Debug)]
struct Selectors {
    comment: ScopeSelectors,
}

impl Default for Selectors {
    fn default() -> Selectors {
        Selectors {
            comment: ScopeSelectors::from_str("comment - punctuation").unwrap(),
        }
    }
}

struct FileExtractor<'a> {
    doc_comment_markers: &'a HashSet<String>,
    source: &'a dyn FileSource,
    parse_state: ParseState,
    syntax_set: &'a SyntaxSet,
    root_path: &'a str,
}

impl<'a> FileExtractor<'a> {
    pub fn new(
        source: &'a dyn FileSource,
        parse_state: ParseState,
        syntax_set: &'a SyntaxSet,
        doc_comment_markers: &'a HashSet<String>,
        root_path: &'a str,
    ) -> Self {
        Self {
            source,
            parse_state,
            syntax_set,
            doc_comment_markers,
            root_path,
        }
    }

    pub fn extract(&mut self) -> HyperlitResult<Vec<Segment>> {
        let filepath = self.source.filepath()?;
        let relative_filepath = filepath
            .strip_prefix(self.root_path)
            .map(SharedString::from)
            .unwrap_or_else(|| filepath.clone());
        let relative_filepath = relative_filepath.replace("\\", "/");
        let relative_filepath = relative_filepath
            .strip_prefix("/")
            .unwrap_or(&relative_filepath);
        let mut reader = BufReader::new(Box::new(self.source.open()?));
        let mut segments = Vec::new();
        let mut line_number = 0;
        let mut read_buffer = Vec::with_capacity(MAXIMUM_LINE_LENGTH + 2);
        let mut stack = ScopeStack::new();
        let selectors = Selectors::default();
        let mut state = ExtractorState::Code;
        let mut line_complete;
        'for_each_line: loop {
            // Limit line length
            let bytes_read = {
                let mut limited_reader = reader.take(MAXIMUM_LINE_LENGTH as u64);
                read_buffer.clear();
                let bytes_read = limited_reader.read_until(b'\n', &mut read_buffer)?;
                reader = limited_reader.into_inner();
                if bytes_read == MAXIMUM_LINE_LENGTH {
                    bail!(
                        "{filepath}:{line_number} - Line too too long (> {MAXIMUM_LINE_LENGTH} bytes)"
                    );
                }
                line_complete = from_utf8(&read_buffer[0..bytes_read])?;
                bytes_read
            };
            if bytes_read == 0 {
                break 'for_each_line;
            }
            line_number += 1;
            let ops = self
                .parse_state
                .parse_line(line_complete, self.syntax_set)?;
            for (text, op) in ScopeRegionIterator::new(&ops, line_complete) {
                stack.apply(op)?;
                if text.is_empty() {
                    // skip empty strings
                    continue;
                }
                if selectors.comment.does_match(stack.as_slice()).is_some() {
                    // Comment

                    match &mut state {
                        ExtractorState::Code => {
                            let Some((indicator, text_rest)) =
                                text.trim_start().split_once(char::is_whitespace)
                            else {
                                // No whitespace found
                                state = ExtractorState::Comment;
                                continue;
                            };
                            if !self.doc_comment_markers.contains(indicator) {
                                // Not a doc comment
                                state = ExtractorState::Comment;
                                continue;
                            }
                            if let Some(line_rest) = text_rest.strip_prefix("...") {
                                // Found ellipsis -> continue previous segment
                                let line_rest = line_rest.trim();
                                let last_segment: &mut Segment = segments
                                    .last_mut()
                                    .ok_or_else(|| err!("No previous segment"))?;
                                last_segment.text.push_str(line_rest);
                                last_segment.text.push(NEWLINE);
                                last_segment.text.push(NEWLINE);
                            } else {
                                // No ellipsis -> start a new segment
                                let tag_extraction_result = extract_hash_tags(text_rest);
                                segments.push(Segment::new(
                                    0,
                                    tag_extraction_result.text,
                                    tag_extraction_result.tags,
                                    "",
                                    Location::new(relative_filepath, line_number),
                                ));
                            }
                            state = ExtractorState::DocComment;
                        }
                        ExtractorState::DocComment => {
                            let last_segment = segments.last_mut().unwrap();
                            last_segment.text.push_str(text);
                        }
                        ExtractorState::Comment => {
                            // ignore plain comments
                        }
                    }
                } else {
                    state = ExtractorState::Code;
                }
            }
        }
        Ok(segments)
    }
}

impl Extractor {
    pub fn extract(&self, source: &dyn FileSource) -> HyperlitResult<Vec<Segment>> {
        let filepath = source.filepath()?;
        // get extension
        let extension = filepath
            .rsplit_once('.')
            .ok_or(err!("No extension found in filepath: '{filepath}'"))?
            .1;
        let syntax = self
            .syntax_set
            .find_syntax_by_extension(extension)
            .ok_or_else(|| {
                err!("{filepath} - No syntax definition found for extension '{extension}'")
            })?;
        let parse_state = ParseState::new(syntax);
        let mut file_extractor = FileExtractor::new(
            source,
            parse_state,
            &self.syntax_set,
            &self.doc_comment_markers,
            &self.root_path,
        );
        file_extractor.extract()
    }
}

#[derive(Debug, PartialEq)]
struct TagExtractionResult {
    pub tags: Vec<String>,
    pub text: String,
}

fn extract_hash_tags(input: &str) -> TagExtractionResult {
    let mut tags = vec![];
    let mut text = String::new();
    let words = input.split_whitespace().collect::<Vec<_>>();
    for word in words {
        if let Some(tag) = word.strip_prefix("#") {
            tags.push(tag.to_string());
        } else {
            if !text.is_empty() {
                text.push(' ');
            }
            text.push_str(word);
        }
    }
    TagExtractionResult { tags, text }
}

#[cfg(test)]
mod tests {
    use crate::extractor::{
        Extractor, MAXIMUM_LINE_LENGTH, TagExtractionResult, extract_hash_tags,
    };
    use hyperlit_base::result::HyperlitResult;
    use hyperlit_model::file_source::InMemoryFileSource;
    use hyperlit_model::location::Location;
    use hyperlit_model::segment::Segment;
    use std::collections::HashMap;

    fn create_test_extractor() -> Extractor {
        Extractor::new(&["📖"], "root".to_string())
    }

    #[test]
    fn test_extract_hash_tags() -> HyperlitResult<()> {
        let testcases = HashMap::from([
            (
                "#tag",
                TagExtractionResult {
                    tags: vec!["tag".to_string()],
                    text: "".to_string(),
                },
            ),
            (
                "#tag #tag2",
                TagExtractionResult {
                    tags: vec!["tag".to_string(), "tag2".to_string()],
                    text: "".to_string(),
                },
            ),
            (
                "#TAG_FOO",
                TagExtractionResult {
                    tags: vec!["TAG_FOO".to_string()],
                    text: "".to_string(),
                },
            ),
            (
                "alpha #beta gamma #delta epsilon",
                TagExtractionResult {
                    tags: vec!["beta".to_string(), "delta".to_string()],
                    text: "alpha gamma epsilon".to_string(),
                },
            ),
        ]);
        for (input, expected) in testcases {
            let result = extract_hash_tags(input);
            assert_eq!(result, expected, "input: {}", input);
        }
        Ok(())
    }
    #[test]
    fn extract_segment() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        /* 📖 The #atag title #btag
This is a test */
        1+2
        "#,
        ))?;
        assert_eq!(
            result,
            vec![Segment::new(
                0,
                "The title",
                vec!["atag".to_string(), "btag".to_string()],
                "This is a test ",
                Location::new("testfile.rs", 2)
            )]
        );
        Ok(())
    }

    #[test]
    fn extract_from_line_comment() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        // One
        // 📖 Two
        Three
        // 📖 Four
        1+2
        code // 📖 Five
        "#,
        ))?;
        assert_eq!(
            result,
            vec![
                Segment::new(0, "Two", vec![], "", Location::new("testfile.rs", 3)),
                Segment::new(0, "Four", vec![], "", Location::new("testfile.rs", 5)),
                Segment::new(0, "Five", vec![], "", Location::new("testfile.rs", 7)),
            ]
        );
        Ok(())
    }

    #[test]
    fn extract_from_line_comment_continued() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        // One
        // 📖 Two
        Three
        // 📖 ... Four
        1+2
        code // 📖 ... Five
        "#,
        ))?;
        assert_eq!(
            result,
            vec![Segment::new(
                0,
                "Two",
                vec![],
                "Four\n\nFive\n\n",
                Location::new("testfile.rs", 3)
            ),]
        );
        Ok(())
    }

    #[test]
    fn extract_from_block_comment_continued() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        // One
        /* 📖 Two
*/
        Three
        /* 📖 ... Four
*/
        1+2
        code /* 📖 ... Five
*/
        "#,
        ))?;
        assert_eq!(
            result,
            vec![Segment::new(
                0,
                "Two",
                vec![],
                "Four\n\nFive\n\n",
                Location::new("testfile.rs", 3)
            ),]
        );
        Ok(())
    }

    #[test]
    fn extract_interleaved_block_comment() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        /* 📖 One */
        /* Two */
        /* 📖 Three */
        /* 📖 Four */
        "#,
        ))?;
        assert_eq!(
            result,
            vec![
                Segment::new(0, "One", vec![], "", Location::new("testfile.rs", 2)),
                Segment::new(0, "Three", vec![], "", Location::new("testfile.rs", 4)),
                Segment::new(0, "Four", vec![], "", Location::new("testfile.rs", 5)),
            ]
        );
        Ok(())
    }

    #[test]
    fn extract_interleaved_block_comment_single_line() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        /* 📖 One */         /* Two */        /* 📖 Three */        /* 📖 Four */   /* Five */     "#,
        ))?;
        assert_eq!(
            result,
            vec![
                Segment::new(0, "One", vec![], "", Location::new("testfile.rs", 2)),
                Segment::new(0, "Three", vec![], "", Location::new("testfile.rs", 2)),
                Segment::new(0, "Four", vec![], "", Location::new("testfile.rs", 2)),
            ]
        );
        Ok(())
    }

    #[test]
    fn extract_interleaved_line_comment() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        // 📖 One
        // Two
        // 📖 Three
        // 📖 Four
        "#,
        ))?;
        assert_eq!(
            result,
            vec![
                Segment::new(0, "One", vec![], "", Location::new("testfile.rs", 2)),
                Segment::new(0, "Three", vec![], "", Location::new("testfile.rs", 4)),
                Segment::new(0, "Four", vec![], "", Location::new("testfile.rs", 5)),
            ]
        );
        Ok(())
    }

    #[test]
    fn ignore_normal_comments() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        /* The #atag title #btag
This is a test */
        1+2
        "#,
        ))?;
        assert_eq!(result, vec![]);
        Ok(())
    }

    #[test]
    fn ignore_comments_in_strings() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.rs",
            r#"
        "/* 📖 The #atag title #btag
This is a test */"
        b"/* 📖 The #atag title #btag
This is a test */"

        "#,
        ))?;
        assert_eq!(result, vec![]);
        Ok(())
    }

    #[test]
    fn test_sass() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor.extract(&InMemoryFileSource::new(
            "testfile.sass",
            r#"
        /* 📖 The #atag title #btag
This is a test */

        "#,
        ))?;
        assert_eq!(
            result,
            vec![Segment::new(
                0,
                "The title",
                vec!["atag".to_string(), "btag".to_string()],
                "This is a test ",
                Location::new("testfile.sass", 2)
            )]
        );
        Ok(())
    }

    #[test]
    fn test_unknown_filetype() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let result = extractor
            .extract(&InMemoryFileSource::new(
                "testfile.unknown",
                r#"
        /* 📖 The #atag title #btag
This is a test */

        "#,
            ))
            .expect_err("unknown filetype should fail");
        assert_eq!(
            result.to_string(),
            "testfile.unknown - No syntax definition found for extension 'unknown'"
        );
        Ok(())
    }

    #[test]
    fn bail_line_too_long() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let long_line = "a".repeat(MAXIMUM_LINE_LENGTH + 1);
        let result = extractor
            .extract(&InMemoryFileSource::new("testfile.java", long_line))
            .expect_err("too long line should fail");
        assert_eq!(
            result.to_string(),
            "testfile.java:0 - Line too too long (> 4096 bytes)"
        );
        Ok(())
    }

    #[test]
    fn bail_line_too_long_multibyte_char() -> HyperlitResult<()> {
        let extractor = create_test_extractor();
        let mut long_line = "a".repeat(MAXIMUM_LINE_LENGTH - 1);
        // put a multibyte char at the maximum line length boundary so that the resulting buffer is not valid UTF-8
        long_line += "📖";
        let result = extractor
            .extract(&InMemoryFileSource::new("testfile.java", long_line))
            .expect_err("too long line should fail");
        assert_eq!(
            result.to_string(),
            "testfile.java:0 - Line too too long (> 4096 bytes)"
        );
        Ok(())
    }
}