-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathread.rkt
More file actions
98 lines (88 loc) · 3.19 KB
/
read.rkt
File metadata and controls
98 lines (88 loc) · 3.19 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#lang racket/base
#|------------------------------------------------------------------------------+
|Pass: src/read |
+-------------------------------------------------------------------------------+
|Author: Andre Kuhlenshmidt (akuhlens@indiana.edu) |
+-------------------------------------------------------------------------------+
|Discription: This pass simply reads in a file given a path. The reader is
|the function read-syntax so that the source locations may be lifted from the
|syntax-objects as we convert them to core forms.
+------------------------------------------------------------------------------|#
(require
"../language/forms.rkt"
"../logging.rkt"
racket/exn
racket/path)
(provide read)
#|
Function: get-name-of-file
This helper function called by read extracts a file name from a path.
|#
(define (get-name-of-file p)
(let ([maybe-name (file-name-from-path p)])
(if maybe-name
(path->string maybe-name)
(error 'grift "expected path to file: ~a" p))))
#|
Function: get-reader
This helper function called by read-syntax-from-file creates a syntax-reader
that annotates the syntax with source location and file name.
|#
;; string -> input-port -> syntax
(define ((get-reader name) p)
(read-syntax name p))
#|
Function: read-syntax-from-file
This helper function called by read collects all syntax in a file and returns
it as a list.
|#
;(: read-syntax-from-file (Path String . -> . (Listof (Syntaxof Any))))
(define (read-syntax-from-file path name)
(unless (file-exists? path)
(error 'grift
"couldn't read grift source code\n\tno such file: ~a"
path))
(when (directory-exists? path)
(error 'grift
(string-append
"couldn't read grift source code:\n"
"\t found director instead of text file: ~a")
path))
(define (handle-unkown-read-exception e)
(error 'grift
"couldn't read grift source code:\n\tunkown exception follows\n~a"
(exn->string e)))
(with-handlers ([exn? handle-unkown-read-exception])
(call-with-input-file path #:mode 'text
(lambda (p) (read-syntax-from-port p name)))))
(define (read-syntax-from-port port name)
(let ((read (get-reader name)))
(let loop ([stx (read port)])
(if (eof-object? stx)
'()
(cons stx (loop (read port)))))))
#|
Pass: read
Collects the syntax from a file and returns it a Syntax-Prog ast.
|#
;(: read (Path . -> . Syntax-Lang))
(define (read path)
(parameterize
;; The following parameters change what the reader is willing
;; to accept as input.
([port-count-lines-enabled #t]
[read-case-sensitive #t]
[read-square-bracket-as-paren #t]
[read-curly-brace-as-paren #t]
[read-accept-box #f]
[read-accept-compiled #f]
[read-accept-bar-quote #f]
[read-accept-graph #f]
[read-decimal-as-inexact #f]
[read-accept-dot #f]
[read-accept-infix-dot #f]
[read-accept-quasiquote #f]
[read-accept-reader #f]
[read-accept-lang #f])
(let ([name (get-name-of-file path)])
(debug path (Prog name (read-syntax-from-file path name))))))