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