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