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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
;;; Guile Lua --- tokenizer
;;; Copyright (C) 2010 Free Software Foundation, Inc.
;;;
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;; Code:
;; This is a simple lexer. It generally matches up Lua data types with
;; Scheme. Reserved words in Lua, like 'not', are returned as keywords,
;; like '#:not'. Operators are returned as keywords like #:==, or
;; characters like #\+ when they're only a character long. Identifiers
;; are returned as symbols
(define-module (language lua lexer)
#:use-module (srfi srfi-8)
#:use-module (srfi srfi-14)
#:use-module (srfi srfi-39)
#:use-module (language lua common)
#:export (make-lexer))
(define stdout (current-output-port))
(define (source-info port)
`((filename . ,(port-filename port))
(line . ,(port-line port))
(column . ,(port-column port))))
;; Character predicates
;; Lua only accepts ASCII characters as of 5.2, so we define our own
;; charsets here
(define (char-predicate string)
(let ((char-set (string->char-set string)))
(lambda (c)
(and (not (eof-object? c)) (char-set-contains? char-set c)))))
(define is-digit? (char-predicate "0123456789"))
(define is-name-first? (char-predicate "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"))
(define (is-name? c) (or (is-name-first? c) (is-digit? c)))
(define (is-newline? c) (and (char? c) (or (char=? c #\newline) (char=? c #\cr))))
(define *reserved-words*
'(return function end if then elseif else true false nil or and
do while repeat until local for break in not))
(define (possible-keyword token)
"Convert a symbol to a keyword if it is a reserved word in Lua"
(if (memq token *reserved-words*)
(symbol->keyword token)
token))
(define (make-lexer port)
;; Buffer management
(define buffer (open-output-string))
(define (drop-buffer)
"Clear the buffer and drop the contents"
(truncate-file buffer 0))
(define (clear-buffer)
"Clear the buffer and return a string of the contents"
(let ((string (get-output-string buffer)))
(drop-buffer)
string))
;; Source code information
(define saved-source-info #f)
(define (save-source-info)
"Save source code information for a particular location e.g. the beginning
of an identifier"
(set! saved-source-info (source-info port)))
(define (get-source-info)
"Get source code information"
(or saved-source-info
(source-info port)))
(define (save-and-next!)
"Shorthand for (write-char (read-char))"
(write-char (read-char)))
(define (eat-comment)
"Consume a comment"
(let consume ((c (read-char)))
(cond ((eof-object? c) #f)
((eqv? c #\newline) #f)
(else (consume (read-char))))))
(define (get-long-string-nesting-level)
"Return the nesting level of a bracketed string, or -1 if it is not one"
(define delimiter (read-char))
(let* ((count
(let loop ((count 0))
(if (eqv? (peek-char) #\=)
(begin
(read-char)
(loop (+ count 1)))
count))))
(if (eqv? (peek-char) delimiter) count -1)))
(define (read-long-string string? nest)
"Read a long string or comment"
;; Skip second bracket
(read-char)
;; Discard initial newlines, which is what Lua does
(while (is-newline? (peek-char))
(read-char))
;; Read string contents
(let loop ((c (peek-char)))
(cond
;; Error out if end-of-file is encountered
((eof-object? c)
(syntax-error (get-source-info)
(string-append "unfinished long "
(if string? "string" "comment"))))
;; Check to see if we've reached the end
((char=? c #\])
(let* ((nest2 (get-long-string-nesting-level)))
(if (= nest nest2)
(begin
(read-char) ;; drop ]
(if string?
(clear-buffer)
(drop-buffer)))
;; Compensate for eating up the nesting levels
(begin
(save-and-next!)
(let lp ((n nest2))
(if (= n 0)
#f
(begin
(write-char #\=)
(lp (- n 1)))))
(write-char #\])
(loop (peek-char))))))
;; Save character and continue
(else (save-and-next!)
(loop (peek-char))))))
;; read a single or double quoted string, with escapes
(define (read-string delimiter)
(read-char) ;; consume delimiter
(let loop ((c (peek-char)))
(cond
;; string ends early
((or (eof-object? c) (eqv? c #\cr) (eqv? c #\newline))
(syntax-error (get-source-info) "unfinished string ~S" c))
;; string escape
((char=? c #\\)
;; discard \ and read next character
(let* ((escape (begin (read-char) (read-char))))
(write-char
(case escape
((#\a) #\alarm)
((#\b) #\backspace)
((#\f) #\page)
((#\n) #\newline)
((#\r) #\return)
((#\t) #\tab)
((#\v) #\vtab)
((#\x) (syntax-error (get-source-info)
"hex escapes unsupported"))
((#\d) (syntax-error (get-source-info)
"decimal escapes unsupported"))
(else escape)))
(loop (peek-char))))
(else
(if (eqv? c delimiter)
(read-char) ;; terminate loop and discard delimiter
(begin
(save-and-next!)
(loop (peek-char)))))))
(clear-buffer))
(define (read-number string)
(save-source-info)
(let* ((main (string-append
(or string "")
(begin
(while (or (is-digit? (peek-char)) (eqv? (peek-char) #\.))
(save-and-next!))
(clear-buffer))))
(exponent
(if (or (eqv? (peek-char) #\e) (eqv? (peek-char) #\E))
(begin
(read-char)
(if (eqv? (peek-char) #\+)
(read-char)
(if (eqv? (peek-char) #\-)
(save-and-next!)))
(if (not (is-digit? (peek-char)))
(syntax-error (get-source-info)
"expecting number after exponent sign"))
(while (is-digit? (peek-char))
(save-and-next!))
(clear-buffer))
#f))
(final (string->number main)))
(if exponent
(* final (expt 10 (string->number exponent)))
final)))
(define (lex)
(parameterize ((current-input-port port)
(current-output-port buffer))
(set! saved-source-info #f)
(let loop ()
(define c (peek-char))
(case c
;; Skip spaces
((#\newline #\return #\space #\page #\tab #\vtab) (read-char) (loop))
;; Either a minus (-), or a long comment, which is a -
;; followed by a bracketed string
((#\-)
(read-char)
(if (eqv? (peek-char) #\-)
;; It's a comment
(begin
(read-char)
;; Long comment
(if (eqv? (peek-char) #\[)
(let* ((nest (get-long-string-nesting-level)))
(drop-buffer)
(if (not (negative? nest))
(begin
(read-long-string #f nest)
(drop-buffer)
(loop))
;; If it's not actually a long comment, drop it
(begin (drop-buffer) (eat-comment) (loop))))
(begin (eat-comment) (loop))))
;; It's a regular minus
#\-))
;; ~=
((#\~)
(read-char)
(if (eqv? (peek-char) #\=)
(begin (read-char) #:~=)
(syntax-error (get-source-info)
"expected = after ~ but got ~c" c)))
;; < or <=
((#\<)
(read-char)
(if (eqv? (peek-char) #\=) (begin (read-char) #:<=) #\<))
;; > or >=
((#\>)
(read-char)
(if (eqv? (peek-char) #\=) (begin (read-char) #:>=) #\>))
;; = or ==
((#\=)
(read-char)
(if (eqv? (peek-char) #\=)
(begin (read-char) #:==)
#:=))
;; . can mean one of: floating point number (.12345), table
;; field access (plain .), concatenation operator (..) or the
;; variable argument indicator (...)
((#\.)
(read-char)
(if (is-digit? (peek-char))
(read-number ".")
(if (eqv? (peek-char) #\.)
(begin
(read-char)
(if (eqv? (peek-char) #\.)
(begin (read-char) #:dots)
#:concat))
#\.)))
;; Double-quoted string
((#\") (read-string #\"))
;; Single-quoted string
((#\') (read-string #\'))
;; Bracketed string
((#\[)
(save-source-info)
(let* ((nest (get-long-string-nesting-level)))
(if (eqv? nest -1)
#\[
(read-long-string #t nest))))
;; Characters that are allowed to fall through directly to the
;; parser
((#\; #\( #\) #\,
#\+ #\/ #\*
#\^ #\{ #\} #\] #\: #\#) (read-char))
;; Numbers
((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
(save-source-info)
(save-and-next!)
(read-number #f))
(else
(cond ((eof-object? c) c)
;; Identifier or keyword
((is-name-first? c)
(save-and-next!)
(save-source-info)
(while (is-name? (peek-char))
(save-and-next!))
(possible-keyword (string->symbol (clear-buffer))))
(else
(syntax-error (get-source-info)
"disallowed character ~c" c))))))))
(values get-source-info lex))
|