@@ -34,3 +34,49 @@ role Parent does Node {
3434 # | REQUIRED: Lazily find (and maybe cache) children, forcing a refresh if requested
3535 method children (::? CLASS: D : Bool : D : $ refresh = False ) { ... }
3636}
37+
38+
39+ # ## STATIC TREE WRAPPING
40+
41+ # | A static leaf node
42+ class StaticLeaf does Leaf {
43+ has $ . source ;
44+ has $ ! short-name is built;
45+ has $ ! long-name is built;
46+
47+ method short-name () { $ ! short-name // = $ . source ~~ Cool
48+ ?? ~ $ . source
49+ !! $ . source .? short-name // $ . source . gist }
50+ method long-name () { $ ! long-name //= $ . source .? long -name // $ . source . raku }
51+ }
52+
53+ # | A parent node whose children do NOT change
54+ class StaticParent does Parent {
55+ has @ . children ;
56+ has $ ! short-name is built;
57+ has $ ! long-name is built;
58+
59+ method set-children (@ ! children ) { }
60+
61+ method short-name () { $ ! short-name // = $ ! parent ?? ' parent of ' ~ @ . children . elems
62+ !! ' root' }
63+ method long-name () { $ ! long-name //= self . raku }
64+ }
65+
66+ # | Helper sub to wrap a static tree that has Positionals as parent nodes;
67+ # | Pairs are named nodes
68+ our sub static-tree ($ source-node , * % attrs ) is export {
69+ do given $ source-node {
70+ when Node { $ _ }
71+ when Pair { static-tree(. value , | % attrs , short-name => . key ) }
72+ when Positional {
73+ my $ parent = StaticParent. new (| % attrs );
74+ my @ children = . map ({ static-tree($ _ , : $ parent ) });
75+ $ parent . set-children(@ children );
76+ $ parent
77+ }
78+ default {
79+ StaticLeaf. new (source => $ _ , | % attrs )
80+ }
81+ }
82+ }
0 commit comments