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