Skip to content

Commit c53391f

Browse files
committed
Handle quantization of box properties
1 parent b1a6dfe commit c53391f

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

lib/CSS/Specification/Compiler/Grammars.rakumod

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ sub rule(RakuAST::Name:D :$name!, RakuAST::Regex:D :$body!) {
2020
)
2121
}
2222

23-
sub property-decl(Str:D $prop-name) {
23+
sub property-decl(Str:D $prop-name, :$quant) {
2424
my RakuAST::Name $name = "decl:sym<$prop-name>".&name;
25+
my RakuAST::Regex $regex-body = RakuAST::Regex::Assertion::Alias.new(
26+
name => "expr",
27+
assertion => ("expr-" ~ $prop-name).&assertion(:!capturing),
28+
);
29+
if $quant ~~ Array:D {
30+
my RakuAST::Regex::Quantifier $quantifier = quant($quant);
31+
$regex-body = RakuAST::Regex::QuantifiedAtom.new: :atom($regex-body), :$quantifier;
32+
}
33+
2534
rule(
2635
:$name,
2736
body => seq (
@@ -40,10 +49,7 @@ sub property-decl(Str:D $prop-name) {
4049
name => 'val'.&name,
4150
args => RakuAST::ArgList.new(
4251
RakuAST::QuotedRegex.new(
43-
body => RakuAST::Regex::Assertion::Alias.new(
44-
name => "expr",
45-
assertion => ("expr-" ~ $prop-name).&assertion(:!capturing),
46-
).&ws,
52+
body => $regex-body.&ws,
4753
),
4854
RakuAST::Var::Compiler::Routine.new.&postfix('WHY'.&call),
4955
),
@@ -53,7 +59,11 @@ sub property-decl(Str:D $prop-name) {
5359
);
5460
}
5561

56-
multi sub compile(:@props!, :$default, :$spec!, Str :$synopsis!, Bool :$inherit = True) {
62+
multi sub compile(:@props!, :$default, Pair :$spec! is copy, Str :$synopsis!, Bool :$inherit = True) {
63+
my $quant;
64+
if $spec.key eq 'occurs' && $spec.value.head ~~ [1,4] {
65+
($quant, $spec) = $spec.value.List;
66+
}
5767
my RakuAST::Regex $body = $spec.&compile;
5868
$body = ('i'.&modifier, $body.&ws, ).&seq;
5969

@@ -64,10 +74,10 @@ multi sub compile(:@props!, :$default, :$spec!, Str :$synopsis!, Bool :$inherit
6474

6575
for @props -> $prop {
6676
@exprs.append: (
67-
$prop.&property-decl.declarator-docs(
77+
$prop.&property-decl(:$quant).declarator-docs(
6878
:$leading
6979
).&expression,
70-
rule(name => ('expr-' ~ $prop).&name, :$body).&expression,
80+
rule(name => ('expr-' ~ $prop).&name, :$body).&expression,
7181
)
7282
}
7383
@exprs;
@@ -274,6 +284,7 @@ multi sub compile(:@combo!, Bool :$required) {
274284
RakuAST::Regex::QuantifiedAtom.new: :$atom, :$quantifier;
275285
}
276286

287+
277288
multi sub compile($arg) { compile |$arg }
278289

279290
method build-grammar(@grammar-id) {

t/00compile.t

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ for (
115115
deparse => "[:my \@S\n; bold \& <keyw><!\{\n !\@S[0]++\n}>| [:my \@S\n; thin \& <keyw><!\{\n !\@S[0]++\n}>| <length><!\{\n !\@S[1]++\n}>]** 2<!\{\n !\@S[1]++\n}>]+",
116116
rule-refs => ['length'],
117117
},
118+
'property-spec' => {
119+
input => join("\t", 'border-color','<color>{1,4}', 'transparent'),
120+
ast => {
121+
:props['border-color'],
122+
:default<transparent>,
123+
:synopsis('<color>{1,4}'),
124+
:spec(:occurs[[1, 4], :rule<color>]),
125+
},
126+
rule-refs => ['color'],
127+
deparse => join("\n",
128+
'#| border-color: <color>{1,4}',
129+
'rule decl:sym<border-color> { :i ("border-color") ":" <val(/<expr=.expr-border-color>** 1..4 /, &?ROUTINE.WHY)> }',
130+
'rule expr-border-color { :i <color> }',
131+
''),
132+
},
118133
'property-spec' => {
119134
input => "'min-width'\t<length> | <percentage> | inherit\t0",
120135
ast => {

t/lib/Test/CSS/Aural/Spec/Grammar.rakumod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ rule specific-voice { :i <identifier> || <string> }
7676
rule decl:sym<volume> { :i (volume) ":" <val(/<expr=.expr-volume> /, &?ROUTINE.WHY)> }
7777
rule expr-volume { :i <number> || <percentage> || [silent | "x-soft" | soft | medium | loud | "x-loud" ]& <keyw> }
7878
#| border-color: [ <color> | transparent ]{1,4}
79-
rule decl:sym<border-color> { :i ("border-color") ":" <val(/<expr=.expr-border-color> /, &?ROUTINE.WHY)> }
80-
rule expr-border-color { :i [<color> || transparent & <keyw> ]** 1..4 }
79+
rule decl:sym<border-color> { :i ("border-color") ":" <val(/<expr=.expr-border-color>** 1..4 /, &?ROUTINE.WHY)> }
80+
rule expr-border-color { :i [<color> || transparent & <keyw> ] }
8181
#| border-top-color: <color> | transparent
8282
rule decl:sym<border-top-color> { :i ("border-top-color") ":" <val(/<expr=.expr-border-top-color> /, &?ROUTINE.WHY)> }
8383
rule expr-border-top-color { :i <color> || transparent & <keyw> }

0 commit comments

Comments
 (0)