|
13 | 13 | [source-directory (-> source? (or/c path? #false))] |
14 | 14 | [source-original (-> source? unmodified-source?)] |
15 | 15 | [source-read-syntax (-> source? syntax?)] |
| 16 | + [source-read-language (-> source? (or/c module-path? #false))] |
16 | 17 | [source-analyze (->* (source?) (#:lines range-set?) source-code-analysis?)] |
17 | 18 | [file-source? (-> any/c boolean?)] |
18 | 19 | [file-source (-> path-string? file-source?)] |
|
67 | 68 | syntax/parse) |
68 | 69 |
|
69 | 70 |
|
| 71 | +(module+ test |
| 72 | + (require (submod "..") |
| 73 | + rackunit)) |
| 74 | + |
| 75 | + |
70 | 76 | ;@---------------------------------------------------------------------------------------------------- |
71 | 77 |
|
72 | 78 |
|
|
121 | 127 | (syntax-label-original-paths (with-input-from-source code read-from-input))) |
122 | 128 |
|
123 | 129 |
|
| 130 | +(define (source-read-language code) |
| 131 | + (define (read-lang-from-input) |
| 132 | + (port-count-lines! (current-input-port)) |
| 133 | + (with-module-reading-parameterization |
| 134 | + (λ () |
| 135 | + (call/ec |
| 136 | + (λ (escape) |
| 137 | + (parameterize ([current-reader-guard escape]) |
| 138 | + (read-syntax)) |
| 139 | + #false))))) |
| 140 | + (define detected-lang (with-input-from-source code read-lang-from-input)) |
| 141 | + (match detected-lang |
| 142 | + [(list 'submod path 'reader) path] |
| 143 | + [#false #false])) |
| 144 | + |
| 145 | + |
| 146 | +(module+ test |
| 147 | + (test-case "source-read-language" |
| 148 | + (check-equal? (source-read-language (string-source "#lang racket")) 'racket) |
| 149 | + (check-equal? (source-read-language (string-source "#lang at-exp racket")) 'at-exp) |
| 150 | + (check-equal? (source-read-language (string-source "#lang scribble/manual")) 'scribble/manual) |
| 151 | + (check-equal? (source-read-language (string-source "#lang info")) 'info) |
| 152 | + (check-equal? (source-read-language (string-source "#lang setup/infotab")) 'setup/infotab) |
| 153 | + (check-equal? (source-read-language (string-source "(void)")) #false))) |
| 154 | + |
| 155 | + |
124 | 156 | (define/guard (source-path code) |
125 | 157 | (guard-match (or (file-source path) (modified-source (file-source path) _)) code #:else #false) |
126 | 158 | path) |
|
0 commit comments