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))) #\))
72 (constraints (? (seq (#\[
73 (constraints (list (:min 1)
79 (fragment (parse-delimited-fragment scanner #\{ #\})))
80 (add-to-module *module*
81 (make-instance 'code-fragment-item
83 :constraints constraints
90 (defun read-module (pathname &key (truename nil truep) location)
91 "Parse the file at PATHNAME as a module, returning it.
93 This is the main entry point for parsing module files. You may well know
94 the file's TRUENAME already (e.g., because `probe-file' dropped it into
95 your lap) so you can avoid repeating the search by providing it.
97 The LOCATION is the thing which wanted the module imported -- usually a
98 `file-location' object, though it might be anything other than `t' which
99 can be printed in the event of circular imports."
101 (setf pathname (merge-pathnames pathname
102 (make-pathname :type "SOD" :case :common)))
103 (unless truep (setf truename (truename pathname)))
104 (define-module (pathname :location location :truename truename)
105 (with-open-file (f-stream pathname :direction :input)
106 (let* ((*readtable* (copy-readtable))
107 (*package* (find-package '#:sod-user))
108 (char-scanner (make-instance 'charbuf-scanner
110 :filename (namestring pathname)))
111 (scanner (make-instance 'sod-token-scanner
112 :char-scanner char-scanner)))
113 (with-default-error-location (scanner)
114 (with-parser-context (token-scanner-context :scanner scanner)
115 (multiple-value-bind (result winp consumedp)
117 (seq ((pset (parse-property-set scanner))
119 (plug module scanner pset)
120 (skip-until (:keep-end nil)
122 (check-unused-properties pset))))
123 (declare (ignore consumedp))
124 (unless winp (syntax-error scanner result)))))))))
126 (define-pluggable-parser module test (scanner pset)
128 (declare (ignore pset))
129 (with-parser-context (token-scanner-context :scanner scanner)
130 (parse (seq ("demo" (string :string) #\;)
131 (format t ";; DEMO ~S~%" string)))))
133 (define-pluggable-parser module file (scanner pset)
134 ;; `import' string `;'
136 (declare (ignore pset))
137 (flet ((common (name type what thunk)
139 (merge-pathnames name
140 (make-pathname :type type
144 (with-parser-context (token-scanner-context :scanner scanner)
145 (parse (or (seq ("import" (name :string) #\;)
146 (common name "SOD" "module"
149 (let ((module (read-module path
152 (module-import module)
157 (cerror* "Error reading module ~S: ~A"
159 (seq ("load" (name :string) #\;)
160 (common name "LISP" "Lisp file"
163 (load true :verbose nil :print nil)
165 (cerror* "Error loading Lisp file ~S: ~A"
166 path error)))))))))))
168 ;;; Setting properties.
170 (define-pluggable-parser module set (scanner pset)
171 ;; `set' list[property] `;'
172 (with-parser-context (token-scanner-context :scanner scanner)
174 (lisp (let ((module-pset (module-pset *module*)))
176 (pset-map (lambda (prop)
179 (p-name prop) (p-value prop)
181 :location (p-location prop))
182 (setf (p-seenp prop) t))
184 (parse (skip-many (:min (if pset 0 1))
185 (error (:ignore-unconsumed t)
186 (parse-property scanner module-pset)
187 (skip-until () #\, #\;))
193 (define-pluggable-parser module lisp (scanner pset)
194 ;; `lisp' s-expression `;'
195 (declare (ignore pset))
196 (with-parser-context (token-scanner-context :scanner scanner)
197 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
198 (string= (token-value scanner) "lisp"))
199 (let* ((stream (make-scanner-stream scanner))
200 (sexp (read stream t)))
201 (scanner-step scanner)
203 (values '((:id "lisp")) nil nil)))
207 ;;;--------------------------------------------------------------------------
208 ;;; Class declarations.
212 (define-pluggable-parser class-item initfrags (scanner class pset)
213 ;; raw-class-item ::= frag-keyword `{' c-fragment `}'
214 ;; frag-keyword ::= `init' | `teardown'
215 (with-parser-context (token-scanner-context :scanner scanner)
216 (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag)
217 (seq ("teardown") #'make-sod-class-tearfrag)))
218 (frag (parse-delimited-fragment scanner #\{ #\})))
219 (funcall make class frag pset scanner)))))
221 (define-pluggable-parser class-item initargs (scanner class pset)
222 ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
223 ;; init-declarator ::= declarator [`=' initializer]
224 (with-parser-context (token-scanner-context :scanner scanner)
225 (parse (seq ("initarg"
226 (base-type (parse-c-type scanner))
227 (nil (skip-many (:min 1)
228 (seq ((declarator (parse-declarator scanner
230 (init (? (parse-delimited-fragment
231 scanner #\= (list #\; #\,)
233 (make-sod-user-initarg class
240 (defun parse-class-body (scanner pset name supers)
241 ;; class-body ::= `{' class-item* `}'
243 ;; class-item ::= property-set raw-class-item
244 (with-parser-context (token-scanner-context :scanner scanner)
245 (make-class-type name)
247 (class (make-sod-class name
249 (mapcar #'find-sod-class supers)
252 (list (find-sod-class "SodObject"))))
254 (nick (sod-class-nickname class)))
256 (labels ((parse-maybe-dotted-declarator (base-type)
257 ;; Parse a declarator or dotted-declarator, i.e., one whose
260 ;; maybe-dotted-identifier ::= [id `.'] id
262 ;; A plain identifier is returned as a string, as usual; a
263 ;; dotted identifier is returned as a cons cell of the two
270 (name-b (? (seq (#\. (id :id)) id))))
271 (if name-b (cons name-a name-b)
274 (parse-message-item (sub-pset type name)
276 ;; declspec+ declarator -!- (method-body | `;')
278 ;; Don't allow a method-body here if the message takes a
279 ;; varargs list, because we don't have a name for the
280 ;; `va_list' parameter.
281 (let ((message (make-sod-message class name type
283 (if (varargs-message-p message)
285 (parse (or #\; (parse-method-item sub-pset
288 (parse-method-item (sub-pset type sub-nick name)
290 ;; declspec+ dotted-declarator -!- method-body
292 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
293 (parse (seq ((body (or (seq ("extern" #\;) nil)
294 (parse-delimited-fragment
296 (make-sod-method class sub-nick name type
297 body sub-pset scanner))))
299 (parse-initializer ()
300 ;; initializer ::= `=' c-fragment
302 ;; Return a VALUE, ready for passing to a `sod-initializer'
304 (parse-delimited-fragment scanner #\= (list #\, #\;)
307 (parse-slot-item (sub-pset base-type type name)
309 ;; declspec+ declarator -!- [initializer]
310 ;; [`,' list[init-declarator]] `;'
312 ;; init-declarator ::= declarator [initializer]
313 (parse (and (seq ((init (? (parse-initializer))))
314 (make-sod-slot class name type
317 (make-sod-instance-initializer
318 class nick name init sub-pset scanner)))
321 (ds (parse-declarator scanner
323 (init (? (parse-initializer))))
324 (make-sod-slot class (cdr ds) (car ds)
327 (make-sod-instance-initializer
328 class nick (cdr ds) init
332 (parse-initializer-item (sub-pset must-init-p constructor)
333 ;; initializer-item ::=
334 ;; [`class'] -!- list[slot-initializer] `;'
336 ;; slot-initializer ::= id `.' id [initializer]
337 (let ((parse-init (if must-init-p #'parse-initializer
338 (parser () (? (parse-initializer))))))
339 (parse (and (skip-many ()
340 (seq ((name-a :id) #\. (name-b :id)
341 (init (funcall parse-init)))
342 (funcall constructor class
348 (class-item-dispatch (sub-pset base-type type name)
349 ;; Logically part of `parse-raw-class-item', but the
350 ;; indentation was getting crazy. We're currently at
352 ;; raw-class-item ::=
353 ;; declspec+ (declarator | dotted-declarator) -!- ...
356 ;; If the declarator is dotted then this must be a method
357 ;; definition; otherwise it might be a message or slot.
358 (cond ((not (typep type 'c-function-type))
361 "Method declarations must have function type")
362 (setf name (cdr name)))
363 (parse-slot-item sub-pset base-type type name))
365 (parse-method-item sub-pset type
366 (car name) (cdr name)))
368 (parse-message-item sub-pset type name))))
370 (parse-raw-class-item (sub-pset)
371 ;; raw-class-item ::=
375 ;; | initializer-item
378 ;; Most of the above begin with declspecs and a declarator
379 ;; (which might be dotted). So we parse that here and
380 ;; dispatch based on what we find.
381 (parse (or (plug class-item scanner class sub-pset)
383 (seq ((ds (parse-c-type scanner))
384 (dc (parse-maybe-dotted-declarator ds))
386 (nil (class-item-dispatch sub-pset
391 (parse-initializer-item sub-pset t
392 #'make-sod-class-initializer))
393 (parse-initializer-item sub-pset nil
394 #'make-sod-instance-initializer)))))
398 (seq ((sub-pset (parse-property-set scanner))
399 (nil (parse-raw-class-item sub-pset)))
400 (check-unused-properties sub-pset))))
401 (nil (error () #\})))
402 (unless (finalize-sod-class class)
405 (add-to-module *module* class))))))))
407 (define-pluggable-parser module class (scanner pset)
408 ;; `class' id `:' list[id] class-body
410 (with-parser-context (token-scanner-context :scanner scanner)
414 (make-class-type name))
415 (seq ((supers (seq (#\: (ids (list () :id #\,)))
417 (nil (parse-class-body
419 pset name supers)))))))))))
421 ;;;----- That's all, folks --------------------------------------------------