src/lexer-proto.lisp, ...: Kill `lexer-error' pointless CONSUMEDP flag.
[sod] / src / lexer-proto.lisp
CommitLineData
dea4d055
MW
1;;; -*-lisp-*-
2;;;
3;;; Protocol for lexical analysis
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 definition.
dea4d055 30
239fa5bd
MW
31(export 'sod-token-scanner)
32(defclass sod-token-scanner (token-scanner)
33 ((char-scanner :initarg :char-scanner :reader token-scanner-char-scanner))
dea4d055 34 (:documentation
239fa5bd 35 "A token scanner for SOD input files.
dea4d055 36
239fa5bd
MW
37 Not a lot here, apart from a character scanner to read from and the
38 standard token scanner infrastructure."))
dea4d055 39
dea4d055 40;;;--------------------------------------------------------------------------
239fa5bd
MW
41;;; Indicators and error messages.
42
1d8cc67a
MW
43(defvar *indicator-map* (make-hash-table)
44 "Hash table mapping indicator objects to human-readable descriptions.")
45
239fa5bd
MW
46(export 'define-indicator)
47(defun define-indicator (indicator description)
48 "Associate an INDICATOR with its textual DESCRIPTION.
49
50 Updates the the `*indicator-map*'."
51 (setf (gethash indicator *indicator-map*) description)
52 indicator)
53
54(export 'syntax-error)
55(defun syntax-error (scanner expected &key (continuep t))
56 "Signal a (maybe) continuable syntax error."
57 (labels ((show-token (type value)
58 (if (characterp type)
59 (format nil "~/sod::show-char/" type)
60 (case type
61 (:id (format nil "<identifier~@[ `~A'~]>" value))
048d0b2d 62 (:int "<integer-literal>")
239fa5bd
MW
63 (:string "<string-literal>")
64 (:char "<character-literal>")
65 (:eof "<end-of-file>")
66 (:ellipsis "`...'")
67 (t (format nil "<? ~S~@[ ~S~]>" type value)))))
68 (show-expected (thing)
69 (acond ((gethash thing *indicator-map*) it)
70 ((atom thing) (show-token thing nil))
71 ((eq (car thing) :id)
72 (format nil "`~A'" (cadr thing)))
73 (t (format nil "<? ~S>" thing)))))
74 (funcall (if continuep #'cerror* #'error)
75 "Syntax error: ~
76 expected ~{~#[<bug>~;~A~;~A or ~A~:;~A, ~]~} ~
77 but found ~A"
78 (mapcar #'show-expected expected)
79 (show-token (token-type scanner) (token-value scanner)))))
80
81(export 'lexer-error)
26c5ecfe 82(defun lexer-error (char-scanner expected)
239fa5bd
MW
83 "Signal a continuable lexical error."
84 (cerror* "Lexical error: ~
c1098ad8 85 expected ~{~#[<bug>~;~A~;~A or ~A~:;~A, ~]~} ~
26c5ecfe 86 but found ~/sod::show-char/"
239fa5bd
MW
87 (mapcar (lambda (exp)
88 (typecase exp
89 (character (format nil "~/sod::show-char/" exp))
90 (string (format nil "`~A'" exp))
91 ((cons (eql :digit) *) (format nil "<radix-~A digit>"
92 (cadr exp)))
93 ((eql :eof) "<end-of-file>")
94 ((eql :any) "<character>")
95 (t (format nil "<? ~S>" exp))))
96 expected)
97 (and (not (scanner-at-eof-p char-scanner))
26c5ecfe 98 (scanner-current-char char-scanner))))
dea4d055 99
33b5686f 100(export 'skip-until)
048d0b2d
MW
101(defparse skip-until (:context (context token-scanner-context)
102 (&key (keep-end nil keep-end-p))
103 &rest token-types)
104 "Discard tokens until we find one listed in TOKEN-TYPES.
105
106 If KEEP-END is true then retain the found token for later; otherwise
107 discard it. KEEP-END defaults to true if multiple TOKEN-TYPES are given;
108 otherwise false. If end-of-file is encountered then the indicator list is
109 simply the list of TOKEN-TYPES; otherwise the result is `nil'."
110 `(skip-until ,(parser-scanner context)
111 (list ,@token-types)
112 :keep-end ,(if keep-end-p keep-end
113 (> (length token-types) 1))))
114
33b5686f 115(export 'error)
048d0b2d 116(defparse error (:context (context token-scanner-context)
012554e1
MW
117 (&key ignore-unconsumed)
118 sub &optional (recover t))
048d0b2d
MW
119 "Try to parse SUB; if it fails then report an error, and parse RECOVER.
120
121 This is the main way to recover from errors and continue parsing. Even
122 then, it's not especially brilliant.
123
124 If the SUB parser succeeds then just propagate its result: it's like we
125 were never here. Otherwise, try to recover in a sensible way so we can
126 continue parsing. The details of this recovery are subject to change, but
127 the final action is generally to invoke the RECOVER parser and return its
012554e1
MW
128 result.
129
130 If IGNORE-UNCONSUMED evaluates non-nil, then just propagate a failure of
131 SUB if it didn't consume input. (This makes it suitable for use where the
132 parser containing `error' might be optional.)"
048d0b2d
MW
133 `(parse-error-recover ,(parser-scanner context)
134 (parser () ,sub)
012554e1
MW
135 (parser () ,recover)
136 :ignore-unconsumed ,ignore-unconsumed))
048d0b2d 137
dea4d055 138;;;--------------------------------------------------------------------------
239fa5bd
MW
139;;; Lexical analysis utilities.
140
33b5686f 141(export 'scan-comment)
239fa5bd
MW
142(defun scan-comment (char-scanner)
143 "Scan a comment (either `/* ... */' or `// ...') from CHAR-SCANNER.
144
145 The result isn't interesting."
146 (with-parser-context (character-scanner-context :scanner char-scanner)
147 (parse (or (and "/*"
148 (and (skip-many ()
149 (and (skip-many () (not #\*))
150 (label "*/" (skip-many (:min 1) #\*)))
151 (not #\/))
152 #\/))
153 (and "//"
154 (skip-many () (not #\newline))
155 (? #\newline))))))
dea4d055
MW
156
157;;;----- That's all, folks --------------------------------------------------