chiark / gitweb /
src/module-parse.lisp: Improve error recovery for `load' and `import' items.
[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 test (scanner pset)
132   ;; `demo' string `;'
133   (declare (ignore pset))
134   (with-parser-context (token-scanner-context :scanner scanner)
135     (parse (seq ("demo" (string (must :string)) (nil (must #\;)))
136              (format t ";; DEMO ~S~%" string)))))
137
138 (define-pluggable-parser module file (scanner pset)
139   ;; `import' string `;'
140   ;; `load' string `;'
141   (declare (ignore pset))
142   (flet ((common (name type what thunk)
143            (when name
144              (find-file scanner
145                         (merge-pathnames name
146                                          (make-pathname :type type
147                                                         :case :common))
148                         what
149                         thunk))))
150     (with-parser-context (token-scanner-context :scanner scanner)
151       (parse (or (seq ("import" (name (must :string)) (nil (must #\;)))
152                    (common name "SOD" "module"
153                            (lambda (path true)
154                              (handler-case
155                                  (let ((module (read-module path
156                                                             :truename true)))
157                                    (when module
158                                      (module-import module)
159                                      (pushnew module
160                                               (module-dependencies
161                                                *module*))))
162                                (file-error (error)
163                                  (cerror* "Error reading module ~S: ~A"
164                                           path error))
165                                (error (error)
166                                  (cerror* "Unexpected error reading ~
167                                            module ~S: ~A"
168                                           path error))))))
169                  (seq ("load" (name (must :string)) (nil (must #\;)))
170                    (common name "LISP" "Lisp file"
171                            (lambda (path true)
172                              (handler-case
173                                  (load true :verbose nil :print nil)
174                                (error (error)
175                                  (cerror* "Error loading Lisp file ~S: ~A"
176                                           path error)))))))))))
177
178 ;;; Setting properties.
179
180 (define-pluggable-parser module set (scanner pset)
181   ;; `set' list[property] `;'
182   (with-parser-context (token-scanner-context :scanner scanner)
183     (parse (and "set"
184                 (lisp (let ((module-pset (module-pset *module*)))
185                         (when pset
186                           (pset-map (lambda (prop)
187                                       (add-property
188                                        module-pset
189                                        (p-name prop) (p-value prop)
190                                        :type (p-type prop)
191                                        :location (p-location prop))
192                                       (setf (p-seenp prop) t))
193                                     pset))
194                         (parse (skip-many (:min (if pset 0 1))
195                                  (error (:ignore-unconsumed t)
196                                      (parse-property scanner module-pset)
197                                    (skip-until () #\, #\;))
198                                  #\,))))
199                 #\;))))
200
201 ;;; Lisp escape.
202
203 (define-pluggable-parser module lisp (scanner pset)
204   ;; `lisp' s-expression `;'
205   (declare (ignore pset))
206   (with-parser-context (token-scanner-context :scanner scanner)
207     (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
208                                 (string= (token-value scanner) "lisp"))
209                            (let* ((stream (make-scanner-stream scanner))
210                                   (sexp (read stream t)))
211                              (scanner-step scanner)
212                              (values sexp t t))
213                            (values '((:id "lisp")) nil nil)))
214                  #\;)
215              (eval sexp)))))
216
217 ;;;--------------------------------------------------------------------------
218 ;;; Class declarations.
219
220 (export 'class-item)
221
222 (define-pluggable-parser class-item initfrags (scanner class pset)
223   ;; raw-class-item ::= frag-keyword `{' c-fragment `}'
224   ;; frag-keyword ::= `init' | `teardown'
225   (with-parser-context (token-scanner-context :scanner scanner)
226     (parse (seq ((make (or (seq ("init") #'make-sod-class-initfrag)
227                            (seq ("teardown") #'make-sod-class-tearfrag)))
228                  (frag (parse-delimited-fragment scanner #\{ #\})))
229              (funcall make class frag pset scanner)))))
230
231 (define-pluggable-parser class-item initargs (scanner class pset)
232   ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
233   ;; init-declarator ::= declarator [`=' initializer]
234   (with-parser-context (token-scanner-context :scanner scanner)
235     (parse (seq ("initarg"
236                  (base-type (parse-c-type scanner))
237                  (nil (skip-many (:min 1)
238                         (seq ((declarator (parse-declarator scanner
239                                                             base-type))
240                               (init (? (parse-delimited-fragment
241                                         scanner #\= (list #\; #\,)
242                                         :keep-end t))))
243                           (make-sod-user-initarg class
244                                                  (cdr declarator)
245                                                  (car declarator)
246                                                  pset init scanner))
247                         #\,))
248                    #\;)))))
249
250 (defun parse-class-body (scanner pset name supers)
251   ;; class-body ::= `{' class-item* `}'
252   ;;
253   ;; class-item ::= property-set raw-class-item
254   (with-parser-context (token-scanner-context :scanner scanner)
255     (make-class-type name)
256     (let* ((duff nil)
257            (class (make-sod-class name
258                                   (restart-case
259                                       (mapcar #'find-sod-class supers)
260                                     (continue ()
261                                       (setf duff t)
262                                       (list (find-sod-class "SodObject"))))
263                                   pset scanner))
264            (nick (sod-class-nickname class)))
265
266       (labels ((parse-maybe-dotted-declarator (base-type)
267                  ;; Parse a declarator or dotted-declarator, i.e., one whose
268                  ;; centre is
269                  ;;
270                  ;; maybe-dotted-identifier ::= [id `.'] id
271                  ;;
272                  ;; A plain identifier is returned as a string, as usual; a
273                  ;; dotted identifier is returned as a cons cell of the two
274                  ;; names.
275                  (parse-declarator
276                   scanner base-type
277                   :keywordp t
278                   :kernel (parser ()
279                             (seq ((name-a :id)
280                                   (name-b (? (seq (#\. (id :id)) id))))
281                               (if name-b (cons name-a name-b)
282                                   name-a)))))
283
284                (parse-message-item (sub-pset type name)
285                  ;; message-item ::=
286                  ;;     declspec+ declarator -!- (method-body | `;')
287                  ;;
288                  ;; Don't allow a method-body here if the message takes a
289                  ;; varargs list, because we don't have a name for the
290                  ;; `va_list' parameter.
291                  (let ((message (make-sod-message class name type
292                                                   sub-pset scanner)))
293                    (if (varargs-message-p message)
294                        (parse #\;)
295                        (parse (or #\; (parse-method-item sub-pset
296                                                          type nick name))))))
297
298                (parse-method-item (sub-pset type sub-nick name)
299                  ;; method-item ::=
300                  ;;     declspec+ dotted-declarator -!- method-body
301                  ;;
302                  ;; method-body ::= `{' c-fragment `}' | `extern' `;'
303                  (parse (seq ((body (or (seq ("extern" #\;) nil)
304                                         (parse-delimited-fragment
305                                          scanner #\{ #\}))))
306                           (make-sod-method class sub-nick name type
307                                            body sub-pset scanner))))
308
309                (parse-initializer ()
310                  ;; initializer ::= `=' c-fragment
311                  ;;
312                  ;; Return a VALUE, ready for passing to a `sod-initializer'
313                  ;; constructor.
314                  (parse-delimited-fragment scanner #\= (list #\, #\;)
315                                            :keep-end t))
316
317                (parse-slot-item (sub-pset base-type type name)
318                  ;; slot-item ::=
319                  ;;     declspec+ declarator -!- [initializer]
320                  ;;             [`,' list[init-declarator]] `;'
321                  ;;
322                  ;; init-declarator ::= declarator [initializer]
323                  (parse (and (seq ((init (? (parse-initializer))))
324                                (make-sod-slot class name type
325                                               sub-pset scanner)
326                                (when init
327                                  (make-sod-instance-initializer
328                                   class nick name init sub-pset scanner)))
329                              (skip-many ()
330                                (seq (#\,
331                                      (ds (parse-declarator scanner
332                                                            base-type))
333                                      (init (? (parse-initializer))))
334                                  (make-sod-slot class (cdr ds) (car ds)
335                                                 sub-pset scanner)
336                                  (when init
337                                    (make-sod-instance-initializer
338                                     class nick (cdr ds) init
339                                     sub-pset scanner))))
340                              #\;)))
341
342                (parse-initializer-item (sub-pset must-init-p constructor)
343                  ;; initializer-item ::=
344                  ;;     [`class'] -!- list[slot-initializer] `;'
345                  ;;
346                  ;; slot-initializer ::= id `.' id [initializer]
347                  (let ((parse-init (if must-init-p #'parse-initializer
348                                        (parser () (? (parse-initializer))))))
349                    (parse (and (skip-many ()
350                                  (seq ((name-a :id) #\. (name-b :id)
351                                        (init (funcall parse-init)))
352                                    (funcall constructor class
353                                             name-a name-b init
354                                             sub-pset scanner))
355                                  #\,)
356                                #\;))))
357
358                (class-item-dispatch (sub-pset base-type type name)
359                  ;; Logically part of `parse-raw-class-item', but the
360                  ;; indentation was getting crazy.  We're currently at
361                  ;;
362                  ;; raw-class-item ::=
363                  ;;     declspec+ (declarator | dotted-declarator) -!- ...
364                  ;;   | other-items
365                  ;;
366                  ;; If the declarator is dotted then this must be a method
367                  ;; definition; otherwise it might be a message or slot.
368                  (cond ((not (typep type 'c-function-type))
369                         (when (consp name)
370                           (cerror*
371                            "Method declarations must have function type")
372                           (setf name (cdr name)))
373                         (parse-slot-item sub-pset base-type type name))
374                        ((consp name)
375                         (parse-method-item sub-pset type
376                                            (car name) (cdr name)))
377                        (t
378                         (parse-message-item sub-pset type name))))
379
380                (parse-raw-class-item (sub-pset)
381                  ;; raw-class-item ::=
382                  ;;     message-item
383                  ;;   | method-item
384                  ;;   | slot-item
385                  ;;   | initializer-item
386                  ;;   | initfrag-item
387                  ;;
388                  ;; Most of the above begin with declspecs and a declarator
389                  ;; (which might be dotted).  So we parse that here and
390                  ;; dispatch based on what we find.
391                  (parse (or (plug class-item scanner class sub-pset)
392                             (peek
393                              (seq ((ds (parse-c-type scanner))
394                                    (dc (parse-maybe-dotted-declarator ds))
395                                    (nil (commit))
396                                    (nil (class-item-dispatch sub-pset
397                                                              ds
398                                                              (car dc)
399                                                              (cdr dc))))))
400                             (and "class"
401                                  (parse-initializer-item sub-pset t
402                                   #'make-sod-class-initializer))
403                             (parse-initializer-item sub-pset nil
404                              #'make-sod-instance-initializer)))))
405
406         (parse (seq (#\{
407                      (nil (skip-many ()
408                             (seq ((sub-pset (parse-property-set scanner))
409                                   (nil (parse-raw-class-item sub-pset)))
410                               (check-unused-properties sub-pset))))
411                      (nil (error () #\})))
412                  (unless (finalize-sod-class class)
413                    (setf duff t))
414                  (unless duff
415                    (add-to-module *module* class))))))))
416
417 (define-pluggable-parser module class (scanner pset)
418   ;; `class' id `:' list[id] class-body
419   ;; `class' id `;'
420   (with-parser-context (token-scanner-context :scanner scanner)
421     (parse (seq ("class"
422                  (name :id)
423                  (nil (or (seq (#\;)
424                             (make-class-type name))
425                           (seq ((supers (seq (#\: (ids (list () :id #\,)))
426                                           ids))
427                                 (nil (parse-class-body
428                                       scanner
429                                       pset name supers)))))))))))
430
431 ;;;----- That's all, folks --------------------------------------------------