src/module-parse.lisp: Improve error recovery for `typename' items.
[sod] / src / module-parse.lisp
CommitLineData
bf090e02
MW
1;;; -*-lisp-*-
2;;;
3;;; Top-level parser for module syntax
4;;;
5;;; (c) 2010 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
e0808c47 10;;; This file is part of the Sensible Object Design, an object system for C.
bf090e02
MW
11;;;
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.
16;;;
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.
21;;;
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.
25
26(in-package #:sod)
27
28;;;--------------------------------------------------------------------------
29;;; Toplevel syntax.
30
bf090e02
MW
31;;; Type names.
32
048d0b2d 33(define-pluggable-parser module typename (scanner pset)
ceed01af 34 ;; `typename' list[id] `;'
048d0b2d 35 (declare (ignore pset))
bf090e02
MW
36 (with-parser-context (token-scanner-context :scanner scanner)
37 (parse (and "typename"
65eebc3b 38 (skip-many ()
c41a95b1
MW
39 (error ()
40 (seq ((id :id))
41 (if (or (gethash id *module-type-map*)
42 (find-simple-c-type id))
43 (cerror* "Type `~A' already defined" id)
44 (add-to-module *module*
45 (make-instance 'type-item
46 :name id))))
47 (skip-until () #\, #\;))
bf090e02 48 #\,)
c41a95b1 49 (must #\;)))))
bf090e02
MW
50
51;;; Fragments.
52
048d0b2d 53(define-pluggable-parser module code (scanner pset)
4fc52153 54 ;; `code' id `:' item-name [constraints] `{' c-fragment `}'
048d0b2d 55 ;;
ceed01af 56 ;; constraints ::= `[' list[constraint] `]'
4fc52153
MW
57 ;; constraint ::= item-name+
58 ;; item-name ::= id | `(' id+ `)'
048d0b2d 59 (declare (ignore pset))
bf090e02 60 (with-parser-context (token-scanner-context :scanner scanner)
4fc52153
MW
61 (labels ((kw ()
62 (parse (seq ((kw :id))
63 (intern (frob-identifier kw) 'keyword))))
64 (item ()
65 (parse (or (kw)
66 (seq (#\( (names (list (:min 1) (kw))) #\))
67 names)))))
9ec578d9
MW
68 (parse (seq ("code"
69 (reason (kw))
70 #\:
4fc52153 71 (name (item))
9ec578d9
MW
72 (constraints (? (seq (#\[
73 (constraints (list (:min 1)
4fc52153
MW
74 (list (:min 1)
75 (item))
9ec578d9
MW
76 #\,))
77 #\])
78 constraints)))
79 (fragment (parse-delimited-fragment scanner #\{ #\})))
80 (add-to-module *module*
81 (make-instance 'code-fragment-item
82 :fragment fragment
83 :constraints constraints
84 :reason reason
85 :name name)))))))
bf090e02
MW
86
87;;; External files.
88
9ec578d9
MW
89(export 'read-module)
90(defun read-module (pathname &key (truename nil truep) location)
bf090e02
MW
91 "Parse the file at PATHNAME as a module, returning it.
92
93 This is the main entry point for parsing module files. You may well know
94 the file's TRUENAME already (e.g., because `probe-file' dropped it into
95 your lap) so you can avoid repeating the search by providing it.
96
97 The LOCATION is the thing which wanted the module imported -- usually a
98 `file-location' object, though it might be anything other than `t' which
99 can be printed in the event of circular imports."
100
9ec578d9
MW
101 (setf pathname (merge-pathnames pathname
102 (make-pathname :type "SOD" :case :common)))
103 (unless truep (setf truename (truename pathname)))
bf090e02
MW
104 (define-module (pathname :location location :truename truename)
105 (with-open-file (f-stream pathname :direction :input)
106 (let* ((*readtable* (copy-readtable))
65e5cd24 107 (*package* (find-package '#:sod-user))
bf090e02 108 (char-scanner (make-instance 'charbuf-scanner
e783e65b
MW
109 :stream f-stream
110 :filename (namestring pathname)))
bf090e02
MW
111 (scanner (make-instance 'sod-token-scanner
112 :char-scanner char-scanner)))
113 (with-default-error-location (scanner)
114 (with-parser-context (token-scanner-context :scanner scanner)
300a3f0a
MW
115 (multiple-value-bind (result winp consumedp)
116 (parse (skip-many ()
117 (seq ((pset (parse-property-set scanner))
118 (nil (error ()
a8bc7831
MW
119 (plug module scanner pset)
120 (skip-until (:keep-end nil)
121 #\; #\}))))
300a3f0a
MW
122 (check-unused-properties pset))))
123 (declare (ignore consumedp))
124 (unless winp (syntax-error scanner result)))))))))
048d0b2d
MW
125
126(define-pluggable-parser module test (scanner pset)
127 ;; `demo' string `;'
128 (declare (ignore pset))
bf090e02
MW
129 (with-parser-context (token-scanner-context :scanner scanner)
130 (parse (seq ("demo" (string :string) #\;)
131 (format t ";; DEMO ~S~%" string)))))
132
048d0b2d
MW
133(define-pluggable-parser module file (scanner pset)
134 ;; `import' string `;'
135 ;; `load' string `;'
136 (declare (ignore pset))
bf090e02
MW
137 (flet ((common (name type what thunk)
138 (find-file scanner
139 (merge-pathnames name
140 (make-pathname :type type
141 :case :common))
142 what
143 thunk)))
144 (with-parser-context (token-scanner-context :scanner scanner)
145 (parse (or (seq ("import" (name :string) #\;)
146 (common name "SOD" "module"
147 (lambda (path true)
148 (handler-case
149 (let ((module (read-module path
150 :truename true)))
151 (when module
152 (module-import module)
153 (pushnew module
154 (module-dependencies
155 *module*))))
156 (file-error (error)
157 (cerror* "Error reading module ~S: ~A"
158 path error))))))
159 (seq ("load" (name :string) #\;)
160 (common name "LISP" "Lisp file"
161 (lambda (path true)
162 (handler-case
163 (load true :verbose nil :print nil)
164 (error (error)
165 (cerror* "Error loading Lisp file ~S: ~A"
166 path error)))))))))))
167
01e3faf9
MW
168;;; Setting properties.
169
170(define-pluggable-parser module set (scanner pset)
ceed01af 171 ;; `set' list[property] `;'
01e3faf9
MW
172 (with-parser-context (token-scanner-context :scanner scanner)
173 (parse (and "set"
174 (lisp (let ((module-pset (module-pset *module*)))
175 (when pset
176 (pset-map (lambda (prop)
5445420e
MW
177 (add-property
178 module-pset
179 (p-name prop) (p-value prop)
180 :type (p-type prop)
181 :location (p-location prop))
01e3faf9
MW
182 (setf (p-seenp prop) t))
183 pset))
99a74df1 184 (parse (skip-many (:min (if pset 0 1))
01e3faf9 185 (error (:ignore-unconsumed t)
5445420e 186 (parse-property scanner module-pset)
65eebc3b 187 (skip-until () #\, #\;))
01e3faf9
MW
188 #\,))))
189 #\;))))
190
bf090e02
MW
191;;; Lisp escape.
192
048d0b2d 193(define-pluggable-parser module lisp (scanner pset)
bf090e02 194 ;; `lisp' s-expression `;'
048d0b2d 195 (declare (ignore pset))
bf090e02
MW
196 (with-parser-context (token-scanner-context :scanner scanner)
197 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
198 (string= (token-value scanner) "lisp"))
199 (let* ((stream (make-scanner-stream scanner))
200 (sexp (read stream t)))
201 (scanner-step scanner)
202 (values sexp t t))
203 (values '((:id "lisp")) nil nil)))
204 #\;)
205 (eval sexp)))))
206
207;;;--------------------------------------------------------------------------
208;;; Class declarations.
209
7f2917d2
MW
210(export 'class-item)
211
a42893dd
MW
212(define-pluggable-parser class-item initfrags (scanner class pset)
213 ;; raw-class-item ::= frag-keyword `{' c-fragment `}'
214 ;; frag-keyword ::= `init' | `teardown'
215 (with-parser-context (token-scanner-context :scanner scanner)
216 (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag)
217 (seq ("teardown") #'make-sod-class-tearfrag)))
218 (frag (parse-delimited-fragment scanner #\{ #\})))
219 (funcall make class frag pset scanner)))))
220
b2983f35 221(define-pluggable-parser class-item initargs (scanner class pset)
ceed01af 222 ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
b2983f35
MW
223 ;; init-declarator ::= declarator [`=' initializer]
224 (with-parser-context (token-scanner-context :scanner scanner)
225 (parse (seq ("initarg"
226 (base-type (parse-c-type scanner))
227 (nil (skip-many (:min 1)
228 (seq ((declarator (parse-declarator scanner
229 base-type))
230 (init (? (parse-delimited-fragment
231 scanner #\= (list #\; #\,)
232 :keep-end t))))
233 (make-sod-user-initarg class
234 (cdr declarator)
235 (car declarator)
236 pset init scanner))
237 #\,))
238 #\;)))))
239
048d0b2d 240(defun parse-class-body (scanner pset name supers)
c91b90c3 241 ;; class-body ::= `{' class-item* `}'
048d0b2d
MW
242 ;;
243 ;; class-item ::= property-set raw-class-item
c91b90c3
MW
244 (with-parser-context (token-scanner-context :scanner scanner)
245 (make-class-type name)
70b33a78
MW
246 (let* ((duff nil)
247 (class (make-sod-class name
248 (restart-case
249 (mapcar #'find-sod-class supers)
250 (continue ()
251 (setf duff t)
252 (list (find-sod-class "SodObject"))))
c91b90c3
MW
253 pset scanner))
254 (nick (sod-class-nickname class)))
255
256 (labels ((parse-maybe-dotted-declarator (base-type)
257 ;; Parse a declarator or dotted-declarator, i.e., one whose
258 ;; centre is
259 ;;
260 ;; maybe-dotted-identifier ::= [id `.'] id
261 ;;
262 ;; A plain identifier is returned as a string, as usual; a
263 ;; dotted identifier is returned as a cons cell of the two
264 ;; names.
265 (parse-declarator
266 scanner base-type
43073476 267 :keywordp t
ea578bb4 268 :kernel (parser ()
c91b90c3
MW
269 (seq ((name-a :id)
270 (name-b (? (seq (#\. (id :id)) id))))
271 (if name-b (cons name-a name-b)
272 name-a)))))
273
c91b90c3
MW
274 (parse-message-item (sub-pset type name)
275 ;; message-item ::=
276 ;; declspec+ declarator -!- (method-body | `;')
2cbdee3d
MW
277 ;;
278 ;; Don't allow a method-body here if the message takes a
279 ;; varargs list, because we don't have a name for the
280 ;; `va_list' parameter.
281 (let ((message (make-sod-message class name type
282 sub-pset scanner)))
283 (if (varargs-message-p message)
284 (parse #\;)
285 (parse (or #\; (parse-method-item sub-pset
286 type nick name))))))
c91b90c3
MW
287
288 (parse-method-item (sub-pset type sub-nick name)
289 ;; method-item ::=
290 ;; declspec+ dotted-declarator -!- method-body
291 ;;
292 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
293 (parse (seq ((body (or (seq ("extern" #\;) nil)
294 (parse-delimited-fragment
295 scanner #\{ #\}))))
296 (make-sod-method class sub-nick name type
297 body sub-pset scanner))))
298
299 (parse-initializer ()
a888e3ac 300 ;; initializer ::= `=' c-fragment
c91b90c3 301 ;;
a888e3ac
MW
302 ;; Return a VALUE, ready for passing to a `sod-initializer'
303 ;; constructor.
304 (parse-delimited-fragment scanner #\= (list #\, #\;)
305 :keep-end t))
c91b90c3
MW
306
307 (parse-slot-item (sub-pset base-type type name)
308 ;; slot-item ::=
309 ;; declspec+ declarator -!- [initializer]
ceed01af 310 ;; [`,' list[init-declarator]] `;'
c91b90c3 311 ;;
ceed01af 312 ;; init-declarator ::= declarator [initializer]
c91b90c3
MW
313 (parse (and (seq ((init (? (parse-initializer))))
314 (make-sod-slot class name type
315 sub-pset scanner)
316 (when init
317 (make-sod-instance-initializer
a888e3ac 318 class nick name init sub-pset scanner)))
c91b90c3
MW
319 (skip-many ()
320 (seq (#\,
321 (ds (parse-declarator scanner
322 base-type))
323 (init (? (parse-initializer))))
324 (make-sod-slot class (cdr ds) (car ds)
325 sub-pset scanner)
326 (when init
327 (make-sod-instance-initializer
a888e3ac 328 class nick (cdr ds) init
048d0b2d 329 sub-pset scanner))))
c91b90c3
MW
330 #\;)))
331
b2983f35 332 (parse-initializer-item (sub-pset must-init-p constructor)
c91b90c3 333 ;; initializer-item ::=
ceed01af 334 ;; [`class'] -!- list[slot-initializer] `;'
c91b90c3 335 ;;
b2983f35 336 ;; slot-initializer ::= id `.' id [initializer]
5445420e 337 (let ((parse-init (if must-init-p #'parse-initializer
b2983f35
MW
338 (parser () (? (parse-initializer))))))
339 (parse (and (skip-many ()
340 (seq ((name-a :id) #\. (name-b :id)
341 (init (funcall parse-init)))
342 (funcall constructor class
343 name-a name-b init
344 sub-pset scanner))
345 #\,)
346 #\;))))
c91b90c3
MW
347
348 (class-item-dispatch (sub-pset base-type type name)
349 ;; Logically part of `parse-raw-class-item', but the
350 ;; indentation was getting crazy. We're currently at
351 ;;
352 ;; raw-class-item ::=
353 ;; declspec+ (declarator | dotted-declarator) -!- ...
354 ;; | other-items
355 ;;
356 ;; If the declarator is dotted then this must be a method
357 ;; definition; otherwise it might be a message or slot.
358 (cond ((not (typep type 'c-function-type))
359 (when (consp name)
65eebc3b 360 (cerror*
a1985b3c 361 "Method declarations must have function type")
c91b90c3
MW
362 (setf name (cdr name)))
363 (parse-slot-item sub-pset base-type type name))
364 ((consp name)
365 (parse-method-item sub-pset type
366 (car name) (cdr name)))
367 (t
368 (parse-message-item sub-pset type name))))
369
370 (parse-raw-class-item (sub-pset)
371 ;; raw-class-item ::=
372 ;; message-item
373 ;; | method-item
374 ;; | slot-item
375 ;; | initializer-item
a42893dd 376 ;; | initfrag-item
c91b90c3
MW
377 ;;
378 ;; Most of the above begin with declspecs and a declarator
379 ;; (which might be dotted). So we parse that here and
380 ;; dispatch based on what we find.
048d0b2d 381 (parse (or (plug class-item scanner class sub-pset)
db2abd9d 382 (peek
c91b90c3
MW
383 (seq ((ds (parse-c-type scanner))
384 (dc (parse-maybe-dotted-declarator ds))
65b1d9d7 385 (nil (commit))
048d0b2d
MW
386 (nil (class-item-dispatch sub-pset
387 ds
388 (car dc)
db2abd9d 389 (cdr dc))))))
c91b90c3 390 (and "class"
5445420e 391 (parse-initializer-item sub-pset t
c91b90c3 392 #'make-sod-class-initializer))
5445420e 393 (parse-initializer-item sub-pset nil
c91b90c3
MW
394 #'make-sod-instance-initializer)))))
395
048d0b2d
MW
396 (parse (seq (#\{
397 (nil (skip-many ()
398 (seq ((sub-pset (parse-property-set scanner))
9ec578d9 399 (nil (parse-raw-class-item sub-pset)))
048d0b2d 400 (check-unused-properties sub-pset))))
9ec578d9 401 (nil (error () #\})))
e45a106d
MW
402 (unless (finalize-sod-class class)
403 (setf duff t))
70b33a78
MW
404 (unless duff
405 (add-to-module *module* class))))))))
048d0b2d
MW
406
407(define-pluggable-parser module class (scanner pset)
ceed01af 408 ;; `class' id `:' list[id] class-body
c91b90c3 409 ;; `class' id `;'
bf090e02 410 (with-parser-context (token-scanner-context :scanner scanner)
c91b90c3
MW
411 (parse (seq ("class"
412 (name :id)
413 (nil (or (seq (#\;)
414 (make-class-type name))
ffc2a7b8
MW
415 (seq ((supers (seq (#\: (ids (list () :id #\,)))
416 ids))
c91b90c3
MW
417 (nil (parse-class-body
418 scanner
419 pset name supers)))))))))))
420
bf090e02 421;;;----- That's all, folks --------------------------------------------------