3 ;;; Top-level parser for module syntax
5 ;;; (c) 2010 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensible Object Design, an object system for C.
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.
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.
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.
28 ;;;--------------------------------------------------------------------------
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"
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
47 (skip-until () #\, #\;))
53 (define-pluggable-parser module code (scanner pset)
54 ;; `code' id `:' item-name [constraints] `{' c-fragment `}'
55 ;; `code' id `:' constraints `;'
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)
63 (parse (seq ((kw :id))
64 (intern (frob-identifier kw) 'keyword))))
67 (seq (#\( (names (list (:min 1) (kw))) #\))
74 (error (:ignore-unconsumed t) (item)
75 (skip-until () :id #\( #\, #\])))
80 (parse-delimited-fragment scanner #\{ #\})))
84 (item (or (seq ((constraints (constraints))
86 (make-instance 'code-fragment-item
88 :constraints constraints))
89 (seq ((name (must (item)))
90 (constraints (? (constraints)))
91 (fragment (fragment)))
93 (make-instance 'code-fragment-item
95 :constraints constraints
97 :fragment fragment))))))
98 (when item (add-to-module *module* item)))))))
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.
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.
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."
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)
120 (make-instance 'charbuf-scanner
122 :filename (namestring pathname)))
123 (scanner (make-instance 'sod-token-scanner
124 :char-scanner char-scanner)))
125 (with-default-error-location (scanner)
127 (token-scanner-context :scanner scanner)
128 (multiple-value-bind (result winp consumedp)
130 (seq ((pset (parse-property-set scanner))
132 (plug module scanner pset)
133 (skip-until (:keep-end nil)
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)
142 (define-pluggable-parser module file (scanner pset)
143 ;; `import' string `;'
145 (declare (ignore pset))
146 (flet ((common (name type what thunk)
148 (find-file (pathname (scanner-filename scanner))
149 (merge-pathnames name
150 (make-pathname :type type
154 (with-parser-context (token-scanner-context :scanner scanner)
155 (parse (or (seq ("import" (name (must :string)) (nil (must #\;)))
156 (common name "SOD" "module"
159 (let ((module (read-module path
165 (module-import module)
166 (pushnew path (module-files *module*))
171 (cerror* "Error reading module ~S: ~A"
174 (cerror* "Unexpected error reading ~
177 (seq ("load" (name (must :string)) (nil (must #\;)))
178 (common name "LISP" "Lisp file"
182 (pushnew path (module-files *module*))
183 (load true :verbose nil :print nil))
185 (cerror* "Error loading Lisp file ~S: ~A"
186 path error)))))))))))
188 ;;; Setting properties.
190 (define-pluggable-parser module set (scanner pset)
191 ;; `set' list[property] `;'
192 (with-parser-context (token-scanner-context :scanner scanner)
194 (lisp (let ((module-pset (module-pset *module*)))
196 (pset-map (lambda (prop)
199 (p-name prop) (p-value prop)
201 :location (p-location prop))
202 (setf (p-seenp prop) t))
204 (parse (skip-many (:min (if pset 0 1))
205 (error (:ignore-unconsumed t)
206 (parse-property scanner module-pset)
207 (skip-until () #\, #\;))
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)
223 (values '((:id "lisp")) nil nil)))
227 ;;;--------------------------------------------------------------------------
228 ;;; Static instances.
230 (define-pluggable-parser module instance (scanner pset)
231 ;; `instance' id id list[slot-initializer] `;'
232 (with-parser-context (token-scanner-context :scanner scanner)
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)
245 (inits (list (:min 0)
246 (seq ((nick (must :id))
250 (parse-delimited-fragment
251 scanner #\= '(#\, #\;)
253 (make-sod-instance-initializer
254 class nick name value
262 (acond ((find-if (lambda (item)
263 (and (typep item 'static-instance)
264 (string= (static-instance-name item)
266 (module-items *module*))
267 (cerror*-with-location floc
268 "Instance with name `~A' ~
271 (info-with-location (file-location it)
272 "Previous definition was ~
275 (add-to-module *module*
276 (make-static-instance class name
281 ;;;--------------------------------------------------------------------------
282 ;;; Class declarations.
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)))))
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
304 (init (? (parse-delimited-fragment
305 scanner #\= (list #\; #\,)
307 (make-sod-user-initarg class
314 (nil (must #\;)))))))
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))))
322 (defun parse-class-body (scanner pset name supers)
323 ;; class-body ::= `{' class-item* `}'
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))
330 (let ((superclasses (restart-case
331 (mapcar #'find-sod-class
332 (or supers (list "SodObject")))
335 (list (find-sod-class "SodObject"))))))
336 (find-duplicates (lambda (first second)
337 (declare (ignore second))
339 (cerror* "Class `~A' has duplicate ~
340 direct superclass `~A'"
343 (delete-duplicates superclasses)))
344 (synthetic-name (or name
345 (let ((var (synthetic-name)))
347 (setf pset (make-property-set)))
348 (unless (pset-get pset "nick")
349 (add-property pset "nick" var :type :id))
351 (class (make-sod-class synthetic-name superclasses pset
353 (nick (sod-class-nickname class)))
356 (parse (must :id (progn (setf duff t) (synthetic-name)))))
358 (parse-maybe-dotted-name ()
359 ;; maybe-dotted-name ::= [id `.'] id
361 ;; A plain identifier is returned as a string, as usual; a
362 ;; dotted identifier is returned as a cons cell of the two
364 (parse (seq ((name-a (must-id))
365 (name-b (? (seq (#\. (id (must-id))) id))))
366 (if name-b (cons name-a name-b)
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
374 :kernel #'parse-maybe-dotted-name))
376 (parse-message-item (sub-pset type name)
378 ;; declspec+ declarator -!- (method-body | `;')
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
385 (if (varargs-message-p message)
387 (parse (or #\; (parse-method-item sub-pset
390 (parse-method-item (sub-pset type sub-nick name)
392 ;; declspec+ dotted-declarator -!- method-body
394 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
395 (parse (seq ((body (or (seq ("extern" #\;) nil)
396 (parse-delimited-fragment
399 (make-sod-method class sub-nick name type
402 (continue () :report "Continue")))))
404 (parse-initializer ()
405 ;; initializer ::= `=' c-fragment
407 ;; Return a VALUE, ready for passing to a `sod-initializer'
409 (parse-delimited-fragment scanner #\= '(#\, #\;)
412 (parse-slot-item (sub-pset base-type type name)
414 ;; declspec+ declarator -!- [initializer]
415 ;; [`,' list[init-declarator]] `;'
417 ;; init-declarator ::= declarator [initializer]
418 (flet ((make-it (name type init)
421 (make-sod-slot class name type sub-pset
424 (make-sod-instance-initializer
425 class nick name init sub-pset
427 (continue () :report "Continue"))))
428 (parse (and (error ()
429 (seq ((init (? (parse-initializer))))
430 (make-it name type init))
431 (skip-until () #\, #\;))
433 (error (:ignore-unconsumed t)
435 (ds (parse-declarator scanner
437 (init (? (parse-initializer))))
438 (make-it (cdr ds) (car ds) init))
439 (skip-until () #\, #\;)))
442 (parse-initializer-item (sub-pset must-init-p constructor)
443 ;; initializer-item ::=
444 ;; [`class'] -!- list[slot-initializer] `;'
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) #\.
453 (init (funcall parse-init)))
455 (funcall constructor class
459 (continue () :report "Continue")))
460 (skip-until () #\, #\;))
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
468 ;; raw-class-item ::=
469 ;; declspec+ (declarator | dotted-declarator) -!- ...
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))
477 "Method declarations must have function type")
478 (setf name (cdr name)))
479 (parse-slot-item sub-pset base-type type name))
481 (parse-method-item sub-pset type
482 (car name) (cdr name)))
484 (parse-message-item sub-pset type name))))
486 (parse-raw-class-item (sub-pset)
487 ;; raw-class-item ::=
491 ;; | initializer-item
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)
499 (seq ((ds (parse-c-type scanner))
500 (dc (parse-maybe-dotted-declarator ds))
502 (nil (class-item-dispatch sub-pset
507 (parse-initializer-item sub-pset t
508 #'make-sod-class-initializer))
509 (parse-initializer-item sub-pset nil
510 #'make-sod-instance-initializer)))))
512 (parse (seq ((nil (must #\{))
514 (seq ((sub-pset (parse-property-set scanner))
515 (nil (parse-raw-class-item sub-pset)))
516 (check-unused-properties sub-pset))))
518 (unless (finalize-sod-class class)
521 (add-to-module *module* class))))))))
523 (define-pluggable-parser module class (scanner pset)
524 ;; `class' id `:' list[id] class-body
526 (with-parser-context (token-scanner-context :scanner scanner)
530 (when name (make-class-type name)))
531 (seq ((supers (must (seq (#\:
532 (ids (list () :id #\,)))
534 (nil (parse-class-body
536 pset name supers)))))))))))
538 ;;;----- That's all, folks --------------------------------------------------