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 `:' id [constraints] `{' c-fragment `}'
53 ;; constrains ::= `[' constraint-list `]'
55 (declare (ignore pset))
56 (with-parser-context (token-scanner-context :scanner scanner)
58 (parse (seq ((kw :id)) (intern (string-upcase kw) 'keyword)))))
63 (constraints (? (seq (#\[
64 (constraints (list (:min 1)
69 (fragment (parse-delimited-fragment scanner #\{ #\})))
70 (add-to-module *module*
71 (make-instance 'code-fragment-item
73 :constraints constraints
80 (defun read-module (pathname &key (truename nil truep) location)
81 "Parse the file at PATHNAME as a module, returning it.
83 This is the main entry point for parsing module files. You may well know
84 the file's TRUENAME already (e.g., because `probe-file' dropped it into
85 your lap) so you can avoid repeating the search by providing it.
87 The LOCATION is the thing which wanted the module imported -- usually a
88 `file-location' object, though it might be anything other than `t' which
89 can be printed in the event of circular imports."
91 (setf pathname (merge-pathnames pathname
92 (make-pathname :type "SOD" :case :common)))
93 (unless truep (setf truename (truename pathname)))
94 (define-module (pathname :location location :truename truename)
95 (with-open-file (f-stream pathname :direction :input)
96 (let* ((*readtable* (copy-readtable))
97 (char-scanner (make-instance 'charbuf-scanner
99 (scanner (make-instance 'sod-token-scanner
100 :char-scanner char-scanner)))
101 (with-default-error-location (scanner)
102 (with-parser-context (token-scanner-context :scanner scanner)
104 (seq ((pset (parse-property-set scanner))
106 (plug module scanner pset))))
107 (check-unused-properties pset))))))))))
109 (define-pluggable-parser module test (scanner pset)
111 (declare (ignore pset))
112 (with-parser-context (token-scanner-context :scanner scanner)
113 (parse (seq ("demo" (string :string) #\;)
114 (format t ";; DEMO ~S~%" string)))))
116 (define-pluggable-parser module file (scanner pset)
117 ;; `import' string `;'
119 (declare (ignore pset))
120 (flet ((common (name type what thunk)
122 (merge-pathnames name
123 (make-pathname :type type
127 (with-parser-context (token-scanner-context :scanner scanner)
128 (parse (or (seq ("import" (name :string) #\;)
129 (common name "SOD" "module"
132 (let ((module (read-module path
135 (module-import module)
140 (cerror* "Error reading module ~S: ~A"
142 (seq ("load" (name :string) #\;)
143 (common name "LISP" "Lisp file"
146 (load true :verbose nil :print nil)
148 (cerror* "Error loading Lisp file ~S: ~A"
149 path error)))))))))))
153 (define-pluggable-parser module lisp (scanner pset)
154 ;; `lisp' s-expression `;'
155 (declare (ignore pset))
156 (with-parser-context (token-scanner-context :scanner scanner)
157 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
158 (string= (token-value scanner) "lisp"))
159 (let* ((stream (make-scanner-stream scanner))
160 (sexp (read stream t)))
161 (scanner-step scanner)
163 (values '((:id "lisp")) nil nil)))
167 ;;;--------------------------------------------------------------------------
168 ;;; Class declarations.
172 (defun parse-class-body (scanner pset name supers)
173 ;; class-body ::= `{' class-item* `}'
175 ;; class-item ::= property-set raw-class-item
176 (with-parser-context (token-scanner-context :scanner scanner)
177 (make-class-type name)
178 (let* ((class (make-sod-class name (mapcar #'find-sod-class supers)
180 (nick (sod-class-nickname class)))
182 (labels ((parse-maybe-dotted-declarator (base-type)
183 ;; Parse a declarator or dotted-declarator, i.e., one whose
186 ;; maybe-dotted-identifier ::= [id `.'] id
188 ;; A plain identifier is returned as a string, as usual; a
189 ;; dotted identifier is returned as a cons cell of the two
195 (name-b (? (seq (#\. (id :id)) id))))
196 (if name-b (cons name-a name-b)
199 (parse-message-item (sub-pset type name)
201 ;; declspec+ declarator -!- (method-body | `;')
203 ;; Don't allow a method-body here if the message takes a
204 ;; varargs list, because we don't have a name for the
205 ;; `va_list' parameter.
206 (let ((message (make-sod-message class name type
208 (if (varargs-message-p message)
210 (parse (or #\; (parse-method-item sub-pset
213 (parse-method-item (sub-pset type sub-nick name)
215 ;; declspec+ dotted-declarator -!- method-body
217 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
218 (parse (seq ((body (or (seq ("extern" #\;) nil)
219 (parse-delimited-fragment
221 (make-sod-method class sub-nick name type
222 body sub-pset scanner))))
224 (parse-initializer ()
225 ;; initializer ::= `=' c-fragment | `=' `{' c-fragment `}'
227 ;; Return (VALUE-KIND . VALUE-FORM), ready for passing to a
228 ;; `sod-initializer' constructor.
230 ;; This is kind of tricky because we have to juggle both
231 ;; layers of the parsing machinery. The character scanner
232 ;; will already have consumed the lookahead token (which, if
233 ;; we're going to do anything, is `=').
234 (let ((char-scanner (token-scanner-char-scanner scanner)))
236 ;; First, skip the character-scanner past any whitespace.
237 ;; We don't record this consumption, which is a bit
238 ;; naughty, but nobody will actually mind.
240 (when (or (scanner-at-eof-p char-scanner)
241 (not (whitespace-char-p
242 (scanner-current-char char-scanner))))
244 (scanner-step char-scanner))
246 ;; Now maybe read an initializer.
247 (cond ((not (eql (token-type scanner) #\=))
248 ;; It's not an `=' after all. There's no
250 (values '(#\=) nil nil))
252 ((and (not (scanner-at-eof-p char-scanner))
253 (char= (scanner-current-char char-scanner)
255 ;; There's a brace after the `=', so we should
256 ;; consume the `=' here, and read a compound
257 ;; initializer enclosed in braces.
258 (parse (seq (#\= (frag (parse-delimited-fragment
260 (cons :compound frag))))
263 ;; No brace, so read from the `=' up to, but not
264 ;; including, the trailing `,' or `;' delimiter.
265 (parse (seq ((frag (parse-delimited-fragment
266 scanner #\= '(#\; #\,)
268 (cons :simple frag)))))))
270 (parse-slot-item (sub-pset base-type type name)
272 ;; declspec+ declarator -!- [initializer]
273 ;; [`,' init-declarator-list] `;'
275 ;; init-declarator-list ::=
276 ;; declarator [initializer] [`,' init-declarator-list]
277 (parse (and (seq ((init (? (parse-initializer))))
278 (make-sod-slot class name type
281 (make-sod-instance-initializer
282 class nick name (car init) (cdr init)
286 (ds (parse-declarator scanner
288 (init (? (parse-initializer))))
289 (make-sod-slot class (cdr ds) (car ds)
292 (make-sod-instance-initializer
294 (car init) (cdr init)
298 (parse-initializer-item (sub-pset constructor)
299 ;; initializer-item ::=
300 ;; [`class'] -!- slot-initializer-list `;'
302 ;; slot-initializer ::= id `.' id initializer
303 (parse (and (skip-many ()
304 (seq ((name-a :id) #\. (name-b :id)
305 (init (parse-initializer)))
306 (funcall constructor class
308 (car init) (cdr init)
313 (class-item-dispatch (sub-pset base-type type name)
314 ;; Logically part of `parse-raw-class-item', but the
315 ;; indentation was getting crazy. We're currently at
317 ;; raw-class-item ::=
318 ;; declspec+ (declarator | dotted-declarator) -!- ...
321 ;; If the declarator is dotted then this must be a method
322 ;; definition; otherwise it might be a message or slot.
323 (cond ((not (typep type 'c-function-type))
325 (cerror*-with-location
327 "Method declarations must have function type.")
328 (setf name (cdr name)))
329 (parse-slot-item sub-pset base-type type name))
331 (parse-method-item sub-pset type
332 (car name) (cdr name)))
334 (parse-message-item sub-pset type name))))
336 (parse-raw-class-item (sub-pset)
337 ;; raw-class-item ::=
341 ;; | initializer-item
343 ;; Most of the above begin with declspecs and a declarator
344 ;; (which might be dotted). So we parse that here and
345 ;; dispatch based on what we find.
346 (parse (or (plug class-item scanner class sub-pset)
348 (seq ((ds (parse-c-type scanner))
349 (dc (parse-maybe-dotted-declarator ds))
350 (nil (class-item-dispatch sub-pset
355 (parse-initializer-item
357 #'make-sod-class-initializer))
358 (parse-initializer-item
360 #'make-sod-instance-initializer)))))
364 (seq ((sub-pset (parse-property-set scanner))
365 (nil (parse-raw-class-item sub-pset)))
366 (check-unused-properties sub-pset))))
367 (nil (error () #\})))
368 (finalize-sod-class class)
369 (add-to-module *module* class)))))))
371 (define-pluggable-parser module class (scanner pset)
372 ;; `class' id [`:' id-list] class-body
374 (with-parser-context (token-scanner-context :scanner scanner)
378 (make-class-type name))
379 (seq ((supers (? (seq (#\: (ids (list () :id #\,)))
381 (nil (parse-class-body
383 pset name supers)))))))))))
385 ;;;----- That's all, folks --------------------------------------------------