3 ;;; Top-level parser for module syntax
5 ;;; (c) 2010 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensble 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 ;;;--------------------------------------------------------------------------
35 (define-pluggable-parser module typename (scanner)
36 ;; `typename' ID ( `,' ID )* `;'
38 (with-parser-context (token-scanner-context :scanner scanner)
39 (parse (and "typename"
42 (if (gethash id *module-type-map*)
43 (cerror* "Type `~A' already defined" id)
44 (add-to-module *module*
45 (make-instance 'type-item
52 (define-pluggable-parser module code (scanner)
53 ;; `code' ID `:' ID [ CONSTRAINTS ] `{' C-FRAGMENT `}'
55 (with-parser-context (token-scanner-context :scanner scanner)
60 (constraints (? (seq (#\[
61 (constraints (list (:min 1)
66 (fragment (parse-delimited-fragment scanner #\{ #\})))
67 (add-to-module *module* (make-instance 'code-fragment-item
69 :constraints constraints
75 (defun read-module (pathname &key (truename (truename pathname)) location)
76 "Parse the file at PATHNAME as a module, returning it.
78 This is the main entry point for parsing module files. You may well know
79 the file's TRUENAME already (e.g., because `probe-file' dropped it into
80 your lap) so you can avoid repeating the search by providing it.
82 The LOCATION is the thing which wanted the module imported -- usually a
83 `file-location' object, though it might be anything other than `t' which
84 can be printed in the event of circular imports."
86 (define-module (pathname :location location :truename truename)
87 (with-open-file (f-stream pathname :direction :input)
88 (let* ((*readtable* (copy-readtable))
89 (char-scanner (make-instance 'charbuf-scanner
91 (scanner (make-instance 'sod-token-scanner
92 :char-scanner char-scanner)))
93 (with-default-error-location (scanner)
94 (with-parser-context (token-scanner-context :scanner scanner)
95 (parse (skip-many () (plug module scanner)))))))))
97 (define-pluggable-parser module test (scanner)
100 (with-parser-context (token-scanner-context :scanner scanner)
101 (parse (seq ("demo" (string :string) #\;)
102 (format t ";; DEMO ~S~%" string)))))
104 (define-pluggable-parser module file (scanner)
105 ;; `import' STRING `;'
108 (flet ((common (name type what thunk)
110 (merge-pathnames name
111 (make-pathname :type type
115 (with-parser-context (token-scanner-context :scanner scanner)
116 (parse (or (seq ("import" (name :string) #\;)
117 (common name "SOD" "module"
120 (let ((module (read-module path
123 (module-import module)
128 (cerror* "Error reading module ~S: ~A"
130 (seq ("load" (name :string) #\;)
131 (common name "LISP" "Lisp file"
134 (load true :verbose nil :print nil)
136 (cerror* "Error loading Lisp file ~S: ~A"
137 path error)))))))))))
141 (define-pluggable-parser module lisp (scanner)
142 ;; `lisp' s-expression `;'
144 (with-parser-context (token-scanner-context :scanner scanner)
145 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
146 (string= (token-value scanner) "lisp"))
147 (let* ((stream (make-scanner-stream scanner))
148 (sexp (read stream t)))
149 (scanner-step scanner)
151 (values '((:id "lisp")) nil nil)))
155 ;;;--------------------------------------------------------------------------
156 ;;; Class declarations.
158 (defun parse-class-body (scaner pset name supers)
159 ;; class-body ::= `{' class-item* `}'
160 (with-parser-context (token-scanner-context :scanner scanner)
161 (make-class-type name)
162 (let* ((class (make-sod-class name (mapcat #'find-sod-class supers)
164 (nick (sod-class-nickname class)))
166 (labels ((parse-maybe-dotted-declarator (base-type)
167 ;; Parse a declarator or dotted-declarator, i.e., one whose
170 ;; maybe-dotted-identifier ::= [id `.'] id
172 ;; A plain identifier is returned as a string, as usual; a
173 ;; dotted identifier is returned as a cons cell of the two
179 (name-b (? (seq (#\. (id :id)) id))))
180 (if name-b (cons name-a name-b)
183 ;; class-item ::= [property-set] raw-class-item
186 (parse-message-item (sub-pset type name)
188 ;; declspec+ declarator -!- (method-body | `;')
189 (make-sod-message class name type sub-pset scanner)
190 (parse (or #\; (parse-method-item nil type nick name))))
192 (parse-method-item (sub-pset type sub-nick name)
194 ;; declspec+ dotted-declarator -!- method-body
196 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
197 (parse (seq ((body (or (seq ("extern" #\;) nil)
198 (parse-delimited-fragment
200 (make-sod-method class sub-nick name type
201 body sub-pset scanner))))
203 (parse-initializer ()
204 ;; initializer ::= `=' c-fragment | `=' `{' c-fragment `}'
206 ;; Return (VALUE-KIND . VALUE-FORM), ready for passing to a
207 ;; `sod-initializer' constructor.
208 (parse (or (peek (seq (#\= (frag (parse-delimited-fragment
210 (cons :compound frag)))
211 (seq ((frag (parse-delimited-fragment
212 scanner #\= '(#\; #\,)
214 (cons :simple frag)))))
216 (parse-slot-item (sub-pset base-type type name)
218 ;; declspec+ declarator -!- [initializer]
219 ;; [`,' init-declarator-list] `;'
221 ;; init-declarator-list ::=
222 ;; declarator [initializer] [`,' init-declarator-list]
223 (parse (and (seq ((init (? (parse-initializer))))
224 (make-sod-slot class name type
227 (make-sod-instance-initializer
228 class nick name (car init) (cdr init)
232 (ds (parse-declarator scanner
234 (init (? (parse-initializer))))
235 (make-sod-slot class (cdr ds) (car ds)
238 (make-sod-instance-initializer
240 (car init) (cdr init)
244 (parse-initializer-item (sub-pset constructor)
245 ;; initializer-item ::=
246 ;; [`class'] -!- slot-initializer-list `;'
248 ;; slot-initializer ::= id `.' id initializer
249 (parse (and (skip-many ()
250 (seq ((name-a :id) #\. (name-b :id)
251 (init (parse-initializer)))
252 (funcall constructor class
254 (car init) (cdr init)
259 (class-item-dispatch (sub-pset base-type type name)
260 ;; Logically part of `parse-raw-class-item', but the
261 ;; indentation was getting crazy. We're currently at
263 ;; raw-class-item ::=
264 ;; declspec+ (declarator | dotted-declarator) -!- ...
267 ;; If the declarator is dotted then this must be a method
268 ;; definition; otherwise it might be a message or slot.
269 (cond ((not (typep type 'c-function-type))
271 (cerror*-with-location
273 "Method declarations must have function type.")
274 (setf name (cdr name)))
275 (parse-slot-item sub-pset base-type type name))
277 (parse-method-item sub-pset type
278 (car name) (cdr name)))
280 (parse-message-item sub-pset type name))))
282 (parse-raw-class-item (sub-pset)
283 ;; raw-class-item ::=
287 ;; | initializer-item
289 ;; Most of the above begin with declspecs and a declarator
290 ;; (which might be dotted). So we parse that here and
291 ;; dispatch based on what we find.
293 (seq ((ds (parse-c-type scanner))
294 (dc (parse-maybe-dotted-declarator ds))
295 (result (class-item-dispatch sub-pset
301 (parse-initializer-item
303 #'make-sod-class-initializer))
304 (parse-initializer-item
306 #'make-sod-instance-initializer)))))
310 (seq ((sub-pset (? (parse-property-set)))
311 (nil (parse-raw-class-item sub-pset)))))
314 (define-pluggable-parser module class (scanner)
315 ;; `class' id [`:' id-list] class-body
318 (with-parser-context (token-scanner-context :scanner scanner)
322 (make-class-type name))
323 (seq ((supers (? (seq (#\: (ids (list () :id #\,)))
325 (nil (parse-class-body
327 pset name supers)))))))))))
334 (supers (? (seq (#\: (supers (list (:min 1) :id #\,)))
339 ;;;----- That's all, folks --------------------------------------------------