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