chiark / gitweb /
test/bad.sod: Test error reporting and recovery.
[sod] / src / module-parse.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Top-level parser for module syntax
4 ;;;
5 ;;; (c) 2010 Straylight/Edgeware
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This file is part of the Sensible Object Design, an object system for C.
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
31 ;;; Type names.
32
33 (define-pluggable-parser module typename (scanner pset)
34   ;; `typename' list[id] `;'
35   (declare (ignore pset))
36   (with-parser-context (token-scanner-context :scanner scanner)
37     (parse (and "typename"
38                 (skip-many ()
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 () #\, #\;))
48                   #\,)
49                 (must #\;)))))
50
51 ;;; Fragments.
52
53 (define-pluggable-parser module code (scanner pset)
54   ;; `code' id `:' item-name [constraints] `{' c-fragment `}'
55   ;;
56   ;; constraints ::= `[' list[constraint] `]'
57   ;; constraint ::= item-name+
58   ;; item-name ::= id | `(' id+ `)'
59   (declare (ignore pset))
60   (with-parser-context (token-scanner-context :scanner scanner)
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)))))
68       (parse (seq ("code"
69                    (reason (must (kw)))
70                    (nil (must #\:))
71                    (name (must (item)))
72                    (constraints (? (seq (#\[
73                                          (constraints
74                                           (list ()
75                                             (list (:min 1)
76                                               (error (:ignore-unconsumed t)
77                                                   (item)
78                                                 (skip-until ()
79                                                   :id #\( #\, #\])))
80                                             #\,))
81                                          #\])
82                                      constraints)))
83                    (fragment (parse-delimited-fragment scanner #\{ #\})))
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))))))))
91
92 ;;; External files.
93
94 (export 'read-module)
95 (defun read-module (pathname &key (truename nil truep) location)
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
106   (setf pathname (merge-pathnames pathname
107                                   (make-pathname :type "SOD" :case :common)))
108   (unless truep (setf truename (truename pathname)))
109   (define-module (pathname :location location :truename truename)
110     (with-open-file (f-stream pathname :direction :input)
111       (let* ((*readtable* (copy-readtable))
112              (*package* (find-package '#:sod-user))
113              (char-scanner (make-instance 'charbuf-scanner
114                                           :stream f-stream
115                                           :filename (namestring pathname)))
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)
120             (multiple-value-bind (result winp consumedp)
121                 (parse (skip-many ()
122                           (seq ((pset (parse-property-set scanner))
123                                 (nil (error ()
124                                          (plug module scanner pset)
125                                        (skip-until (:keep-end nil)
126                                          #\; #\}))))
127                             (check-unused-properties pset))))
128               (declare (ignore consumedp))
129               (unless winp (syntax-error scanner result)))))))))
130
131 (define-pluggable-parser module file (scanner pset)
132   ;; `import' string `;'
133   ;; `load' string `;'
134   (declare (ignore pset))
135   (flet ((common (name type what thunk)
136            (when name
137              (find-file (pathname (scanner-filename scanner))
138                         (merge-pathnames name
139                                          (make-pathname :type type
140                                                         :case :common))
141                         what
142                         thunk))))
143     (with-parser-context (token-scanner-context :scanner scanner)
144       (parse (or (seq ("import" (name (must :string)) (nil (must #\;)))
145                    (common name "SOD" "module"
146                            (lambda (path true)
147                              (handler-case
148                                  (let ((module (read-module path
149                                                             :truename true)))
150                                    (when module
151                                      (module-import module)
152                                      (pushnew path (module-files *module*))
153                                      (pushnew module
154                                               (module-dependencies
155                                                *module*))))
156                                (file-error (error)
157                                  (cerror* "Error reading module ~S: ~A"
158                                           path error))
159                                (error (error)
160                                  (cerror* "Unexpected error reading ~
161                                            module ~S: ~A"
162                                           path error))))))
163                  (seq ("load" (name (must :string)) (nil (must #\;)))
164                    (common name "LISP" "Lisp file"
165                            (lambda (path true)
166                              (handler-case
167                                  (progn
168                                    (pushnew path (module-files *module*))
169                                    (load true :verbose nil :print nil))
170                                (error (error)
171                                  (cerror* "Error loading Lisp file ~S: ~A"
172                                           path error)))))))))))
173
174 ;;; Setting properties.
175
176 (define-pluggable-parser module set (scanner pset)
177   ;; `set' list[property] `;'
178   (with-parser-context (token-scanner-context :scanner scanner)
179     (parse (and "set"
180                 (lisp (let ((module-pset (module-pset *module*)))
181                         (when pset
182                           (pset-map (lambda (prop)
183                                       (add-property
184                                        module-pset
185                                        (p-name prop) (p-value prop)
186                                        :type (p-type prop)
187                                        :location (p-location prop))
188                                       (setf (p-seenp prop) t))
189                                     pset))
190                         (parse (skip-many (:min (if pset 0 1))
191                                  (error (:ignore-unconsumed t)
192                                      (parse-property scanner module-pset)
193                                    (skip-until () #\, #\;))
194                                  #\,))))
195                 #\;))))
196
197 ;;; Lisp escape.
198
199 (define-pluggable-parser module lisp (scanner pset)
200   ;; `lisp' s-expression `;'
201   (declare (ignore pset))
202   (with-parser-context (token-scanner-context :scanner scanner)
203     (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
204                                 (string= (token-value scanner) "lisp"))
205                            (let* ((stream (make-scanner-stream scanner))
206                                   (sexp (read stream t)))
207                              (scanner-step scanner)
208                              (values sexp t t))
209                            (values '((:id "lisp")) nil nil)))
210                  (nil (must #\;)))
211              (eval sexp)))))
212
213 ;;;--------------------------------------------------------------------------
214 ;;; Class declarations.
215
216 (export 'class-item)
217
218 (define-pluggable-parser class-item initfrags (scanner class pset)
219   ;; raw-class-item ::= frag-keyword `{' c-fragment `}'
220   ;; frag-keyword ::= `init' | `teardown'
221   (with-parser-context (token-scanner-context :scanner scanner)
222     (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag)
223                            (seq ("teardown") #'make-sod-class-tearfrag)))
224                  (frag (parse-delimited-fragment scanner #\{ #\})))
225              (funcall make class frag pset :location scanner)))))
226
227 (define-pluggable-parser class-item initargs (scanner class pset)
228   ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
229   ;; init-declarator ::= declarator [`=' initializer]
230   (with-parser-context (token-scanner-context :scanner scanner)
231     (parse (seq ("initarg"
232                  (base-type (parse-c-type scanner))
233                  (nil (skip-many (:min 1)
234                         (seq ((declarator (parse-declarator scanner
235                                                             base-type))
236                               (init (? (parse-delimited-fragment
237                                         scanner #\= (list #\; #\,)
238                                         :keep-end t))))
239                           (make-sod-user-initarg class
240                                                  (cdr declarator)
241                                                  (car declarator)
242                                                  pset
243                                                  :default init
244                                                  :location scanner))
245                         #\,))
246                  (nil (must #\;)))))))
247
248 (defun synthetic-name ()
249   "Return an obviously bogus synthetic not-identifier."
250   (let ((ix *temporary-index*))
251     (incf *temporary-index*)
252     (make-instance 'temporary-variable :tag (format nil "%%#~A" ix))))
253
254 (defun parse-class-body (scanner pset name supers)
255   ;; class-body ::= `{' class-item* `}'
256   ;;
257   ;; class-item ::= property-set raw-class-item
258   (with-parser-context (token-scanner-context :scanner scanner)
259     (when name (make-class-type name))
260     (let* ((duff (null name))
261            (superclasses
262             (let ((superclasses (restart-case
263                                     (mapcar #'find-sod-class
264                                             (or supers (list "SodObject")))
265                                   (continue ()
266                                     (setf duff t)
267                                     (list (find-sod-class "SodObject"))))))
268               (find-duplicates (lambda (first second)
269                                  (declare (ignore second))
270                                  (setf duff t)
271                                  (cerror* "Class `~A' has duplicate ~
272                                            direct superclass `~A'"
273                                           name first))
274                                superclasses)
275               (delete-duplicates superclasses)))
276            (synthetic-name (or name
277                                (let ((var (synthetic-name)))
278                                  (unless pset
279                                    (setf pset (make-property-set)))
280                                  (unless (pset-get pset "nick")
281                                    (add-property pset "nick" var :type :id))
282                                  var)))
283            (class (make-sod-class synthetic-name superclasses pset
284                                   :location scanner))
285            (nick (sod-class-nickname class)))
286
287       (labels ((must-id ()
288                  (parse (must :id (progn (setf duff t) (synthetic-name)))))
289
290                (parse-maybe-dotted-name ()
291                  ;; maybe-dotted-name ::= [id `.'] id
292                  ;;
293                  ;; A plain identifier is returned as a string, as usual; a
294                  ;; dotted identifier is returned as a cons cell of the two
295                  ;; names.
296                  (parse (seq ((name-a (must-id))
297                               (name-b (? (seq (#\. (id (must-id))) id))))
298                           (if name-b (cons name-a name-b)
299                               name-a))))
300
301                (parse-maybe-dotted-declarator (base-type)
302                  ;; Parse a declarator or dotted-declarator, i.e., one whose
303                  ;; centre is maybe-dotted-name above.
304                  (parse-declarator scanner base-type
305                                    :keywordp t
306                                    :kernel #'parse-maybe-dotted-name))
307
308                (parse-message-item (sub-pset type name)
309                  ;; message-item ::=
310                  ;;     declspec+ declarator -!- (method-body | `;')
311                  ;;
312                  ;; Don't allow a method-body here if the message takes a
313                  ;; varargs list, because we don't have a name for the
314                  ;; `va_list' parameter.
315                  (let ((message (make-sod-message class name type sub-pset
316                                                   :location scanner)))
317                    (if (varargs-message-p message)
318                        (parse #\;)
319                        (parse (or #\; (parse-method-item sub-pset
320                                                          type nick name))))))
321
322                (parse-method-item (sub-pset type sub-nick name)
323                  ;; method-item ::=
324                  ;;     declspec+ dotted-declarator -!- method-body
325                  ;;
326                  ;; method-body ::= `{' c-fragment `}' | `extern' `;'
327                  (parse (seq ((body (or (seq ("extern" #\;) nil)
328                                         (parse-delimited-fragment
329                                          scanner #\{ #\}))))
330                           (restart-case
331                               (make-sod-method class sub-nick name type
332                                                body sub-pset
333                                                :location scanner)
334                             (continue () :report "Continue")))))
335
336                (parse-initializer ()
337                  ;; initializer ::= `=' c-fragment
338                  ;;
339                  ;; Return a VALUE, ready for passing to a `sod-initializer'
340                  ;; constructor.
341                  (parse-delimited-fragment scanner #\= '(#\, #\;)
342                                            :keep-end t))
343
344                (parse-slot-item (sub-pset base-type type name)
345                  ;; slot-item ::=
346                  ;;     declspec+ declarator -!- [initializer]
347                  ;;             [`,' list[init-declarator]] `;'
348                  ;;
349                  ;; init-declarator ::= declarator [initializer]
350                  (flet ((make-it (name type init)
351                           (restart-case
352                               (progn
353                                 (make-sod-slot class name type sub-pset
354                                                :location scanner)
355                                 (when init
356                                   (make-sod-instance-initializer
357                                    class nick name init sub-pset
358                                    :location scanner)))
359                             (continue () :report "Continue"))))
360                    (parse (and (error ()
361                                    (seq ((init (? (parse-initializer))))
362                                      (make-it name type init))
363                                  (skip-until () #\, #\;))
364                                (skip-many ()
365                                  (error (:ignore-unconsumed t)
366                                      (seq (#\,
367                                            (ds (parse-declarator scanner
368                                                                  base-type))
369                                            (init (? (parse-initializer))))
370                                        (make-it (cdr ds) (car ds) init))
371                                    (skip-until () #\, #\;)))
372                                (must #\;)))))
373
374                (parse-initializer-item (sub-pset must-init-p constructor)
375                  ;; initializer-item ::=
376                  ;;     [`class'] -!- list[slot-initializer] `;'
377                  ;;
378                  ;; slot-initializer ::= id `.' id [initializer]
379                  (let ((parse-init (if must-init-p #'parse-initializer
380                                        (parser () (? (parse-initializer))))))
381                    (parse (and (skip-many ()
382                                  (error (:ignore-unconsumed t)
383                                      (seq ((name-a :id) #\.
384                                            (name-b (must-id))
385                                            (init (funcall parse-init)))
386                                        (restart-case
387                                            (funcall constructor class
388                                                     name-a name-b init
389                                                     sub-pset
390                                                     :location scanner)
391                                          (continue () :report "Continue")))
392                                    (skip-until () #\, #\;))
393                                  #\,)
394                                (must #\;)))))
395
396                (class-item-dispatch (sub-pset base-type type name)
397                  ;; Logically part of `parse-raw-class-item', but the
398                  ;; indentation was getting crazy.  We're currently at
399                  ;;
400                  ;; raw-class-item ::=
401                  ;;     declspec+ (declarator | dotted-declarator) -!- ...
402                  ;;   | other-items
403                  ;;
404                  ;; If the declarator is dotted then this must be a method
405                  ;; definition; otherwise it might be a message or slot.
406                  (cond ((not (typep type 'c-function-type))
407                         (when (consp name)
408                           (cerror*
409                            "Method declarations must have function type")
410                           (setf name (cdr name)))
411                         (parse-slot-item sub-pset base-type type name))
412                        ((consp name)
413                         (parse-method-item sub-pset type
414                                            (car name) (cdr name)))
415                        (t
416                         (parse-message-item sub-pset type name))))
417
418                (parse-raw-class-item (sub-pset)
419                  ;; raw-class-item ::=
420                  ;;     message-item
421                  ;;   | method-item
422                  ;;   | slot-item
423                  ;;   | initializer-item
424                  ;;   | initfrag-item
425                  ;;
426                  ;; Most of the above begin with declspecs and a declarator
427                  ;; (which might be dotted).  So we parse that here and
428                  ;; dispatch based on what we find.
429                  (parse (or (plug class-item scanner class sub-pset)
430                             (peek
431                              (seq ((ds (parse-c-type scanner))
432                                    (dc (parse-maybe-dotted-declarator ds))
433                                    (nil (commit))
434                                    (nil (class-item-dispatch sub-pset
435                                                              ds
436                                                              (car dc)
437                                                              (cdr dc))))))
438                             (and "class"
439                                  (parse-initializer-item sub-pset t
440                                   #'make-sod-class-initializer))
441                             (parse-initializer-item sub-pset nil
442                              #'make-sod-instance-initializer)))))
443
444         (parse (seq ((nil (must #\{))
445                      (nil (skip-many ()
446                             (seq ((sub-pset (parse-property-set scanner))
447                                   (nil (parse-raw-class-item sub-pset)))
448                               (check-unused-properties sub-pset))))
449                      (nil (must #\})))
450                  (unless (finalize-sod-class class)
451                    (setf duff t))
452                  (unless duff
453                    (add-to-module *module* class))))))))
454
455 (define-pluggable-parser module class (scanner pset)
456   ;; `class' id `:' list[id] class-body
457   ;; `class' id `;'
458   (with-parser-context (token-scanner-context :scanner scanner)
459     (parse (seq ("class"
460                  (name (must :id))
461                  (nil (or (seq (#\;)
462                             (when name (make-class-type name)))
463                           (seq ((supers (must (seq (#\:
464                                                     (ids (list () :id #\,)))
465                                                 ids)))
466                                 (nil (parse-class-body
467                                       scanner
468                                       pset name supers)))))))))))
469
470 ;;;----- That's all, folks --------------------------------------------------