-
Notifications
You must be signed in to change notification settings - Fork 2
/
phps-mode-lexer.el
274 lines (226 loc) · 8.8 KB
/
phps-mode-lexer.el
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
;;; phps-mode-lexer.el -- Lexer for PHPs -*- lexical-binding: t -*-
;; Copyright (C) 2018-2024 Free Software Foundation, Inc.
;;; Commentary:
;; The idea is gathering everything directly related to the lexer in this file,
;; any higher order meta-lexer logic goes into `phps-mode-lex-analyzer.el'.
;;
;; Features:
;; * Defines the lexer for this grammar based on the Zend PHP 8.3 Lexer at
;; https://raw.githubusercontent.com/php/php-src/PHP-8.3/Zend/zend_language_scanner.l
;; which is using re2c.
;;; Code:
(require 'phps-mode-macros)
(require 'phps-mode-lexer-generator)
(require 'subr-x)
(define-error
'phps-lexer-error
"PHPs Lexer Error")
;; INITIALIZE SETTINGS
(phps-mode-lexer--CG
'parser-mode t)
(phps-mode-lexer--CG
'short-tags t)
;; VARIABLES
(defvar phps-mode-lexer--lambdas-by-state
(eval-when-compile (phps-mode-lexer-generator--lambdas))
"Hash-table of lex-analyzer rules organized by state.")
(defvar-local phps-mode-lexer--cached nil
"Hash-table of performed lexes to enable incremental lexing for the parser.")
(defvar-local phps-mode-lexer--cached-point nil
"The point up to where the cache should be used.")
;; LEXER FUNCTIONS BELOW
;; If multiple rules match, re2c prefers the longest match.
;; If rules match the same string, the earlier rule has priority.
;; @see http://re2c.org/manual/syntax/syntax.html
(defun phps-mode-lexer--re2c (index old-state)
"Elisp port of original Zend re2c lexer."
(let ((cache-key index)
(cache))
;; So if we have a cached lex, and we should use the cache for this point in the stream
;; make sure that it does not use a move operation or that the move operation is below
;; the valid point of the cache
(when (and
phps-mode-lexer--cached-point
(<= index phps-mode-lexer--cached-point)
phps-mode-lexer--cached)
(setq
cache
(gethash cache-key phps-mode-lexer--cached)))
(if (and
cache
(or
(equal (nth 1 cache) nil)
(<= (nth 1 cache) phps-mode-lexer--cached-point)))
(progn
(phps-mode-debug-message
(message
"\nReturning cached lex for key %S: %S"
cache-key
cache))
cache)
(let ((eof (>= index (point-max))))
(if eof
(progn
(phps-mode-debug-message
(message "Signal end of input at %S" index))
(list nil nil old-state))
;; Set state here
(let ((old-state-state (nth 0 old-state))
(old-state-stack (nth 1 old-state))
(old-state-heredoc-label (nth 2 old-state))
(old-state-heredoc-label-stack (nth 3 old-state))
(old-state-nest-location-stack (nth 4 old-state)))
(setq
phps-mode-lexer--state
old-state-state)
(setq
phps-mode-lexer--state-stack
old-state-stack)
(setq
phps-mode-lexer--heredoc-label
old-state-heredoc-label)
(setq
phps-mode-lexer--heredoc-label-stack
old-state-heredoc-label-stack)
(setq
phps-mode-lexer--nest-location-stack
old-state-nest-location-stack))
;; Reset generated tokens
(goto-char index)
(setq
phps-mode-lexer--generated-new-tokens
nil)
(setq
phps-mode-lexer--generated-new-tokens-index
index)
(let ((tokens)
(new-state)
(move-to-index)
(continue-lexer t))
(while continue-lexer
(phps-mode-debug-message
(let ((start (point))
(end (+ (point) 5))
(lookahead))
(when (> end (point-max))
(setq end (point-max)))
(setq
lookahead
(buffer-substring-no-properties
start
end))
(message
"\nRunning lexer from point %s, state: %S, lookahead: '%s'.."
(point)
old-state
lookahead)))
(setq phps-mode-lexer--move-flag nil)
(setq phps-mode-lexer--restart-flag nil)
(setq continue-lexer nil)
;; Run rules based on state
(phps-mode-lexer--reset-match-data)
(when-let ((lambdas
(gethash
phps-mode-lexer--state
phps-mode-lexer--lambdas-by-state)))
(let ((lambda-i 0)
(lambda-length (length lambdas)))
(phps-mode-debug-message
(message
"Found %d lexer rules in state"
lambda-length))
(while (< lambda-i lambda-length)
(let ((lambd (nth lambda-i lambdas)))
(let ((lambda-result
(funcall (nth 0 lambd))))
(when lambda-result
(let ((match-end (match-end 0))
(match-beginning (match-beginning 0)))
(let ((matching-length (- match-end match-beginning)))
(when (> matching-length 0)
(when (or
(not phps-mode-lexer--match-length)
(> matching-length phps-mode-lexer--match-length))
(setq
phps-mode-lexer--match-length matching-length)
(setq
phps-mode-lexer--match-body (nth 1 lambd))
(setq
phps-mode-lexer--match-data (match-data))
;; Debug new matches
(phps-mode-debug-message
(message
"Found new best match, with length: %d, and body: %s"
phps-mode-lexer--match-length
phps-mode-lexer--match-body))))))))
(when (fboundp 'thread-yield)
(thread-yield)))
(setq lambda-i (1+ lambda-i)))))
(unless phps-mode-lexer--match-length
(error "Failed to lex at %S" index))
(phps-mode-debug-message
(message
"Found final match %s"
phps-mode-lexer--match-body))
(phps-mode-lexer--re2c-execute)
(cond
(phps-mode-lexer--move-flag
(phps-mode-debug-message
(message
"Found move signal to %s"
phps-mode-lexer--move-flag))
(setq
move-to-index
phps-mode-lexer--move-flag))
(t
(when phps-mode-lexer--restart-flag
(phps-mode-debug-message
(message "Found signal to restart lexer"))
(setq continue-lexer t)))
)
)
(setq
tokens
phps-mode-lexer--generated-new-tokens)
(setq
new-state
(list
phps-mode-lexer--state
phps-mode-lexer--state-stack
phps-mode-lexer--heredoc-label
phps-mode-lexer--heredoc-label-stack
phps-mode-lexer--nest-location-stack))
(unless phps-mode-lexer--cached
(setq
phps-mode-lexer--cached
(make-hash-table :test 'equal)))
(let ((lexer-response
(list tokens move-to-index new-state)))
(phps-mode-debug-message
(message
"\nStored cached lex for key %S: %S"
cache-key
lexer-response))
(puthash
cache-key
lexer-response
phps-mode-lexer--cached)
lexer-response)))))))
(defun phps-mode-lexer--re2c-execute ()
"Execute matching body (if any)."
(if phps-mode-lexer--match-body
(progn
(set-match-data phps-mode-lexer--match-data)
(funcall phps-mode-lexer--match-body))
(signal
'phps-lexer-error
(list
(format "Found no matching lexer rule to execute at %d" (point))
(point)))))
(defun phps-mode-lexer--reset-match-data ()
"Reset match data."
(setq phps-mode-lexer--match-length 0)
(setq phps-mode-lexer--match-data nil)
(setq phps-mode-lexer--match-body nil))
(provide 'phps-mode-lexer)
;;; phps-mode-lexer.el ends here