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