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