-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlexer.lisp
187 lines (166 loc) · 6.09 KB
/
lexer.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(in-package #:org.shirakumo.plump.lexer)
(defvar *string*)
(defvar *length*)
(defvar *index*)
(defvar *matchers* (make-hash-table))
(declaim (fixnum *length* *index*)
(simple-string *string*)
(hash-table *matchers*))
(defmacro with-lexer-environment ((string) &body body)
`(let* ((*string* ,string)
(*string* (etypecase *string*
(simple-string *string*)
(string (copy-seq *string*))))
(*length* (length *string*))
(*index* 0))
(handler-bind ((error #'(lambda (err)
(declare (ignore err))
(format T "Error during lexing at index ~a~%" *index*))))
,@body)))
(declaim (ftype (function () (or character null)) consume)
(inline consume))
(defun consume ()
(declare (optimize (speed 3) (safety 0)))
(when (< *index* *length*)
(prog1 (aref *string* *index*)
#+plump-debug-lexer (format T "~a +~%" *index*)
(incf *index*))))
(declaim (ftype (function () (or fixnum null)) advance)
(inline advance))
(defun advance ()
(declare (optimize (speed 3) (safety 0)))
(when (< *index* *length*)
#+plump-debug-lexer (format T "~a +~%" *index*)
(incf *index*)))
(declaim (ftype (function () fixnum) unread)
(inline unread))
(defun unread ()
(declare (optimize (speed 3) (safety 0)))
(when (< 0 *index*)
#+plump-debug-lexer (format T "~a -~%" *index*)
(decf *index*))
*index*)
(declaim (ftype (function () (or character null)) peek)
(inline peek))
(defun peek ()
(declare (optimize (speed 3) (safety 0)))
(when (< *index* *length*)
#+plump-debug-lexer (format T "~a ?~%" *index*)
(aref *string* *index*)))
(declaim (ftype (function (fixnum) fixnum) advance-n)
(inline advance-n))
(defun advance-n (n)
(declare (optimize (speed 3) (safety 0)))
(declare (fixnum n))
#+plump-debug-lexer (format T "~a +~d~%" *index* n)
(incf *index* n)
(when (<= *length* *index*)
(setf *index* *length*))
*index*)
(declaim (ftype (function (fixnum) fixnum) unread-n)
(inline unread-n))
(defun unread-n (n)
(declare (optimize (speed 3) (safety 0)))
(declare (fixnum n))
#+plump-debug-lexer (format T "~a -~d~%" *index* n)
(decf *index* n)
(when (< *index* 0)
(setf *index* 0))
*index*)
(declaim (ftype (function (function) string) consume-until))
(defun consume-until (matcher)
(declare (function matcher))
(loop with start = *index*
until (funcall matcher)
while (advance)
finally (return (subseq *string* start *index*))))
(declaim (ftype (function (character) function) matcher-character))
(defun matcher-character (character)
#'(lambda ()
(let ((char (peek)))
(when char
(char= char character)))))
(declaim (ftype (function (simple-string) function) matcher-string))
(defun matcher-string (string)
(declare (simple-string string))
(let ((len (length string)))
#'(lambda ()
(let ((len (+ *index* len)))
(and (<= len *length*)
(string= string *string* :start2 *index* :end2 len))))))
(declaim (ftype (function ((or fixnum character string) (or fixnum character string)) function) matcher-range))
(defun matcher-range (from to)
(flet ((normalize (in) (etypecase in
(fixnum in)
(character (char-code in))
(string (char-code (aref in 0))))))
(let ((from (normalize from))
(to (normalize to)))
#'(lambda ()
(let ((char (peek)))
(when char
(<= from (char-code char) to)))))))
(declaim (ftype (function (list) function) matcher-find))
(defun matcher-find (list)
#'(lambda ()
(let ((char (peek)))
(and char (member char list :test #'char=)))))
(declaim (ftype (function (&rest function) function) matcher-or))
(defun matcher-or (&rest matchers)
#'(lambda ()
(loop for matcher of-type function in matchers
thereis (funcall matcher))))
(declaim (ftype (function (&rest function) function) matcher-and))
(defun matcher-and (&rest matchers)
#'(lambda ()
(loop for matcher of-type function in matchers
always (funcall matcher))))
(declaim (ftype (function (function) function) matcher-not))
(defun matcher-not (matcher)
(declare (function matcher))
#'(lambda ()
(not (funcall matcher))))
(declaim (ftype (function (function) function) matcher-next))
(defun matcher-next (matcher)
#'(lambda ()
(let ((*index* (1+ *index*)))
(when (< *index* *length*)
(funcall matcher)))))
(declaim (ftype (function (function) function) matcher-prev))
(defun matcher-prev (matcher)
#'(lambda ()
(let ((*index* (1- *index*)))
(when (<= 0 *index*)
(funcall matcher)))))
(defmacro matcher-any (&rest is)
`(matcher-or ,@(loop for i in is
collect `(,(typecase i
(string 'matcher-string)
(character 'matcher-character)
(T 'matcher-string)) ,i))))
(defmacro make-matcher (form)
(labels ((transform (form)
(etypecase form
(keyword
`(gethash ',form *matchers*))
(atom form)
(T
(cons
(case (find-symbol (string (car form)) "PLUMP-LEXER")
(not 'matcher-not)
(and 'matcher-and)
(or 'matcher-or)
(is (typecase (second form)
(string 'matcher-string)
(character 'matcher-character)
(T 'matcher-string)))
(in 'matcher-range)
(next 'matcher-next)
(prev 'matcher-prev)
(any 'matcher-any)
(find 'matcher-find)
(T (car form)))
(mapcar #'transform (cdr form)))))))
(transform form)))
(defmacro define-matcher (name form)
`(setf (gethash ,(intern (string name) "KEYWORD") *matchers*) (make-matcher ,form)))