-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlexer.red
538 lines (525 loc) · 11.5 KB
/
lexer.red
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
Red [
Title: "Red runtime lexer"
Author: "bitbegin"
File: %lexer.red
Tabs: 4
Rights: "Copyright (C) 2020 Red Foundation. All rights reserved."
]
lexer: context [
all-path!: reduce [path! lit-path! get-path! set-path!]
noset-path!: reduce [path! lit-path! get-path!]
all-pair!: reduce [block! paren! map!]
pre-path!: reduce [lit-path! get-path!]
uri-to-file: function [uri [string!] return: [file!]][
src: copy find/tail uri "file:///"
to-red-file dehex src
]
file-to-uri: function [file [file!] return: [string!]][
src: to-local-file file
replace/all src "\" "/"
insert src "file:///"
src
]
parse-line: function [stack [block!] src [string!]][
append stack src
append stack index? src
while [src: find/tail src #"^/"][
append stack index? src
]
]
line-pos?: function [stack [block!] line [integer!] column [integer!] return: [string!]][
pos: pick stack line + 1
skip at stack/1 pos column - 1
]
index-line?: function [stack [block!] pos [integer!] return: [pair!]][
stack: next stack
forall stack [
if all [
stack/1 <= pos
any [
none? stack/2
stack/2 > pos
]
][
column: pos - stack/1
return as-pair (index? stack) - 1 column + 1
]
]
none
]
pos-line?: function [stack [block!] pos [string!] return: [pair!]][
index-line? stack index? pos
]
pos-range?: function [stack [block!] s [string!] e [string!] return: [block!]][
range: make block! 4
append range pos-line? stack s
append range pos-line? stack e
range
]
;-- Range in lsp is zero-based position, use /keep for print
form-range: function [range [block!] return: [map!] /keep][
make map! reduce [
'start make map! reduce [
'line either keep [range/1/x][range/1/x - 1]
'character either keep [range/1/y][range/1/y - 1]
]
'end make map! reduce [
'line either keep [range/2/x][range/2/x - 1]
'character either keep [range/2/y][range/2/y - 1]
]
]
]
load-range: function [stack [block!] range [block!]][
start: line-pos? stack range/1/x range/1/y
stop: line-pos? stack range/2/x range/2/y
try [load copy/part start stop]
]
remove-last-empty-nested: func [
item [block!]
][
if all [
item/nested
empty? item/nested
][
item/nested: none
]
]
insert-node: function [
stack [block!]
lines [block!]
src [string!]
return: [block!]
][
add-node: func [
x [integer!]
y [integer!]
type [datatype! word!]
expr
error
/local nested
][
nested: select last stack 'nested
repend/only nested [
'range reduce [index-line? lines x index-line? lines y]
'type type
'upper back tail stack
]
if expr [
repend last nested ['expr reduce [expr]]
]
if error [
repend last nested ['error error]
]
]
push-node: func [
x [integer!]
type [datatype!]
return: [block!]
/local nested
][
nested: select last stack 'nested
repend/only nested [
'range reduce [index-line? lines x]
'type type
'upper back tail stack
]
repend last nested ['nested reduce []]
stack: nested
]
lex: func [
event [word!]
input [string! binary!]
type [datatype! word! none!]
line [integer!]
token
return: [logic!]
/local ltype x y err str
][
[prescan scan load open close error]
close-path: func [
y [integer!]
err [block!]
/local item
][
item: last stack
append item/range index-line? lines y
repend item ['error err]
stack: item/upper
]
match-pair: func [
x [integer!]
y [integer!]
/local ltype item nstop
][
forever [
unless ltype: select last stack 'type [ ;-- check if top
add-node x y type none [code only-closed]
break
]
unless nstop [
nstop: index-line? lines y
]
if any [ ;-- match the upper's type
ltype = type
all [
find noset-path! ltype
type = set-path!
]
all [
ltype = map!
type = paren!
]
][
item: last stack
if type = set-path! [
item/type: type
]
append item/range nstop
remove-last-empty-nested item
stack: item/upper
break
]
item: last stack
append item/range nstop
repend item ['error [code only-opened]]
remove-last-empty-nested item
stack: item/upper
]
]
in-path?: func [
/local p? ltype
][
p?: no
if all [
ltype: select last stack 'type
ltype
][
if find noset-path! ltype [
p?: yes
]
]
p?
]
;print [event mold type token mold input]
switch event [
prescan [
pretoken: token
if all [
type = 'eof
input/1 = #";"
][
add-node token/x token/y 'comment none none
]
true
]
scan [
stoken: either all [start stop][ ;-- string! need adjust the position
as-pair start stop
][token]
stype: type
if stype = 'comment [
add-node stoken/x stoken/y stype none none
]
true
]
load [
add-node stoken/x stoken/y stype token none
start: stop: none stoken: none
true
]
open [
either type = string! [
if none? start [
start: token/x
]
][
push-node token/x type
]
true
]
close [
either type = string! [
stop: token/y + 1
][
nstop: none
x: token/x
either find noset-path! type [
y: token/y
][
switch input/1 [
#")" [type: paren!]
#"]" [type: block!]
#"}" [type: string!]
]
y: token/y + 1
input: next input
]
close-y: token/y
match-pair x y
]
true
]
error [
case [
find noset-path! type [
case [
input/1 = #"/" [ ;-- eof after /
y: token/y + 1
input: next input
err: [code slash]
]
input/-1 = #"/" [ ;-- slash
y: token/y
err: [code slash]
]
true [
y: token/y + 1
input: next input
err: [code unknown]
]
]
close-path y err
]
find all-pair! type [
if all [
token/y > close-y
find ")]}" input/1
][
switch input/1 [
#")" [type: paren!]
#"]" [type: block!]
#"}" [type: string!]
]
match-pair token/x token/y + 1
]
input: next input
]
in-path? [
s: skip input token/x - token/y
err: reduce ['code type 'expr copy/part s input]
close-path token/y err
]
type = string! [
;-- multiline
either start [
x: start
y: token/y
input: next input
][
;-- have scaned
either stoken [
x: pretoken/x
y: pretoken/y + 1
input: next input
][
either input/1 = #"^"" [
x: pretoken/x
y: pretoken/y + 1
input: next input
][
x: pretoken/x
y: pretoken/y
]
]
]
stoken: none
err: reduce ['code 'only-opened 'at token/x - x]
add-node x y type none err
]
type = error! [
either input/1 = #"}" [
type: string!
err: [code only-closed]
][
err: [code only-opened]
]
input: next input
add-node token/x token/y + 1 type none err
]
type = char! [
either input/1 = #"^"" [
y: token/y + 1
input: next input
err: [code invalid]
][
y: token/y
err: [code not-closed]
]
add-node token/x y type none err
]
type = binary! [
either input/1 = #"}" [
y: token/y + 1
input: next input
err: [code invalid]
][
y: token/y
err: [code not-closed]
]
add-node token/x y type none err
]
true [
add-node token/x token/y type none [code unknown]
]
]
false
]
]
]
top: stack
pretoken: none ;-- used for store prescan token
start: none ;-- used for mark the begin of string!
stop: none ;-- used for mark the end of string!
stoken: none ;-- used for store scan token
stype: none ;-- used for store scan type
close-y: 0
system/words/transcode/trace src :lex
stop: none
while [stack <> top][
unless stop [
stop: index-line? lines index? tail src
]
item: last stack
append item/range stop
either item/error [
item/error/code: 'only-opened
][
append item [error [code only-opened]]
]
remove-last-empty-nested item
stack: item/upper
]
top
]
transcode: function [
src [string!]
return: [block!]
][
unless head? src [return none]
lines: make block! 64
parse-line lines src
range: make block! 1
append range 1x1
append range pos-line? lines tail src
stack: reduce [reduce ['source src 'lines lines 'range range 'nested reduce []]]
top: stack
insert-node stack lines src
if empty? top/1/nested [
top/1/nested: none
]
top
]
format: function [top [block!]][
buffer: make string! 1000
newline: function [cnt [integer!]] [
append buffer lf
append/dup buffer " " cnt
]
format*: function [pc [block!] depth [integer!]][
pad: depth * 4
newline pad
append buffer "["
forall pc [
newline pad + 2
append buffer "["
if pc/1/expr [
newline pad + 4
append buffer "expr: "
append buffer mold/flat/part pc/1/expr/1 20
]
if pc/1/range [
newline pad + 4
append buffer "range: "
append buffer mold/flat pc/1/range
]
if pc/1/type [
newline pad + 4
append buffer "type: "
append buffer mold/flat pc/1/type
]
if pc/1/nested [
newline pad + 4
append buffer "nested: "
format* pc/1/nested depth + 1
]
if pc/1/source [
newline pad + 4
append buffer "source: "
append buffer mold/flat/part pc/1/source 10
]
if lines: pc/1/lines [
newline pad + 4
append buffer "lines: ["
newline pad + 6
append buffer mold/flat/part lines/1 10
lines: next lines
forall lines [
newline pad + 6
append buffer mold lines/1
]
newline pad + 4
append buffer "]"
]
if upper: pc/1/upper [
newline pad + 4
append buffer "upper: "
append buffer mold/flat upper/1/range
]
if error: pc/1/error [
newline pad + 4
append buffer "error: "
append buffer mold/flat/part error 30
]
newline pad + 2
append buffer "]"
]
newline pad
append buffer "]"
]
format* top 0
buffer
]
sformat: function [top [block!]][
buffer: make string! 1000
newline: function [cnt [integer!]] [
append buffer lf
append/dup buffer " " cnt
]
format*: function [pc [block!] depth [integer!]][
pad: depth * 4
newline pad
append buffer "["
forall pc [
newline pad + 2
append buffer "["
append buffer "range: "
append buffer mold/flat pc/1/range
if upper: pc/1/upper [
append buffer " upper: "
append buffer mold/flat upper/1/range
]
if pc/1/type [
append buffer " type: "
append buffer mold/flat pc/1/type
]
if pc/1/expr [
append buffer " expr: "
append buffer mold/flat/part pc/1/expr/1 20
]
if error: pc/1/error [
append buffer " error: "
append buffer mold/flat/part error 30
]
if pc/1/nested [
append buffer " nested: "
format* pc/1/nested depth + 1
]
append buffer "]"
]
newline pad
append buffer "]"
]
format* top 0
buffer
]
]