lib/sod-hosted.c (sod_makev): Use two statements rather than tricky expression.
[sod] / src / parser / scanner-proto.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Scanner protocol definitions.
4 ;;;
5 ;;; (c) 2009 Straylight/Edgeware
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This file is part of the Sensible 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 (cl:in-package #:sod-parser)
27
28 ;;;--------------------------------------------------------------------------
29 ;;; Scanner context protocol.
30
31 (export 'parser-scanner)
32 (defgeneric parser-scanner (context)
33 (:documentation
34 "Return the symbol naming the CONTEXT's run-time scanner."))
35
36 (export 'scanner-context)
37 (defclass scanner-context ()
38 ((scanner :initarg :scanner :type symbol :reader parser-scanner))
39 (:documentation
40 "Base class for scanner contexts.
41
42 A scanner is simply an object maintaining the run-time state of a parsing
43 operation, in the same way as a parser context maintains the compile-time
44 state. So the scanner context is a compile-time context which expands to
45 calls to use the run-time scanner. See?
46
47 This class provides common compile-time behaviour for `parser-at-eof-p'
48 and friends by invoking corresponding methods on the scanner object at
49 run-time."))
50
51 ;;;--------------------------------------------------------------------------
52 ;;; Basic scanner protocol.
53
54 (export 'scanner-at-eof-p)
55 (defgeneric scanner-at-eof-p (scanner)
56 (:documentation
57 "Answer whether the SCANNER is at end-of-file.
58
59 It is an error to query the current item when at end-of-file."))
60
61 (export 'scanner-step)
62 (defgeneric scanner-step (scanner)
63 (:documentation
64 "Advance the SCANNER to the next item.
65
66 The precise nature of the items isn't known at this level, so a protocol
67 for accessing them is left for later."))
68
69 ;;;--------------------------------------------------------------------------
70 ;;; Scanner place-capture protocol.
71
72 (export 'scanner-capture-place)
73 (defgeneric scanner-capture-place (scanner)
74 (:documentation
75 "Capture the SCANNER's current place and return it.")
76 (:method (scanner)
77 (error "Scanner ~S doesn't support rewinding" scanner)))
78
79 (export 'scanner-restore-place)
80 (defgeneric scanner-restore-place (scanner place)
81 (:documentation
82 "`Rewind' the SCANNER to the captured PLACE.
83
84 The place was previously captured by `scanner-capture-place'."))
85
86 (export 'scanner-release-place)
87 (defgeneric scanner-release-place (scanner place)
88 (:documentation
89 "Release a PLACE captured from the SCANNER.
90
91 The place was previously captured by `scanner-capture-place'.")
92 (:method (scanner place) nil))
93
94 (export 'with-scanner-place)
95 (defmacro with-scanner-place ((place scanner) &body body)
96 "Evaluate BODY with PLACE bound to the captured current place.
97
98 Automatically releases the place when the BODY finishes. Note that
99 if you wanted to circumvent the cleanup then you should have used
100 `with-parser-place', which does all of this in the meta-level."
101 (once-only (scanner)
102 (multiple-value-bind (docs decls body) (parse-body body :docp nil)
103 (declare (ignore docs))
104 `(let ((,place (scanner-capture-place ,scanner)))
105 ,@decls
106 (unwind-protect (progn ,@body)
107 (when ,place (scanner-release-place ,scanner ,place)))))))
108
109 ;;;--------------------------------------------------------------------------
110 ;;; Character scanner protocol.
111
112 (export 'character-scanner)
113 (defclass character-scanner ()
114 ()
115 (:documentation "Base class for character scanners."))
116
117 (export 'character-scanner-context)
118 (defclass character-scanner-context
119 (scanner-context character-parser-context)
120 ()
121 (:documentation
122 "A context for a richer character-oriented scanner."))
123
124 (export 'scanner-current-char)
125 (defgeneric scanner-current-char (scanner)
126 (:documentation
127 "Returns the SCANNER's current character.
128
129 You advance to the next one using `scanner-step'."))
130
131 (export 'scanner-unread)
132 (defgeneric scanner-unread (scanner char)
133 (:documentation
134 "Rewind SCANNER by one character, specifically CHAR.
135
136 CHAR must be the character most recently stepped over by `scanner-step' --
137 it is an error to unread before the first call to `scanner-step'. It is
138 also an error to unread after encountering end-of-file."))
139
140 (export 'scanner-interval)
141 (defgeneric scanner-interval (scanner place-a &optional place-b)
142 (:documentation
143 "Return the characters from PLACE-A up to (but not including) PLACE-B.
144
145 The characters are returned as a string. If PLACE-B is omitted, return
146 the characters up to (but not including) the current position. It is an
147 error if PLACE-B precedes PLACE-A or they are from different scanners."))
148
149 (export '(scanner-filename scanner-line scanner-column))
150 (defgeneric scanner-filename (scanner)
151 (:documentation "Return the filename backing the SCANNER.")
152 (:method (scanner) nil))
153 (defgeneric scanner-line (scanner)
154 (:documentation "Return the SCANNER's current line number.")
155 (:method (scanner) nil))
156 (defgeneric scanner-column (scanner)
157 (:documentation "Return the SCANNER's current column number.")
158 (:method (scanner) nil))
159
160 (export 'scanner-file-location)
161 (defun scanner-file-location (scanner)
162 "Capture the current location of the SCANNER.
163
164 This uses the generic functions `scanner-filename', `scanner-line' and
165 `scanner-column' to compute its result. There are default methods on
166 these functions which make up dummy results.
167
168 There is a method for `file-location' defined on `character-scanner' which
169 simply calls this function; but since some scanners are structure-objects
170 rather than standard-objects they can't include `character-scanner' as a
171 superclass."
172 (make-file-location (scanner-filename scanner)
173 (scanner-line scanner)
174 (scanner-column scanner)))
175
176 ;;;--------------------------------------------------------------------------
177 ;;; Token scanner protocol.
178
179 ;; The token scanner base class and parser context.
180
181 (export '(token-scanner token-type token-value))
182 (defclass token-scanner ()
183 ((%type :reader token-type)
184 (value :reader token-value)
185 (captures :initform 0 :type fixnum)
186 (tail :initform nil :type (or token-scanner-place null))
187 (filename :initarg :filename :type string :reader scanner-filename)
188 (line :initarg :line :initform 1 :type fixnum :accessor scanner-line)
189 (column :initarg :column :initform 0
190 :type fixnum :accessor scanner-column))
191 (:documentation
192 "A rewindable scanner for tokenizing.
193
194 The scanner should be used via the parser protocol; see also the token
195 scanner protocol, which explains the model.
196
197 Subclasses must provide the detailed scanning behaviour -- most notably
198 the `scanner-token' generic function -- and also implement a method on
199 `file-location' to return the location. The `scanner-token' method should
200 also update the `line' and `column' slots to track the position in the
201 underlying source, if appropriate. This class will handle the remaining
202 details, such as dealing correctly with rewinding."))
203
204 (export 'token-scanner-context)
205 (defclass token-scanner-context (scanner-context token-parser-context)
206 ()
207 (:documentation
208 "A parser context for a richer token-based scanners."))
209
210 ;; A place marker.
211
212 (export '(token-scanner-place token-scanner-place-p))
213 (defstruct (token-scanner-place
214 (:constructor make-token-scanner-place
215 (&key scanner next type value line column
216 &aux (%type type))))
217 "A link in the chain of lookahead tokens; capturable as a place.
218
219 If the scanner's place is captured, it starts to maintain a list of
220 lookahead tokens. The list contains internal links -- it works out
221 slightly easier that way. This is basically a simpler version of the
222 charbuf scanner (q.v.); most significantly, the chain links here do double
223 duty as place markers.
224
225 The details of this structure are not a defined part of the token scanner
226 protocol."
227
228 (scanner nil :type token-scanner :read-only t)
229 (next nil :type (or token-scanner-place null))
230 (%type nil :read-only t)
231 (value nil :read-only t)
232 (line 1 :type (or fixnum null) :read-only t)
233 (column 0 :type (or fixnum null) :read-only t))
234 (define-access-wrapper token-scanner-place-type token-scanner-place-%type
235 :read-only t)
236
237 ;; Protocol.
238
239 (export 'scanner-token)
240 (defgeneric scanner-token (scanner)
241 (:documentation
242 "Internal protocol: read the next token from the SCANNER.
243
244 This function is called by `scanner-step' to actually read the next token
245 if necessary. It should return two values: the token's `type' and its
246 `value'."))
247
248 ;;;--------------------------------------------------------------------------
249 ;;; Character scanner streams.
250 ;;;
251 ;;; This seems like an abstraction inversion, but it's important if we're to
252 ;;; `read' from a character scanner.
253
254 (export 'character-scanner-stream)
255 (defclass character-scanner-stream (fundamental-character-input-stream)
256 ((scanner :initarg :scanner))
257 (:documentation
258 "A stream which reads from a character scanner.
259
260 The SCANNER must implement the character scanner protcol, including
261 `scanner-current-char', `scanner-step', and `scanner-unread'; it is not
262 necessary that the scanner implement the place-capture protocol.
263
264 The stream can be made more efficient by implementing
265 `stream-read-sequence' and `stream-read-line' in a scanner-specific
266 manner."))
267
268 (export 'make-scanner-stream)
269 (defgeneric make-scanner-stream (scanner)
270 (:documentation
271 "Return a stream which reads from the SCANNER.
272
273 The default method simply constructs a `character-scanner-stream'
274 instance. Subclasses of `character-scanner' can override this method in
275 order to return instances of more efficient stream subclasses.")
276 (:method ((scanner character-scanner))
277 (make-instance 'character-scanner-stream :scanner scanner)))
278
279 ;;;----- That's all, folks --------------------------------------------------