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 `}'
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)
62 (parse (seq ((kw :id))
63 (intern (frob-identifier kw) 'keyword))))
66 (seq (#\( (names (list (:min 1) (kw))) #\))
73 (error (:ignore-unconsumed t) (item)
74 (skip-until () :id #\( #\, #\])))
79 (parse-delimited-fragment scanner #\{ #\})))
83 (item (or (seq ((name (must (item)))
84 (constraints (? (constraints)))
85 (fragment (fragment)))
87 (make-instance 'code-fragment-item
89 :constraints constraints
91 :fragment fragment))))))
92 (when item (add-to-module *module* item)))))))
97 (defun read-module (pathname &key (truename nil truep) location)
98 "Parse the file at PATHNAME as a module, returning it.
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.
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."
108 (setf pathname (merge-pathnames pathname
109 (make-pathname :type "SOD" :case :common)))
110 (unless truep (setf truename (truename pathname)))
111 (define-module (pathname :location location :truename truename)
112 (with-open-file (f-stream pathname :direction :input)
113 (let* ((char-scanner (make-instance 'charbuf-scanner
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)
122 (seq ((pset (parse-property-set scanner))
124 (plug module scanner pset)
125 (skip-until (:keep-end nil)
127 (check-unused-properties pset))))
128 (declare (ignore consumedp))
129 (unless winp (syntax-error scanner result)))))))))
131 (define-pluggable-parser module file (scanner pset)
132 ;; `import' string `;'
134 (declare (ignore pset))
135 (flet ((common (name type what thunk)
137 (find-file (pathname (scanner-filename scanner))
138 (merge-pathnames name
139 (make-pathname :type type
143 (with-parser-context (token-scanner-context :scanner scanner)
144 (parse (or (seq ("import" (name (must :string)) (nil (must #\;)))
145 (common name "SOD" "module"
148 (let ((module (read-module path
151 (module-import module)
152 (pushnew path (module-files *module*))
157 (cerror* "Error reading module ~S: ~A"
160 (cerror* "Unexpected error reading ~
163 (seq ("load" (name (must :string)) (nil (must #\;)))
164 (common name "LISP" "Lisp file"
168 (pushnew path (module-files *module*))
169 (load true :verbose nil :print nil))
171 (cerror* "Error loading Lisp file ~S: ~A"
172 path error)))))))))))
174 ;;; Setting properties.
176 (define-pluggable-parser module set (scanner pset)
177 ;; `set' list[property] `;'
178 (with-parser-context (token-scanner-context :scanner scanner)
180 (lisp (let ((module-pset (module-pset *module*)))
182 (pset-map (lambda (prop)
185 (p-name prop) (p-value prop)
187 :location (p-location prop))
188 (setf (p-seenp prop) t))
190 (parse (skip-many (:min (if pset 0 1))
191 (error (:ignore-unconsumed t)
192 (parse-property scanner module-pset)
193 (skip-until () #\, #\;))
199 (define-pluggable-parser module lisp (scanner pset)
200 ;; `lisp' s-expression `;'
201 (declare (ignore pset))
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)
209 (values '((:id "lisp")) nil nil)))
213 ;;;--------------------------------------------------------------------------
214 ;;; Class declarations.
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 #\{ #\})))
225 (funcall make class frag pset :location scanner)))))
227 (define-pluggable-parser class-item initargs (scanner class pset)
228 ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
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
236 (init (? (parse-delimited-fragment
237 scanner #\= (list #\; #\,)
239 (make-sod-user-initarg class
246 (nil (must #\;)))))))
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))))
254 (defun parse-class-body (scanner pset name supers)
255 ;; class-body ::= `{' class-item* `}'
257 ;; class-item ::= property-set raw-class-item
258 (with-parser-context (token-scanner-context :scanner scanner)
259 (when name (make-class-type name))
260 (let* ((duff (null name))
262 (let ((superclasses (restart-case
263 (mapcar #'find-sod-class
264 (or supers (list "SodObject")))
267 (list (find-sod-class "SodObject"))))))
268 (find-duplicates (lambda (first second)
269 (declare (ignore second))
271 (cerror* "Class `~A' has duplicate ~
272 direct superclass `~A'"
275 (delete-duplicates superclasses)))
276 (synthetic-name (or name
277 (let ((var (synthetic-name)))
279 (setf pset (make-property-set)))
280 (unless (pset-get pset "nick")
281 (add-property pset "nick" var :type :id))
283 (class (make-sod-class synthetic-name superclasses pset
285 (nick (sod-class-nickname class)))
288 (parse (must :id (progn (setf duff t) (synthetic-name)))))
290 (parse-maybe-dotted-name ()
291 ;; maybe-dotted-name ::= [id `.'] id
293 ;; A plain identifier is returned as a string, as usual; a
294 ;; dotted identifier is returned as a cons cell of the two
296 (parse (seq ((name-a (must-id))
297 (name-b (? (seq (#\. (id (must-id))) id))))
298 (if name-b (cons name-a name-b)
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
306 :kernel #'parse-maybe-dotted-name))
308 (parse-message-item (sub-pset type name)
310 ;; declspec+ declarator -!- (method-body | `;')
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.
315 (let ((message (make-sod-message class name type sub-pset
317 (if (varargs-message-p message)
319 (parse (or #\; (parse-method-item sub-pset
322 (parse-method-item (sub-pset type sub-nick name)
324 ;; declspec+ dotted-declarator -!- method-body
326 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
327 (parse (seq ((body (or (seq ("extern" #\;) nil)
328 (parse-delimited-fragment
331 (make-sod-method class sub-nick name type
334 (continue () :report "Continue")))))
336 (parse-initializer ()
337 ;; initializer ::= `=' c-fragment
339 ;; Return a VALUE, ready for passing to a `sod-initializer'
341 (parse-delimited-fragment scanner #\= '(#\, #\;)
344 (parse-slot-item (sub-pset base-type type name)
346 ;; declspec+ declarator -!- [initializer]
347 ;; [`,' list[init-declarator]] `;'
349 ;; init-declarator ::= declarator [initializer]
350 (flet ((make-it (name type init)
353 (make-sod-slot class name type sub-pset
356 (make-sod-instance-initializer
357 class nick name init sub-pset
359 (continue () :report "Continue"))))
360 (parse (and (error ()
361 (seq ((init (? (parse-initializer))))
362 (make-it name type init))
363 (skip-until () #\, #\;))
365 (error (:ignore-unconsumed t)
367 (ds (parse-declarator scanner
369 (init (? (parse-initializer))))
370 (make-it (cdr ds) (car ds) init))
371 (skip-until () #\, #\;)))
374 (parse-initializer-item (sub-pset must-init-p constructor)
375 ;; initializer-item ::=
376 ;; [`class'] -!- list[slot-initializer] `;'
378 ;; slot-initializer ::= id `.' id [initializer]
379 (let ((parse-init (if must-init-p #'parse-initializer
380 (parser () (? (parse-initializer))))))
381 (parse (and (skip-many ()
382 (error (:ignore-unconsumed t)
383 (seq ((name-a :id) #\.
385 (init (funcall parse-init)))
387 (funcall constructor class
391 (continue () :report "Continue")))
392 (skip-until () #\, #\;))
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
400 ;; raw-class-item ::=
401 ;; declspec+ (declarator | dotted-declarator) -!- ...
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))
409 "Method declarations must have function type")
410 (setf name (cdr name)))
411 (parse-slot-item sub-pset base-type type name))
413 (parse-method-item sub-pset type
414 (car name) (cdr name)))
416 (parse-message-item sub-pset type name))))
418 (parse-raw-class-item (sub-pset)
419 ;; raw-class-item ::=
423 ;; | initializer-item
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.
429 (parse (or (plug class-item scanner class sub-pset)
431 (seq ((ds (parse-c-type scanner))
432 (dc (parse-maybe-dotted-declarator ds))
434 (nil (class-item-dispatch sub-pset
439 (parse-initializer-item sub-pset t
440 #'make-sod-class-initializer))
441 (parse-initializer-item sub-pset nil
442 #'make-sod-instance-initializer)))))
444 (parse (seq ((nil (must #\{))
446 (seq ((sub-pset (parse-property-set scanner))
447 (nil (parse-raw-class-item sub-pset)))
448 (check-unused-properties sub-pset))))
450 (unless (finalize-sod-class class)
453 (add-to-module *module* class))))))))
455 (define-pluggable-parser module class (scanner pset)
456 ;; `class' id `:' list[id] class-body
458 (with-parser-context (token-scanner-context :scanner scanner)
462 (when name (make-class-type name)))
463 (seq ((supers (must (seq (#\:
464 (ids (list () :id #\,)))
466 (nil (parse-class-body
468 pset name supers)))))))))))
470 ;;;----- That's all, folks --------------------------------------------------