|
28 | 28 | [source-code-analysis-code (-> source-code-analysis? source?)] |
29 | 29 | [source-code-analysis-visited-forms (-> source-code-analysis? (listof syntax?))] |
30 | 30 | [source-code-analysis-expansion-time-output (-> source-code-analysis? immutable-string?)] |
| 31 | + [source-code-analysis-namespace (-> source-code-analysis? namespace?)] |
31 | 32 | [syntax-source-location (-> syntax? source-location?)] |
32 | 33 | [with-input-from-source (-> source? (-> any) any)])) |
33 | 34 |
|
|
79 | 80 | #:guard (λ (original contents _) (values original (string->immutable-string contents)))) |
80 | 81 |
|
81 | 82 |
|
82 | | -(define-record-type source-code-analysis (code visited-forms expansion-time-output)) |
| 83 | +(define-record-type source-code-analysis (code visited-forms expansion-time-output namespace)) |
83 | 84 | (define-record-type source-location (source line column position span)) |
84 | 85 |
|
85 | 86 |
|
|
127 | 128 |
|
128 | 129 |
|
129 | 130 | (define (source-analyze code #:lines [lines (range-set (unbounded-range #:comparator natural<=>))]) |
130 | | - (parameterize ([current-directory (or (source-directory code) (current-directory))]) |
| 131 | + (define ns (make-base-namespace)) |
| 132 | + (parameterize ([current-directory (or (source-directory code) (current-directory))] |
| 133 | + [current-namespace ns]) |
131 | 134 | (define code-linemap (string-linemap (source->string code))) |
132 | 135 | (define program-stx (source-read-syntax code)) |
133 | 136 | (define program-source-name (syntax-source program-stx)) |
|
184 | 187 | [current-output-port output-port]) |
185 | 188 | (expand program-stx))) |
186 | 189 |
|
| 190 | + ;; We evaluate the module in order to ensure it's declared in the namespace, then we attach it at |
| 191 | + ;; expansion time to ensure the module is visited (but not instantiated). This allows refactoring |
| 192 | + ;; rules to access expansion-time values reflectively via the analysis namespace. |
| 193 | + (eval expanded) |
| 194 | + (namespace-require/expansion-time (extract-module-require-spec expanded)) |
| 195 | + |
187 | 196 | (define output (get-output-string output-port)) |
188 | 197 | (define binding-table (fully-expanded-syntax-binding-table expanded)) |
189 | 198 | (define original-binding-table-by-position |
|
226 | 235 | (sorting syntax-source-location<=> #:key syntax-source-location) |
227 | 236 | #:into into-list)) |
228 | 237 |
|
229 | | - (source-code-analysis #:code code #:visited-forms visited #:expansion-time-output output))) |
| 238 | + (source-code-analysis #:code code |
| 239 | + #:visited-forms visited |
| 240 | + #:expansion-time-output output |
| 241 | + #:namespace ns))) |
230 | 242 |
|
231 | 243 |
|
232 | 244 | (define (syntax-source-location stx) |
|
241 | 253 | (define syntax-source-location<=> |
242 | 254 | (comparator-chain (comparator-map real<=> source-location-position) |
243 | 255 | (comparator-map (comparator-reverse real<=>) source-location-span))) |
| 256 | + |
| 257 | + |
| 258 | +(define (extract-module-require-spec mod-stx) |
| 259 | + (syntax-parse mod-stx |
| 260 | + [(_ name _ . _) `',(syntax-e #'name)])) |
0 commit comments