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