summaryrefslogtreecommitdiff
path: root/parser/annotation-parser.scm
blob: 4ae33cf6e1cd920ef22e8990d1ea4706212530c3 (about) (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
(define *annotation-escape* '())

(define (parse-annotations)
 (let ((save-layout (dynamic *layout-stack*)))
  (setf (dynamic *layout-stack*) '())
  (advance-token)
  (let/cc annotation-escape
   (setf *annotation-escape* (lambda () 
			       (setf (dynamic *layout-stack*) save-layout)
			       (advance-to-annotation-end)
			       (funcall annotation-escape '())))
   (let ((res (start-layout (function parse-annotation-list-1))))
    (setf (dynamic *layout-stack*) save-layout)
    (token-case
     (end-annotation res)
     (else (signal-annotation-error)))))))

(define (parse-annotation-list-1 in-layout?)
  (let ((kind (get-annotation-kind)))
    (cond ((eq? kind 'decl)
	   (let ((d (parse-annotation-decl)))
	     (token-case
	      (\; (cons d (parse-annotation-list-1 in-layout?)))
	      (else (close-layout in-layout?)
		    (list d)))))
	  ((eq? kind 'value)
	   (let ((d (parse-annotation-value)))
	     (token-case
	      (\; (cons d (parse-annotation-list-1 in-layout?)))
	      (else (close-layout in-layout?)
		    (list d)))))
	  (else
	   (close-layout in-layout?)
	   '()))))

(define (get-annotation-kind)
  (token-case
   ((no-advance end-annotation) 'end)
   ((no-advance \() 'decl)
   ((var con)
    (let ((next (peek-1-type)))
      (cond ((eq? next '|,|)
	     'decl)
	    ((eq? next '|::|)
	     'decl)
	    (else
	     'value))))
   (else 'error)))

(define (parse-annotation-decl)
  (let* ((names (parse-aname-list))
	 (decls (parse-aval-list)))
    (make annotation-decl (names names) (annotations decls))))

(define (parse-aname-list)
 (let ((name 'foo))
  (token-case
   (var
    (setf name (var->symbol)))
   (con
    (setf name (con->symbol)))
   (else (signal-annotation-error)))
  (token-case (\, (cons name (parse-aname-list)))
	      (|::| (list name))
	      (else (signal-annotation-error)))))


(define (parse-aval-list)
  (let ((ann (parse-annotation-value)))
    (token-case (\, (cons ann (parse-aval-list)))
		(else (list ann)))))

(define (parse-annotation-value)
  (token-case
   (name (let* ((name (token->symbol))
		(args (parse-annotation-args name)))
	   (make annotation-value (name name) (args args))))))

(define (parse-annotation-args name)
  (token-case
   (\( (parse-annotation-args-1 name 0))
   (else '())))

;;; This routine can invoke special parsers for the arguments

(define (parse-annotation-args-1 name i)
  (let* ((argtype (get-annotation-arg-description name i))
	 (arg (parse-annotation-arg argtype)))
    (token-case
     (\) (list arg))
     (\, (cons arg (parse-annotation-args-1 name (1+ i))))
     (else (signal-annotation-error)))))

(define (parse-annotation-arg type)
  (cond ((eq? type 'string)
	 (token-case
	  ((string no-advance)
	   (let ((res (car *token-args*)))
	     (advance-token)
	     res))
	  (else (signal-annotation-error))))
	;; The following is for a datatype import/export.  It is
	;; Type(Con1(strs),Con2(strs),...)
	((eq? type 'integer)
	 (token-case
	  ((integer no-advance) (token->integer))
	  (else (signal-annotation-error))))
	((eq? type 'constr-list)
	 (parse-annotation-constr-list))
	(else
	 (signal-annotation-error))))
	   
(define (signal-annotation-error)
  (parser-error/recoverable 'annotation-error "Error in annotation syntax")
  (funcall *annotation-escape*))

(define (parse-annotation-constr-list)
  (token-case
   (tycon (let ((type-name (token->symbol)))
	    (token-case (\( (let* ((args (parse-acl1))
				   (res (tuple type-name args)))
			      (token-case  ; leave the ) to end the args
			       ((no-advance \)) (list res))
			       (\, (cons res (parse-annotation-constr-list)))
			       (else (signal-annotation-error)))))
			(else (signal-annotation-error)))))
   (else (signal-annotation-error))))

(define (parse-acl1)
  (token-case
   (con (let ((con-name (con->symbol)))
	  (token-case (\( (let ((str-args (parse-string-list)))
			    (token-case
			     (\, (cons (tuple con-name str-args)
				       (parse-acl1)))
			     (\) (list (tuple con-name str-args)))
			     (else (signal-annotation-error)))))
		      (else (signal-annotation-error)))))
   (else (signal-annotation-error))))

(define (parse-string-list)
  (token-case
   ((string no-advance)
    (let ((res (read-lisp-object (car *token-args*))))
      (advance-token)
      (token-case
       (\) (list res))
       (\, (cons res (parse-string-list)))
       (else (signal-annotation-error)))))
   (else (signal-annotation-error))))

(define (advance-to-annotation-end)
  (token-case
   (eof '())
   (end-annotation
     (advance-token))
   (else
    (advance-token)
    (advance-to-annotation-end))))
  
(define *known-annotations* '(
  (|LispName| string)
  (|Prelude|)
  (|Strictness| string)
  (|Strict|)
  (|NoConversion|)
  (|Inline|)
  (|STRICT|)
  (|ImportLispType| constr-list)
  (|ExportLispType| constr-list)
  (|Complexity| integer)
  ))

(define (get-annotation-arg-description annotation i)
  (let ((s (assq annotation *known-annotations*)))
    (cond ((eq? s '#f)
	   (parser-error/recoverable 'unknown-annotation
             "Annotation ~A is not defined in this system - ignored."
	     annotation)
	   'unknown)
	  ((>= i (length s))
	   'error)
	  (else (list-ref s (1+ i))))))