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