3 ;;; Protocol for lexical analysis
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 ;;;--------------------------------------------------------------------------
41 ;;; Indicators and error messages.
43 (export 'define-indicator)
44 (defun define-indicator (indicator description)
45 "Associate an INDICATOR with its textual DESCRIPTION.
47 Updates the the `*indicator-map*'."
48 (setf (gethash indicator *indicator-map*) description)
51 (export 'syntax-error)
52 (defun syntax-error (scanner expected &key (continuep t))
53 "Signal a (maybe) continuable syntax error."
54 (labels ((show-token (type value)
56 (format nil "~/sod::show-char/" type)
58 (:id (format nil "<identifier~@[ `~A'~]>" value))
59 (:string "<string-literal>")
60 (:char "<character-literal>")
61 (:eof "<end-of-file>")
63 (t (format nil "<? ~S~@[ ~S~]>" type value)))))
64 (show-expected (thing)
65 (acond ((gethash thing *indicator-map*) it)
66 ((atom thing) (show-token thing nil))
68 (format nil "`~A'" (cadr thing)))
69 (t (format nil "<? ~S>" thing)))))
70 (funcall (if continuep #'cerror* #'error)
72 expected ~{~#[<bug>~;~A~;~A or ~A~:;~A, ~]~} ~
74 (mapcar #'show-expected expected)
75 (show-token (token-type scanner) (token-value scanner)))))
78 (defun lexer-error (char-scanner expected consumedp)
79 "Signal a continuable lexical error."
80 (cerror* "Lexical error: ~
81 expected ~{~#[<bug>~;~A~;~A or ~A~;:~A, ~]~} ~
82 but found ~/sod::show-char/~
86 (character (format nil "~/sod::show-char/" exp))
87 (string (format nil "`~A'" exp))
88 ((cons (eql :digit) *) (format nil "<radix-~A digit>"
90 ((eql :eof) "<end-of-file>")
91 ((eql :any) "<character>")
92 (t (format nil "<? ~S>" exp))))
94 (and (not (scanner-at-eof-p char-scanner))
95 (scanner-current-char char-scanner))
96 (and consumedp (file-location char-scanner))))
98 ;;;--------------------------------------------------------------------------
99 ;;; Lexical analysis utilities.
101 (defun scan-comment (char-scanner)
102 "Scan a comment (either `/* ... */' or `// ...') from CHAR-SCANNER.
104 The result isn't interesting."
105 (with-parser-context (character-scanner-context :scanner char-scanner)
108 (and (skip-many () (not #\*))
109 (label "*/" (skip-many (:min 1) #\*)))
113 (skip-many () (not #\newline))
116 ;;;----- That's all, folks --------------------------------------------------