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