3 ;;; Lexical analysis for input parser
5 ;;; (c) 2009 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 ;;;--------------------------------------------------------------------------
31 (export 'sod-token-scanner)
32 (defclass sod-token-scanner (token-scanner)
33 ((char-scanner :initarg :char-scanner :reader token-scanner-char-scanner))
35 "A token scanner for SOD input files.
37 Not a lot here, apart from a character scanner to read from and the
38 standard token scanner infrastructure."))
40 (defmethod shared-initialize :after
41 ((scanner sod-token-scanner) slot-names &key)
42 (default-slot (scanner 'sod-parser::filename slot-names)
43 (scanner-filename (token-scanner-char-scanner scanner))))
45 ;;;--------------------------------------------------------------------------
48 (defun show-char (stream char &optional colonp atsignp)
49 "Format CHAR to STREAM in a readable way.
51 Usable in `format''s ~/.../ command."
52 (declare (ignore colonp atsignp))
53 (cond ((null char) (write-string "<eof>" stream))
54 ((and (graphic-char-p char) (char/= char #\space))
55 (format stream "`~C'" char))
56 (t (format stream "<~(~:C~)>" char))))
58 (defun scan-comment (scanner)
59 "Scan a comment (either `/* ... */' or `// ...') from SCANNER.
61 The result isn't interesting."
62 (with-parser-context (character-scanner-context :scanner scanner)
65 (and (skip-many () (not #\*))
66 (label "*/" (skip-many (:min 1) #\*)))
70 (skip-many () (not #\newline))
73 (defmethod make-scanner-stream ((scanner sod-token-scanner))
74 (make-scanner-stream (token-scanner-char-scanner scanner)))
76 ;;;--------------------------------------------------------------------------
79 (defvar *indicator-map* (make-hash-table)
80 "Hash table mapping indicator objects to human-readable descriptions.")
82 (defun define-indicator (indicator description)
83 (setf (gethash indicator *indicator-map*) description)
86 (export 'syntax-error)
87 (defun syntax-error (scanner expected &key (continuep t))
88 "Signal a (maybe) continuable syntax error."
89 (labels ((show-token (type value)
91 (format nil "~/sod::show-char/" type)
93 (:id (format nil "<identifier~@[ `~A'~]>" value))
94 (:string "<string-literal>")
95 (:char "<character-literal>")
96 (:eof "<end-of-file>")
98 (t (format nil "<? ~S~@[ ~S~]>" type value)))))
99 (show-expected (thing)
100 (acond ((gethash thing *indicator-map*) it)
101 ((atom thing) (show-token thing nil))
102 ((eq (car thing) :id)
103 (format nil "`~A'" (cadr thing)))
104 (t (format nil "<? ~S>" thing)))))
105 (funcall (if continuep #'cerror* #'error)
107 expected ~{~#[<bug>~;~A~;~A or ~A~:;~A, ~]~} ~
109 (mapcar #'show-expected expected)
110 (show-token (token-type scanner) (token-value scanner)))))
112 (export 'lexer-error)
113 (defun lexer-error (char-scanner expected consumedp)
114 "Signal a continuable lexical error."
115 (cerror* "Lexical error: ~
116 expected ~{~#[<bug>~;~A~;~A or ~A~;:~A, ~]~} ~
117 but found ~/sod::show-char/~
119 (mapcar (lambda (exp)
121 (character (format nil "~/sod::show-char/" exp))
122 (string (format nil "`~A'" exp))
123 ((cons (eql :digit) *) (format nil "<radix-~A digit>"
125 ((eql :eof) "<end-of-file>")
126 ((eql :any) "<character>")
127 (t (format nil "<? ~S>" exp))))
129 (and (not (scanner-at-eof-p char-scanner))
130 (scanner-current-char char-scanner))
131 (and consumedp (file-location char-scanner))))
133 ;;;--------------------------------------------------------------------------
134 ;;; Token scanner protocol implementation.
136 (defmethod scanner-token ((scanner sod-token-scanner))
137 (with-slots (char-scanner line column) scanner
138 (with-parser-context (character-scanner-context :scanner char-scanner)
140 (flet ((scan-digits (&key (radix 10) (min 1) (init 0))
141 ;; Scan an return a sequence of digits.
142 (parse (many (acc init (+ (* acc radix) it) :min min)
143 (label (list :digit radix)
145 (digit-char-p ch radix))))))))
147 ;; Skip initial junk, and remember the place.
149 (setf (scanner-line scanner) (scanner-line char-scanner)
150 (scanner-column scanner) (scanner-column char-scanner))
151 (cond-parse (:consumedp cp :expected exp)
152 ((satisfies whitespace-char-p) (parse :whitespace))
153 ((scan-comment char-scanner))
154 (t (if cp (lexer-error char-scanner exp cp) (return)))))
156 ;; Now parse something.
157 (cond-parse (:consumedp cp :expected exp)
159 ;; Alphanumerics mean we read an identifier.
160 ((or #\_ (satisfies alpha-char-p))
161 (values :id (with-output-to-string (out)
163 (parse (many (nil nil (write-char it out))
164 (or #\_ (satisfies alphanumericp)))))))
166 ;; Quotes introduce a literal.
167 ((seq ((quote (or #\" #\'))
168 (contents (many (out (make-string-output-stream)
169 (progn (write-char it out) out)
170 :final (get-output-stream-string out))
171 (or (and #\\ :any) (not quote))))
175 (#\' (case (length contents)
176 (1 (char contents 0))
177 (0 (cerror* "Empty character literal") #\?)
178 (t (cerror* "Too many characters in literal")
179 (char contents 0))))))
180 (values (etypecase it
185 ;; Zero introduces a chosen-radix integer.
187 (or (and (or #\b #\B) (scan-digits :radix 2))
188 (and (or #\o #\O) (scan-digits :radix 8))
189 (and (or #\x #\X) (scan-digits :radix 16))
190 (scan-digits :radix 8 :min 0)))
193 ;; Any other digit forces radix-10.
194 ((seq ((d (filter digit-char-p))
195 (i (scan-digits :radix 10 :min 0 :init d)))
199 ;; Some special punctuation sequences are single tokens.
200 ("..." (values :ellipsis nil))
202 ;; Any other character is punctuation.
203 (:any (values it nil))
205 ;; End of file means precisely that.
206 (:eof (values :eof nil))
208 ;; Report errors and try again. Because we must have consumed some
209 ;; input in order to get here (we've matched both :any and :eof) we
210 ;; must make progress on every call.
213 (lexer-error char-scanner exp cp)
214 (scanner-token scanner)))))))
216 ;;;----- That's all, folks --------------------------------------------------