Skip to content

Commit 1a4ebf7

Browse files
committed
Fully cut-over to RakuAST
1 parent 5a05967 commit 1a4ebf7

File tree

21 files changed

+278
-730
lines changed

21 files changed

+278
-730
lines changed

META6.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
"CSS::Specification": "lib/CSS/Specification.rakumod",
1919
"CSS::Specification::AST": "lib/CSS/Specification/AST.rakumod",
2020
"CSS::Specification::Actions": "lib/CSS/Specification/Actions.rakumod",
21-
"CSS::Specification::Build": "lib/CSS/Specification/Build.rakumod",
2221
"CSS::Specification::Defs": "lib/CSS/Specification/Defs.rakumod",
2322
"CSS::Specification::Defs::Actions": "lib/CSS/Specification/Defs/Actions.rakumod",
2423
"CSS::Specification::Compiler": "lib/CSS/Specification/Compiler.rakumod",
24+
"CSS::Specification::Compiler::Util": "lib/CSS/Specification/Compiler/Util.rakumod",
25+
"CSS::Specification::Compiler::Grammars": "lib/CSS/Specification/Compiler/Grammars.rakumod",
26+
"CSS::Specification::Compiler::Roles": "lib/CSS/Specification/Compiler/Roles.rakumod",
2527
"CSS::Specification::Compiler::Actions": "lib/CSS/Specification/Compiler/Actions.rakumod",
26-
"CSS::Specification::Compiler::RakuAST": "lib/CSS/Specification/Compiler/RakuAST.rakumod",
27-
"CSS::Specification::Compiler::RakuAST::Actions": "lib/CSS/Specification/Compiler/RakuAST/Actions.rakumod",
28-
"CSS::Specification::Compiler::RakuAST::Grammars": "lib/CSS/Specification/Compiler/RakuAST/Grammars.rakumod",
29-
"CSS::Specification::Compiler::RakuAST::Roles": "lib/CSS/Specification/Compiler/RakuAST/Roles.rakumod",
30-
"CSS::Specification::Compiler::Runtime::Grammar": "lib/CSS/Specification/Compiler/Runtime/Grammar.rakumod",
31-
"CSS::Specification::Compiler::Runtime::Actions": "lib/CSS/Specification/Compiler/Runtime/Actions.rakumod"
28+
"CSS::Specification::Runtime::Grammar": "lib/CSS/Specification/Runtime/Grammar.rakumod",
29+
"CSS::Specification::Runtime::Actions": "lib/CSS/Specification/Runtime/Actions.rakumod"
3230
},
3331
"resources": [
3432
],
Lines changed: 63 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
unit class CSS::Specification::Actions;
22

3+
use Method::Also;
4+
35
# these actions translate a CSS property specification to Raku
46
# rules or actions.
5-
has %.prop-refs is rw;
7+
has %.rule-refs is rw;
68
has %.props is rw;
79
has %.rules is rw;
810
has %.child-props is rw;
@@ -14,12 +16,9 @@ method property-spec($/) {
1416
%.props{$_}++ for @props;
1517

1618
my $spec = $<spec>.ast;
19+
my $synopsis = ~$<spec>;
1720

18-
my %prop-def = (
19-
props => @props,
20-
synopsis => ~$<spec>,
21-
raku => $spec,
22-
);
21+
my %prop-def = (:@props, :$synopsis, :$spec);
2322

2423
%prop-def<inherit> = .ast with $<inherit>;
2524

@@ -30,23 +29,23 @@ method property-spec($/) {
3029
}
3130

3231
method rule-spec($/) {
33-
3432
my $rule = $<rule>.ast,
35-
my $raku = $<spec>.ast;
33+
my $spec = $<spec>.ast;
3634
my $synopsis = ~$<spec>;
3735
%.props{$rule}++;
3836

39-
make %( :$rule, :$synopsis, :$raku );
37+
my %rule-def = (
38+
:$rule, :$synopsis, :$spec
39+
);
40+
41+
make %rule-def;
4042
}
4143

4244
method yes($/) { make True }
4345
method no($/) { make False }
4446

4547
method spec($/) {
4648
my $spec = $<seq>.ast;
47-
$spec = ':my @S; ' ~ $spec
48-
if $*CHOICE;
49-
5049
make $spec;
5150
}
5251

@@ -57,126 +56,110 @@ method prop-names($/) {
5756

5857
method id($/) { make ~$/ }
5958
method id-quoted($/) { make $<id>.ast }
60-
method keyw($/) { make $<id>.subst(/\-/, '\-'):g }
61-
method digits($/) { make $/.Int }
59+
method keyw($/) { make 'keyw' => ~$<id> }
60+
method digits($/) { make 'num' => $/.Int }
6261
method rule($/) { make $<id>.ast }
6362

64-
method seq($/) {
65-
make @<term>>>.ast.join(' ');
63+
method !make-term($/, $name) {
64+
my @term = @<term>>>.ast;
65+
make @term == 1 ?? @term[0] !! ($name => @term);
6666
}
6767

68-
method term-options($/) {
69-
my @choices = @<term>>>.ast;
70-
71-
make @choices > 1
72-
?? [~] '[ ', @choices.join(' || '), ' ]'
73-
!! @choices[0];
68+
method seq($/) is also<term-seq> {
69+
self!make-term: $/, 'seq';
7470
}
7571

76-
method !choose(@choices) {
77-
my $choices := @choices.map({[~] ($_, ' <!{@S[', $*CHOICE++, ']++}>')}).join(' | ');
78-
return [~] '[ ', $choices, ' ]';
72+
method term-options($/) {
73+
self!make-term: $/, 'alt';
7974
}
8075

8176
method term-combo($/) {
82-
my @choices = @<term>>>.ast;
83-
84-
make @choices > 1
85-
?? self!choose( @choices ) ~ '+'
86-
!! @choices[0];
77+
self!make-term: $/, 'combo';
8778
}
8879

8980
method term-required($/) {
90-
my @choices = $<term>>>.ast;
91-
92-
make @choices > 1
93-
?? [~] self!choose( @choices ), '**', @choices.Int
94-
!! @choices[0];
95-
}
96-
97-
method term-seq($/) {
98-
make @<term>>>.ast.join(' ');
81+
self!make-term: $/, 'required';
9982
}
10083

10184
method term($/) {
10285
my $value = $<value>.ast;
103-
$value ~= .ast
104-
with $<occurs>;
10586

106-
make $value;
87+
make $<occurs>
88+
?? :occurs[$<occurs>.ast, $value]
89+
!! $value;
10790
}
10891

10992
method occurs:sym<maybe>($/) { make '?' }
11093
method occurs:sym<once-plus>($/) { make '+' }
11194
method occurs:sym<zero-plus>($/) { make '*' }
112-
method occurs:sym<list>($/) {
113-
my $quant = $<range> ?? $<range>.ast !! '+';
114-
make "{$quant}% <op(',')>"
95+
method occurs:sym<list>($/) {
96+
with $<range> {
97+
given .ast {
98+
make [.[0], .[1], ',' ];
99+
}
100+
}
101+
else {
102+
make ',';
103+
}
115104
}
116105
method occurs:sym<range>($/) { make $<range>.ast }
117106
method range($/) {
118-
my $range = ' ** ' ~ $<min>.ast;
119-
$range ~= '..' ~ $<max>.ast
120-
if $<max>;
121-
122-
make $range;
107+
my $min = $<min>.ast.value;
108+
my $max = do with $<max> { .ast.value } else { $min };
109+
make [$min, $max];
123110
}
124111

125-
method value:sym<func>($/) {
112+
method value:sym<func>($/) {
126113
# todo - save function prototype
127-
%.prop-refs{ ~$<id>.ast }++;
128-
make [~] '<', $<id>.ast, '>';
114+
my $rule = $<id>.ast;
115+
%.rule-refs{ $rule }++;
116+
make (:$rule);
129117
}
130118

131119
method value:sym<keywords>($/) {
132-
my $keywords = @<keyw> > 1
133-
?? [~] '[ ', @<keyw>>>.ast.join(' | '), ' ]'
134-
!! @<keyw>[0].ast;
135-
136-
make $keywords ~ ' & <keyw>';
120+
my @keywords = @<keyw>.map: {.ast.value};
121+
make (:@keywords);
137122
}
138123

139124
method value:sym<keyw-quant>($/) {
140-
make [~] '[ ', $<keyw>.ast, ' & <keyw> ]', $<occurs>.ast
125+
make 'occurs' => [$<occurs>.ast, $<keyw>.ast];
141126
}
142127

143128
method value:sym<numbers>($/) {
144-
my $keywords = @<digits> > 1
145-
?? [~] '[ ', @<digits>>>.ast.join(' | '), ' ]'
146-
!! @<digits>[0].ast;
147-
148-
make $keywords ~ ' & <number>';
129+
my @numbers = @<digits>.map: {.ast.value};
130+
make (:@numbers);
149131
}
150132

151133
method value:sym<num-quant>($/) {
152-
make [~] '[ ', $<digits>.ast, ' & <number> ]', $<occurs>.ast
134+
make 'occurs' => [$<occurs>.ast, $<digits>.ast];
153135
}
154136

155137
method value:sym<group>($/) {
156-
my $val = $<seq>.ast;
157-
make [~] '[ ', $val, ' ]';
138+
my $group = $<seq>.ast;
139+
make (:$group);
158140
}
159141

160142
method value:sym<rule>($/) {
161-
my $val = ~$<rule>.ast;
162-
%.prop-refs{ $val }++;
163-
make [~] '<', $val, '>'
143+
my $rule = ~$<rule>.ast;
144+
%.rule-refs{ $rule }++;
145+
make (:$rule);
164146
}
165147

166-
method value:sym<op>($/) { make [~] "<op('", $/.trim, "')>" }
148+
method value:sym<op>($/) { my $op = $/.trim; make (:$op); }
167149

168-
method property-ref:sym<css21>($/) { make $<id>.ast }
169-
method property-ref:sym<css3>($/) { make $<id>.ast }
150+
method property-ref:sym<css21>($/) { make 'ref' => $<id>.ast }
151+
method property-ref:sym<css3>($/) { make 'ref' => $<id>.ast }
170152
method value:sym<prop-ref>($/) {
171-
my $prop-ref = $<property-ref>.ast;
172-
%.prop-refs{ 'expr-' ~ $prop-ref }++;
173-
%.child-props{$_}.push: $prop-ref for @*PROP-NAMES;
174-
make [~] '<expr-', $prop-ref, '>';
153+
my Pair $prop-ref = $<property-ref>.ast;
154+
my $rule = 'expr-' ~ $prop-ref.value;
155+
%.rule-refs{ $rule }++;
156+
%.child-props{$_}.push: $prop-ref for @*PROP-NAMES;
157+
make (:$rule);
175158
}
176159

177-
method value:sym<literal>($/) { make [~] "'", ~$0, "'" }
160+
method value:sym<literal>($/) { make 'string' => ~$0 }
178161

179-
method value:sym<num>($/) { make ~$/ }
162+
method value:sym<num>($/) { make 'num' => $/.Int }
180163

181-
method value:sym<keyw>($/) { make ~$/ }
164+
method value:sym<keyw>($/) { make 'ident' => ~$/ }
182165

0 commit comments

Comments
 (0)