Skip to content

Commit 844d483

Browse files
Copilotjackfirth
andcommitted
Store visited paths instead of syntaxes in source-code-analysis
- Change source-code-analysis record to store enriched-syntax and visited-paths - Add backward-compatible accessor for visited-forms that uses syntax-ref - Update source-analyze to compute paths and enrich syntax once - Extract and preserve expander-added properties (like 'class-body') - Combine expander and analyzer properties before enrichment - This reduces duplication by enriching the source once instead of per-visit Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com>
1 parent d25422c commit 844d483

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

private/analysis.rkt

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
source-code-analysis?)]
1212
[source-code-analysis? (-> any/c boolean?)]
1313
[source-code-analysis-code (-> source-code-analysis? source?)]
14+
[source-code-analysis-enriched-syntax (-> source-code-analysis? syntax?)]
15+
[source-code-analysis-visited-paths (-> source-code-analysis? (listof syntax-path?))]
1416
[source-code-analysis-visited-forms (-> source-code-analysis? (listof syntax?))]
1517
[source-code-analysis-expansion-time-output (-> source-code-analysis? immutable-string?)]
1618
[source-code-analysis-namespace (-> source-code-analysis? namespace?)]
@@ -55,7 +57,15 @@
5557

5658

5759
(define-record-type source-code-analysis
58-
(code visited-forms expansion-time-output namespace added-syntax-properties))
60+
(code enriched-syntax visited-paths expansion-time-output namespace added-syntax-properties))
61+
62+
63+
;; Backward-compatible accessor that computes visited forms from paths
64+
(define (source-code-analysis-visited-forms analysis)
65+
(define stx (source-code-analysis-enriched-syntax analysis))
66+
(define paths (source-code-analysis-visited-paths analysis))
67+
(for/list ([path (in-list paths)])
68+
(syntax-ref stx path)))
5969

6070

6171
(define (source-analyze code
@@ -198,35 +208,45 @@
198208
#:parent-srcloc-modifier values
199209
#:parent-props-modifier values))
200210

201-
(define visited
211+
(define visited-paths
202212
(transduce (build-vector original-visits)
203213
(peeking
204214
(λ (visit)
205215
(unless (syntax-original-path visit)
206216
(raise-arguments-error
207-
'source-analyze "pre-enriched visit is missing original path"
217+
'source-analyze "visit is missing original path"
208218
"visited syntax" visit))))
209-
(deduplicating #:key syntax-original-path)
210-
(mapping
211-
(λ (visit)
212-
(define path (syntax-original-path visit))
213-
(define visit-props
214-
(syntax-property-bundle-get-all-properties
215-
expansion-analyzer-props-adjusted-for-visits path))
216-
(syntax-add-all-properties visit visit-props)))
217-
(mapping enrich)
218-
(peeking
219-
(λ (visit)
220-
(unless (syntax-original-path visit)
221-
(raise-arguments-error
222-
'source-analyze "post-enriched visit is missing original path"
223-
"visited syntax" visit))))
224-
(sorting syntax-path<=> #:key syntax-original-path)
219+
(mapping syntax-original-path)
220+
(deduplicating)
221+
(sorting syntax-path<=>)
225222
#:into into-list))
223+
224+
;; Extract expander-added properties from visits
225+
;; Known expander properties that need to be preserved
226+
(define expander-property-keys '(class-body))
227+
(define expander-property-entries
228+
(for*/list ([(path visit) (in-hash most-recent-visits-by-original-path)]
229+
[key (in-list expander-property-keys)]
230+
[val (in-value (syntax-property visit key))]
231+
#:when val)
232+
(syntax-property-entry path key val)))
233+
234+
;; Combine expander and analyzer properties
235+
(define all-property-entries
236+
(append (sequence->list (syntax-property-bundle-entries expansion-analyzer-props-adjusted-for-visits))
237+
expander-property-entries))
238+
(define all-properties (sequence->syntax-property-bundle all-property-entries))
239+
240+
;; Label the original program syntax with paths, then add all properties and enrich
241+
(define program-stx-with-paths (syntax-label-original-paths program-stx))
242+
(define program-stx-with-props
243+
(syntax-add-all-properties program-stx-with-paths all-properties))
244+
(define enriched-program-stx (enrich program-stx-with-props))
226245

227-
(log-resyntax-debug "visited ~a forms" (length visited))
246+
(log-resyntax-debug "visited ~a forms" (length visited-paths))
228247
(source-code-analysis #:code code
229-
#:visited-forms visited
248+
#:enriched-syntax enriched-program-stx
249+
#:visited-paths visited-paths
230250
#:expansion-time-output output
231251
#:namespace ns
232252
#:added-syntax-properties expansion-analyzer-props-adjusted-for-visits)))

0 commit comments

Comments
 (0)