chiark / gitweb /
cb3fcb26d6cd3b6fa1d2c2875e5e2287fbabce31
[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 (kw))
70                    #\:
71                    (name (item))
72                    (constraints (? (seq (#\[
73                                          (constraints (list (:min 1)
74                                                         (list (:min 1)
75                                                           (item))
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)))))))
86
87 ;;; External files.
88
89 (export 'read-module)
90 (defun read-module (pathname &key (truename nil truep) location)
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
101   (setf pathname (merge-pathnames pathname
102                                   (make-pathname :type "SOD" :case :common)))
103   (unless truep (setf truename (truename pathname)))
104   (define-module (pathname :location location :truename truename)
105     (with-open-file (f-stream pathname :direction :input)
106       (let* ((*readtable* (copy-readtable))
107              (*package* (find-package '#:sod-user))
108              (char-scanner (make-instance 'charbuf-scanner
109                                           :stream f-stream
110                                           :filename (namestring pathname)))
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)
115             (multiple-value-bind (result winp consumedp)
116                 (parse (skip-many ()
117                           (seq ((pset (parse-property-set scanner))
118                                 (nil (error ()
119                                          (plug module scanner pset)
120                                        (skip-until (:keep-end nil)
121                                          #\; #\}))))
122                             (check-unused-properties pset))))
123               (declare (ignore consumedp))
124               (unless winp (syntax-error scanner result)))))))))
125
126 (define-pluggable-parser module test (scanner pset)
127   ;; `demo' string `;'
128   (declare (ignore pset))
129   (with-parser-context (token-scanner-context :scanner scanner)
130     (parse (seq ("demo" (string :string) #\;)
131              (format t ";; DEMO ~S~%" string)))))
132
133 (define-pluggable-parser module file (scanner pset)
134   ;; `import' string `;'
135   ;; `load' string `;'
136   (declare (ignore pset))
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
168 ;;; Setting properties.
169
170 (define-pluggable-parser module set (scanner pset)
171   ;; `set' list[property] `;'
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)
177                                       (add-property
178                                        module-pset
179                                        (p-name prop) (p-value prop)
180                                        :type (p-type prop)
181                                        :location (p-location prop))
182                                       (setf (p-seenp prop) t))
183                                     pset))
184                         (parse (skip-many (:min (if pset 0 1))
185                                  (error (:ignore-unconsumed t)
186                                      (parse-property scanner module-pset)
187                                    (skip-until () #\, #\;))
188                                  #\,))))
189                 #\;))))
190
191 ;;; Lisp escape.
192
193 (define-pluggable-parser module lisp (scanner pset)
194   ;; `lisp' s-expression `;'
195   (declare (ignore pset))
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
210 (export 'class-item)
211
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
221 (define-pluggable-parser class-item initargs (scanner class pset)
222   ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
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
240 (defun parse-class-body (scanner pset name supers)
241   ;; class-body ::= `{' class-item* `}'
242   ;;
243   ;; class-item ::= property-set raw-class-item
244   (with-parser-context (token-scanner-context :scanner scanner)
245     (make-class-type name)
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"))))
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
267                   :keywordp t
268                   :kernel (parser ()
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
274                (parse-message-item (sub-pset type name)
275                  ;; message-item ::=
276                  ;;     declspec+ declarator -!- (method-body | `;')
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))))))
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 ()
300                  ;; initializer ::= `=' c-fragment
301                  ;;
302                  ;; Return a VALUE, ready for passing to a `sod-initializer'
303                  ;; constructor.
304                  (parse-delimited-fragment scanner #\= (list #\, #\;)
305                                            :keep-end t))
306
307                (parse-slot-item (sub-pset base-type type name)
308                  ;; slot-item ::=
309                  ;;     declspec+ declarator -!- [initializer]
310                  ;;             [`,' list[init-declarator]] `;'
311                  ;;
312                  ;; init-declarator ::= declarator [initializer]
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
318                                   class nick name init sub-pset scanner)))
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
328                                     class nick (cdr ds) init
329                                     sub-pset scanner))))
330                              #\;)))
331
332                (parse-initializer-item (sub-pset must-init-p constructor)
333                  ;; initializer-item ::=
334                  ;;     [`class'] -!- list[slot-initializer] `;'
335                  ;;
336                  ;; slot-initializer ::= id `.' id [initializer]
337                  (let ((parse-init (if must-init-p #'parse-initializer
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                                #\;))))
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)
360                           (cerror*
361                            "Method declarations must have function type")
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
376                  ;;   | initfrag-item
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.
381                  (parse (or (plug class-item scanner class sub-pset)
382                             (peek
383                              (seq ((ds (parse-c-type scanner))
384                                    (dc (parse-maybe-dotted-declarator ds))
385                                    (nil (commit))
386                                    (nil (class-item-dispatch sub-pset
387                                                              ds
388                                                              (car dc)
389                                                              (cdr dc))))))
390                             (and "class"
391                                  (parse-initializer-item sub-pset t
392                                   #'make-sod-class-initializer))
393                             (parse-initializer-item sub-pset nil
394                              #'make-sod-instance-initializer)))))
395
396         (parse (seq (#\{
397                      (nil (skip-many ()
398                             (seq ((sub-pset (parse-property-set scanner))
399                                   (nil (parse-raw-class-item sub-pset)))
400                               (check-unused-properties sub-pset))))
401                      (nil (error () #\})))
402                  (unless (finalize-sod-class class)
403                    (setf duff t))
404                  (unless duff
405                    (add-to-module *module* class))))))))
406
407 (define-pluggable-parser module class (scanner pset)
408   ;; `class' id `:' list[id] class-body
409   ;; `class' id `;'
410   (with-parser-context (token-scanner-context :scanner scanner)
411     (parse (seq ("class"
412                  (name :id)
413                  (nil (or (seq (#\;)
414                             (make-class-type name))
415                           (seq ((supers (seq (#\: (ids (list () :id #\,)))
416                                           ids))
417                                 (nil (parse-class-body
418                                       scanner
419                                       pset name supers)))))))))))
420
421 ;;;----- That's all, folks --------------------------------------------------