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 pset)
36 ;; `typename' id ( `,' id )* `;'
37 (declare (ignore pset))
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 pset)
53 ;; `code' id `:' id [constraints] `{' c-fragment `}'
55 ;; constrains ::= `[' constraint-list `]'
57 (declare (ignore pset))
58 (with-parser-context (token-scanner-context :scanner scanner)
63 (constraints (? (seq (#\[
64 (constraints (list (:min 1)
69 (fragment (parse-delimited-fragment scanner #\{ #\})))
70 (add-to-module *module* (make-instance 'code-fragment-item
72 :constraints constraints
78 (defun read-module (pathname &key (truename (truename pathname)) location)
79 "Parse the file at PATHNAME as a module, returning it.
81 This is the main entry point for parsing module files. You may well know
82 the file's TRUENAME already (e.g., because `probe-file' dropped it into
83 your lap) so you can avoid repeating the search by providing it.
85 The LOCATION is the thing which wanted the module imported -- usually a
86 `file-location' object, though it might be anything other than `t' which
87 can be printed in the event of circular imports."
89 (define-module (pathname :location location :truename truename)
90 (with-open-file (f-stream pathname :direction :input)
91 (let* ((*readtable* (copy-readtable))
92 (char-scanner (make-instance 'charbuf-scanner
94 (scanner (make-instance 'sod-token-scanner
95 :char-scanner char-scanner)))
96 (with-default-error-location (scanner)
97 (with-parser-context (token-scanner-context :scanner scanner)
99 (seq ((pset (parse-property-set scanner))
101 (plug module scanner pset))))
102 (check-unused-properties pset))))))))))
104 (define-pluggable-parser module test (scanner pset)
106 (declare (ignore pset))
107 (with-parser-context (token-scanner-context :scanner scanner)
108 (parse (seq ("demo" (string :string) #\;)
109 (format t ";; DEMO ~S~%" string)))))
111 (define-pluggable-parser module file (scanner pset)
112 ;; `import' string `;'
114 (declare (ignore pset))
115 (flet ((common (name type what thunk)
117 (merge-pathnames name
118 (make-pathname :type type
122 (with-parser-context (token-scanner-context :scanner scanner)
123 (parse (or (seq ("import" (name :string) #\;)
124 (common name "SOD" "module"
127 (let ((module (read-module path
130 (module-import module)
135 (cerror* "Error reading module ~S: ~A"
137 (seq ("load" (name :string) #\;)
138 (common name "LISP" "Lisp file"
141 (load true :verbose nil :print nil)
143 (cerror* "Error loading Lisp file ~S: ~A"
144 path error)))))))))))
148 (define-pluggable-parser module lisp (scanner pset)
149 ;; `lisp' s-expression `;'
150 (declare (ignore pset))
151 (with-parser-context (token-scanner-context :scanner scanner)
152 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
153 (string= (token-value scanner) "lisp"))
154 (let* ((stream (make-scanner-stream scanner))
155 (sexp (read stream t)))
156 (scanner-step scanner)
158 (values '((:id "lisp")) nil nil)))
162 ;;;--------------------------------------------------------------------------
163 ;;; Class declarations.
165 (defun parse-class-body (scanner pset name supers)
166 ;; class-body ::= `{' class-item* `}'
168 ;; class-item ::= property-set raw-class-item
169 (with-parser-context (token-scanner-context :scanner scanner)
170 (make-class-type name)
171 (let* ((class (make-sod-class name (mapcar #'find-sod-class supers)
173 (nick (sod-class-nickname class)))
175 (labels ((parse-maybe-dotted-declarator (base-type)
176 ;; Parse a declarator or dotted-declarator, i.e., one whose
179 ;; maybe-dotted-identifier ::= [id `.'] id
181 ;; A plain identifier is returned as a string, as usual; a
182 ;; dotted identifier is returned as a cons cell of the two
188 (name-b (? (seq (#\. (id :id)) id))))
189 (if name-b (cons name-a name-b)
192 (parse-message-item (sub-pset type name)
194 ;; declspec+ declarator -!- (method-body | `;')
195 (make-sod-message class name type sub-pset scanner)
196 (parse (or #\; (parse-method-item sub-pset
199 (parse-method-item (sub-pset type sub-nick name)
201 ;; declspec+ dotted-declarator -!- method-body
203 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
204 (parse (seq ((body (or (seq ("extern" #\;) nil)
205 (parse-delimited-fragment
207 (make-sod-method class sub-nick name type
208 body sub-pset scanner))))
210 (parse-initializer ()
211 ;; initializer ::= `=' c-fragment | `=' `{' c-fragment `}'
213 ;; Return (VALUE-KIND . VALUE-FORM), ready for passing to a
214 ;; `sod-initializer' constructor.
215 (parse (or (peek (seq (#\= (frag (parse-delimited-fragment
217 (cons :compound frag)))
218 (seq ((frag (parse-delimited-fragment
219 scanner #\= '(#\; #\,)
221 (cons :simple frag)))))
223 (parse-slot-item (sub-pset base-type type name)
225 ;; declspec+ declarator -!- [initializer]
226 ;; [`,' init-declarator-list] `;'
228 ;; init-declarator-list ::=
229 ;; declarator [initializer] [`,' init-declarator-list]
230 (parse (and (seq ((init (? (parse-initializer))))
231 (make-sod-slot class name type
234 (make-sod-instance-initializer
235 class nick name (car init) (cdr init)
239 (ds (parse-declarator scanner
241 (init (? (parse-initializer))))
242 (make-sod-slot class (cdr ds) (car ds)
245 (make-sod-instance-initializer
247 (car init) (cdr init)
251 (parse-initializer-item (sub-pset constructor)
252 ;; initializer-item ::=
253 ;; [`class'] -!- slot-initializer-list `;'
255 ;; slot-initializer ::= id `.' id initializer
256 (parse (and (skip-many ()
257 (seq ((name-a :id) #\. (name-b :id)
258 (init (parse-initializer)))
259 (funcall constructor class
261 (car init) (cdr init)
266 (class-item-dispatch (sub-pset base-type type name)
267 ;; Logically part of `parse-raw-class-item', but the
268 ;; indentation was getting crazy. We're currently at
270 ;; raw-class-item ::=
271 ;; declspec+ (declarator | dotted-declarator) -!- ...
274 ;; If the declarator is dotted then this must be a method
275 ;; definition; otherwise it might be a message or slot.
276 (cond ((not (typep type 'c-function-type))
278 (cerror*-with-location
280 "Method declarations must have function type.")
281 (setf name (cdr name)))
282 (parse-slot-item sub-pset base-type type name))
284 (parse-method-item sub-pset type
285 (car name) (cdr name)))
287 (parse-message-item sub-pset type name))))
289 (parse-raw-class-item (sub-pset)
290 ;; raw-class-item ::=
294 ;; | initializer-item
296 ;; Most of the above begin with declspecs and a declarator
297 ;; (which might be dotted). So we parse that here and
298 ;; dispatch based on what we find.
299 (parse (or (plug class-item scanner class sub-pset)
301 (seq ((ds (parse-c-type scanner))
302 (dc (parse-maybe-dotted-declarator ds))
303 (nil (class-item-dispatch sub-pset
308 (parse-initializer-item
310 #'make-sod-class-initializer))
311 (parse-initializer-item
313 #'make-sod-instance-initializer)))))
317 (seq ((sub-pset (parse-property-set scanner))
319 (parse-raw-class-item sub-pset))))
320 (check-unused-properties sub-pset))))
322 (finalize-sod-class class)
323 (add-to-module *module* class)))))))
325 (define-pluggable-parser module class (scanner pset)
326 ;; `class' id [`:' id-list] class-body
328 (with-parser-context (token-scanner-context :scanner scanner)
332 (make-class-type name))
333 (seq ((supers (? (seq (#\: (ids (list () :id #\,)))
335 (nil (parse-class-body
337 pset name supers)))))))))))
339 ;;;----- That's all, folks --------------------------------------------------