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"
40 (if (gethash id *module-type-map*)
41 (cerror* "Type `~A' already defined" id)
42 (add-to-module *module*
43 (make-instance 'type-item
50 (define-pluggable-parser module code (scanner pset)
51 ;; `code' id `:' item-name [constraints] `{' c-fragment `}'
53 ;; constraints ::= `[' list[constraint] `]'
54 ;; constraint ::= item-name+
55 ;; item-name ::= id | `(' id+ `)'
56 (declare (ignore pset))
57 (with-parser-context (token-scanner-context :scanner scanner)
59 (parse (seq ((kw :id))
60 (intern (frob-identifier kw) 'keyword))))
63 (seq (#\( (names (list (:min 1) (kw))) #\))
69 (constraints (? (seq (#\[
70 (constraints (list (:min 1)
76 (fragment (parse-delimited-fragment scanner #\{ #\})))
77 (add-to-module *module*
78 (make-instance 'code-fragment-item
80 :constraints constraints
87 (defun read-module (pathname &key (truename nil truep) location)
88 "Parse the file at PATHNAME as a module, returning it.
90 This is the main entry point for parsing module files. You may well know
91 the file's TRUENAME already (e.g., because `probe-file' dropped it into
92 your lap) so you can avoid repeating the search by providing it.
94 The LOCATION is the thing which wanted the module imported -- usually a
95 `file-location' object, though it might be anything other than `t' which
96 can be printed in the event of circular imports."
98 (setf pathname (merge-pathnames pathname
99 (make-pathname :type "SOD" :case :common)))
100 (unless truep (setf truename (truename pathname)))
101 (define-module (pathname :location location :truename truename)
102 (with-open-file (f-stream pathname :direction :input)
103 (let* ((*readtable* (copy-readtable))
104 (*package* (find-package '#:sod-user))
105 (char-scanner (make-instance 'charbuf-scanner
107 (scanner (make-instance 'sod-token-scanner
108 :char-scanner char-scanner)))
109 (with-default-error-location (scanner)
110 (with-parser-context (token-scanner-context :scanner scanner)
111 (multiple-value-bind (result winp consumedp)
113 (seq ((pset (parse-property-set scanner))
115 (plug module scanner pset)
116 (skip-until (:keep-end nil)
118 (check-unused-properties pset))))
119 (declare (ignore consumedp))
120 (unless winp (syntax-error scanner result)))))))))
122 (define-pluggable-parser module test (scanner pset)
124 (declare (ignore pset))
125 (with-parser-context (token-scanner-context :scanner scanner)
126 (parse (seq ("demo" (string :string) #\;)
127 (format t ";; DEMO ~S~%" string)))))
129 (define-pluggable-parser module file (scanner pset)
130 ;; `import' string `;'
132 (declare (ignore pset))
133 (flet ((common (name type what thunk)
135 (merge-pathnames name
136 (make-pathname :type type
140 (with-parser-context (token-scanner-context :scanner scanner)
141 (parse (or (seq ("import" (name :string) #\;)
142 (common name "SOD" "module"
145 (let ((module (read-module path
148 (module-import module)
153 (cerror* "Error reading module ~S: ~A"
155 (seq ("load" (name :string) #\;)
156 (common name "LISP" "Lisp file"
159 (load true :verbose nil :print nil)
161 (cerror* "Error loading Lisp file ~S: ~A"
162 path error)))))))))))
164 ;;; Setting properties.
166 (define-pluggable-parser module set (scanner pset)
167 ;; `set' list[property] `;'
168 (with-parser-context (token-scanner-context :scanner scanner)
170 (lisp (let ((module-pset (module-pset *module*)))
172 (pset-map (lambda (prop)
175 (p-name prop) (p-value prop)
177 :location (p-location prop))
178 (setf (p-seenp prop) t))
180 (parse (skip-many (:min 0)
181 (error (:ignore-unconsumed t)
182 (parse-property scanner module-pset)
183 (skip-until () #\, #\;))
189 (define-pluggable-parser module lisp (scanner pset)
190 ;; `lisp' s-expression `;'
191 (declare (ignore pset))
192 (with-parser-context (token-scanner-context :scanner scanner)
193 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
194 (string= (token-value scanner) "lisp"))
195 (let* ((stream (make-scanner-stream scanner))
196 (sexp (read stream t)))
197 (scanner-step scanner)
199 (values '((:id "lisp")) nil nil)))
203 ;;;--------------------------------------------------------------------------
204 ;;; Class declarations.
208 (define-pluggable-parser class-item initfrags (scanner class pset)
209 ;; raw-class-item ::= frag-keyword `{' c-fragment `}'
210 ;; frag-keyword ::= `init' | `teardown'
211 (with-parser-context (token-scanner-context :scanner scanner)
212 (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag)
213 (seq ("teardown") #'make-sod-class-tearfrag)))
214 (frag (parse-delimited-fragment scanner #\{ #\})))
215 (funcall make class frag pset scanner)))))
217 (define-pluggable-parser class-item initargs (scanner class pset)
218 ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
219 ;; init-declarator ::= declarator [`=' initializer]
220 (with-parser-context (token-scanner-context :scanner scanner)
221 (parse (seq ("initarg"
222 (base-type (parse-c-type scanner))
223 (nil (skip-many (:min 1)
224 (seq ((declarator (parse-declarator scanner
226 (init (? (parse-delimited-fragment
227 scanner #\= (list #\; #\,)
229 (make-sod-user-initarg class
236 (defun parse-class-body (scanner pset name supers)
237 ;; class-body ::= `{' class-item* `}'
239 ;; class-item ::= property-set raw-class-item
240 (with-parser-context (token-scanner-context :scanner scanner)
241 (make-class-type name)
243 (class (make-sod-class name
245 (mapcar #'find-sod-class supers)
248 (list (find-sod-class "SodObject"))))
250 (nick (sod-class-nickname class)))
252 (labels ((parse-maybe-dotted-declarator (base-type)
253 ;; Parse a declarator or dotted-declarator, i.e., one whose
256 ;; maybe-dotted-identifier ::= [id `.'] id
258 ;; A plain identifier is returned as a string, as usual; a
259 ;; dotted identifier is returned as a cons cell of the two
266 (name-b (? (seq (#\. (id :id)) id))))
267 (if name-b (cons name-a name-b)
270 (parse-message-item (sub-pset type name)
272 ;; declspec+ declarator -!- (method-body | `;')
274 ;; Don't allow a method-body here if the message takes a
275 ;; varargs list, because we don't have a name for the
276 ;; `va_list' parameter.
277 (let ((message (make-sod-message class name type
279 (if (varargs-message-p message)
281 (parse (or #\; (parse-method-item sub-pset
284 (parse-method-item (sub-pset type sub-nick name)
286 ;; declspec+ dotted-declarator -!- method-body
288 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
289 (parse (seq ((body (or (seq ("extern" #\;) nil)
290 (parse-delimited-fragment
292 (make-sod-method class sub-nick name type
293 body sub-pset scanner))))
295 (parse-initializer ()
296 ;; initializer ::= `=' c-fragment
298 ;; Return a VALUE, ready for passing to a `sod-initializer'
300 (parse-delimited-fragment scanner #\= (list #\, #\;)
303 (parse-slot-item (sub-pset base-type type name)
305 ;; declspec+ declarator -!- [initializer]
306 ;; [`,' list[init-declarator]] `;'
308 ;; init-declarator ::= declarator [initializer]
309 (parse (and (seq ((init (? (parse-initializer))))
310 (make-sod-slot class name type
313 (make-sod-instance-initializer
314 class nick name init sub-pset scanner)))
317 (ds (parse-declarator scanner
319 (init (? (parse-initializer))))
320 (make-sod-slot class (cdr ds) (car ds)
323 (make-sod-instance-initializer
324 class nick (cdr ds) init
328 (parse-initializer-item (sub-pset must-init-p constructor)
329 ;; initializer-item ::=
330 ;; [`class'] -!- list[slot-initializer] `;'
332 ;; slot-initializer ::= id `.' id [initializer]
333 (let ((parse-init (if must-init-p #'parse-initializer
334 (parser () (? (parse-initializer))))))
335 (parse (and (skip-many ()
336 (seq ((name-a :id) #\. (name-b :id)
337 (init (funcall parse-init)))
338 (funcall constructor class
344 (class-item-dispatch (sub-pset base-type type name)
345 ;; Logically part of `parse-raw-class-item', but the
346 ;; indentation was getting crazy. We're currently at
348 ;; raw-class-item ::=
349 ;; declspec+ (declarator | dotted-declarator) -!- ...
352 ;; If the declarator is dotted then this must be a method
353 ;; definition; otherwise it might be a message or slot.
354 (cond ((not (typep type 'c-function-type))
357 "Method declarations must have function type")
358 (setf name (cdr name)))
359 (parse-slot-item sub-pset base-type type name))
361 (parse-method-item sub-pset type
362 (car name) (cdr name)))
364 (parse-message-item sub-pset type name))))
366 (parse-raw-class-item (sub-pset)
367 ;; raw-class-item ::=
371 ;; | initializer-item
374 ;; Most of the above begin with declspecs and a declarator
375 ;; (which might be dotted). So we parse that here and
376 ;; dispatch based on what we find.
377 (parse (or (plug class-item scanner class sub-pset)
379 (seq ((ds (parse-c-type scanner))
380 (dc (parse-maybe-dotted-declarator ds))
382 (nil (class-item-dispatch sub-pset
387 (parse-initializer-item sub-pset t
388 #'make-sod-class-initializer))
389 (parse-initializer-item sub-pset nil
390 #'make-sod-instance-initializer)))))
394 (seq ((sub-pset (parse-property-set scanner))
395 (nil (parse-raw-class-item sub-pset)))
396 (check-unused-properties sub-pset))))
397 (nil (error () #\})))
398 (unless (finalize-sod-class class)
401 (add-to-module *module* class))))))))
403 (define-pluggable-parser module class (scanner pset)
404 ;; `class' id `:' list[id] class-body
406 (with-parser-context (token-scanner-context :scanner scanner)
410 (make-class-type name))
411 (seq ((supers (seq (#\: (ids (list () :id #\,)))
413 (nil (parse-class-body
415 pset name supers)))))))))))
417 ;;;----- That's all, folks --------------------------------------------------