Skip to content

Commit cc4f610

Browse files
committed
Added normal error support to src/lexer.lisp
1 parent ff30ed2 commit cc4f610

File tree

1 file changed

+86
-31
lines changed

1 file changed

+86
-31
lines changed

DiffBackend/src/lexer.lisp

Lines changed: 86 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,37 @@
88

99
(in-package :diff-backend/lexer)
1010

11-
(declaim (optimize safety (debug 3)))
11+
(declaim (optimize (debug 3)))
1212

1313
(defclass* lexem ()
14-
((line
15-
:accessor lexem-line
16-
:initarg :line
17-
:type integer)
18-
(column
19-
:accessor lexem-column
20-
:initarg :column
21-
:type integer)
22-
(type
23-
:accessor lexem-type
24-
:initarg :type
25-
:type symbol)
26-
(string
27-
:accessor lexem-string
28-
:initarg :string
29-
:type string))
30-
(:documentation "Lexem info"))
14+
((id
15+
:accessor id
16+
:initarg :id)
17+
(line
18+
:accessor lexem-line
19+
:initarg :line
20+
:type integer)
21+
(column
22+
:accessor lexem-column
23+
:initarg :column
24+
:type integer)
25+
(type
26+
:accessor lexem-type
27+
:initarg :type
28+
:type symbol)
29+
(string
30+
:accessor lexem-string
31+
:initarg :string
32+
:type string))
33+
(:documentation "Lexem info"))
34+
35+
(defclass* lexem-error ()
36+
((error-text
37+
:accessor error-text
38+
:initarg :error-text)
39+
(error-lex-id
40+
:accessor error-lex-id
41+
:initarg :error-lex-id)))
3142

3243
(defmethod print-object ((lex lexem) stream)
3344
(with-slots (line column string) lex
@@ -38,24 +49,30 @@
3849
(with-slots ((line1 line)
3950
(column1 column)
4051
(type1 type)
41-
(string1 string))
52+
(string1 string)
53+
(id1 id))
4254
lex1
4355
(with-slots ((line2 line)
4456
(column2 column)
4557
(type2 type)
46-
(string2 string))
58+
(string2 string)
59+
(id2 id))
4760
lex2
4861
(and (= line1 line2)
4962
(= column1 column2)
5063
(eq type1 type2)
51-
(string= string1 string2)))))
64+
(string= string1 string2)
65+
(or (= id1 -1)
66+
(= id2 -1)
67+
(= id1 id2))))))
5268

53-
(defun make-lexem (string line column type)
69+
(defun make-lexem (string line column type &key (id -1))
5470
(make-instance 'lexem
5571
:string string
5672
:line line
5773
:column column
58-
:type type))
74+
:type type
75+
:id id))
5976

6077
(defun is-lexem-symbol?= (lexem symbol-string)
6178
(when (eq (lexem-type lexem) :symbol)
@@ -92,8 +109,11 @@
92109
(when char1
93110
(char= char1 char2)))
94111

112+
(defparameter *cur-id* 0)
113+
95114
(defun lexer (file-str)
96-
(let ((stream (make-string-input-stream file-str))
115+
(let ((*cur-id* 0)
116+
(stream (make-string-input-stream file-str))
97117
(lexems)
98118
(comments-table (make-hash-table))
99119
(lex-errors)
@@ -122,7 +142,7 @@
122142
((ch= cur-char #\;) (go COMMENT))
123143
((is-whitespace? cur-char) (go WHITESPACE))
124144
((null cur-char) (go END))
125-
(t (error "Incorrect char ~s~%" cur-char)))
145+
(t (go ERROR_LEXEM)))
126146
INTEGER
127147
(incf column)
128148
(push cur-char lexem-l)
@@ -138,7 +158,8 @@
138158
(push (make-lexem (coerce (reverse lexem-l) 'string)
139159
cur-lexem-line
140160
cur-lexem-column
141-
:integer)
161+
:integer
162+
:id (incf *cur-id*))
142163
lexems)
143164
(setf lexem-l nil)
144165
(go COMMON)
@@ -154,15 +175,17 @@
154175
(push (make-lexem (coerce (reverse lexem-l) 'string)
155176
line
156177
cur-lexem-column
157-
:symbol)
178+
:symbol
179+
:id (incf *cur-id*))
158180
lexems)
159181
(setf lexem-l nil)
160182
(go COMMON)
161183
OUT_LEFT_PARENT
162184
(push (make-lexem "("
163185
line
164186
cur-lexem-column
165-
:left-parent)
187+
:left-parent
188+
:id (incf *cur-id*))
166189
lexems)
167190
(setf cur-char (read-char stream nil))
168191
(incf column)
@@ -171,7 +194,8 @@
171194
(push (make-lexem ")"
172195
line
173196
cur-lexem-column
174-
:right-parent)
197+
:right-parent
198+
:id (incf *cur-id*))
175199
lexems)
176200
(setf cur-char (read-char stream nil))
177201
(incf column)
@@ -188,7 +212,8 @@
188212
(push (make-lexem "'"
189213
line
190214
cur-lexem-column
191-
:quote)
215+
:quote
216+
:id (incf *cur-id*))
192217
lexems)
193218
(incf column)
194219
(setf cur-char (read-char stream nil))
@@ -205,6 +230,8 @@
205230
((ch= cur-char #\")
206231
(push cur-char lexem-l)
207232
(go OUT_STRING))
233+
((null cur-char)
234+
(go ERROR_LEXEM_OUT))
208235
(t (go STRING)))
209236
STRING_ESCAPE_SYMBOL
210237
(incf column)
@@ -215,7 +242,8 @@
215242
(push (make-lexem (coerce (reverse lexem-l) 'string)
216243
cur-lexem-line
217244
cur-lexem-column
218-
:string)
245+
:string
246+
:id (incf *cur-id*))
219247
lexems)
220248
(setf lexem-l nil)
221249
(setf cur-char (read-char stream nil))
@@ -236,6 +264,33 @@
236264
:column ,cur-lexem-column))
237265
(setf lexem-l nil)
238266
(go COMMON)
267+
ERROR_LEXEM
268+
(incf column)
269+
(push cur-char lexem-l)
270+
(setf cur-char (read-char stream nil))
271+
(cond ((or (ch= cur-char #\()
272+
(ch= cur-char #\))
273+
(ch= cur-char #\')
274+
(ch= cur-char #\")
275+
(ch= cur-char #\;)
276+
(is-whitespace? cur-char)
277+
(null cur-char))
278+
(go ERROR_LEXEM_OUT))
279+
(t (go ERROR_LEXEM)))
280+
ERROR_LEXEM_OUT
281+
(push (make-lexem (coerce (reverse lexem-l) 'string)
282+
cur-lexem-line
283+
cur-lexem-column
284+
:error-lexem
285+
:id (incf *cur-id*))
286+
lexems)
287+
(setf lexem-l nil)
288+
(push (make-instance
289+
'lexem-error
290+
:error-text (format nil "At (~S:~S) error lexem" cur-lexem-line cur-lexem-column)
291+
:error-lex-id *cur-id*)
292+
lex-errors)
293+
(go COMMON)
239294
END
240295
(return))
241296
(values (reverse lexems)

0 commit comments

Comments
 (0)