-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.r
663 lines (574 loc) · 16.5 KB
/
lexer.r
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
REBOL [
Title: "Red Lexical Scanner"
Author: "Nenad Rakocevic"
File: %lexer.r
Tabs: 4
Rights: "Copyright (C) 2011-2015 Nenad Rakocevic. All rights reserved."
License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt"
]
lexer: context [
verbose: 0
line: none ;-- source code lines counter
lines: [] ;-- offsets of newlines marker in current block
count?: yes ;-- if TRUE, lines counter is enabled
cnt: none ;-- counts nested {} in multi-line strings
pos: none ;-- source input position (error reporting)
path: none ;-- path input position (error reporting)
s: none ;-- mark start position of new value
e: none ;-- mark end position of new value
value: none ;-- new value
value2: none ;-- secondary new value
fail?: none ;-- used for failing some parsing rules
type: none ;-- define the type of the new value
rs?: no ;-- if TRUE, do lexing for Red/System
neg?: no ;-- if TRUE, denotes a negative number value
base: 16 ;-- binary base
;====== Parsing rules ======
four: charset "01234"
half: charset "012345"
non-zero: charset "123456789"
digit: union non-zero charset "0"
dot: #"."
comma: #","
byte: [
"25" half
| "2" four digit
| "1" digit digit
| non-zero digit
| digit
]
hexa: union digit charset "ABCDEF"
hexa-char: union hexa charset "abcdef"
base64-char: union digit charset [#"A" - #"Z" #"a" - #"z" #"+" #"/" #"="]
;-- UTF-8 encoding rules from: http://tools.ietf.org/html/rfc3629#section-4
UTF-8-BOM: #{EFBBBF}
ws-ASCII: charset " ^-^M" ;-- ASCII common whitespaces
ws-U+2k: charset [#"^(80)" - #"^(8A)"] ;-- Unicode spaces in the U+2000-U+200A range
UTF8-tail: charset [#"^(80)" - #"^(BF)"]
UTF8-1: charset [#"^(00)" - #"^(7F)"]
UTF8-2: reduce [
charset [#"^(C2)" - #"^(DF)"]
UTF8-tail
]
UTF8-3: reduce [
#{E0} charset [#"^(A0)" - #"^(BF)"] UTF8-tail
'| charset [#"^(E1)" - #"^(EC)"] 2 UTF8-tail
'| #{ED} charset [#"^(80)" - #"^(9F)"] UTF8-tail
'| charset [#"^(EE)" - #"^(EF)"] 2 UTF8-tail
]
UTF8-4: reduce [
#{F0} charset [#"^(90)" - #"^(BF)"] 2 UTF8-tail
'| charset [#"^(F1)" - #"^(F3)"] 3 UTF8-tail
'| #{F4} charset [#"^(80)" - #"^(8F)"] 2 UTF8-tail
]
UTF8-char: [pos: UTF8-1 | UTF8-2 | UTF8-3 | UTF8-4]
not-word-char: charset {/\^^,[](){}"#%$@:;}
not-word-1st: union union not-word-char digit charset {'}
not-file-char: charset {[](){}"@:;}
not-url-char: charset {[](){}";}
not-str-char: #"^""
not-mstr-char: #"}"
caret-char: charset [#"^(40)" - #"^(5F)"]
non-printable-char: charset [#"^(00)" - #"^(1F)"]
integer-end: charset {^{"[]();x}
path-end: charset {^{"[]();}
stop: none
control-char: reduce [ ;-- Control characters
charset [#"^(00)" - #"^(1F)"] ;-- C0 control codes
'| #"^(C2)" charset [#"^(80)" - #"^(9F)"] ;-- C1 control codes (UTF-8 encoded)
]
UTF8-filtered-char: [
[pos: stop :pos (fail?: [end skip]) | UTF8-char e: (fail?: none)]
fail?
]
UTF8-printable: [
[non-printable-char | not-str-char (fail?: [end skip]) | UTF8-char (fail?: none)]
fail?
]
;-- Whitespaces list from: http://en.wikipedia.org/wiki/Whitespace_character
ws: [
pos: #"^/" (
if count? [
line: line + 1
append/only lines to block! stack/tail?
]
)
| ws-ASCII ;-- only the common whitespaces are matched
| #{C2} [
#{85} ;-- U+0085 (Newline)
| #{A0} ;-- U+00A0 (No-break space)
]
| #{E1} [
#{9A80} ;-- U+1680 (Ogham space mark)
| #{A08E} ;-- U+180E (Mongolian vowel separator)
]
| #{E2} [
#{80} [
ws-U+2k ;-- U+2000-U+200A range
| #{A8} ;-- U+2028 (Line separator)
| #{A9} ;-- U+2029 (Paragraph separator)
| #{AF} ;-- U+202F (Narrow no-break space)
]
| #{819F} ;-- U+205F (Medium mathematical space)
]
| #{E38080} ;-- U+3000 (Ideographic space)
]
newline-char: [
#"^/"
| #{C285} ;-- U+0085 (Newline)
| #{E280} [
#{A8} ;-- U+2028 (Line separator)
| #{A9} ;-- U+2029 (Paragraph separator)
]
]
counted-newline: [pos: #"^/" (line: line + 1)]
ws-no-count: [(count?: no) ws (count?: yes)]
any-ws: [pos: any ws]
symbol-rule: [
(stop: [not-word-char | ws-no-count | control-char])
some UTF8-filtered-char e:
]
begin-symbol-rule: [ ;-- 1st char in symbols is restricted
(stop: [not-word-1st | ws-no-count | control-char])
UTF8-filtered-char
opt symbol-rule
]
path-rule: [
pos: slash :pos ( ;-- path detection barrier
stack/push path!
stack/push to type copy/part s e ;-- push 1st path element
)
some [
slash
s: [
integer-number-rule
| begin-symbol-rule (type: word!)
| paren-rule (type: paren!)
| #":" s: begin-symbol-rule (type: get-word!)
;@@ add more datatypes here
] (
stack/push either type = paren! [ ;-- append path element
value
][
to type copy/part s e
]
type: path!
)
]
opt [#":" (type: set-path!)]
e: [path-end | ws-no-count | end | (pos: path throw-error)] :e ;-- detect invalid tail characters
(value: stack/pop type)
]
word-rule: [
(type: word!)
#"%" ws-no-count (value: "%") ;-- special case for remainder op!
| path: s: begin-symbol-rule [
url-rule
| path-rule ;-- path matched
| (value: copy/part s e) ;-- word matched
opt [#":" (type: set-word!)]
]
]
get-word-rule: [
#":" (type: get-word!) s: begin-symbol-rule [
path-rule (
value/1: to get-word! value/1 ;-- workaround missing get-path! in R2
)
| (
type: get-word!
value: copy/part s e ;-- word matched
)
]
]
lit-word-rule: [
#"'" (type: word!) s: begin-symbol-rule [
path-rule (type: lit-path!) ;-- path matched
| (
type: lit-word!
value: copy/part s e ;-- word matched
)
][s: #":" :s (throw-error) | none]
]
map-rule: [
"#(" (stack/push block!) any-value #")" (
stack/push/head #!map!
value: stack/pop block!
)
]
issue-rule: [#"#" (type: issue!) s: symbol-rule]
refinement-rule: [slash (type: refinement!) s: symbol-rule]
slash-rule: [s: [slash opt slash] e:]
hexa-rule: [2 8 hexa e: #"h" (type: integer!)]
sticky-word-rule: [
pos: [ ;-- protection rule from typo with sticky words
[integer-end | ws-no-count | end] (fail?: none)
| skip (fail?: [end skip])
] :pos
fail?
]
tuple-value-rule: [
(type: tuple!)
byte dot byte 1 8 [dot byte] e:
]
tuple-rule: [
tuple-value-rule
sticky-word-rule
]
integer-number-rule: [
(type: integer!)
opt [#"-" | #"+"] digit any [digit | #"'" digit] e:
]
integer-rule: [
decimal-special e: ;-- escape path for NaN, INFs
(type: issue! value: load-number copy/part s e)
| integer-number-rule
opt [decimal-number-rule | decimal-exp-rule e: (type: decimal!)]
opt [#"%" e: (type: issue!)]
sticky-word-rule
(value: load-number copy/part s e)
opt [
#"x" (
type: pair!
value2: to pair! reduce [value 0]
)
s: integer-number-rule
(value2/2: load-number copy/part s e value: value2)
]
]
decimal-special: [
s: "-0.0" | (neg?: no) opt [#"-" (neg?: yes)] "1.#" s: [
[[#"N" | #"n"] [#"a" | #"A"] [#"N" | #"n"]]
| [[#"I" | #"i"] [#"N" | #"n"] [#"F" | #"f"]]
]
]
decimal-exp-rule: [
[[#"e" | #"E"] opt [#"-" | #"+"] 1 3 digit]
]
decimal-number-rule: [
[dot | comma] digit any [digit | #"'" digit]
opt decimal-exp-rule e: (type: decimal!)
]
decimal-rule: [
decimal-number-rule opt [#"%" e: (type: issue!)]
sticky-word-rule
]
block-rule: [#"[" (stack/push block!) any-value #"]" (value: stack/pop block!)]
paren-rule: [#"(" (stack/push paren!) any-value #")" (value: stack/pop paren!)]
escaped-char: [
"^^(" [
[ ;-- special case first
"null" (value: #"^(00)")
| "back" (value: #"^(08)")
| "tab" (value: #"^(09)")
| "line" (value: #"^(0A)")
| "page" (value: #"^(0C)")
| "esc" (value: #"^(1B)")
| "del" (value: #"^(7F)")
]
| pos: [2 6 hexa-char] e: ( ;-- Unicode values allowed up to 10FFFFh
either rs? [
value: to-char to-integer debase/base copy/part pos e 16
][value: encode-UTF8-char pos e]
)
] #")"
| #"^^" [
[
#"/" (value: #"^/")
| #"-" (value: #"^-")
| #"?" (value: #"^(del)")
| #"^^" (value: #"^^") ;-- caret escaping case
| #"{" (value: #"{")
| #"}" (value: #"}")
| #"^"" (value: #"^"")
]
| pos: caret-char (value: pos/1 - 64)
]
]
char-rule: [
{#"} (type: char!) [
s: escaped-char
| copy value UTF8-printable (value: as-binary value)
| #"^-" (value: s/1)
] {"}
]
line-string: [
{"} s: (type: string! stop: [not-str-char | newline-char])
any [{^^"} | escaped-char | UTF8-filtered-char]
e: {"}
]
nested-curly-braces: [
(cnt: 1 fail?: none)
any [[
counted-newline
| "^^{" | "^^}"
| #"{" (cnt: cnt + 1)
| e: #"}" (if zero? cnt: cnt - 1 [fail?: [end skip]])
| UTF8-char
] fail?
]
#"}"
]
multiline-string: [#"{" s: (type: string!) nested-curly-braces]
string-rule: [line-string | multiline-string]
base-2-rule: [
"2#{" (type: binary!)
s: any [counted-newline | 8 [#"0" | #"1" ] | ws-no-count | comment-rule]
e: #"}" (base: 2)
]
base-16-rule: [
"#{" (type: binary!)
s: any [counted-newline | 2 hexa-char | ws-no-count | comment-rule]
e: #"}" (base: 16)
]
base-64-rule: [
"64#{" (type: binary!)
s: any [counted-newline | base64-char | ws-no-count | comment-rule]
e: #"}" (base: 64)
]
binary-rule: [base-16-rule | base-2-rule | base-64-rule]
file-rule: [
#"%" (type: file! stop: [not-file-char | ws-no-count]) [
#"^"" s: any UTF8-filtered-char e: #"^""
| s: any UTF8-filtered-char e:
]
]
url-rule: [
#":" (type: url! stop: [not-url-char | ws-no-count])
some UTF8-filtered-char e: (value: dehex copy/part s e)
]
escaped-rule: [
"#[" any-ws [
"true" (value: true)
| "false" (value: false)
| s: [
"none!" | "logic!" | "block!" | "integer!" | "word!"
| "set-word!" | "get-word!" | "lit-word!" | "refinement!"
| "binary!" | "string!" | "char!" | "bitset!" | "path!"
| "set-path!" | "lit-path!" | "native!" | "action!"
| "issue!" | "paren!" | "function!"
] e: (value: get to word! copy/part s e)
| "none" (value: none)
] any-ws #"]"
]
comment-rule: [#";" [to #"^/" | to end]]
wrong-delimiters: [
pos: [
#"]" (value: #"[") | #")" (value: #"(")
| #"[" (value: #"]") | #"(" (value: #")")
] :pos
(throw-error/with ["missing matching" value])
]
literal-value: [
pos: (e: none) s: [
comment-rule
| escaped-rule (stack/push value)
| integer-rule (stack/push value)
| decimal-rule (stack/push load-decimal copy/part s e)
| tuple-rule (stack/push to tuple! copy/part s e)
| hexa-rule (stack/push decode-hexa copy/part s e)
| word-rule (stack/push to type value)
| lit-word-rule (stack/push to type value)
| get-word-rule (stack/push to type value)
| refinement-rule (stack/push to refinement! copy/part s e)
| slash-rule (stack/push to word! copy/part s e)
| file-rule (stack/push load-file copy/part s e)
| char-rule (stack/push decode-UTF8-char value)
| block-rule (stack/push value)
| paren-rule (stack/push value)
| string-rule (stack/push load-string s e)
| binary-rule (stack/push load-binary s e base)
| map-rule (stack/push value)
| issue-rule (stack/push to issue! copy/part s e)
]
]
any-value: [pos: any [literal-value | ws]]
header: [
pos: thru "Red" (rs?: no) opt ["/System" (rs?: yes stack/push 'Red/System)]
any-ws block-rule (stack/push value)
| (throw-error/with "Invalid Red program") end skip
]
program: [
pos: opt UTF-8-BOM
header
any-value
opt wrong-delimiters
]
;====== Helper functions ======
stack: context [
stk: []
push: func [value /head][
either any [value = block! value = paren! value = path!][
if value = path! [value: block!]
insert/only tail stk value: make value 1
value
][
either head [
insert/only last stk :value
][
insert/only tail last stk :value
]
]
]
pop: func [type [datatype!]][
if any [type = path! type = set-path!][type: block!]
if type <> type? last stk [
throw-error/with ["invalid" mold type "closing delimiter"]
]
also last stk remove back tail stk
]
tail?: does [tail last stk]
reset: does [clear stk]
]
throw-error: func [/with msg [string! block!]][
print rejoin [
"*** Syntax Error: " either with [
uppercase/part reform msg 1
][
reform ["Invalid" mold type "value"]
]
"^/*** line: " line
"^/*** at: " mold copy/part pos 40
]
either encap? [quit][halt]
]
add-line-markers: func [blk [block!]][
foreach pos lines [new-line pos yes]
clear lines
]
pad-head: func [s [string!]][
head insert/dup s #"0" 8 - length? s
]
encode-UTF8-char: func [s [string!] e [string!] /local c code new][
c: debase/base pad-head copy/part s e 16
while [c/1 = 0][c: next c] ;-- trim heading zeros
code: to integer! c
case [
code <= 127 [
new: to char! code ;-- c <= 7Fh
]
code <= 2047 [ ;-- c <= 07FFh
new: (shift/left (shift code 6) or #"^(C0)" 8)
or (code and #"^(3F)") or #"^(80)"
]
code <= 65535 [ ;-- c <= FFFFh
new: (shift/left (shift code 12) or #"^(E0)" 16)
or (shift/left (shift code 6) and #"^(3F)" or #"^(80)" 8)
or (code and #"^(3F)") or #"^(80)"
]
code <= 1114111 [ ;-- c <= 10FFFFh
new: (shift/left (shift code 18) or #"^(F0)" 24)
or (shift/left (shift code 12) and #"^(3F)" or #"^(80)" 16)
or (shift/left (shift code 6) and #"^(3F)" or #"^(80)" 8)
or (code and #"^(3F)") or #"^(80)"
]
'else [
throw-error/with "Codepoints above U+10FFFF are not supported"
]
]
if integer? new [
new: debase/base to-hex new 16
remove-each byte new [byte = #"^(null)"]
]
new
]
decode-UTF8-char: func [value /redbin][
if all [not redbin char? value][return encode-char to integer! value]
value: switch/default length? value [
1 [value]
2 [
value: value and #{1F3F}
value: add shift/left value/1 6 value/2
]
3 [
value: value and #{0F3F3F}
value: add add
shift/left value/1 12
shift/left value/2 6
value/3
]
4 [
value: value and #{073F3F3F}
value: add add add
shift/left value/1 18
shift/left value/2 12
shift/left value/3 6
value/4
]
][
throw-error/with "Unsupported or invalid UTF-8 encoding"
]
either redbin [value][
encode-char to integer! value ;-- special encoding for Unicode char!
]
]
decode-UTF8-string: func [str [string!] /local new s e][
new: make string! length? str
parse/all str [
some [
s: UTF8-char e: (
append new debase/base skip decode-UTF8-char as-binary copy/part s e 7 16
)
]
]
head change/part str new tail str
]
encode-char: func [value [integer!]][
head insert to-hex value #"'"
]
decode-hexa: func [s [string!]][
to integer! debase/base s 16
]
load-number: func [s [string!]][
switch/default type [
#[datatype! decimal!][s: load-decimal s]
#[datatype! issue! ][
if s = "-0.0" [s: "0-"] ;-- re-encoded for consistency
s: to issue! either #"%" = last s [s][join "." s]
if neg? [append s #"-"]
]
][
unless integer? s: to integer! s [throw-error]
]
s
]
load-decimal: func [s [string!]][
unless attempt [s: to decimal! s][throw-error]
s
]
load-string: func [s [string!] e [string!] /local new filter][
new: make string! offset? s e ;-- allocated size close to final size
filter: get pick [UTF8-char UTF8-filtered-char] s/-1 = #"{"
parse/all/case copy/part s e [
any [
escaped-char (insert tail new value)
| s: filter e: (insert/part tail new s e)
] ;-- exit on matching " or }
]
new
]
load-binary: func [s [string!] e [string!] base [integer!] /local new str][
new: make string! offset? s e ;-- allocated size above final size
parse/all/case s [
some [
copy str some base64-char (insert tail new str)
| ws | comment-rule
| #"}" end skip
]
]
new: debase/base new base
if none? new [throw-error]
new
]
load-file: func [s [string!]][
to file! dehex s
]
process: func [src [string! binary!] /local blk][
line: 1
count?: yes
blk: stack/push block! ;-- root block
unless parse/all/case src program [throw-error]
add-line-markers blk
stack/reset
blk
]
]