Created
July 11, 2024 13:39
-
-
Save hiiamboris/2834eb7d9b280bded508da5ac73e1b0f to your computer and use it in GitHub Desktop.
Automatic tree construction during parsing (a dusty experiment)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [] | |
context [ | |
keywords: make hash! [ | |
case quote skip none end opt not ahead any some while to thru if | |
into fail break reject set copy collect keep remove insert change | |
] | |
log: [] | |
last-push: [] | |
tracer: function [event [word!] match? [logic!] rule [block!] input [series!] stack [block!] /local name /extern last-push] [ | |
set/any 'name pick rule event <> 'iterate | |
if all [ | |
find [pop push iterate] event | |
word? :name | |
not find keywords name | |
block? get/any name | |
][ | |
switch event [ | |
push iterate [ | |
repend log [rule name input] | |
repend clear last-push [rule name] | |
] | |
fetch [ | |
unless all [ | |
rule =? last-push/1 | |
name == last-push/2 | |
][ | |
repend log [rule name input] | |
] | |
clear last-push | |
] | |
; iterate [ | |
; take/last/part log 3 | |
; ] | |
pop [ | |
either match? [ | |
name: to /ref name | |
unless name == pick tail log -2 [ | |
repend log [rule name input] | |
] | |
][ | |
take/last/part log 3 | |
] | |
clear last-push | |
] | |
] | |
; print [pad event 8 pad match? 8 pad rule/1 10 pad mold input 20 mold stack] | |
] | |
; print [pad event 8 pad match? 8 pad rule 20 pad mold input 20 mold stack] | |
yes | |
] | |
emit-tree: function [log] [ | |
; probe new-line/skip log yes 3 | |
append/only stack: [] tree: copy [] | |
s: none | |
foreach [_ name input] log [ | |
leaf: last stack | |
either word? name [ | |
repend leaf [name leaf: copy []] | |
repend stack [leaf] | |
][ | |
leaf: take/last stack | |
if empty? leaf [ | |
leaf: last stack | |
change back tail leaf copy/part s input | |
] | |
new-line leaf yes | |
] | |
s: input | |
] | |
new-line tree yes | |
clear stack | |
tree | |
] | |
set 'parse-into-tree function [input rule] [ | |
parse/trace input rule :tracer | |
emit-tree log | |
] | |
] | |
alpha: charset [#"a" - #"z"] | |
digit: charset [#"0" - #"9"] | |
name: [some alpha] | |
value: [some digit] | |
elem: [name some sp opt [some [value any sp]]] | |
probe tree: parse-into-tree "aa bb 123 cc 4 5" [any elem] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment