Debian Codegraph @

sass-spec/spec/parser/operations/operations.pl

raw
1 use strict;
2 use warnings;
3
4 use File::chdir;
5 use File::Slurp qw(write_file);
6 use Algorithm::Combinatorics qw(:all);
7
8 # all operators ('+', '-', '*', '/', '%', '<', '==', ...)
9 # operator spacing ('+', '+ ', ' +', ' + ')
10 # interpolation on left, right, both and none
11 # test two and more operations (precedence?)
12 # mix number, strings, null and other types
13
14 my %op = (
15 'modulo' => '%',
16 'addition' => '+',
17 'subtract' => '-',
18 'multiply' => '*',
19 'division' => '/',
20 'logic_gt' => '>',
21 'logic_lt' => '<',
22 'logic_ge' => '>=',
23 'logic_le' => '<=',
24 'logic_eq' => '==',
25 'logic_ne' => '!=',
26 );
27 my %types = (
28 'numbers' => [
29 '10',
30 #'-26',
31 #'+39',
32 '#{10}',
33 '1#{0}',
34 '#{1}0',
35 ],
36 'fractions' => [
37 '.25',
38 '-.25',
39 #'+.325',
40 '#{.25}',
41 '#{-.25}',
42 '#{.}25',
43 ],
44 'strings' => [
45 'literal',
46 '"quoted"',
47 '#{interpolant}',
48 'lschema_#{ritlp}',
49 '#{litlp}_rschema',
50 ],
51 'dimensions' => [
52 '10',
53 # '-10',
54 '10px',
55 # '-10px',
56 '#{10}px',
57 '1#{0}px',
58 '10#{px}',
59 '10#{p}x',
60 ],
61 'mixed' => [
62 '10',
63 #'-10',
64 '10%',
65 #'-10%',
66 '10px',
67 #'-10px',
68 '#AAA',
69 #'-#AAA',
70 '#{itpl}',
71 #'-#{itpl}',
72 ],
73 );
74 my %variants = (
75 'pairs' => {
76 'combinations' => sub {
77 my ($o, $v, $t) = @_;
78 return combinations_with_repetition($t, 2);
79 },
80 'render' => sub {
81 my ($c, $o, $v, $f) = @_;
82 my $compare_op = ($o =~ m/^\s*[<>]=?\s*$/);
83 my $multiply_op = ($o =~ m/^\s*[\*%]\s*$/);
84 my $skip_literal = $compare_op || $multiply_op;
85 return '' if $skip_literal && $f->[0] eq '"quoted"';
86 return '' if $skip_literal && $f->[1] eq '"quoted"';
87 return '' if $skip_literal && $f->[0] eq 'literal';
88 return '' if $skip_literal && $f->[1] eq 'literal';
89 return " test-$c: $f->[0]$o$f->[1];\n";
90 }
91 },
92 'tripplets' => {
93 'combinations' => sub {
94 my ($o, $v, $t) = @_;
95 return combinations_with_repetition($t, 3);
96 },
97 'render' => sub {
98 my ($c, $o, $v, $f) = @_;
99 # logic operations do not work with tripplets
100 # first pair would evaluate to a boolean
101 # boolean
102 return '' if ($o =~ m/^\s*[<>]=?\s*$/);
103 return " test-$c: $f->[0]$o$f->[1]$o$f->[2];\n";
104 }
105 },
106 );
107
108 local $CWD = $CWD;
109
110 sub create_variants
111 {
112 my ($op, $type) = @_;
113 foreach my $variant (keys %variants)
114 {
115
116 # skip some combination which have some problems with deprecation warnings
117 next if ($op eq "multiply" && $type eq "dimensions" && $variant eq "pairs");
118
119 mkdir $variant;
120 local $CWD = $variant;
121 my $o = $op{$op};
122 my $a = $types{$type};
123 my $v = $variants{$variant};
124
125 # init combination iterator of this variant
126 my $combinator = $v->{'combinations'}->($o, $v, $a);
127
128 my $count = 0;
129 my $result = "foo {\n";
130
131 while (my $f = $combinator->next) {
132 $result .= $v->{'render'}->(++$count, "$o", $v, $f);
133 $result .= $v->{'render'}->(++$count, " $o", $v, $f);
134 $result .= $v->{'render'}->(++$count, "$o ", $v, $f);
135 $result .= $v->{'render'}->(++$count, " $o ", $v, $f);
136 }
137
138 print 'created ', join('/', $op, $type, $variant, 'input.scss'), "\n";
139 write_file('input.scss', { binmode => ':raw' }, $result. "}\n");
140
141 }
142 }
143
144 sub create_types
145 {
146 my ($op) = @_;
147 foreach my $type (keys %types)
148 {
149 mkdir $type;
150 local $CWD = $type;
151 create_variants($op, $type);
152 }
153 }
154
155 sub create_ops
156 {
157 foreach my $op (keys %op)
158 {
159 mkdir $op;
160 local $CWD = $op;
161 create_types($op);
162 }
163 }
164
165 create_ops();
166
167 __DATA__
168
169
170 #{10}px + 10px;
171 10#{px} + 10px;
172
173 #{10}px+10px;
174 #{10}px+ 10px;
175 #{10}px +10px;
176
177 10#{px} + 10px;
178
179
180 my @input = (
181 ['01_literal', qq(literal)],
182 ['02_double_quoted', qq("dquoted")],
183 ['03_single_quoted', qq('squoted')],
184 ['04_space_list_quoted', qq("alpha" 'beta')],
185 ['05_comma_list_quoted', qq("alpha", 'beta')],
186 ['06_space_list_complex', qq(gamme "'"delta"'")],
187 ['07_comma_list_complex', qq(gamma, "'"delta"'")],
188 ['10_escaped_backslash', qq(\\\\)],
189 ['11_escaped_literal', qq(l\\\\ite\\ral)],
190 ['12_escaped_double_quoted', qq("l\\\\ite\\ral")],
191 ['13_escaped_single_quoted', qq('l\\\\ite\\ral')],
192
193 ['14_escapes_literal_numbers', qq(\\1\\2\\3\\4\\5\\6\\7\\8\\9)],
194 ['15_escapes_double_quoted_numbers', qq("\\1\\2\\3\\4\\5\\6\\7\\8\\9")],
195 ['16_escapes_single_quoted_numbers', qq('\\1\\2\\3\\4\\5\\6\\7\\8\\9')],
196 ['17_escapes_literal_lowercase', qq(\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\l\\m\\n\\o\\p\\q\\r\\s\\t\\u\\v\\w\\x\\y\\z)],
197 ['18_escapes_double_quoted_lowercase', qq("\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\l\\m\\n\\o\\p\\q\\r\\s\\t\\u\\v\\w\\x\\y\\z")],
198 ['19_escapes_single_quoted_lowercase', qq('\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\l\\m\\n\\o\\p\\q\\r\\s\\t\\u\\v\\w\\x\\y\\z')],
199 ['20_escapes_literal_uppercase', qq(\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z)],
200 ['21_escapes_double_quoted_uppercase', qq("\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z")],
201 ['22_escapes_single_quoted_uppercase', qq('\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z')],
202
203 ['23_escapes_literal_specials', qq(\\0_\\a_\\A)],
204 ['24_escapes_double_quoted_specials', qq("\\0_\\a_\\A")],
205 ['25_escapes_single_quoted_specials', qq('\\0_\\a_\\A')],
206
207 ['26_escaped_literal_quotes', qq(\\\"\\\')],
208 ['27_escaped_double_quotes', qq("\\\"")],
209 ['28_escaped_single_quotes', qq('\\\'')],
210
211 ['29_binary_operation', qq("foo#{'ba' + 'r'}baz")],
212 ['30_base_test', qq("foo#{'ba' + 'r'}baz")],
213
214 ['31_schema_simple', qq("["'foo'"]")],
215 ['32_comma_list', qq("["',foo, '"]")],
216 ['33_space_list', qq("["'foo '"]" "bar")],
217 ['34_mixed_list', qq("["',foo , '"]" "bar")],
218
219 );
220
221 my @template;
222
223 push @template, "01_inline";
224 push @template, << "EOF";
225 .result {
226 output: %%;
227 output: #{%%};
228 output: "[#{%%}]";
229 output: "#{%%}";
230 output: '#{%%}';
231 output: "['#{%%}']";
232 }
233 EOF
234
235
236 push @template, "02_variable";
237 push @template, << "EOF";
238 \$input: %%;
239 .result {
240 output: \$input;
241 output: #{\$input};
242 output: "[#{\$input}]";
243 output: "#{\$input}";
244 output: '#{\$input}';
245 output: "['#{\$input}']";
246 }
247 EOF
248
249 push @template, "03_inline_double";
250 push @template, << "EOF";
251 .result {
252 output: #{#{%%}};
253 output: #{"[#{%%}]"};
254 output: #{"#{%%}"};
255 output: #{'#{%%}'};
256 output: #{"['#{%%}']"};
257 }
258 EOF
259
260 push @template, "04_variable_double";
261 push @template, << "EOF";
262 \$input: %%;
263 .result {
264 output: #{#{\$input}};
265 output: #{"[#{\$input}]"};
266 output: #{"#{\$input}"};
267 output: #{'#{\$input}'};
268 output: #{"['#{\$input}']"};
269 }
270 EOF
271
272 push @template, "05_variable_quoted_double";
273 push @template, << "EOF";
274 \$input: %%;
275 .result {
276 dquoted: "#{#{\$input}}";
277 dquoted: "#{"[#{\$input}]"}";
278 dquoted: "#{"#{\$input}"}";
279 dquoted: "#{'#{\$input}'}";
280 dquoted: "#{"['#{\$input}']"}";
281 squoted: '#{#{\$input}}';
282 squoted: '#{"[#{\$input}]"}';
283 squoted: '#{"#{\$input}"}';
284 squoted: '#{'#{\$input}'}';
285 squoted: '#{"['#{\$input}']"}';
286 }
287 EOF
288 # ruby sass cannot handle these cases ...
289 # pop(@template); pop(@template);
290
291 push @template, "06_escape_interpolation";
292 push @template, << "EOF";
293 \$input: %%;
294 .result {
295 output: "[\\#{%%}]";
296 output: "\\#{%%}";
297 output: '\\#{%%}';
298 output: "['\\#{%%}']";
299 }
300 EOF
301
302
303 sub render {
304 use File::Slurp qw(write_file);
305 my ($names, $template, $input) = @_;
306 $template =~ s/\%\%/$input/g;
307 local $CWD = $CWD;
308 foreach (@{$names}) {
309 mkdir $_;
310 $CWD = $_;
311 }
312 print "created ", join("/", @{$names}), "\n";
313 return write_file('input.scss', { binmode => ':raw' }, $template);
314
315 }
316
317 while (defined(my $name = shift @template)) {
318 my $template = shift(@template);
319 foreach my $input (@input) {
320 render([$input->[0], $name], $template, $input->[1]);
321 }
322 }
323
324 # <>;