3 ;;; Top-level parser for module syntax
5 ;;; (c) 2010 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Sensble Object Design, an object system for C.
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.
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.
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.
28 ;;;--------------------------------------------------------------------------
35 (define-pluggable-parser module typename (scanner)
36 ;; `typename' ID ( `,' ID )* `;'
38 (with-parser-context (token-scanner-context :scanner scanner)
39 (parse (and "typename"
42 (if (gethash id *module-type-map*)
43 (cerror* "Type `~A' already defined" id)
44 (add-to-module *module*
45 (make-instance 'type-item
52 (define-pluggable-parser module code (scanner)
53 ;; `code' ID `:' ID [ CONSTRAINTS ] `{' C-FRAGMENT `}'
55 (with-parser-context (token-scanner-context :scanner scanner)
60 (constraints (? (seq (#\[
61 (constraints (list (:min 1)
66 (fragment (parse-delimited-fragment scanner #\{ #\})))
67 (add-to-module *module* (make-instance 'code-fragment-item
69 :constraints constraints
75 (defun read-module (pathname &key (truename (truename pathname)) location)
76 "Parse the file at PATHNAME as a module, returning it.
78 This is the main entry point for parsing module files. You may well know
79 the file's TRUENAME already (e.g., because `probe-file' dropped it into
80 your lap) so you can avoid repeating the search by providing it.
82 The LOCATION is the thing which wanted the module imported -- usually a
83 `file-location' object, though it might be anything other than `t' which
84 can be printed in the event of circular imports."
86 (define-module (pathname :location location :truename truename)
87 (with-open-file (f-stream pathname :direction :input)
88 (let* ((*readtable* (copy-readtable))
89 (char-scanner (make-instance 'charbuf-scanner
91 (scanner (make-instance 'sod-token-scanner
92 :char-scanner char-scanner)))
93 (with-default-error-location (scanner)
94 (with-parser-context (token-scanner-context :scanner scanner)
95 (parse (skip-many () (plug module scanner)))))))))
97 (define-pluggable-parser module test (scanner)
100 (with-parser-context (token-scanner-context :scanner scanner)
101 (parse (seq ("demo" (string :string) #\;)
102 (format t ";; DEMO ~S~%" string)))))
104 (define-pluggable-parser module file (scanner)
105 ;; `import' STRING `;'
108 (flet ((common (name type what thunk)
110 (merge-pathnames name
111 (make-pathname :type type
115 (with-parser-context (token-scanner-context :scanner scanner)
116 (parse (or (seq ("import" (name :string) #\;)
117 (common name "SOD" "module"
120 (let ((module (read-module path
123 (module-import module)
128 (cerror* "Error reading module ~S: ~A"
130 (seq ("load" (name :string) #\;)
131 (common name "LISP" "Lisp file"
134 (load true :verbose nil :print nil)
136 (cerror* "Error loading Lisp file ~S: ~A"
137 path error)))))))))))
141 (define-pluggable-parser module lisp (scanner)
142 ;; `lisp' s-expression `;'
144 (with-parser-context (token-scanner-context :scanner scanner)
145 (parse (seq ((sexp (if (and (eql (token-type scanner) :id)
146 (string= (token-value scanner) "lisp"))
147 (let* ((stream (make-scanner-stream scanner))
148 (sexp (read stream t)))
149 (scanner-step scanner)
151 (values '((:id "lisp")) nil nil)))
155 ;;;--------------------------------------------------------------------------
156 ;;; Class declarations.
158 (define-pluggable-parser module class (scanner)
159 ;; `class' id [`:' id-list] `{' class-item* `}'
161 (with-parser-context (token-scanner-context :scanner scanner)
162 (labels ((parse-item ()
163 ;; class-item ::= property-set
166 (supers (? (seq (#\: (supers (list (:min 1) :id #\,)))
171 ;;;----- That's all, folks --------------------------------------------------