An actual running implementation, which makes code that compiles.
[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;;;
10;;; This file is part of the Sensble 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
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
MW
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)))))))
bf090e02
MW
76
77;;; External files.
78
9ec578d9
MW
79(export 'read-module)
80(defun read-module (pathname &key (truename nil truep) location)
bf090e02
MW
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
9ec578d9
MW
91 (setf pathname (merge-pathnames pathname
92 (make-pathname :type "SOD" :case :common)))
93 (unless truep (setf truename (truename pathname)))
bf090e02
MW
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)
048d0b2d
MW
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))
bf090e02
MW
112 (with-parser-context (token-scanner-context :scanner scanner)
113 (parse (seq ("demo" (string :string) #\;)
114 (format t ";; DEMO ~S~%" string)))))
115
048d0b2d
MW
116(define-pluggable-parser module file (scanner pset)
117 ;; `import' string `;'
118 ;; `load' string `;'
119 (declare (ignore pset))
bf090e02
MW
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;;; Lisp escape.
152
048d0b2d 153(define-pluggable-parser module lisp (scanner pset)
bf090e02 154 ;; `lisp' s-expression `;'
048d0b2d 155 (declare (ignore pset))
bf090e02
MW
156 (with-parser-context (token-scanner-context :scanner scanner)
157 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
158 (string= (token-value scanner) "lisp"))
159 (let* ((stream (make-scanner-stream scanner))
160 (sexp (read stream t)))
161 (scanner-step scanner)
162 (values sexp t t))
163 (values '((:id "lisp")) nil nil)))
164 #\;)
165 (eval sexp)))))
166
167;;;--------------------------------------------------------------------------
168;;; Class declarations.
169
048d0b2d 170(defun parse-class-body (scanner pset name supers)
c91b90c3 171 ;; class-body ::= `{' class-item* `}'
048d0b2d
MW
172 ;;
173 ;; class-item ::= property-set raw-class-item
c91b90c3
MW
174 (with-parser-context (token-scanner-context :scanner scanner)
175 (make-class-type name)
048d0b2d 176 (let* ((class (make-sod-class name (mapcar #'find-sod-class supers)
c91b90c3
MW
177 pset scanner))
178 (nick (sod-class-nickname class)))
179
180 (labels ((parse-maybe-dotted-declarator (base-type)
181 ;; Parse a declarator or dotted-declarator, i.e., one whose
182 ;; centre is
183 ;;
184 ;; maybe-dotted-identifier ::= [id `.'] id
185 ;;
186 ;; A plain identifier is returned as a string, as usual; a
187 ;; dotted identifier is returned as a cons cell of the two
188 ;; names.
189 (parse-declarator
190 scanner base-type
ea578bb4 191 :kernel (parser ()
c91b90c3
MW
192 (seq ((name-a :id)
193 (name-b (? (seq (#\. (id :id)) id))))
194 (if name-b (cons name-a name-b)
195 name-a)))))
196
c91b90c3
MW
197 (parse-message-item (sub-pset type name)
198 ;; message-item ::=
199 ;; declspec+ declarator -!- (method-body | `;')
200 (make-sod-message class name type sub-pset scanner)
048d0b2d
MW
201 (parse (or #\; (parse-method-item sub-pset
202 type nick name))))
c91b90c3
MW
203
204 (parse-method-item (sub-pset type sub-nick name)
205 ;; method-item ::=
206 ;; declspec+ dotted-declarator -!- method-body
207 ;;
208 ;; method-body ::= `{' c-fragment `}' | `extern' `;'
209 (parse (seq ((body (or (seq ("extern" #\;) nil)
210 (parse-delimited-fragment
211 scanner #\{ #\}))))
212 (make-sod-method class sub-nick name type
213 body sub-pset scanner))))
214
215 (parse-initializer ()
216 ;; initializer ::= `=' c-fragment | `=' `{' c-fragment `}'
217 ;;
218 ;; Return (VALUE-KIND . VALUE-FORM), ready for passing to a
219 ;; `sod-initializer' constructor.
9ec578d9
MW
220
221 ;; This is kind of tricky because we have to juggle both
222 ;; layers of the parsing machinery. The character scanner
223 ;; will already have consumed the lookahead token (which, if
224 ;; we're going to do anything, is `=').
225 (let ((char-scanner (token-scanner-char-scanner scanner)))
226
227 ;; First, skip the character-scanner past any whitespace.
228 ;; We don't record this consumption, which is a bit
229 ;; naughty, but nobody will actually mind.
230 (loop
231 (when (or (scanner-at-eof-p char-scanner)
232 (not (whitespace-char-p
233 (scanner-current-char char-scanner))))
234 (return))
235 (scanner-step char-scanner))
236
237 ;; Now maybe read an initializer.
238 (cond ((not (eql (token-type scanner) #\=))
239 ;; It's not an `=' after all. There's no
240 ;; initializer.
241 (values '(#\=) nil nil))
242
243 ((and (not (scanner-at-eof-p char-scanner))
244 (char= (scanner-current-char char-scanner)
245 #\{))
246 ;; There's a brace after the `=', so we should
247 ;; consume the `=' here, and read a compound
248 ;; initializer enclosed in braces.
249 (parse (seq (#\= (frag (parse-delimited-fragment
250 scanner #\{ #\})))
251 (cons :compound frag))))
252
253 (t
254 ;; No brace, so read from the `=' up to, but not
255 ;; including, the trailing `,' or `;' delimiter.
256 (parse (seq ((frag (parse-delimited-fragment
257 scanner #\= '(#\; #\,)
258 :keep-end t)))
259 (cons :simple frag)))))))
c91b90c3
MW
260
261 (parse-slot-item (sub-pset base-type type name)
262 ;; slot-item ::=
263 ;; declspec+ declarator -!- [initializer]
264 ;; [`,' init-declarator-list] `;'
265 ;;
266 ;; init-declarator-list ::=
267 ;; declarator [initializer] [`,' init-declarator-list]
268 (parse (and (seq ((init (? (parse-initializer))))
269 (make-sod-slot class name type
270 sub-pset scanner)
271 (when init
272 (make-sod-instance-initializer
273 class nick name (car init) (cdr init)
048d0b2d 274 sub-pset scanner)))
c91b90c3
MW
275 (skip-many ()
276 (seq (#\,
277 (ds (parse-declarator scanner
278 base-type))
279 (init (? (parse-initializer))))
280 (make-sod-slot class (cdr ds) (car ds)
281 sub-pset scanner)
282 (when init
283 (make-sod-instance-initializer
284 class nick (cdr ds)
285 (car init) (cdr init)
048d0b2d 286 sub-pset scanner))))
c91b90c3
MW
287 #\;)))
288
289 (parse-initializer-item (sub-pset constructor)
290 ;; initializer-item ::=
291 ;; [`class'] -!- slot-initializer-list `;'
292 ;;
293 ;; slot-initializer ::= id `.' id initializer
294 (parse (and (skip-many ()
295 (seq ((name-a :id) #\. (name-b :id)
296 (init (parse-initializer)))
297 (funcall constructor class
298 name-a name-b
299 (car init) (cdr init)
300 sub-pset scanner))
301 #\,)
302 #\;)))
303
304 (class-item-dispatch (sub-pset base-type type name)
305 ;; Logically part of `parse-raw-class-item', but the
306 ;; indentation was getting crazy. We're currently at
307 ;;
308 ;; raw-class-item ::=
309 ;; declspec+ (declarator | dotted-declarator) -!- ...
310 ;; | other-items
311 ;;
312 ;; If the declarator is dotted then this must be a method
313 ;; definition; otherwise it might be a message or slot.
314 (cond ((not (typep type 'c-function-type))
315 (when (consp name)
316 (cerror*-with-location
317 scanner
318 "Method declarations must have function type.")
319 (setf name (cdr name)))
320 (parse-slot-item sub-pset base-type type name))
321 ((consp name)
322 (parse-method-item sub-pset type
323 (car name) (cdr name)))
324 (t
325 (parse-message-item sub-pset type name))))
326
327 (parse-raw-class-item (sub-pset)
328 ;; raw-class-item ::=
329 ;; message-item
330 ;; | method-item
331 ;; | slot-item
332 ;; | initializer-item
333 ;;
334 ;; Most of the above begin with declspecs and a declarator
335 ;; (which might be dotted). So we parse that here and
336 ;; dispatch based on what we find.
048d0b2d 337 (parse (or (plug class-item scanner class sub-pset)
9ec578d9 338 ;(peek
c91b90c3
MW
339 (seq ((ds (parse-c-type scanner))
340 (dc (parse-maybe-dotted-declarator ds))
048d0b2d
MW
341 (nil (class-item-dispatch sub-pset
342 ds
343 (car dc)
9ec578d9 344 (cdr dc)))));)
c91b90c3
MW
345 (and "class"
346 (parse-initializer-item
347 sub-pset
348 #'make-sod-class-initializer))
349 (parse-initializer-item
350 sub-pset
351 #'make-sod-instance-initializer)))))
352
048d0b2d
MW
353 (parse (seq (#\{
354 (nil (skip-many ()
355 (seq ((sub-pset (parse-property-set scanner))
9ec578d9 356 (nil (parse-raw-class-item sub-pset)))
048d0b2d 357 (check-unused-properties sub-pset))))
9ec578d9 358 (nil (error () #\})))
048d0b2d
MW
359 (finalize-sod-class class)
360 (add-to-module *module* class)))))))
361
362(define-pluggable-parser module class (scanner pset)
c91b90c3
MW
363 ;; `class' id [`:' id-list] class-body
364 ;; `class' id `;'
bf090e02 365 (with-parser-context (token-scanner-context :scanner scanner)
c91b90c3
MW
366 (parse (seq ("class"
367 (name :id)
368 (nil (or (seq (#\;)
369 (make-class-type name))
370 (seq ((supers (? (seq (#\: (ids (list () :id #\,)))
371 ids)))
372 (nil (parse-class-body
373 scanner
374 pset name supers)))))))))))
375
bf090e02 376;;;----- That's all, folks --------------------------------------------------