-
Notifications
You must be signed in to change notification settings - Fork 3
/
location.rkt
55 lines (43 loc) · 2.1 KB
/
location.rkt
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
#lang typed/racket/base
(provide (all-defined-out))
(require racket/path)
(require racket/list)
(require (for-syntax racket/base))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-syntax (#%full-module stx)
(syntax/loc stx
(let ([rmp (variable-reference->resolved-module-path (#%variable-reference))])
(if (not rmp) '<nota-module> (resolved-module-path-name rmp)))))
(define-syntax (#%file stx)
(syntax/loc stx
(let ([full (ann (#%full-module) (U Symbol Path (Pairof Path (Listof Symbol))))])
(cond [(path? full) full]
[(pair? full) (car full)]
[else (current-directory)]))))
(define-syntax (#%module stx)
(syntax/loc stx
(let ([full (ann (#%full-module) (U Symbol Path (Pairof Path (Listof Symbol))))])
(cond [(path? full) (string->symbol (path->string (path-replace-extension (assert (file-name-from-path full) path?) "")))]
[(pair? full) (last (cdr full))]
[else '<anonymous>]))))
(define-syntax (#%modules stx)
(syntax/loc stx
(let ([full (ann (#%full-module) (U Symbol Path (Pairof Path (Listof Symbol))))])
(cond [(path? full) (list (string->symbol (path->string (path-replace-extension full ""))))]
[(pair? full) (cons (string->symbol (path->string (path-replace-extension (car full) ""))) (cdr full))]
[else (list '<anonymous>)]))))
(define-syntax (#%line stx)
(quasisyntax/loc stx (quote #,(syntax-line stx))))
(define-syntax (#%column stx)
(quasisyntax/loc stx (quote #,(syntax-column stx))))
(define-syntax (#%position stx)
(quasisyntax/loc stx (quote #,(syntax-position stx))))
(define-syntax (#%location stx)
(quasisyntax/loc stx (cons #,(syntax-line stx)
#,(syntax-column stx))))
(define-syntax (#%function stx) ; class method has a symbol name looks like "[name] method in [class%]"
(syntax/loc stx
(let use-next-id : Symbol ([stacks (continuation-mark-set->context (current-continuation-marks))])
(if (null? stacks) 'λ
(or (caar stacks)
(use-next-id (cdr stacks)))))))