chiark / gitweb /
src/method-impl.lisp: Initialize `suppliedp' flags properly.
[sod] / src / lexer-impl.lisp
CommitLineData
dea4d055
MW
1;;; -*-lisp-*-
2;;;
3;;; Implementation of lexical analysis protocol.
4;;;
5;;; (c) 2009 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
e0808c47 10;;; This file is part of the Sensible Object Design, an object system for C.
dea4d055
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(cl:in-package #:sod)
27
28;;;--------------------------------------------------------------------------
239fa5bd 29;;; Class implementation.
dea4d055 30
239fa5bd
MW
31(defmethod shared-initialize :after
32 ((scanner sod-token-scanner) slot-names &key)
33 (default-slot (scanner 'sod-parser::filename slot-names)
34 (scanner-filename (token-scanner-char-scanner scanner))))
dea4d055 35
239fa5bd
MW
36(defmethod make-scanner-stream ((scanner sod-token-scanner))
37 (make-scanner-stream (token-scanner-char-scanner scanner)))
dea4d055
MW
38
39;;;--------------------------------------------------------------------------
239fa5bd 40;;; Indicators and error messages.
dea4d055 41
239fa5bd
MW
42(defun show-char (stream char &optional colonp atsignp)
43 "Format CHAR to STREAM in a readable way.
dea4d055 44
239fa5bd
MW
45 Usable in `format''s ~/.../ command."
46 (declare (ignore colonp atsignp))
47 (cond ((null char) (write-string "<eof>" stream))
48 ((and (graphic-char-p char) (char/= char #\space))
49 (format stream "`~C'" char))
50 (t (format stream "<~(~:C~)>" char))))
dea4d055 51
048d0b2d
MW
52(defun skip-until (scanner token-types &key keep-end)
53 "This is the implementation of the `skip-until' parser."
54 (do ((consumedp nil t))
55 ((member (token-type scanner) token-types)
56 (unless keep-end (scanner-step scanner))
57 (values nil t (or keep-end consumedp)))
58 (when (scanner-at-eof-p scanner)
59 (return (values token-types nil consumedp)))
60 (scanner-step scanner)))
61
012554e1 62(defun parse-error-recover (scanner parser recover &key ignore-unconsumed)
048d0b2d
MW
63 "This is the implementation of the `error' parser."
64 (multiple-value-bind (result win consumedp) (funcall parser)
012554e1
MW
65 (cond ((or win
66 (and (not consumedp)
67 (or ignore-unconsumed
68 (scanner-at-eof-p scanner))))
69 ;; If we succeeded, or if we didn't consume any tokens and the
70 ;; caller's OK with that, then there's nothing for us to do here.
71 ;; On the other hand, if we failed, didn't consume any tokens, and
72 ;; we're at end-of-file, then there's not much hope of making
73 ;; onward progress, so in this case we propagate the failure
74 ;; rather than trying to recover. And we assume that the
75 ;; continuation will somehow arrange to report the problem, and
76 ;; avoid inundating the user with error reports.
048d0b2d
MW
77 (values result win consumedp))
78 (t
79 ;; Now we have to do some kind of sensible error recovery. The
80 ;; important thing to do here is to make sure that we make some
81 ;; progress. If we consumed any tokens then we're fine, and we'll
82 ;; just try the provided recovery strategy. Otherwise, if we're
83 ;; not at EOF, then we can ensure progress by discarding the
84 ;; current token. Finally, if we are at EOF then our best bet is
85 ;; simply to propagate the current failure back to the caller, but
86 ;; we handled that case above.
87 (syntax-error scanner result :continuep t)
88 (unless consumedp (scanner-step scanner))
89 (funcall recover)))))
90
239fa5bd
MW
91;;;--------------------------------------------------------------------------
92;;; Token scanning.
93
94(defmethod scanner-token ((scanner sod-token-scanner))
95 (with-slots (char-scanner line column) scanner
96 (with-parser-context (character-scanner-context :scanner char-scanner)
97
98 (flet ((scan-digits (&key (radix 10) (min 1) (init 0))
99 ;; Scan and return a sequence of digits.
100 (parse (many (acc init (+ (* acc radix) it) :min min)
101 (label (list :digit radix)
102 (filter (lambda (ch)
103 (digit-char-p ch radix))))))))
104
105 ;; Skip initial junk, and remember the place.
106 (loop
107 (setf (scanner-line scanner) (scanner-line char-scanner)
108 (scanner-column scanner) (scanner-column char-scanner))
109 (cond-parse (:consumedp cp :expected exp)
110 ((satisfies whitespace-char-p) (parse :whitespace))
111 ((scan-comment char-scanner))
112 (t (if cp (lexer-error char-scanner exp cp) (return)))))
113
114 ;; Now parse something.
115 (cond-parse (:consumedp cp :expected exp)
116
117 ;; Alphanumerics mean we read an identifier.
118 ((or #\_ (satisfies alpha-char-p))
119 (values :id (with-output-to-string (out)
120 (write-char it out)
121 (parse (many (nil nil (write-char it out))
122 (or #\_ (satisfies alphanumericp)))))))
123
124 ;; Quotes introduce a literal.
125 ((seq ((quote (or #\" #\'))
126 (contents (many (out (make-string-output-stream)
127 (progn (write-char it out) out)
128 :final (get-output-stream-string out))
129 (or (and #\\ :any) (not quote))))
130 (nil (char quote)))
131 (ecase quote
132 (#\" contents)
133 (#\' (case (length contents)
134 (1 (char contents 0))
135 (0 (cerror* "Empty character literal") #\?)
136 (t (cerror* "Too many characters in literal")
137 (char contents 0))))))
138 (values (etypecase it
139 (character :char)
140 (string :string))
141 it))
142
143 ;; Zero introduces a chosen-radix integer.
144 ((and #\0
145 (or (and (or #\b #\B) (scan-digits :radix 2))
146 (and (or #\o #\O) (scan-digits :radix 8))
147 (and (or #\x #\X) (scan-digits :radix 16))
148 (scan-digits :radix 8 :min 0)))
149 (values :int it))
150
151 ;; Any other digit forces radix-10.
152 ((seq ((d (filter digit-char-p))
153 (i (scan-digits :radix 10 :min 0 :init d)))
154 i)
155 (values :int it))
156
157 ;; Some special punctuation sequences are single tokens.
158 ("..." (values :ellipsis nil))
159
160 ;; Any other character is punctuation.
161 (:any (values it nil))
162
163 ;; End of file means precisely that.
164 (:eof (values :eof nil))
165
166 ;; Report errors and try again. Because we must have consumed some
167 ;; input in order to get here (we've matched both :any and :eof) we
168 ;; must make progress on every call.
169 (t
170 (assert cp)
171 (lexer-error char-scanner exp cp)
172 (scanner-token scanner)))))))
dea4d055
MW
173
174;;;----- That's all, folks --------------------------------------------------