lib/keyword.c (kw_parseempty): Use correct variable scanning `kwval' list.
[sod] / src / module-parse.lisp
... / ...
CommitLineData
1;;; -*-lisp-*-
2;;;
3;;; Top-level parser for module syntax
4;;;
5;;; (c) 2010 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
10;;; This file is part of the Sensible Object Design, an object system for C.
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
31;;; Type names.
32
33(define-pluggable-parser module typename (scanner pset)
34 ;; `typename' list[id] `;'
35 (declare (ignore pset))
36 (with-parser-context (token-scanner-context :scanner scanner)
37 (parse (and "typename"
38 (skip-many ()
39 (error ()
40 (seq ((id :id))
41 (if (or (gethash id *module-type-map*)
42 (find-simple-c-type id))
43 (cerror* "Type `~A' already defined" id)
44 (add-to-module *module*
45 (make-instance 'type-item
46 :name id))))
47 (skip-until () #\, #\;))
48 #\,)
49 (must #\;)))))
50
51;;; Fragments.
52
53(define-pluggable-parser module code (scanner pset)
54 ;; `code' id `:' item-name [constraints] `{' c-fragment `}'
55 ;; `code' id `:' constraints `;'
56 ;;
57 ;; constraints ::= `[' list[constraint] `]'
58 ;; constraint ::= item-name+
59 ;; item-name ::= id | `(' id+ `)'
60 (declare (ignore pset))
61 (with-parser-context (token-scanner-context :scanner scanner)
62 (labels ((kw ()
63 (parse (seq ((kw :id))
64 (intern (frob-identifier kw) 'keyword))))
65 (item ()
66 (parse (or (kw)
67 (seq (#\( (names (list (:min 1) (kw))) #\))
68 names))))
69 (constraints ()
70 (parse (seq (#\[
71 (constraints
72 (list ()
73 (list (:min 1)
74 (error (:ignore-unconsumed t) (item)
75 (skip-until () :id #\( #\, #\])))
76 #\,))
77 #\])
78 constraints)))
79 (fragment ()
80 (parse-delimited-fragment scanner #\{ #\})))
81 (parse (seq ("code"
82 (reason (must (kw)))
83 (nil (must #\:))
84 (item (or (seq ((constraints (constraints))
85 (nil (must #\;)))
86 (make-instance 'code-fragment-item
87 :reason reason
88 :constraints constraints))
89 (seq ((name (must (item)))
90 (constraints (? (constraints)))
91 (fragment (fragment)))
92 (and name
93 (make-instance 'code-fragment-item
94 :reason reason
95 :constraints constraints
96 :name name
97 :fragment fragment))))))
98 (when item (add-to-module *module* item)))))))
99
100;;; External files.
101
102(export 'read-module)
103(defun read-module (pathname &key (truename nil truep) location stream)
104 "Parse the file at PATHNAME as a module, returning it.
105
106 This is the main entry point for parsing module files. You may well know
107 the file's TRUENAME already (e.g., because `probe-file' dropped it into
108 your lap) so you can avoid repeating the search by providing it.
109
110 The LOCATION is the thing which wanted the module imported -- usually a
111 `file-location' object, though it might be anything other than `t' which
112 can be printed in the event of circular imports."
113
114 (setf pathname (merge-pathnames pathname
115 (make-pathname :type "SOD" :case :common)))
116 (unless truep (setf truename (truename pathname)))
117 (define-module (pathname :location location :truename truename)
118 (flet ((parse (f-stream)
119 (let* ((char-scanner
120 (make-instance 'charbuf-scanner
121 :stream f-stream
122 :filename (namestring pathname)))
123 (scanner (make-instance 'sod-token-scanner
124 :char-scanner char-scanner)))
125 (with-default-error-location (scanner)
126 (with-parser-context
127 (token-scanner-context :scanner scanner)
128 (multiple-value-bind (result winp consumedp)
129 (parse (skip-many ()
130 (seq ((pset (parse-property-set scanner))
131 (nil (error ()
132 (plug module scanner pset)
133 (skip-until (:keep-end nil)
134 #\; #\}))))
135 (check-unused-properties pset))))
136 (declare (ignore consumedp))
137 (unless winp (syntax-error scanner result))))))))
138 (if stream (parse stream)
139 (with-open-file (stream pathname :direction :input)
140 (parse stream))))))
141
142(define-pluggable-parser module file (scanner pset)
143 ;; `import' string `;'
144 ;; `load' string `;'
145 (declare (ignore pset))
146 (flet ((common (name type what thunk)
147 (when name
148 (find-file (pathname (scanner-filename scanner))
149 (merge-pathnames name
150 (make-pathname :type type
151 :case :common))
152 what
153 thunk))))
154 (with-parser-context (token-scanner-context :scanner scanner)
155 (parse (or (seq ("import" (name (must :string)) (nil (must #\;)))
156 (common name "SOD" "module"
157 (lambda (path true)
158 (handler-case
159 (let ((module (read-module path
160 :truename true
161 :location
162 (file-location
163 scanner))))
164 (when module
165 (module-import module)
166 (pushnew path (module-files *module*))
167 (pushnew module
168 (module-dependencies
169 *module*))))
170 (file-error (error)
171 (cerror* "Error reading module ~S: ~A"
172 path error))
173 (error (error)
174 (cerror* "Unexpected error reading ~
175 module ~S: ~A"
176 path error))))))
177 (seq ("load" (name (must :string)) (nil (must #\;)))
178 (common name "LISP" "Lisp file"
179 (lambda (path true)
180 (handler-case
181 (progn
182 (pushnew path (module-files *module*))
183 (load true :verbose nil :print nil))
184 (error (error)
185 (cerror* "Error loading Lisp file ~S: ~A"
186 path error)))))))))))
187
188;;; Setting properties.
189
190(define-pluggable-parser module set (scanner pset)
191 ;; `set' list[property] `;'
192 (with-parser-context (token-scanner-context :scanner scanner)
193 (parse (and "set"
194 (lisp (let ((module-pset (module-pset *module*)))
195 (when pset
196 (pset-map (lambda (prop)
197 (add-property
198 module-pset
199 (p-name prop) (p-value prop)
200 :type (p-type prop)
201 :location (p-location prop))
202 (setf (p-seenp prop) t))
203 pset))
204 (parse (skip-many (:min (if pset 0 1))
205 (error (:ignore-unconsumed t)
206 (parse-property scanner module-pset)
207 (skip-until () #\, #\;))
208 #\,))))
209 #\;))))
210
211;;; Lisp escape.
212
213(define-pluggable-parser module lisp (scanner pset)
214 ;; `lisp' s-expression `;'
215 (declare (ignore pset))
216 (with-parser-context (token-scanner-context :scanner scanner)
217 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
218 (string= (token-value scanner) "lisp"))
219 (let* ((stream (make-scanner-stream scanner))
220 (sexp (read stream t)))
221 (scanner-step scanner)
222 (values sexp t t))
223 (values '((:id "lisp")) nil nil)))
224 (nil (must #\;)))
225 (eval sexp)))))
226
227;;;--------------------------------------------------------------------------
228;;; Static instances.
229
230(define-pluggable-parser module instance (scanner pset)
231 ;; `instance' id id list[slot-initializer] `;'
232 (with-parser-context (token-scanner-context :scanner scanner)
233 (let ((duff nil)
234 (floc nil)
235 (empty-pset (make-property-set)))
236 (parse (seq ("instance"
237 (class (seq ((class-name (must :id)))
238 (setf floc (file-location scanner))
239 (restart-case (find-sod-class class-name)
240 (continue ()
241 (setf duff t)
242 nil))))
243 (name (must :id))
244 (inits (? (seq (#\:
245 (inits (list (:min 0)
246 (seq ((nick (must :id))
247 #\.
248 (name (must :id))
249 (value
250 (parse-delimited-fragment
251 scanner #\= '(#\, #\;)
252 :keep-end t)))
253 (make-sod-instance-initializer
254 class nick name value
255 empty-pset
256 :add-to-class nil
257 :location scanner))
258 #\,)))
259 inits)))
260 #\;)
261 (unless duff
262 (acond ((find-if (lambda (item)
263 (and (typep item 'static-instance)
264 (string= (static-instance-name item)
265 name)))
266 (module-items *module*))
267 (cerror*-with-location floc
268 "Instance with name `~A' ~
269 already defined."
270 name)
271 (info-with-location (file-location it)
272 "Previous definition was ~
273 here."))
274 (t
275 (add-to-module *module*
276 (make-static-instance class name
277 inits
278 pset
279 floc))))))))))
280
281;;;--------------------------------------------------------------------------
282;;; Class declarations.
283
284(export 'class-item)
285
286(define-pluggable-parser class-item initfrags (scanner class pset)
287 ;; raw-class-item ::= frag-keyword `{' c-fragment `}'
288 ;; frag-keyword ::= `init' | `teardown'
289 (with-parser-context (token-scanner-context :scanner scanner)
290 (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag)
291 (seq ("teardown") #'make-sod-class-tearfrag)))
292 (frag (parse-delimited-fragment scanner #\{ #\})))
293 (funcall make class frag pset :location scanner)))))
294
295(define-pluggable-parser class-item initargs (scanner class pset)
296 ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
297 ;; init-declarator ::= declarator [`=' initializer]
298 (with-parser-context (token-scanner-context :scanner scanner)
299 (parse (seq ("initarg"
300 (base-type (parse-c-type scanner))
301 (nil (skip-many (:min 1)
302 (seq ((declarator (parse-declarator scanner
303 base-type))
304 (init (? (parse-delimited-fragment
305 scanner #\= (list #\; #\,)
306 :keep-end t))))
307 (make-sod-user-initarg class
308 (cdr declarator)
309 (car declarator)
310 pset
311 :default init
312 :location scanner))
313 #\,))
314 (nil (must #\;)))))))
315
316(defun synthetic-name ()
317 "Return an obviously bogus synthetic not-identifier."
318 (let ((ix *temporary-index*))
319 (incf *temporary-index*)
320 (make-instance 'temporary-variable :tag (format nil "%%#~A" ix))))
321
322(defun parse-class-body (scanner pset name supers)
323 ;; class-body ::= `{' class-item* `}'
324 ;;
325 ;; class-item ::= property-set raw-class-item
326 (with-parser-context (token-scanner-context :scanner scanner)
327 (when name (make-class-type name))
328 (let* ((duff (null name))
329 (superclasses
330 (let ((superclasses (restart-case
331 (mapcar #'find-sod-class
332 (or supers (list "SodObject")))
333 (continue ()
334 (setf duff t)
335 (list (find-sod-class "SodObject"))))))
336 (find-duplicates (lambda (first second)
337 (declare (ignore second))
338 (setf duff t)
339 (cerror* "Class `~A' has duplicate ~
340 direct superclass `~A'"
341 name first))
342 superclasses)
343 (delete-duplicates superclasses)))
344 (synthetic-name (or name
345 (let ((var (synthetic-name)))
346 (unless pset
347 (setf pset (make-property-set)))
348 (unless (pset-get pset "nick")
349 (add-property pset "nick" var :type :id))
350 var)))
351 (class (make-sod-class synthetic-name superclasses pset
352 :location scanner))
353 (nick (sod-class-nickname class)))
354
355 (labels ((must-id ()
356 (parse (must :id (progn (setf duff t) (synthetic-name)))))
357
358 (parse-maybe-dotted-name ()
359 ;; maybe-dotted-name ::= [id `.'] id
360 ;;
361 ;; A plain identifier is returned as a string, as usual; a
362 ;; dotted identifier is returned as a cons cell of the two
363 ;; names.
364 (parse (seq ((name-a (must-id))
365 (name-b (? (seq (#\. (id (must-id))) id))))
366 (if name-b (cons name-a name-b)
367 name-a))))
368
369 (parse-maybe-dotted-declarator (base-type)
370 ;; Parse a declarator or dotted-declarator, i.e., one whose
371 ;; centre is maybe-dotted-name above.
372 (parse-declarator scanner base-type
373 :keywordp t
374 :kernel #'parse-maybe-dotted-name))
375
376 (parse-message-item (sub-pset type name)
377 ;; message-item ::=
378 ;; declspec+ declarator -!- (method-body | `;')
379 ;;
380 ;; Don't allow a method-body here if the message takes a
381 ;; varargs list, because we don't have a name for the
382 ;; `va_list' parameter.
383 (let ((message (make-sod-message class name type sub-pset
384 :location scanner)))
385 (if (varargs-message-p message)
386 (parse #\;)
387 (parse (or #\; (parse-method-item sub-pset
388 type nick name))))))
389
390 (parse-method-item (sub-pset type sub-nick name)
391 ;; method-item ::=
392 ;; declspec+ dotted-declarator -!- method-body
393 ;;
394 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
395 (parse (seq ((body (or (seq ("extern" #\;) nil)
396 (parse-delimited-fragment
397 scanner #\{ #\}))))
398 (restart-case
399 (make-sod-method class sub-nick name type
400 body sub-pset
401 :location scanner)
402 (continue () :report "Continue")))))
403
404 (parse-initializer ()
405 ;; initializer ::= `=' c-fragment
406 ;;
407 ;; Return a VALUE, ready for passing to a `sod-initializer'
408 ;; constructor.
409 (parse-delimited-fragment scanner #\= '(#\, #\;)
410 :keep-end t))
411
412 (parse-slot-item (sub-pset base-type type name)
413 ;; slot-item ::=
414 ;; declspec+ declarator -!- [initializer]
415 ;; [`,' list[init-declarator]] `;'
416 ;;
417 ;; init-declarator ::= declarator [initializer]
418 (flet ((make-it (name type init)
419 (restart-case
420 (progn
421 (make-sod-slot class name type sub-pset
422 :location scanner)
423 (when init
424 (make-sod-instance-initializer
425 class nick name init sub-pset
426 :location scanner)))
427 (continue () :report "Continue"))))
428 (parse (and (error ()
429 (seq ((init (? (parse-initializer))))
430 (make-it name type init))
431 (skip-until () #\, #\;))
432 (skip-many ()
433 (error (:ignore-unconsumed t)
434 (seq (#\,
435 (ds (parse-declarator scanner
436 base-type))
437 (init (? (parse-initializer))))
438 (make-it (cdr ds) (car ds) init))
439 (skip-until () #\, #\;)))
440 (must #\;)))))
441
442 (parse-initializer-item (sub-pset must-init-p constructor)
443 ;; initializer-item ::=
444 ;; [`class'] -!- list[slot-initializer] `;'
445 ;;
446 ;; slot-initializer ::= id `.' id [initializer]
447 (let ((parse-init (if must-init-p #'parse-initializer
448 (parser () (? (parse-initializer))))))
449 (parse (and (skip-many ()
450 (error (:ignore-unconsumed t)
451 (seq ((name-a :id) #\.
452 (name-b (must-id))
453 (init (funcall parse-init)))
454 (restart-case
455 (funcall constructor class
456 name-a name-b init
457 sub-pset
458 :location scanner)
459 (continue () :report "Continue")))
460 (skip-until () #\, #\;))
461 #\,)
462 (must #\;)))))
463
464 (class-item-dispatch (sub-pset base-type type name)
465 ;; Logically part of `parse-raw-class-item', but the
466 ;; indentation was getting crazy. We're currently at
467 ;;
468 ;; raw-class-item ::=
469 ;; declspec+ (declarator | dotted-declarator) -!- ...
470 ;; | other-items
471 ;;
472 ;; If the declarator is dotted then this must be a method
473 ;; definition; otherwise it might be a message or slot.
474 (cond ((not (typep type 'c-function-type))
475 (when (consp name)
476 (cerror*
477 "Method declarations must have function type")
478 (setf name (cdr name)))
479 (parse-slot-item sub-pset base-type type name))
480 ((consp name)
481 (parse-method-item sub-pset type
482 (car name) (cdr name)))
483 (t
484 (parse-message-item sub-pset type name))))
485
486 (parse-raw-class-item (sub-pset)
487 ;; raw-class-item ::=
488 ;; message-item
489 ;; | method-item
490 ;; | slot-item
491 ;; | initializer-item
492 ;; | initfrag-item
493 ;;
494 ;; Most of the above begin with declspecs and a declarator
495 ;; (which might be dotted). So we parse that here and
496 ;; dispatch based on what we find.
497 (parse (or (plug class-item scanner class sub-pset)
498 (peek
499 (seq ((ds (parse-c-type scanner))
500 (dc (parse-maybe-dotted-declarator ds))
501 (nil (commit))
502 (nil (class-item-dispatch sub-pset
503 ds
504 (car dc)
505 (cdr dc))))))
506 (and "class"
507 (parse-initializer-item sub-pset t
508 #'make-sod-class-initializer))
509 (parse-initializer-item sub-pset nil
510 #'make-sod-instance-initializer)))))
511
512 (parse (seq ((nil (must #\{))
513 (nil (skip-many ()
514 (seq ((sub-pset (parse-property-set scanner))
515 (nil (parse-raw-class-item sub-pset)))
516 (check-unused-properties sub-pset))))
517 (nil (must #\})))
518 (unless (finalize-sod-class class)
519 (setf duff t))
520 (unless duff
521 (add-to-module *module* class))))))))
522
523(define-pluggable-parser module class (scanner pset)
524 ;; `class' id `:' list[id] class-body
525 ;; `class' id `;'
526 (with-parser-context (token-scanner-context :scanner scanner)
527 (parse (seq ("class"
528 (name (must :id))
529 (nil (or (seq (#\;)
530 (when name (make-class-type name)))
531 (seq ((supers (must (seq (#\:
532 (ids (list () :id #\,)))
533 ids)))
534 (nil (parse-class-body
535 scanner
536 pset name supers)))))))))))
537
538;;;----- That's all, folks --------------------------------------------------