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' id ( `,' 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 ;; constrains ::= `[' constraint-list `]'
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 (check-unused-properties pset))))
117 (declare (ignore consumedp))
118 (unless winp (syntax-error scanner result)))))))))
120 (define-pluggable-parser module test (scanner pset)
122 (declare (ignore pset))
123 (with-parser-context (token-scanner-context :scanner scanner)
124 (parse (seq ("demo" (string :string) #\;)
125 (format t ";; DEMO ~S~%" string)))))
127 (define-pluggable-parser module file (scanner pset)
128 ;; `import' string `;'
130 (declare (ignore pset))
131 (flet ((common (name type what thunk)
133 (merge-pathnames name
134 (make-pathname :type type
138 (with-parser-context (token-scanner-context :scanner scanner)
139 (parse (or (seq ("import" (name :string) #\;)
140 (common name "SOD" "module"
143 (let ((module (read-module path
146 (module-import module)
151 (cerror* "Error reading module ~S: ~A"
153 (seq ("load" (name :string) #\;)
154 (common name "LISP" "Lisp file"
157 (load true :verbose nil :print nil)
159 (cerror* "Error loading Lisp file ~S: ~A"
160 path error)))))))))))
162 ;;; Setting properties.
164 (define-pluggable-parser module set (scanner pset)
165 ;; `set' property-list `;'
166 (with-parser-context (token-scanner-context :scanner scanner)
168 (lisp (let ((module-pset (module-pset *module*)))
170 (pset-map (lambda (prop)
171 (add-property module-pset
175 :location (p-location prop))
176 (setf (p-seenp prop) t))
178 (parse (skip-many (:min 0)
179 (error (:ignore-unconsumed t)
180 (parse-property scanner module-pset)
181 (skip-until (:keep-end t) #\, #\;))
187 (define-pluggable-parser module lisp (scanner pset)
188 ;; `lisp' s-expression `;'
189 (declare (ignore pset))
190 (with-parser-context (token-scanner-context :scanner scanner)
191 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
192 (string= (token-value scanner) "lisp"))
193 (let* ((stream (make-scanner-stream scanner))
194 (sexp (read stream t)))
195 (scanner-step scanner)
197 (values '((:id "lisp")) nil nil)))
201 ;;;--------------------------------------------------------------------------
202 ;;; Class declarations.
206 (define-pluggable-parser class-item initfrags (scanner class pset)
207 ;; raw-class-item ::= frag-keyword `{' c-fragment `}'
208 ;; frag-keyword ::= `init' | `teardown'
209 (with-parser-context (token-scanner-context :scanner scanner)
210 (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag)
211 (seq ("teardown") #'make-sod-class-tearfrag)))
212 (frag (parse-delimited-fragment scanner #\{ #\})))
213 (funcall make class frag pset scanner)))))
215 (define-pluggable-parser class-item initargs (scanner class pset)
216 ;; initarg-item ::= `initarg' declspec+ init-declarator-list
217 ;; init-declarator ::= declarator [`=' initializer]
218 (with-parser-context (token-scanner-context :scanner scanner)
219 (parse (seq ("initarg"
220 (base-type (parse-c-type scanner))
221 (nil (skip-many (:min 1)
222 (seq ((declarator (parse-declarator scanner
224 (init (? (parse-delimited-fragment
225 scanner #\= (list #\; #\,)
227 (make-sod-user-initarg class
234 (defun parse-class-body (scanner pset name supers)
235 ;; class-body ::= `{' class-item* `}'
237 ;; class-item ::= property-set raw-class-item
238 (with-parser-context (token-scanner-context :scanner scanner)
239 (make-class-type name)
240 (let* ((class (make-sod-class name (mapcar #'find-sod-class supers)
242 (nick (sod-class-nickname class)))
244 (labels ((parse-maybe-dotted-declarator (base-type)
245 ;; Parse a declarator or dotted-declarator, i.e., one whose
248 ;; maybe-dotted-identifier ::= [id `.'] id
250 ;; A plain identifier is returned as a string, as usual; a
251 ;; dotted identifier is returned as a cons cell of the two
258 (name-b (? (seq (#\. (id :id)) id))))
259 (if name-b (cons name-a name-b)
262 (parse-message-item (sub-pset type name)
264 ;; declspec+ declarator -!- (method-body | `;')
266 ;; Don't allow a method-body here if the message takes a
267 ;; varargs list, because we don't have a name for the
268 ;; `va_list' parameter.
269 (let ((message (make-sod-message class name type
271 (if (varargs-message-p message)
273 (parse (or #\; (parse-method-item sub-pset
276 (parse-method-item (sub-pset type sub-nick name)
278 ;; declspec+ dotted-declarator -!- method-body
280 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
281 (parse (seq ((body (or (seq ("extern" #\;) nil)
282 (parse-delimited-fragment
284 (make-sod-method class sub-nick name type
285 body sub-pset scanner))))
287 (parse-initializer ()
288 ;; initializer ::= `=' c-fragment
290 ;; Return a VALUE, ready for passing to a `sod-initializer'
292 (parse-delimited-fragment scanner #\= (list #\, #\;)
295 (parse-slot-item (sub-pset base-type type name)
297 ;; declspec+ declarator -!- [initializer]
298 ;; [`,' init-declarator-list] `;'
300 ;; init-declarator-list ::=
301 ;; declarator [initializer] [`,' init-declarator-list]
302 (parse (and (seq ((init (? (parse-initializer))))
303 (make-sod-slot class name type
306 (make-sod-instance-initializer
307 class nick name init sub-pset scanner)))
310 (ds (parse-declarator scanner
312 (init (? (parse-initializer))))
313 (make-sod-slot class (cdr ds) (car ds)
316 (make-sod-instance-initializer
317 class nick (cdr ds) init
321 (parse-initializer-item (sub-pset must-init-p constructor)
322 ;; initializer-item ::=
323 ;; [`class'] -!- slot-initializer-list `;'
325 ;; slot-initializer ::= id `.' id [initializer]
326 (let ((parse-init (if must-init-p
328 (parser () (? (parse-initializer))))))
329 (parse (and (skip-many ()
330 (seq ((name-a :id) #\. (name-b :id)
331 (init (funcall parse-init)))
332 (funcall constructor class
338 (class-item-dispatch (sub-pset base-type type name)
339 ;; Logically part of `parse-raw-class-item', but the
340 ;; indentation was getting crazy. We're currently at
342 ;; raw-class-item ::=
343 ;; declspec+ (declarator | dotted-declarator) -!- ...
346 ;; If the declarator is dotted then this must be a method
347 ;; definition; otherwise it might be a message or slot.
348 (cond ((not (typep type 'c-function-type))
350 (cerror*-with-location
352 "Method declarations must have function type.")
353 (setf name (cdr name)))
354 (parse-slot-item sub-pset base-type type name))
356 (parse-method-item sub-pset type
357 (car name) (cdr name)))
359 (parse-message-item sub-pset type name))))
361 (parse-raw-class-item (sub-pset)
362 ;; raw-class-item ::=
366 ;; | initializer-item
369 ;; Most of the above begin with declspecs and a declarator
370 ;; (which might be dotted). So we parse that here and
371 ;; dispatch based on what we find.
372 (parse (or (plug class-item scanner class sub-pset)
374 (seq ((ds (parse-c-type scanner))
375 (dc (parse-maybe-dotted-declarator ds))
377 (nil (class-item-dispatch sub-pset
382 (parse-initializer-item
384 #'make-sod-class-initializer))
385 (parse-initializer-item
387 #'make-sod-instance-initializer)))))
391 (seq ((sub-pset (parse-property-set scanner))
392 (nil (parse-raw-class-item sub-pset)))
393 (check-unused-properties sub-pset))))
394 (nil (error () #\})))
395 (finalize-sod-class class)
396 (add-to-module *module* class)))))))
398 (define-pluggable-parser module class (scanner pset)
399 ;; `class' id [`:' id-list] class-body
401 (with-parser-context (token-scanner-context :scanner scanner)
405 (make-class-type name))
406 (seq ((supers (? (seq (#\: (ids (list () :id #\,)))
408 (nil (parse-class-body
410 pset name supers)))))))))))
412 ;;;----- That's all, folks --------------------------------------------------