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