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