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 (char-scanner (make-instance 'charbuf-scanner
106 (scanner (make-instance 'sod-token-scanner
107 :char-scanner char-scanner)))
108 (with-default-error-location (scanner)
109 (with-parser-context (token-scanner-context :scanner scanner)
111 (seq ((pset (parse-property-set scanner))
113 (plug module scanner pset))))
114 (check-unused-properties pset))))))))))
116 (define-pluggable-parser module test (scanner pset)
118 (declare (ignore pset))
119 (with-parser-context (token-scanner-context :scanner scanner)
120 (parse (seq ("demo" (string :string) #\;)
121 (format t ";; DEMO ~S~%" string)))))
123 (define-pluggable-parser module file (scanner pset)
124 ;; `import' string `;'
126 (declare (ignore pset))
127 (flet ((common (name type what thunk)
129 (merge-pathnames name
130 (make-pathname :type type
134 (with-parser-context (token-scanner-context :scanner scanner)
135 (parse (or (seq ("import" (name :string) #\;)
136 (common name "SOD" "module"
139 (let ((module (read-module path
142 (module-import module)
147 (cerror* "Error reading module ~S: ~A"
149 (seq ("load" (name :string) #\;)
150 (common name "LISP" "Lisp file"
153 (load true :verbose nil :print nil)
155 (cerror* "Error loading Lisp file ~S: ~A"
156 path error)))))))))))
158 ;;; Setting properties.
160 (define-pluggable-parser module set (scanner pset)
161 ;; `set' property-list `;'
162 (with-parser-context (token-scanner-context :scanner scanner)
164 (lisp (let ((module-pset (module-pset *module*)))
166 (pset-map (lambda (prop)
167 (add-property module-pset
171 :location (p-location prop))
172 (setf (p-seenp prop) t))
174 (parse (skip-many (:min 0)
175 (error (:ignore-unconsumed t)
176 (parse-property scanner module-pset)
177 (skip-until (:keep-end t) #\, #\;))
183 (define-pluggable-parser module lisp (scanner pset)
184 ;; `lisp' s-expression `;'
185 (declare (ignore pset))
186 (with-parser-context (token-scanner-context :scanner scanner)
187 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
188 (string= (token-value scanner) "lisp"))
189 (let* ((stream (make-scanner-stream scanner))
190 (sexp (read stream t)))
191 (scanner-step scanner)
193 (values '((:id "lisp")) nil nil)))
197 ;;;--------------------------------------------------------------------------
198 ;;; Class declarations.
202 (defun parse-class-body (scanner pset name supers)
203 ;; class-body ::= `{' class-item* `}'
205 ;; class-item ::= property-set raw-class-item
206 (with-parser-context (token-scanner-context :scanner scanner)
207 (make-class-type name)
208 (let* ((class (make-sod-class name (mapcar #'find-sod-class supers)
210 (nick (sod-class-nickname class)))
212 (labels ((parse-maybe-dotted-declarator (base-type)
213 ;; Parse a declarator or dotted-declarator, i.e., one whose
216 ;; maybe-dotted-identifier ::= [id `.'] id
218 ;; A plain identifier is returned as a string, as usual; a
219 ;; dotted identifier is returned as a cons cell of the two
225 (name-b (? (seq (#\. (id :id)) id))))
226 (if name-b (cons name-a name-b)
229 (parse-message-item (sub-pset type name)
231 ;; declspec+ declarator -!- (method-body | `;')
233 ;; Don't allow a method-body here if the message takes a
234 ;; varargs list, because we don't have a name for the
235 ;; `va_list' parameter.
236 (let ((message (make-sod-message class name type
238 (if (varargs-message-p message)
240 (parse (or #\; (parse-method-item sub-pset
243 (parse-method-item (sub-pset type sub-nick name)
245 ;; declspec+ dotted-declarator -!- method-body
247 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
248 (parse (seq ((body (or (seq ("extern" #\;) nil)
249 (parse-delimited-fragment
251 (make-sod-method class sub-nick name type
252 body sub-pset scanner))))
254 (parse-initializer ()
255 ;; initializer ::= `=' c-fragment | `=' `{' c-fragment `}'
257 ;; Return (VALUE-KIND . VALUE-FORM), ready for passing to a
258 ;; `sod-initializer' constructor.
260 ;; This is kind of tricky because we have to juggle both
261 ;; layers of the parsing machinery. The character scanner
262 ;; will already have consumed the lookahead token (which, if
263 ;; we're going to do anything, is `=').
264 (let ((char-scanner (token-scanner-char-scanner scanner)))
266 ;; First, skip the character-scanner past any whitespace.
267 ;; We don't record this consumption, which is a bit
268 ;; naughty, but nobody will actually mind.
270 (when (or (scanner-at-eof-p char-scanner)
271 (not (whitespace-char-p
272 (scanner-current-char char-scanner))))
274 (scanner-step char-scanner))
276 ;; Now maybe read an initializer.
277 (cond ((not (eql (token-type scanner) #\=))
278 ;; It's not an `=' after all. There's no
280 (values '(#\=) nil nil))
282 ((and (not (scanner-at-eof-p char-scanner))
283 (char= (scanner-current-char char-scanner)
285 ;; There's a brace after the `=', so we should
286 ;; consume the `=' here, and read a compound
287 ;; initializer enclosed in braces.
288 (parse (seq (#\= (frag (parse-delimited-fragment
290 (cons :compound frag))))
293 ;; No brace, so read from the `=' up to, but not
294 ;; including, the trailing `,' or `;' delimiter.
295 (parse (seq ((frag (parse-delimited-fragment
296 scanner #\= '(#\; #\,)
298 (cons :simple frag)))))))
300 (parse-slot-item (sub-pset base-type type name)
302 ;; declspec+ declarator -!- [initializer]
303 ;; [`,' init-declarator-list] `;'
305 ;; init-declarator-list ::=
306 ;; declarator [initializer] [`,' init-declarator-list]
307 (parse (and (seq ((init (? (parse-initializer))))
308 (make-sod-slot class name type
311 (make-sod-instance-initializer
312 class nick name (car init) (cdr init)
316 (ds (parse-declarator scanner
318 (init (? (parse-initializer))))
319 (make-sod-slot class (cdr ds) (car ds)
322 (make-sod-instance-initializer
324 (car init) (cdr init)
328 (parse-initializer-item (sub-pset constructor)
329 ;; initializer-item ::=
330 ;; [`class'] -!- slot-initializer-list `;'
332 ;; slot-initializer ::= id `.' id initializer
333 (parse (and (skip-many ()
334 (seq ((name-a :id) #\. (name-b :id)
335 (init (parse-initializer)))
336 (funcall constructor class
338 (car init) (cdr init)
343 (class-item-dispatch (sub-pset base-type type name)
344 ;; Logically part of `parse-raw-class-item', but the
345 ;; indentation was getting crazy. We're currently at
347 ;; raw-class-item ::=
348 ;; declspec+ (declarator | dotted-declarator) -!- ...
351 ;; If the declarator is dotted then this must be a method
352 ;; definition; otherwise it might be a message or slot.
353 (cond ((not (typep type 'c-function-type))
355 (cerror*-with-location
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
373 ;; Most of the above begin with declspecs and a declarator
374 ;; (which might be dotted). So we parse that here and
375 ;; dispatch based on what we find.
376 (parse (or (plug class-item scanner class sub-pset)
378 (seq ((ds (parse-c-type scanner))
379 (dc (parse-maybe-dotted-declarator ds))
380 (nil (class-item-dispatch sub-pset
385 (parse-initializer-item
387 #'make-sod-class-initializer))
388 (parse-initializer-item
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 (finalize-sod-class class)
399 (add-to-module *module* class)))))))
401 (define-pluggable-parser module class (scanner pset)
402 ;; `class' id [`:' id-list] class-body
404 (with-parser-context (token-scanner-context :scanner scanner)
408 (make-class-type name))
409 (seq ((supers (? (seq (#\: (ids (list () :id #\,)))
411 (nil (parse-class-body
413 pset name supers)))))))))))
415 ;;;----- That's all, folks --------------------------------------------------