chiark / gitweb /
src/module-parse.lisp (`set'): Parse at least one property if no pset.
[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 33(define-pluggable-parser module typename (scanner pset)
ceed01af 34 ;; `typename' list[id] `;'
048d0b2d 35 (declare (ignore pset))
bf090e02
MW
36 (with-parser-context (token-scanner-context :scanner scanner)
37 (parse (and "typename"
65eebc3b 38 (skip-many ()
bf090e02
MW
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 52 ;;
ceed01af 53 ;; constraints ::= `[' list[constraint] `]'
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 105 (char-scanner (make-instance 'charbuf-scanner
e783e65b
MW
106 :stream f-stream
107 :filename (namestring pathname)))
bf090e02
MW
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)
300a3f0a
MW
112 (multiple-value-bind (result winp consumedp)
113 (parse (skip-many ()
114 (seq ((pset (parse-property-set scanner))
115 (nil (error ()
a8bc7831
MW
116 (plug module scanner pset)
117 (skip-until (:keep-end nil)
118 #\; #\}))))
300a3f0a
MW
119 (check-unused-properties pset))))
120 (declare (ignore consumedp))
121 (unless winp (syntax-error scanner result)))))))))
048d0b2d
MW
122
123(define-pluggable-parser module test (scanner pset)
124 ;; `demo' string `;'
125 (declare (ignore pset))
bf090e02
MW
126 (with-parser-context (token-scanner-context :scanner scanner)
127 (parse (seq ("demo" (string :string) #\;)
128 (format t ";; DEMO ~S~%" string)))))
129
048d0b2d
MW
130(define-pluggable-parser module file (scanner pset)
131 ;; `import' string `;'
132 ;; `load' string `;'
133 (declare (ignore pset))
bf090e02
MW
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
01e3faf9
MW
165;;; Setting properties.
166
167(define-pluggable-parser module set (scanner pset)
ceed01af 168 ;; `set' list[property] `;'
01e3faf9
MW
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)
5445420e
MW
174 (add-property
175 module-pset
176 (p-name prop) (p-value prop)
177 :type (p-type prop)
178 :location (p-location prop))
01e3faf9
MW
179 (setf (p-seenp prop) t))
180 pset))
99a74df1 181 (parse (skip-many (:min (if pset 0 1))
01e3faf9 182 (error (:ignore-unconsumed t)
5445420e 183 (parse-property scanner module-pset)
65eebc3b 184 (skip-until () #\, #\;))
01e3faf9
MW
185 #\,))))
186 #\;))))
187
bf090e02
MW
188;;; Lisp escape.
189
048d0b2d 190(define-pluggable-parser module lisp (scanner pset)
bf090e02 191 ;; `lisp' s-expression `;'
048d0b2d 192 (declare (ignore pset))
bf090e02
MW
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
7f2917d2
MW
207(export 'class-item)
208
a42893dd
MW
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
b2983f35 218(define-pluggable-parser class-item initargs (scanner class pset)
ceed01af 219 ;; initarg-item ::= `initarg' declspec+ list[init-declarator]
b2983f35
MW
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
048d0b2d 237(defun parse-class-body (scanner pset name supers)
c91b90c3 238 ;; class-body ::= `{' class-item* `}'
048d0b2d
MW
239 ;;
240 ;; class-item ::= property-set raw-class-item
c91b90c3
MW
241 (with-parser-context (token-scanner-context :scanner scanner)
242 (make-class-type name)
70b33a78
MW
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"))))
c91b90c3
MW
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
43073476 264 :keywordp t
ea578bb4 265 :kernel (parser ()
c91b90c3
MW
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
c91b90c3
MW
271 (parse-message-item (sub-pset type name)
272 ;; message-item ::=
273 ;; declspec+ declarator -!- (method-body | `;')
2cbdee3d
MW
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))))))
c91b90c3
MW
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 ()
a888e3ac 297 ;; initializer ::= `=' c-fragment
c91b90c3 298 ;;
a888e3ac
MW
299 ;; Return a VALUE, ready for passing to a `sod-initializer'
300 ;; constructor.
301 (parse-delimited-fragment scanner #\= (list #\, #\;)
302 :keep-end t))
c91b90c3
MW
303
304 (parse-slot-item (sub-pset base-type type name)
305 ;; slot-item ::=
306 ;; declspec+ declarator -!- [initializer]
ceed01af 307 ;; [`,' list[init-declarator]] `;'
c91b90c3 308 ;;
ceed01af 309 ;; init-declarator ::= declarator [initializer]
c91b90c3
MW
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
a888e3ac 315 class nick name init sub-pset scanner)))
c91b90c3
MW
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
a888e3ac 325 class nick (cdr ds) init
048d0b2d 326 sub-pset scanner))))
c91b90c3
MW
327 #\;)))
328
b2983f35 329 (parse-initializer-item (sub-pset must-init-p constructor)
c91b90c3 330 ;; initializer-item ::=
ceed01af 331 ;; [`class'] -!- list[slot-initializer] `;'
c91b90c3 332 ;;
b2983f35 333 ;; slot-initializer ::= id `.' id [initializer]
5445420e 334 (let ((parse-init (if must-init-p #'parse-initializer
b2983f35
MW
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 #\;))))
c91b90c3
MW
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)
65eebc3b 357 (cerror*
a1985b3c 358 "Method declarations must have function type")
c91b90c3
MW
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
a42893dd 373 ;; | initfrag-item
c91b90c3
MW
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.
048d0b2d 378 (parse (or (plug class-item scanner class sub-pset)
db2abd9d 379 (peek
c91b90c3
MW
380 (seq ((ds (parse-c-type scanner))
381 (dc (parse-maybe-dotted-declarator ds))
65b1d9d7 382 (nil (commit))
048d0b2d
MW
383 (nil (class-item-dispatch sub-pset
384 ds
385 (car dc)
db2abd9d 386 (cdr dc))))))
c91b90c3 387 (and "class"
5445420e 388 (parse-initializer-item sub-pset t
c91b90c3 389 #'make-sod-class-initializer))
5445420e 390 (parse-initializer-item sub-pset nil
c91b90c3
MW
391 #'make-sod-instance-initializer)))))
392
048d0b2d
MW
393 (parse (seq (#\{
394 (nil (skip-many ()
395 (seq ((sub-pset (parse-property-set scanner))
9ec578d9 396 (nil (parse-raw-class-item sub-pset)))
048d0b2d 397 (check-unused-properties sub-pset))))
9ec578d9 398 (nil (error () #\})))
e45a106d
MW
399 (unless (finalize-sod-class class)
400 (setf duff t))
70b33a78
MW
401 (unless duff
402 (add-to-module *module* class))))))))
048d0b2d
MW
403
404(define-pluggable-parser module class (scanner pset)
ceed01af 405 ;; `class' id `:' list[id] class-body
c91b90c3 406 ;; `class' id `;'
bf090e02 407 (with-parser-context (token-scanner-context :scanner scanner)
c91b90c3
MW
408 (parse (seq ("class"
409 (name :id)
410 (nil (or (seq (#\;)
411 (make-class-type name))
ffc2a7b8
MW
412 (seq ((supers (seq (#\: (ids (list () :id #\,)))
413 ids))
c91b90c3
MW
414 (nil (parse-class-body
415 scanner
416 pset name supers)))))))))))
417
bf090e02 418;;;----- That's all, folks --------------------------------------------------