chiark / gitweb /
src/class-finalize-*.lisp: Improve finalization error reporting.
[sod] / src / module-parse.lisp
CommitLineData
bf090e02
MW
1;;; -*-lisp-*-
2;;;
3;;; Top-level parser for module syntax
4;;;
5;;; (c) 2010 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
e0808c47 10;;; This file is part of the Sensible Object Design, an object system for C.
bf090e02
MW
11;;;
12;;; SOD is free software; you can redistribute it and/or modify
13;;; it under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 2 of the License, or
15;;; (at your option) any later version.
16;;;
17;;; SOD is distributed in the hope that it will be useful,
18;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with SOD; if not, write to the Free Software Foundation,
24;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26(in-package #:sod)
27
28;;;--------------------------------------------------------------------------
29;;; Toplevel syntax.
30
bf090e02
MW
31;;; Type names.
32
048d0b2d
MW
33(define-pluggable-parser module typename (scanner pset)
34 ;; `typename' id ( `,' id )* `;'
35 (declare (ignore pset))
bf090e02
MW
36 (with-parser-context (token-scanner-context :scanner scanner)
37 (parse (and "typename"
38 (skip-many (:min 1)
39 (seq ((id :id))
40 (if (gethash id *module-type-map*)
41 (cerror* "Type `~A' already defined" id)
42 (add-to-module *module*
43 (make-instance 'type-item
44 :name id))))
45 #\,)
46 #\;))))
47
48;;; Fragments.
49
048d0b2d 50(define-pluggable-parser module code (scanner pset)
4fc52153 51 ;; `code' id `:' item-name [constraints] `{' c-fragment `}'
048d0b2d
MW
52 ;;
53 ;; constrains ::= `[' constraint-list `]'
4fc52153
MW
54 ;; constraint ::= item-name+
55 ;; item-name ::= id | `(' id+ `)'
048d0b2d 56 (declare (ignore pset))
bf090e02 57 (with-parser-context (token-scanner-context :scanner scanner)
4fc52153
MW
58 (labels ((kw ()
59 (parse (seq ((kw :id))
60 (intern (frob-identifier kw) 'keyword))))
61 (item ()
62 (parse (or (kw)
63 (seq (#\( (names (list (:min 1) (kw))) #\))
64 names)))))
9ec578d9
MW
65 (parse (seq ("code"
66 (reason (kw))
67 #\:
4fc52153 68 (name (item))
9ec578d9
MW
69 (constraints (? (seq (#\[
70 (constraints (list (:min 1)
4fc52153
MW
71 (list (:min 1)
72 (item))
9ec578d9
MW
73 #\,))
74 #\])
75 constraints)))
76 (fragment (parse-delimited-fragment scanner #\{ #\})))
77 (add-to-module *module*
78 (make-instance 'code-fragment-item
79 :fragment fragment
80 :constraints constraints
81 :reason reason
82 :name name)))))))
bf090e02
MW
83
84;;; External files.
85
9ec578d9
MW
86(export 'read-module)
87(defun read-module (pathname &key (truename nil truep) location)
bf090e02
MW
88 "Parse the file at PATHNAME as a module, returning it.
89
90 This is the main entry point for parsing module files. You may well know
91 the file's TRUENAME already (e.g., because `probe-file' dropped it into
92 your lap) so you can avoid repeating the search by providing it.
93
94 The LOCATION is the thing which wanted the module imported -- usually a
95 `file-location' object, though it might be anything other than `t' which
96 can be printed in the event of circular imports."
97
9ec578d9
MW
98 (setf pathname (merge-pathnames pathname
99 (make-pathname :type "SOD" :case :common)))
100 (unless truep (setf truename (truename pathname)))
bf090e02
MW
101 (define-module (pathname :location location :truename truename)
102 (with-open-file (f-stream pathname :direction :input)
103 (let* ((*readtable* (copy-readtable))
65e5cd24 104 (*package* (find-package '#:sod-user))
bf090e02
MW
105 (char-scanner (make-instance 'charbuf-scanner
106 :stream f-stream))
107 (scanner (make-instance 'sod-token-scanner
108 :char-scanner char-scanner)))
109 (with-default-error-location (scanner)
110 (with-parser-context (token-scanner-context :scanner scanner)
300a3f0a
MW
111 (multiple-value-bind (result winp consumedp)
112 (parse (skip-many ()
113 (seq ((pset (parse-property-set scanner))
114 (nil (error ()
115 (plug module scanner pset))))
116 (check-unused-properties pset))))
117 (declare (ignore consumedp))
118 (unless winp (syntax-error scanner result)))))))))
048d0b2d
MW
119
120(define-pluggable-parser module test (scanner pset)
121 ;; `demo' string `;'
122 (declare (ignore pset))
bf090e02
MW
123 (with-parser-context (token-scanner-context :scanner scanner)
124 (parse (seq ("demo" (string :string) #\;)
125 (format t ";; DEMO ~S~%" string)))))
126
048d0b2d
MW
127(define-pluggable-parser module file (scanner pset)
128 ;; `import' string `;'
129 ;; `load' string `;'
130 (declare (ignore pset))
bf090e02
MW
131 (flet ((common (name type what thunk)
132 (find-file scanner
133 (merge-pathnames name
134 (make-pathname :type type
135 :case :common))
136 what
137 thunk)))
138 (with-parser-context (token-scanner-context :scanner scanner)
139 (parse (or (seq ("import" (name :string) #\;)
140 (common name "SOD" "module"
141 (lambda (path true)
142 (handler-case
143 (let ((module (read-module path
144 :truename true)))
145 (when module
146 (module-import module)
147 (pushnew module
148 (module-dependencies
149 *module*))))
150 (file-error (error)
151 (cerror* "Error reading module ~S: ~A"
152 path error))))))
153 (seq ("load" (name :string) #\;)
154 (common name "LISP" "Lisp file"
155 (lambda (path true)
156 (handler-case
157 (load true :verbose nil :print nil)
158 (error (error)
159 (cerror* "Error loading Lisp file ~S: ~A"
160 path error)))))))))))
161
01e3faf9
MW
162;;; Setting properties.
163
164(define-pluggable-parser module set (scanner pset)
165 ;; `set' property-list `;'
166 (with-parser-context (token-scanner-context :scanner scanner)
167 (parse (and "set"
168 (lisp (let ((module-pset (module-pset *module*)))
169 (when pset
170 (pset-map (lambda (prop)
171 (add-property module-pset
172 (p-name prop)
173 (p-value prop)
174 :type (p-type prop)
175 :location (p-location prop))
176 (setf (p-seenp prop) t))
177 pset))
178 (parse (skip-many (:min 0)
179 (error (:ignore-unconsumed t)
180 (parse-property scanner module-pset)
181 (skip-until (:keep-end t) #\, #\;))
182 #\,))))
183 #\;))))
184
bf090e02
MW
185;;; Lisp escape.
186
048d0b2d 187(define-pluggable-parser module lisp (scanner pset)
bf090e02 188 ;; `lisp' s-expression `;'
048d0b2d 189 (declare (ignore pset))
bf090e02
MW
190 (with-parser-context (token-scanner-context :scanner scanner)
191 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
192 (string= (token-value scanner) "lisp"))
193 (let* ((stream (make-scanner-stream scanner))
194 (sexp (read stream t)))
195 (scanner-step scanner)
196 (values sexp t t))
197 (values '((:id "lisp")) nil nil)))
198 #\;)
199 (eval sexp)))))
200
201;;;--------------------------------------------------------------------------
202;;; Class declarations.
203
7f2917d2
MW
204(export 'class-item)
205
a42893dd
MW
206(define-pluggable-parser class-item initfrags (scanner class pset)
207 ;; raw-class-item ::= frag-keyword `{' c-fragment `}'
208 ;; frag-keyword ::= `init' | `teardown'
209 (with-parser-context (token-scanner-context :scanner scanner)
210 (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag)
211 (seq ("teardown") #'make-sod-class-tearfrag)))
212 (frag (parse-delimited-fragment scanner #\{ #\})))
213 (funcall make class frag pset scanner)))))
214
b2983f35
MW
215(define-pluggable-parser class-item initargs (scanner class pset)
216 ;; initarg-item ::= `initarg' declspec+ init-declarator-list
217 ;; init-declarator ::= declarator [`=' initializer]
218 (with-parser-context (token-scanner-context :scanner scanner)
219 (parse (seq ("initarg"
220 (base-type (parse-c-type scanner))
221 (nil (skip-many (:min 1)
222 (seq ((declarator (parse-declarator scanner
223 base-type))
224 (init (? (parse-delimited-fragment
225 scanner #\= (list #\; #\,)
226 :keep-end t))))
227 (make-sod-user-initarg class
228 (cdr declarator)
229 (car declarator)
230 pset init scanner))
231 #\,))
232 #\;)))))
233
048d0b2d 234(defun parse-class-body (scanner pset name supers)
c91b90c3 235 ;; class-body ::= `{' class-item* `}'
048d0b2d
MW
236 ;;
237 ;; class-item ::= property-set raw-class-item
c91b90c3
MW
238 (with-parser-context (token-scanner-context :scanner scanner)
239 (make-class-type name)
70b33a78
MW
240 (let* ((duff nil)
241 (class (make-sod-class name
242 (restart-case
243 (mapcar #'find-sod-class supers)
244 (continue ()
245 (setf duff t)
246 (list (find-sod-class "SodObject"))))
c91b90c3
MW
247 pset scanner))
248 (nick (sod-class-nickname class)))
249
250 (labels ((parse-maybe-dotted-declarator (base-type)
251 ;; Parse a declarator or dotted-declarator, i.e., one whose
252 ;; centre is
253 ;;
254 ;; maybe-dotted-identifier ::= [id `.'] id
255 ;;
256 ;; A plain identifier is returned as a string, as usual; a
257 ;; dotted identifier is returned as a cons cell of the two
258 ;; names.
259 (parse-declarator
260 scanner base-type
43073476 261 :keywordp t
ea578bb4 262 :kernel (parser ()
c91b90c3
MW
263 (seq ((name-a :id)
264 (name-b (? (seq (#\. (id :id)) id))))
265 (if name-b (cons name-a name-b)
266 name-a)))))
267
c91b90c3
MW
268 (parse-message-item (sub-pset type name)
269 ;; message-item ::=
270 ;; declspec+ declarator -!- (method-body | `;')
2cbdee3d
MW
271 ;;
272 ;; Don't allow a method-body here if the message takes a
273 ;; varargs list, because we don't have a name for the
274 ;; `va_list' parameter.
275 (let ((message (make-sod-message class name type
276 sub-pset scanner)))
277 (if (varargs-message-p message)
278 (parse #\;)
279 (parse (or #\; (parse-method-item sub-pset
280 type nick name))))))
c91b90c3
MW
281
282 (parse-method-item (sub-pset type sub-nick name)
283 ;; method-item ::=
284 ;; declspec+ dotted-declarator -!- method-body
285 ;;
286 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
287 (parse (seq ((body (or (seq ("extern" #\;) nil)
288 (parse-delimited-fragment
289 scanner #\{ #\}))))
290 (make-sod-method class sub-nick name type
291 body sub-pset scanner))))
292
293 (parse-initializer ()
a888e3ac 294 ;; initializer ::= `=' c-fragment
c91b90c3 295 ;;
a888e3ac
MW
296 ;; Return a VALUE, ready for passing to a `sod-initializer'
297 ;; constructor.
298 (parse-delimited-fragment scanner #\= (list #\, #\;)
299 :keep-end t))
c91b90c3
MW
300
301 (parse-slot-item (sub-pset base-type type name)
302 ;; slot-item ::=
303 ;; declspec+ declarator -!- [initializer]
304 ;; [`,' init-declarator-list] `;'
305 ;;
306 ;; init-declarator-list ::=
307 ;; declarator [initializer] [`,' init-declarator-list]
308 (parse (and (seq ((init (? (parse-initializer))))
309 (make-sod-slot class name type
310 sub-pset scanner)
311 (when init
312 (make-sod-instance-initializer
a888e3ac 313 class nick name init sub-pset scanner)))
c91b90c3
MW
314 (skip-many ()
315 (seq (#\,
316 (ds (parse-declarator scanner
317 base-type))
318 (init (? (parse-initializer))))
319 (make-sod-slot class (cdr ds) (car ds)
320 sub-pset scanner)
321 (when init
322 (make-sod-instance-initializer
a888e3ac 323 class nick (cdr ds) init
048d0b2d 324 sub-pset scanner))))
c91b90c3
MW
325 #\;)))
326
b2983f35 327 (parse-initializer-item (sub-pset must-init-p constructor)
c91b90c3
MW
328 ;; initializer-item ::=
329 ;; [`class'] -!- slot-initializer-list `;'
330 ;;
b2983f35
MW
331 ;; slot-initializer ::= id `.' id [initializer]
332 (let ((parse-init (if must-init-p
333 #'parse-initializer
334 (parser () (? (parse-initializer))))))
335 (parse (and (skip-many ()
336 (seq ((name-a :id) #\. (name-b :id)
337 (init (funcall parse-init)))
338 (funcall constructor class
339 name-a name-b init
340 sub-pset scanner))
341 #\,)
342 #\;))))
c91b90c3
MW
343
344 (class-item-dispatch (sub-pset base-type type name)
345 ;; Logically part of `parse-raw-class-item', but the
346 ;; indentation was getting crazy. We're currently at
347 ;;
348 ;; raw-class-item ::=
349 ;; declspec+ (declarator | dotted-declarator) -!- ...
350 ;; | other-items
351 ;;
352 ;; If the declarator is dotted then this must be a method
353 ;; definition; otherwise it might be a message or slot.
354 (cond ((not (typep type 'c-function-type))
355 (when (consp name)
356 (cerror*-with-location
357 scanner
a1985b3c 358 "Method declarations must have function type")
c91b90c3
MW
359 (setf name (cdr name)))
360 (parse-slot-item sub-pset base-type type name))
361 ((consp name)
362 (parse-method-item sub-pset type
363 (car name) (cdr name)))
364 (t
365 (parse-message-item sub-pset type name))))
366
367 (parse-raw-class-item (sub-pset)
368 ;; raw-class-item ::=
369 ;; message-item
370 ;; | method-item
371 ;; | slot-item
372 ;; | initializer-item
a42893dd 373 ;; | initfrag-item
c91b90c3
MW
374 ;;
375 ;; Most of the above begin with declspecs and a declarator
376 ;; (which might be dotted). So we parse that here and
377 ;; dispatch based on what we find.
048d0b2d 378 (parse (or (plug class-item scanner class sub-pset)
db2abd9d 379 (peek
c91b90c3
MW
380 (seq ((ds (parse-c-type scanner))
381 (dc (parse-maybe-dotted-declarator ds))
65b1d9d7 382 (nil (commit))
048d0b2d
MW
383 (nil (class-item-dispatch sub-pset
384 ds
385 (car dc)
db2abd9d 386 (cdr dc))))))
c91b90c3
MW
387 (and "class"
388 (parse-initializer-item
b2983f35 389 sub-pset t
c91b90c3
MW
390 #'make-sod-class-initializer))
391 (parse-initializer-item
b2983f35 392 sub-pset nil
c91b90c3
MW
393 #'make-sod-instance-initializer)))))
394
048d0b2d
MW
395 (parse (seq (#\{
396 (nil (skip-many ()
397 (seq ((sub-pset (parse-property-set scanner))
9ec578d9 398 (nil (parse-raw-class-item sub-pset)))
048d0b2d 399 (check-unused-properties sub-pset))))
9ec578d9 400 (nil (error () #\})))
e45a106d
MW
401 (unless (finalize-sod-class class)
402 (setf duff t))
70b33a78
MW
403 (unless duff
404 (add-to-module *module* class))))))))
048d0b2d
MW
405
406(define-pluggable-parser module class (scanner pset)
ffc2a7b8 407 ;; `class' id `:' id-list class-body
c91b90c3 408 ;; `class' id `;'
bf090e02 409 (with-parser-context (token-scanner-context :scanner scanner)
c91b90c3
MW
410 (parse (seq ("class"
411 (name :id)
412 (nil (or (seq (#\;)
413 (make-class-type name))
ffc2a7b8
MW
414 (seq ((supers (seq (#\: (ids (list () :id #\,)))
415 ids))
c91b90c3
MW
416 (nil (parse-class-body
417 scanner
418 pset name supers)))))))))))
419
bf090e02 420;;;----- That's all, folks --------------------------------------------------