|
1 |
| -use v6; |
| 1 | +unit class CSS::Specification::Defs::Actions; |
2 | 2 |
|
3 |
| -class CSS::Specification::Defs::Actions { |
| 3 | +use CSS::Grammar::Defs :CSSValue; |
| 4 | +use CSS::Grammar::AST; |
4 | 5 |
|
5 |
| - use CSS::Grammar::Defs :CSSValue; |
6 |
| - use CSS::Specification::AST; |
| 6 | +method build { CSS::Grammar::AST } |
7 | 7 |
|
8 |
| - method build { CSS::Specification::AST } |
| 8 | +method val($/) { |
| 9 | + my %ast; |
9 | 10 |
|
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] |
15 | 17 | }
|
16 | 18 | 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})); |
25 | 22 | }
|
26 | 23 | }
|
27 |
| - |
28 |
| - make %ast; |
29 | 24 | }
|
30 | 25 |
|
31 |
| - method usage($/) { |
32 |
| - make ~ $*USAGE; |
33 |
| - } |
| 26 | + make %ast; |
| 27 | +} |
34 | 28 |
|
35 |
| - # ---- CSS::Grammar overrides ---- # |
| 29 | +method usage($/) { |
| 30 | + make ~ $*USAGE; |
| 31 | +} |
36 | 32 |
|
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($/); |
47 | 41 | }
|
| 42 | + else { |
| 43 | + $.warning('ignoring function', $<Ident>.ast.lc); |
| 44 | + } |
| 45 | +} |
48 | 46 |
|
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>); |
59 | 56 | }
|
60 | 57 | }
|
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; |
64 | 62 |
|
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 | + } |
69 | 67 |
|
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; |
74 | 70 | }
|
| 71 | + make $.build.token( %ast, :type(CSSValue::Property) ); |
| 72 | +} |
75 | 73 |
|
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>) } |
78 | 76 |
|
79 |
| - #---- Language Extensions ----# |
| 77 | +#---- Language Extensions ----# |
80 | 78 |
|
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 | +} |
84 | 82 |
|
85 |
| - method length:sym<percent>($/) { |
86 |
| - make $<percentage>.ast; |
87 |
| - } |
| 83 | +method length:sym<percent>($/) { |
| 84 | + make $<percentage>.ast; |
| 85 | +} |
88 | 86 |
|
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 | +} |
92 | 90 |
|
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 | +} |
96 | 94 |
|
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 | +} |
100 | 98 |
|
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'; |
108 | 109 | %v{$name} = $rgb;
|
109 |
| - with $name.index("gray") { |
110 |
| - $name.substr-rw($_, 4) = 'grey'; |
111 |
| - %v{$name} = $rgb; |
112 |
| - } |
113 | 110 | }
|
114 |
| - %v; |
115 | 111 | }
|
116 |
| - method colors { %Colors } |
| 112 | + %v; |
| 113 | +} |
| 114 | +method colors { %Colors } |
117 | 115 |
|
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; |
122 | 120 |
|
123 |
| - my @color = @rgb.map: { (CSSValue::NumberComponent) => $_ }; |
| 121 | + my @color = @rgb.map: { (CSSValue::NumberComponent) => $_ }; |
124 | 122 |
|
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 | +} |
134 | 125 |
|
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)) |
142 | 131 | }
|
| 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