|
18 | 18 | [source-expand (-> source? syntax?)] |
19 | 19 | [source-can-expand? (-> source? boolean?)] |
20 | 20 | [source-text-of (-> source? syntax? immutable-string?)] |
| 21 | + [source-comment-locations (-> source? immutable-range-set?)] |
21 | 22 | [file-source? (-> any/c boolean?)] |
22 | 23 | [file-source (-> path-string? file-source?)] |
23 | 24 | [file-source-path (-> file-source? path?)] |
|
38 | 39 | rebellion/base/immutable-string |
39 | 40 | resyntax/private/syntax-neighbors |
40 | 41 | syntax/modread |
41 | | - syntax/parse) |
| 42 | + rebellion/base/comparator |
| 43 | + rebellion/base/range |
| 44 | + rebellion/collection/range-set |
| 45 | + rebellion/collection/vector/builder |
| 46 | + rebellion/streaming/transducer |
| 47 | + syntax-color/lexer-contract |
| 48 | + syntax-color/module-lexer) |
42 | 49 |
|
43 | 50 |
|
44 | 51 | (module+ test |
|
179 | 186 | (define start (sub1 (syntax-position stx))) |
180 | 187 | (define end (+ start (syntax-span stx))) |
181 | 188 | (string->immutable-string (substring (source->string code) start end))) |
| 189 | + |
| 190 | + |
| 191 | +(define (source-comment-locations src) |
| 192 | + (transduce (source-tokens src) |
| 193 | + (filtering lexical-token-comment?) |
| 194 | + (mapping lexical-token-location) |
| 195 | + #:into (into-range-set natural<=>))) |
| 196 | + |
| 197 | + |
| 198 | +(struct lexical-token (text start end type delimiter-kind attributes) #:transparent) |
| 199 | + |
| 200 | + |
| 201 | +(define (source-tokens src) |
| 202 | + (with-input-from-source src |
| 203 | + (λ () |
| 204 | + (define tokens (make-vector-builder)) |
| 205 | + (let loop ([offset 0] [mode #false]) |
| 206 | + (define-values (text raw-attributes delimiter-kind start end _ new-mode) |
| 207 | + (module-lexer* (current-input-port) offset mode)) |
| 208 | + (unless (eof-object? text) |
| 209 | + (define type |
| 210 | + (if (symbol? raw-attributes) |
| 211 | + raw-attributes |
| 212 | + (hash-ref raw-attributes 'type))) |
| 213 | + (define attributes |
| 214 | + (if (symbol? raw-attributes) |
| 215 | + (hasheq) |
| 216 | + (hash-remove raw-attributes 'type))) |
| 217 | + (vector-builder-add tokens (lexical-token text (sub1 start) (sub1 end) type delimiter-kind attributes)) |
| 218 | + (loop (sub1 end) (if (dont-stop? new-mode) (dont-stop-val new-mode) new-mode)))) |
| 219 | + (build-vector tokens)))) |
| 220 | + |
| 221 | + |
| 222 | +(define (lexical-token-comment? token) |
| 223 | + (define type (lexical-token-type token)) |
| 224 | + (or (equal? type 'comment) |
| 225 | + (equal? type 'sexp-comment) |
| 226 | + (hash-ref (lexical-token-attributes token) 'comment? #false))) |
| 227 | + |
| 228 | + |
| 229 | +(define (lexical-token-location token) |
| 230 | + (closed-open-range (lexical-token-start token) (lexical-token-end token) #:comparator natural<=>)) |
0 commit comments