-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlest.reb
2377 lines (2182 loc) · 49.7 KB
/
lest.reb
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
REBOL[
Title: "LEST - Low Entropy System for Templating"
Author: "Boleslav Brezovsky"
Name: 'lest
Version: 0.0.5
Date: 19-9-2014
Created: 7-12-2013
; Type: 'module
; Exports: [lest]
Needs: [%prestyle.reb %md.reb] ; %compile-rules.reb]
; Options: [isolate]
Notes: [
9-1-15 "BB" {LEST sets 'lest-styles word that holds list of all used CSS styles.
This will be later changed to object! that will hold more informations
about the parsed Lest source.
This block can be used with (patched) StyleTalk to check if all styles defined
in StyleTalk file are used by Lest source.
}
13-1-15 "BB" {LEST now adds ID to radio, when no ID is present.
ID is in the form: radio_<radio-name>_<radio-value>
Check if not problematic.
}
]
To-do: [
"AS and JOIN AS should use same implementation"
"HTML entities"
"Cleanup variables in lest"
"Change header rules to emit to main data"
{
get rid of lest in rules
currently used in:
CAROUSEL, CAROUSEL-ITEM
ENABLE: BOOTSTRAP, SMOOTH-SCROLLING, PRETTY-PHOTO, PASSWORD-STRENGTH
}
"support char! as basic input (beside string!)"
"add anything! type for user rules that will parse anything parsable in bootrapy"
"Bootstrap BOX component"
{
Add webserver that can serve pages directly:
when run with argument (serve index.page) it will open browser and show page
when run without argument, it will open in current directory with list of files and some help
... other ideas
}
"plugin design: instead of startup just list required css and js files"
"FORM is Bootstrap optimized, divide"
"FIX: form leaks default, value, name"
"FIX: main-rule and match-content mišmaš: one rule with all rules and one rule to match that rule, block, commands and string (not in that order)"
{
GET-USER-VALUE: (MATCH-VALUE)
Rule will check for user content (using get-word! or word!),
replace it and continue parsing:
GET-USER-VALUE SET value string!
so data like: [TAG :USER-VAL] are changed to [TAG "user-val"]
}
{
REPEAT: remove replace
instead of: REPEAT [a :link] REPLACE :link WITH [...]
it will be: REPEAT [a (...)]
}
]
]
css-path: %css/
js-path: %js/
; SETTINGS
; TODO: move settings to .PAGE files
plugin-path: %plugins/
text-style: 'html
dot: #"."
;
; _____ _ _ _____ _____ ____ _____ _______ ______ _ _ _ _ _____ _____
; / ____| | | | | | __ \ | __ \ / __ \ | __ \ |__ __| | ____| | | | | | \ | | / ____| / ____|
; | (___ | | | | | |__) | | |__) | | | | | | |__) | | | | |__ | | | | | \| | | | | (___
; \___ \ | | | | | ___/ | ___/ | | | | | _ / | | | __| | | | | | . ` | | | \___ \
; ____) | | |__| | | | | | | |__| | | | \ \ | | | | | |__| | | |\ | | |____ ____) |
; |_____/ \____/ |_| |_| \____/ |_| \_\ |_| |_| \____/ |_| \_| \_____| |_____/
;
attach: function [
"Append value to block only when not present. Return FALSE when value is present."
block
value
] [
either found: find block value [
found
] [
append block value
true
]
]
escape-entities: funct [
"Escape HTML entities. Only partial support now."
data
] [
output: make string! 1.1 * length? data
; simple map that is modified to parse rule
entities: [
#"<" "lt"
#">" "gt"
#"&" "amp"
]
rule: make block! length? entities
forskip entities 2 [
repend rule [
entities/1
to paren! reduce ['append 'output rejoin [#"&" entities/2 #";"] ]
'|
]
]
append rule [set value skip (append output value)]
; debug-print ["parse escape entities"]
parse data [some rule]
output
]
catenate: funct [
"Joins values with delimiter."
src [ block! ]
delimiter [ char! string! ]
/as-is "Mold values"
] [
out: make string! 200
forall src [ repend out [ either as-is [mold src/1] [src/1] delimiter ] ]
len: either char? delimiter [ 1 ][ length? delimiter ]
head remove/part skip tail out negate len len
]
replace-deep: funct [
target
'search
'replace
] [
rule: compose [
change (:search) (:replace)
| any-string!
| into [some rule]
| skip
]
parse target [some rule]
target
]
change-code: func [
"Replace code at cuurent position (to have unified function for better testing and debugging)"
pos
data
/only " Only change a block as a single value (not the contents of the block)"
] [
pos/1: data
]
rule: func [
"Make PARSE rule with local variables"
local [word! block!] "Local variable(s)"
rule [block!] "PARSE rule"
] [
if word? local [local: reduce [local]]
; compile-rules use local reduce [rule]
use local reduce [rule]
]
add-rule: func [
"Add new rule to PARSE rules block!"
rules [block!]
rule [block!]
] [
unless empty? rules [
append rules '|
]
append/only rules rule
]
to-www-form: func [
"Convert object body (block!) to application/x-www-form-urlencoded"
data
/local out
][
out: copy {}
foreach [ key value ] data [
if issue? value [ value: next value ]
repend out [
to word! key
#"="
value
#"&"
]
]
head remove back tail out
]
build-tag: funct [
name [ word! ]
values [ block! object! map! ]
][
tag: make string! 256
repend tag [ #"<" name space ]
unless block? values [ values: body-of values ]
foreach [ name value ] values [
skip?: false
value: switch/default type?/word value [
block! [
if empty? value [ skip?: true ]
catenate value #" "
]
string! [ if empty? value [ skip?: true ] value ]
none! [ skip?: true ]
][
form value
]
unless skip? [
repend tag [ to word! name {="} value {" } ]
]
]
;
; TODO: support attributes without values (version from make-tag)
;
; unless empty? attributes [
; append out join #" " form attributes
; ]
;
head change back tail tag #">"
]
entag: func [
"Enclose value in tag"
data
tag
/with
values
] [
unless with [ values: clear [] ]
ajoin [
build-tag tag values
reduce data
close-tag tag
]
]
close-tag: func [
type
][
ajoin ["</" type ">"]
]
; TODO: get-integer and lest-integer? should be one function (or get-integer should use lest-integer?)
get-integer: func [
"Get integer! value from string! or pass integer! (return NONE otherwise)"
value
/local number int-rule
] [
if integer? value [return value]
unless string? value [return none]
number: charset "0123456789"
; float-rule: [opt #"-" some number [opt #"." some number]]
int-rule: [opt #"-" some number]
either parse value int-rule [to integer! value] [none]
]
lest-integer?: func [
value
/local number int-rule
] [
number: charset "0123456789"
int-rule: [opt #"-" some number]
any [
integer? value
all [string? value parse value int-rule]
]
]
lest: use [
debug-print
buffer
page
tag
tag-name
tag-stack
includes
rules
header?
safe?
pos
locals
local
current-text-style
used-styles
last-id
name
value
emit
emit-label
emit-stylesheet
add-js
user-rules
user-words
user-words-meta
user-values
plugins
load-plugin
] [
add-js: func [
"Add code do javascript code buffer"
target
data
/only "Do not end command with semicolon"
] [
head append target rejoin [data either only "" #";"]
]
set-user-word: func [
name
value
/type
'word-type
/custom
custom-data
] [
name: to lit-word! name
debug-print ["SET-USER-WORD"]
debug-print ["uw:" mold user-words]
debug-print ["SET:" name mold value "(rebol:" type? value ")"]
debug-print ["word-type" mold word-type get-integer value]
word-type: case [
word-type (to lit-word! word-type)
get-integer value (value: form value 'integer)
string? value ('string)
equal? #"." first form value ('class) ; doesn't check for word! but should be sufficient
word? value ('word)
block? value ('block)
issue? value ('id)
map? value ('map)
]
debug-print ["SET:" name mold value "(lest:" word-type ")"]
obj: object reduce/no-set [
type: quote word-type
; value: :value
]
if custom [append object custom-data]
append user-words compose/only [
(to set-word! name) (:value)
]
append user-words-meta compose [
(to set-word! name) (obj)
]
]
get-user-word: func [
'name
] [
get in user-words name
]
get-user-type: func [
name
] [
; debug-print ["GUT:" mold name "in" mold user-words-meta]
; debug-print ["GUT:" mold get in user-words-meta :name]
if name: get in user-words-meta name [
name/type
]
]
; === actions
emit: func [
data [ string! block! tag! ]
][
if block? data [ data: ajoin data ]
if tag? data [ data: mold data ]
append buffer data ;join data newline
]
emit-label: func [
label
elem
/class
styles
][
unless empty? label [emit entag/with label 'label reduce/no-set [ for: elem class: styles ]]
]
emit-script: func [
script
/insert
/append
][
case [
insert [lib/append includes/header script]
append [lib/append includes/body-end script]
true [emit script]
]
]
emit-stylesheet: func [
stylesheet
/local suffix
][
; if path? stylesheet [ stylesheet: get stylesheet ]
local: stylesheet
if all [
file? stylesheet
not equal? %.css suffix: suffix? stylesheet
] [
write
local: replace copy stylesheet suffix %.css
prestyle load stylesheet
]
unless find includes/stylesheets stylesheet [
repend includes/stylesheets [{<link href="} local {" rel="stylesheet">} newline ]
]
]
; _____ _ _ _ ______ _____
; | __ \ | | | | | | | ____| / ____|
; | |__) | | | | | | | | |__ | (___
; | _ / | | | | | | | __| \___ \
; | | \ \ | |__| | | |____ | |____ ____) |
; |_| \_\ \____/ |______| |______| |_____/
;
rules: object [
; -- reference to some words: external plugins are bound to RULES, but cannot see TAG
; or INCLUDES so we need this references (or multiple binding, which is ugly)
tag: tag
tag-name: tag-name
value-to-emit: none
emit-value: [
(emit value-to-emit)
]
if-content: rule [pos] [
pos:
if (not empty? pos)
]
; --- subrules
load-rule: rule [pos value] [
; LOAD AND RETURN FILE
'load pos: set value [ file! | url! ]
(
debug-print ["##LOAD" value]
change-code/only pos load value
)
:pos
]
import-rule: rule [pos value] [
; LOAD AND EMIT FILE
'import pos: set value [ file! | url! ]
(
debug-print ["##IMPORT" value]
change-code/only pos load value
)
:pos main-rule
]
text-settings: rule [type] [
set type ['plain | 'html | 'markdown]
'text
(text-style: type)
]
eval: [
(debug-print "!!EVAL!!")
any [
commands (debug-print "!!EVAL!!command")
| user-values (debug-print "!!EVAL!!user-val")
| process-code (debug-print "!!EVAL!!code")
| plugins (debug-print "!!EVAL!!plugin")
| comparators (debug-print "!!EVAL!!comparator")
]
(debug-print "!!EVAL!!END!!")
]
eval-strict: [any [user-values | process-code | commands ]] ; ignore plugins
process-code: rule [ p value ] [
(debug-print "--process code")
; DO PAREN! AND EMIT LAST VALUE
p: set value paren!
(
debug-print ["==CODE:" mold value]
p/1: either safe? [
""
] [
do bind to block! value user-words
]
)
:p
]
set-at-rule: rule [word index value block] [
'set
set word word!
'at
eval set index integer!
eval set value any-type!
(
debug-print ["==SET@:" word "@" index "=" value]
block: get-user-word :word
block/:index: value
set-user-word word block
)
]
set-rule: rule [labels values] [
[
'set set labels [word! | block!]
| set labels set-word! (labels: to word! labels)
]
eval set values any-type!
(
unless block? labels [
labels: reduce [labels]
values: reduce [values]
]
debug-print ["==SET:" length? labels "values"]
repeat i length? labels [
label: labels/:i
value: values/:i
value: switch/default value [
; predefined values
true yes on [lib/true]
false no off [lib/false]
][value]
; add rules, if not exists
unless in user-words label [
debug-print ["==SET/create:" label]
append second user-values compose [
|
(to lit-word! label)
(to paren! compose [change/only pos get-user-word (label)])
]
]
; extend user context with new value
debug-print ["==SET:" label ":" mold value]
set-user-word label value
]
)
]
get-user-value: rule [value] [
pos:
set value any-type!
(
all [
word? value
in user-words value
; pos/1: user-words/:value
change-code/only pos user-words/:value
]
)
:pos
]
new-get-user-value: rule [name] [
pos:
set name word!
(
change-code/only pos get-user-word name
)
:pos
]
user-rule: rule [name label type value urule args pos this-rule] [
set name set-word!
(
args: copy []
idx: none
if block? pos: attach user-rule-names name [
; rule already exists, remove it
idx: (index? pos) * 2 + 1
]
this-rule: reduce [
to set-word! 'pos
to lit-word! name
to paren! compose [debug-print (rejoin ["UU:user-rule: " name " <start> matched."])]
]
)
any [
set label word!
set type word!
(
add-rule args rule [px] reduce [
to set-word! 'px to lit-word! label
to paren! reduce/no-set [ to set-path! 'px/1 label ]
]
repend this-rule ['eval to set-word! 'pos 'set label type ]
)
]
set value block!
(
append this-rule reduce [
to paren! compose/only [
; TODO: move rule outside
urule: ( compose [
any-string!
| into [ some urule ]
; FIXME: for rules without args it returns [into [...] | | skip ] so skip cannot be reached
| (args)
| skip
] )
debug-print ["parse in user-rule"]
parse temp: copy/deep (value) [ some urule ]
; change/only pos temp
change-code/only pos temp
]
to get-word! 'pos 'into main-rule
]
either idx [
; existing rule, modify
change/only at user-rules idx this-rule
] [
; new rule, add
add-rule user-rules this-rule
]
)
]
template-rule: rule [name label type value urule args pos this-rule] [
set name set-word!
'template
(
debug-print ["==TEMPLATE:" name]
args: copy []
idx: none
if block? pos: attach user-rule-names name [
; rule already exists, remove it
idx: (index? pos) * 2 + 1
]
this-rule: reduce [
to set-word! 'pos
to lit-word! name
to paren! compose [debug-print (rejoin ["UU:user-rule: " name " <start> matched."])]
]
)
opt into [
some [
set label word!
(
debug-print ["==TEMPLATE arg:" label]
add-rule args rule [px] reduce [
to set-word! 'px to lit-word! label
to paren! reduce/no-set [ to set-path! 'px/1 label ]
]
repend this-rule ['eval to set-word! 'pos 'set label 'any-type! ]
)
]
]
set value block!
(
append this-rule reduce [
to paren! compose/only [
; TODO: move rule outside
urule: ( compose [
any-string!
| into [ some urule ]
; FIXME: for rules without args it returns [into [...] | | skip ] so skip cannot be reached
| (args)
| skip
] )
debug-print ["parse in user-rule"]
parse temp: copy/deep (value) [ some urule ]
; change/only pos temp
change-code/only pos temp
]
to get-word! 'pos 'into main-rule
]
set-user-word/type name value template
either idx [
; existing rule, modify
change/only at user-rules idx this-rule
] [
; new rule, add
add-rule user-rules this-rule
]
)
]
enable-plugin: rule [name t] [
; WARNING: very fragile, touch in antistatic handgloves only!
'enable pos: set name word! (
; NOTE: [change/part pos t 1] is absolute neccessity,
; because [pos/1: t] crashes Rebol!!!
either t: load-plugin name [
; change/part pos t 1
change-code pos t
] [pos: next pos]
)
:pos [main-rule | into main-rule]
]
init-tag: [
(
insert tag-stack reduce [ tag-name tag: context [ id: none class: copy [] ] ]
debug-print ["INIT TAG:" tag-name]
debug-stack tag-stack
)
]
take-tag: [(set [tag-name tag] take/part tag-stack 2)]
emit-tag: [ (
emit build-tag tag-name tag
debug-print ["EMIT TAG:" tag-name ", stack: " length? tag-stack]
) ]
end-tag: [
take-tag
(
emit close-tag tag-name
debug-print ["END TAG:" tag-name ", stack: " length? tag-stack]
)
]
init-div: [
( tag-name: 'div )
init-tag
]
comparators: [
comparison-rule
]
comparison-rule: rule [val1 val2 comparator pos res] [
; NOTE: all values are formed before comparison
; this leads to double conversion if numbers ( 3 -> "3" -> 3 )
; and needs to be optimalized
set val1 any-type!
set comparator ['= | '> | '< | '>= | '<= | '<>]
set val2 any-type!
pos:
(
debug-print ["<>COMPARE:" mold val1 type? val1 comparator mold val2 type? val2]
; TODO: simplify this american engineering
if word? val1 [
type: get-user-type val1
val1: get-user-word :val1
debug-print ["GOT" mold val1]
; if type = 'integer [val1: get-integer val1]
]
if lest-integer? val1 [val1: get-integer val1]
if word? val2 [
type: get-user-type val2
val2: get-user-word :val2
debug-print ["GOT" mold val2]
; if type = 'integer [val2: get-integer val2]
]
if lest-integer? val2 [val2: get-integer val2]
debug-print ["<>COMPARE:" mold val1 comparator mold val2]
res: do reduce [val1 comparator val2]
debug-print ["<>COMPARE:" mold res]
change-code/only pos: back pos res
)
:pos
]
math-commands: [
incr-rule
| math-rule
]
incr-rule: rule [action word value] [
set action ['++ | '--]
set word word!
(
debug-print ["++MATH incr:" word action]
; TODO: should return error on non-integer values or silently ignore?
action: select [++ + -- -] action
all [
value: get-user-word :word
value: get-integer value
integer? value
value: do reduce ['value action 1]
set-user-word word form value
]
)
]
math-rule: rule [pos action val1 val2] [
set val1 [string! | integer! | word!]
set action ['+ | '- | '*]
pos: set val2 [string! | integer! | word!]
(
debug-print ["++MATH input:" val1 action val2]
if word? val1 [val1: get-user-word :val1]
if word? val2 [val2: get-user-word :val2]
val1: get-integer val1
val2: get-integer val2
debug-print ["++MATH output:" val1 action val2]
; pos/1: form do reduce ['val1 action 'val2]
change-code pos form do reduce ['val1 action 'val2]
)
:pos
]
commands: [
pos: (debug-print ["match commands@" pos/1])
[
if-rule
| either-rule
| switch-rule
| for-rule
| repeat-rule
| pipe-loop-rule
| as-map-rule
| as-rule
| join-rule
| let-rule
| length-rule
| insert-append-rule
| math-commands
| load-rule
| import-rule
| pass
| stop
| run
| comment
| debug-rule
| template-rule
| user-rule
| set-at-rule
| set-rule
| enable-plugin
| plugins
]
]
if-rule: rule [cond true-val pos res] [
'if
opt comparators
set cond [logic! | word! | paren!]
pos:
set true-val any-type!
(
if all [safe? paren? cond] [cond: false]
debug-print ["??COMPARE/if: " cond " +" mold true-val]
res: if/only do bind to block! cond user-words true-val
debug-print ["??COMPARE/if: " res]
either res [
change/part pos res 1
; change-code/only pos res
] [
pos: next pos
]
)
:pos
]
either-rule: rule [cond true-val false-val pos ret] [
'either
opt comparators
set cond [logic! | word! | paren!]
set true-val any-type!
pos:
set false-val any-type!
(
if all [safe? paren? cond] [cond: false]
debug-print ["??COMPARE/either: " cond " +" mold true-val " -" mold false-val]
; change/part
; pos
; either/only do bind to block! cond user-words true-val false-val
; 1
change-code/only pos either/only do bind to block! cond user-words true-val false-val
debug-print ["??COMPARE/either[out]: " pos/1]
)
:pos
]
switch-rule: rule [value cases defval pos] [
'switch
(defval: none)
set value word!
set cases block!
opt [
'default
set defval any-type!
]
pos:
(
pos: back pos
forskip cases 2 [
if integer? cases/1 [cases/1: form cases/1]
cases/2: append/only copy/deep [] cases/2
]
value: get bind value user-words
defval: append/only copy [] defval
debug-print ["??COMPARE/switch: " mold value " ?" mold cases "-" mold defval]
change-code/only pos switch/default value cases defval
)
:pos
]
; FIXME: FOR set variable with user name in user-words
; it doesn't clean it and can rewrite user's variable
for-rule: rule [pos out var src content] [
'for
(debug-print "FOR command")
set var [word! | block!]
[
'in eval set src [word! | block! | file! | url!]
| eval set src [integer! | string!] 'times (src: get-integer src)
]
pos: set content block! (
debug-print "FOR matched"
src: case [
any [url? src file? src] [load src]
word? src [get-user-word :src]
integer? src [use 'i [reverse array/initial i: src func [][-- i]]]
true [src]
]
out: make block! length? src
forall src [
append out compose [set index (index? src)]
either block? var [
repeat i length? var [
append out compose/only copy/deep [set (var/:i) (src/:i)]
]
src: skip src -1 + length? var
append/only out copy/deep content
] [
append out compose/only copy/deep [set (var) (src/1) (copy/deep content)]
]
]
change-code/only pos out
)
:pos
if (not locals/lazy?)
main-rule
(local lazy? true)
]
repeat-rule: rule [offset element count value values data pos current out] [
'repeat
(
offset: none
values: make block! 4
)
get-user-value
set element block!
'replace
some [set value get-word! (append values value)]
opt [
set count [integer! | if (not safe?) paren!]
'times
]
opt [
'offset
set offset integer!
]
[
[
'from
pos: set data [ block! | word! ]
(
if word? data [ data: get data ]
out: make block! length? data
foreach item data [
current: copy/deep element
foreach value values [
replace-deep current value item
; FIXME: won't work for multiple values
]
if offset [
insert skip find current 'col 2 reduce [ 'offset offset ]
offset: none
]
append out current
]
; change/part pos out 1