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