Skip to content

Commit 5e3f7ec

Browse files
committed
Backport CSS::Specification::AST to CSS::Grammar::AST
1 parent 180aaff commit 5e3f7ec

File tree

4 files changed

+110
-147
lines changed

4 files changed

+110
-147
lines changed

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{{$NEXT}}
2+
- Backported CSS::Specification::AST methods to
3+
CSS::Grammar::AST (v0.3.13)
24

35
0.4.13 2023-01-01T10:28:12+13:00
46
- Fix for Color::Names api(v2)

META6.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"build-depends": [
77
],
88
"depends": [
9-
"CSS::Grammar:ver<0.3.9+>",
9+
"CSS::Grammar:ver<0.3.12+>",
1010
"Color::Names"
1111
],
1212
"description": "CSS Property definition grammar and generation tools",

lib/CSS/Specification/AST.rakumod

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
use CSS::Grammar::AST;
2-
3-
class CSS::Specification::AST
4-
is CSS::Grammar::AST {
5-
6-
method proforma { [] } # e.g. ['inherit', 'initial']
7-
8-
method decl($/, :$obj!) {
9-
10-
my %ast;
11-
12-
%ast<ident> = .trim.lc
13-
with $0;
14-
15-
with $<val> {
16-
my Hash $val = .ast;
17-
18-
with $val<usage> -> $synopsis {
19-
my $usage = 'usage ' ~ $synopsis;
20-
$usage ~= ' | ' ~ $_
21-
for @.proforma;
22-
$obj.warning($usage);
23-
return;
24-
}
25-
elsif ! $val<expr> {
26-
$obj.warning('dropping declaration', %ast<ident>);
27-
return;
28-
}
29-
else {
30-
%ast<expr> = $val<expr>;
31-
}
32-
}
33-
34-
return %ast;
35-
}
36-
37-
method rule($/) {
38-
$.node($/).pairs[0];
39-
}
1+
class CSS::Specification::AST {
402

3+
use CSS::Grammar::AST;
4+
also is CSS::Grammar::AST;
415
}
Lines changed: 104 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,139 @@
1-
use v6;
1+
unit class CSS::Specification::Defs::Actions;
22

3-
class CSS::Specification::Defs::Actions {
3+
use CSS::Grammar::Defs :CSSValue;
4+
use CSS::Grammar::AST;
45

5-
use CSS::Grammar::Defs :CSSValue;
6-
use CSS::Specification::AST;
6+
method build { CSS::Grammar::AST }
77

8-
method build { CSS::Specification::AST }
8+
method val($/) {
9+
my %ast;
910

10-
method val($/) {
11-
my %ast;
12-
13-
with $<usage> {
14-
%ast<usage> = .ast;
11+
with $<usage> {
12+
%ast<usage> = .ast;
13+
}
14+
else {
15+
with $<proforma> {
16+
%ast<expr> = [.ast]
1517
}
1618
else {
17-
with $<proforma> {
18-
%ast<expr> = [.ast]
19-
}
20-
else {
21-
with $<rx><expr> {
22-
%ast<expr> = $.build.list($_)
23-
unless .can('caps') && (!.caps || .caps.first({! .value.ast.defined}));
24-
}
19+
with $<rx><expr> {
20+
%ast<expr> = $.build.list($_)
21+
unless .can('caps') && (!.caps || .caps.first({! .value.ast.defined}));
2522
}
2623
}
27-
28-
make %ast;
2924
}
3025

31-
method usage($/) {
32-
make ~ $*USAGE;
33-
}
26+
make %ast;
27+
}
3428

35-
# ---- CSS::Grammar overrides ---- #
29+
method usage($/) {
30+
make ~ $*USAGE;
31+
}
3632

37-
method any-function($/) {
38-
## nextsame if $.lax;
39-
if $.lax {
40-
$<any-args>
41-
?? $.warning('skipping function arguments', ~$<any-args>)
42-
!! make $.build.node($/);
43-
}
44-
else {
45-
$.warning('ignoring function', $<Ident>.ast.lc);
46-
}
33+
# ---- CSS::Grammar overrides ---- #
34+
35+
method any-function($/) {
36+
## nextsame if $.lax;
37+
if $.lax {
38+
$<any-args>
39+
?? $.warning('skipping function arguments', ~$<any-args>)
40+
!! make $.build.node($/);
4741
}
42+
else {
43+
$.warning('ignoring function', $<Ident>.ast.lc);
44+
}
45+
}
4846

49-
multi method declaration($/ where $<any-declaration>) {
50-
with $<any-declaration>.ast -> $ast {
51-
my ($key, $value) = $ast.kv;
52-
if $.lax {
53-
make $key => $value;
54-
}
55-
else {
56-
$.warning('dropping unknown property',
57-
$value<at-keyw> ?? '@'~$value<at-keyw> !! $value<ident>);
58-
}
47+
multi method declaration($/ where $<any-declaration>) {
48+
with $<any-declaration>.ast -> $ast {
49+
my ($key, $value) = $ast.kv;
50+
if $.lax {
51+
make $key => $value;
52+
}
53+
else {
54+
$.warning('dropping unknown property',
55+
$value<at-keyw> ?? '@'~$value<at-keyw> !! $value<ident>);
5956
}
6057
}
61-
multi method declaration($/) {
62-
my %ast = %( $.build.decl($<decl>, :obj(self)) )
63-
|| return;
58+
}
59+
multi method declaration($/) {
60+
my %ast = %( $.build.decl($<decl>, :obj(self)) )
61+
|| return;
6462

65-
if $<any-arg> {
66-
return $.warning("extra terms following '{%ast<ident>}' declaration",
67-
~$<any-arg>, 'dropped');
68-
}
63+
if $<any-arg> {
64+
return $.warning("extra terms following '{%ast<ident>}' declaration",
65+
~$<any-arg>, 'dropped');
66+
}
6967

70-
if $<prio> && (my $prio = $<prio>.ast) {
71-
%ast ,= :$prio;
72-
}
73-
make $.build.token( %ast, :type(CSSValue::Property) );
68+
if $<prio> && (my $prio = $<prio>.ast) {
69+
%ast ,= :$prio;
7470
}
71+
make $.build.token( %ast, :type(CSSValue::Property) );
72+
}
7573

76-
method proforma:sym<inherit>($/) { make (:keyw<inherit>) }
77-
method proforma:sym<initial>($/) { make (:keyw<initial>) }
74+
method proforma:sym<inherit>($/) { make (:keyw<inherit>) }
75+
method proforma:sym<initial>($/) { make (:keyw<initial>) }
7876

79-
#---- Language Extensions ----#
77+
#---- Language Extensions ----#
8078

81-
method length:sym<zero>($/) {
82-
make $.build.token(0, :type<px>)
83-
}
79+
method length:sym<zero>($/) {
80+
make $.build.token(0, :type<px>)
81+
}
8482

85-
method length:sym<percent>($/) {
86-
make $<percentage>.ast;
87-
}
83+
method length:sym<percent>($/) {
84+
make $<percentage>.ast;
85+
}
8886

89-
method angle:sym<zero>($/) {
90-
make $.build.token(0, :type<deg>)
91-
}
87+
method angle:sym<zero>($/) {
88+
make $.build.token(0, :type<deg>)
89+
}
9290

93-
method time:sym<zero>($/) {
94-
make $.build.token(0, :type<s>)
95-
}
91+
method time:sym<zero>($/) {
92+
make $.build.token(0, :type<s>)
93+
}
9694

97-
method frequency:sym<zero>($/) {
98-
make $.build.token(0, :type<hz>)
99-
}
95+
method frequency:sym<zero>($/) {
96+
make $.build.token(0, :type<hz>)
97+
}
10098

101-
use Color::Names::CSS3 :colors;
102-
my constant %Colors = do {
103-
my %v;
104-
for COLORS.pairs {
105-
my (Str $name, Hash $val) = .kv;
106-
$name .= substr(0, $_) with $name.index('-');
107-
my List $rgb = $val<rgb>;
99+
use Color::Names::CSS3 :colors;
100+
my constant %Colors = do {
101+
my %v;
102+
for COLORS.pairs {
103+
my (Str $name, Hash $val) = .kv;
104+
$name .= substr(0, $_) with $name.index('-');
105+
my List $rgb = $val<rgb>;
106+
%v{$name} = $rgb;
107+
with $name.index("gray") {
108+
$name.substr-rw($_, 4) = 'grey';
108109
%v{$name} = $rgb;
109-
with $name.index("gray") {
110-
$name.substr-rw($_, 4) = 'grey';
111-
%v{$name} = $rgb;
112-
}
113110
}
114-
%v;
115111
}
116-
method colors { %Colors }
112+
%v;
113+
}
114+
method colors { %Colors }
117115

118-
method color:sym<named>($/) {
119-
my Str $color-name = $<keyw>.ast.value;
120-
my @rgb = @( $.colors{$color-name} )
121-
or die "unknown color: " ~ $color-name;
116+
method color:sym<named>($/) {
117+
my Str $color-name = $<keyw>.ast.value;
118+
my @rgb = @( $.colors{$color-name} )
119+
or die "unknown color: " ~ $color-name;
122120

123-
my @color = @rgb.map: { (CSSValue::NumberComponent) => $_ };
121+
my @color = @rgb.map: { (CSSValue::NumberComponent) => $_ };
124122

125-
make $.build.token(@color, :type<rgb>);
126-
}
127-
128-
method integer($/) {
129-
my Int $val = $<uint>.ast;
130-
$val = -$val
131-
if $<sign> && $<sign> eq '-';
132-
make $.build.token($val, :type(CSSValue::IntegerComponent))
133-
}
123+
make $.build.token(@color, :type<rgb>);
124+
}
134125

135-
method number($/) { make $.build.token($<num>.ast, :type(CSSValue::NumberComponent)) }
136-
method uri($/) { make $<url>.ast }
137-
method keyw($/) { make $.build.token($<id>.lc, :type(CSSValue::KeywordComponent)) }
138-
# case sensitive identifiers
139-
method identifier($/) { make $.build.token($<name>.ast, :type(CSSValue::IdentifierComponent)) }
140-
# identifiers strung-together, e.g New Century Schoolbook
141-
method identifiers($/) { make $.build.token( $<identifier>.map({ .ast.value }).join(' '), :type(CSSValue::IdentifierComponent)) }
126+
method integer($/) {
127+
my Int $val = $<uint>.ast;
128+
$val = -$val
129+
if $<sign> && $<sign> eq '-';
130+
make $.build.token($val, :type(CSSValue::IntegerComponent))
142131
}
132+
133+
method number($/) { make $.build.token($<num>.ast, :type(CSSValue::NumberComponent)) }
134+
method uri($/) { make $<url>.ast }
135+
method keyw($/) { make $.build.token($<id>.lc, :type(CSSValue::KeywordComponent)) }
136+
# case sensitive identifiers
137+
method identifier($/) { make $.build.token($<name>.ast, :type(CSSValue::IdentifierComponent)) }
138+
# identifiers strung-together, e.g New Century Schoolbook
139+
method identifiers($/) { make $.build.token( $<identifier>.map({ .ast.value }).join(' '), :type(CSSValue::IdentifierComponent)) }

0 commit comments

Comments
 (0)