Skip to content

Commit 8c91382

Browse files
committed
Start generating property declarations
1 parent e683d96 commit 8c91382

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

lib/CSS/Specification.rakumod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ grammar CSS::Specification:ver<0.4.15> {
4040
rule digits { \d+ }
4141
rule rule { '<'~'>' <id> }
4242
43-
rule seq { <term=.term-options>+ }
43+
rule seq { <term=.term-options>+ }
4444
rule term-options { <term=.term-combo> +% '|' }
4545
rule term-combo { <term=.term-required> +% '||' }
4646
rule term-required { <term=.term-seq> +% '&&' }

lib/CSS/Specification/Compiler/Actions.rakumod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ method value:sym<rule>($/) {
147147

148148
method value:sym<op>($/) { my $op = $/.trim; make (:$op); }
149149

150-
method property-ref:sym<css21>($/) { make $<id>.ast }
151-
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 }
152152
method value:sym<prop-ref>($/) {
153-
my $prop-ref = $<property-ref>.ast;
154-
my $rule = 'expr-' ~ $prop-ref;
153+
my Pair $prop-ref = $<property-ref>.ast;
154+
my $rule = 'expr-' ~ $prop-ref.value;
155155
%.rule-refs{ $rule; }++;
156156
%.child-props{$_}.push: $prop-ref for @*PROP-NAMES;
157157
make (:$rule);

lib/CSS/Specification/Compiler/RakuAST.rakumod

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ our proto sub compile (|c) is export(:compile) {
88

99
multi sub modifier('i') { RakuAST::Regex::InternalModifier::IgnoreCase.new }
1010

11-
sub rule(RakuAST::Name:D :$name!, RakuAST::Regex:D :$body!, Str :$leading) {
11+
sub rule(RakuAST::Name:D :$name!, RakuAST::Regex:D :$body!) {
1212
RakuAST::RuleDeclaration.new(
1313
:$name,
1414
:$body,
15-
).declarator-docs(
16-
:$leading
1715
)
1816
}
1917

@@ -28,16 +26,55 @@ sub expression($expression) {
2826
RakuAST::Statement::Expression.new: :$expression;
2927
}
3028

29+
sub property-decl(Str:D $prop-name) {
30+
rule(
31+
name => RakuAST::Name.from-identifier("decl:sym<$prop-name>"),
32+
body => seq (
33+
'i'.&modifier,
34+
RakuAST::Regex::CapturingGroup.new(
35+
(
36+
$prop-name.&lit,
37+
).&seq
38+
).&ws,
39+
RakuAST::Regex::Quote.new(
40+
RakuAST::QuotedString.new(
41+
segments => (
42+
RakuAST::StrLiteral.new(":"),
43+
)
44+
)
45+
).&ws,
46+
RakuAST::Regex::Assertion::Named::Args.new(
47+
name => 'val'.&name,
48+
args => RakuAST::ArgList.new(
49+
RakuAST::QuotedRegex.new(
50+
body => RakuAST::Regex::WithWhitespace.new(
51+
RakuAST::Regex::Assertion::Alias.new(
52+
name => "expr",
53+
assertion => ("expr-" ~ $prop-name).&assertion(:!capturing),
54+
)
55+
)
56+
),
57+
RakuAST::Var::Compiler::Routine.new.&postfix('WHY'.&call),
58+
),
59+
:capturing
60+
).&ws
61+
)
62+
);
63+
}
64+
3165
multi sub compile(:@props!, :$default, :$spec, Str :$synopsis, Bool :$inherit = True) {
3266
die "todo: {@props}" unless @props == 1;
3367
my $prop = @props.head;
3468
my RakuAST::Regex $body = $spec.&compile;
35-
$body = seq [ modifier('i'), ws($body), ];
69+
$body = ('i'.&modifier, $body.&ws, ).&seq;
3670

3771
my Str $leading = $_ ~ "\n" with $synopsis;
3872

3973
(
40-
rule(name => ('expr-' ~ $prop).&name, :$body, :$leading).&expression,
74+
$prop.&property-decl.declarator-docs(
75+
:$leading
76+
),
77+
rule(name => ('expr-' ~ $prop).&name, :$body).&expression,
4178
).&statements;
4279
}
4380

@@ -60,7 +97,7 @@ multi sub quant(Array:D $_ where .elems >= 2) {
6097

6198
sub name(Str:D $id) is export { RakuAST::Name.from-identifier($id) }
6299

63-
sub look-ahead(RakuAST::Regex::Assertion $assertion, Bool :$negated = False, Bool :$capturing = False) is export {
100+
sub look-ahead(RakuAST::Regex::Assertion $assertion, Bool :$negated = False) is export {
64101
RakuAST::Regex::Assertion::Lookahead.new(
65102
:$assertion, :$negated
66103
);
@@ -93,7 +130,6 @@ multi sub arg(Int:D $arg) {
93130
multi sub ws(RakuAST::Regex $r) is export { RakuAST::Regex::WithWhitespace.new($r) }
94131

95132
sub lit(Str:D $s) is export { RakuAST::Regex::Literal.new($s) }
96-
sub lit-ws(Str:D() $_) is export { .&lit.&ws }
97133

98134
sub group(RakuAST::Regex $r) is export { RakuAST::Regex::Group.new: $r }
99135

@@ -178,6 +214,8 @@ multi sub compile(:@numbers!) {
178214
_choice @numbers.map(&lit-ws), 'number'.&assertion;
179215
}
180216

217+
sub lit-ws(Str:D() $_) is export { .&lit.&ws }
218+
181219
multi sub compile(Str:D :$op!) {
182220
my RakuAST::ArgList $args = ','.&arg;
183221
'op'.&assertion(:$args);

t/00compile.t

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ for (
118118
rule-refs => ['length', 'percentage'],
119119
deparse => join("\n",
120120
'#| <length> | <percentage> | inherit',
121-
'rule expr-min-width {',
122-
':i <length> | <percentage> | [inherit & <keyw>] }',
121+
'rule decl:sym<min-width> {;',
122+
':i ("min-width") ":" <val(/ <expr=.expr-min-width> /, &?ROUTINE.WHY)> }',
123+
'rule expr-min-width { :i <length> | <percentage> | [inherit & <keyw>] }',
123124
''),
124125
},
125126
'property-spec' => {input => "'content'\tnormal | none | [ <string> | <uri> | <counter> | attr(<identifier>) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit\tnormal :before and :after pseudo-elements no",
@@ -137,8 +138,9 @@ for (
137138
rule-refs => [<attr counter identifier string uri>],
138139
deparse => join("\n",
139140
'#| normal | none | [ <string> | <uri> | <counter> | attr(<identifier>) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit',
140-
'rule expr-content {',
141-
':i [[normal | none ]& <keyw>] | [<string> | <uri> | <counter> | <attr> | [["open-quote" | "close-quote" | "no-open-quote" | "no-close-quote" ]& <keyw>] ]+ | [inherit & <keyw>] }',
141+
'rule decl:sym<content> {;', # stray semicolon?
142+
':i (content) ":" <val(/ <expr=.expr-content> /, &?ROUTINE.WHY)> }',
143+
'rule expr-content { :i [[normal | none ]& <keyw>] | [<string> | <uri> | <counter> | <attr> | [["open-quote" | "close-quote" | "no-open-quote" | "no-close-quote" ]& <keyw>] ]+ | [inherit & <keyw>] }',
142144
''
143145
),
144146

0 commit comments

Comments
 (0)