1#!/usr/bin/env raku
2
3=begin pod
4=head1 Some test data for B<Chroma>
5
6=head2 Multi-line
7heading
8
9=head2 One-line heading
10=begin code :lang<go>
11fmt.Println("Hello from Go")
12=end code
13
14 =begin DESCRIPTION
15 Some description
16 =item one item
17 =end DESCRIPTION
18
19=for defn :numbered
20 We
21 Need
22 Numbers
23
24=end pod
25
26say $=pod[0].config<numbered>;
27
28sub f1($a) {
29 $a+1; #=> Comment looking like a pod declaration, but with a closing bracket!
30}
31
322.&f1;
33
34sub f2($a,$b) {
35 $a+1;
36}
37
382.&f2(3);
39
40Module::function($var);
41
42#| Fibonacci with Multiple dispatch
43multi sub fib (0 --> 0) {}
44multi sub fib (1 --> 1) {}
45multi sub fib (\n where * > 1) {
46 fib(n - 1) + fib(n - 2)
47}
48say fib 10;
49# OUTPUT: 55
50
51#| B<A C<<uh U<Shape> umm>> role>
52role Shape {
53 method area { ... }
54
55 method print_area {
56 say "Area of {self.^name} is {self.area}.";
57 }
58}
59
60class Rectangle does Shape {
61 has $.width is required;
62 has $.height is required;
63
64 method area {
65 $!width * $!height
66 }
67}
68
69Rectangle.new(width => 5, height => 7).print_area;
70
71# Inifinite and lazy list
72my @fib = 0, 1, * + * ... ∞;
73say @fib[^11];
74# OUTPUT: (0 1 1 2 3 5 8 13 21 34 55)
75
76# Feed operator
77@fib[^20] ==> grep(&is-prime) ==> say();
78# OUTPUT: (2 3 5 13 89 233 1597)
79
80# Function composition
81my &reverse_primes = &reverse ∘ &grep.assuming(&is-prime);
82say reverse_primes ^20;
83# OUTPUT: (19 17 13 11 7 5 3 2)
84
85my @a = 1..4;
86my @b = 'a'..'d';
87# Zip two lists using Z meta operator
88say @a Z @b;
89# OUTPUT: ((1 a) (2 b) (3 c) (4 d))
90say @a Z=> @b;
91# OUTPUT: (1 => a 2 => b 3 => c 4 => d)
92say [\R<] 1, 5, 6;
93
94# Hyper Operators
95say @b «~» @a;
96# OUTPUT: [a1 b2 c3 d4]
97
98# Junctions
99say 'Find all the words starting with a lowercase vowel'.words.grep: *.starts-with: any <a e i o u>;
100
101sub MAIN(
102 Str $file where *.IO.f = 'file.dat', #= an existing file to frobnicate
103 Int :size(:$length) = 24, #= length/size needed for frobnication
104 Bool :$verbose, #= required verbosity
105) {
106 say $length if $length.defined;
107 say $file if $file.defined;
108 say 'Verbosity ', ($verbose ?? 'on' !! 'off');
109}
110
111#|「[
112INI Parser
113C<SomeCode>
114=head1 heading
115]」
116grammar INIParser {
117 token TOP { <block> <section>* }
118 token section { <header> <block> }
119 token header { '[' ~ ']' \w+ \n+ }
120 token block { [<pair> | <comment>]* }
121 rule pair { <key> '=' <value> }
122 token comment { ';' \N* \n+ }
123 token key { \w+ }
124 token value { <-[\n ;]>+ }
125}
126
127my $match = INIParser.parse: q:to/END/;
128; Comment
129key1=value1
130key2 = value2
131
132; Section 1
133[section1]
134key3=value3
135END
136
137grammar Nested {
138 token TOP { <block> <section>* { if $/ { say $/ } } }
139 token block {
140 <?before <.[\)\]\}]>>
141 }
142
143 token you_are_here {
144 <?{ nqp::getlexdyn('$?FILES') ~~ /\.setting$/ }> ||
145 \w+ 'some text' \d+
146 { self.typed_panic('X::Syntax::Reserved',
147 reserved => 'use of {YOU_ARE_HERE} outside of a setting',
148 instead => ' (use whitespace if not a setting, or rename file with .setting extension?)');
149 }
150 }
151
152 rule statement_control:sym<if> {
153 $<sym>=[if|with]<.kok> {}
154 <xblock(~$<sym>[0] ~~ /with/ ?? $PBLOCK_REQUIRED_TOPIC !! $PBLOCK_NO_TOPIC)>
155 [
156 [
157 | 'else'\h*'if' <.typed_panic: 'X::Syntax::Malformed::Elsif'>
158 | 'elif' { $/.typed_panic('X::Syntax::Malformed::Elsif', what => "elif") }
159 | $<sym>='elsif' <xblock>
160 | $<sym>='orwith' <xblock($PBLOCK_REQUIRED_TOPIC)>
161 ]
162 ]*
163 {}
164 [
165 'else'
166 <else=.pblock(~$<sym>[-1] ~~ /with/ ?? $PBLOCK_REQUIRED_TOPIC !! $PBLOCK_NO_TOPIC)>
167 ]?
168 }
169
170 token special_variable:sym<$\\> {
171 '$\\' <?before \s | ',' | '=' | <.terminator> >
172 <.obsvar('$\\')>
173 }
174
175 token type_declarator:sym<enum> {
176 :my %*MYSTERY;
177 [ <?[<(«]> <term> <.ws> || <.panic: 'An enum must supply an expression using <>, «», or ()'> ]
178 <.explain_mystery> <.cry_sorrows>
179 <?{
180 elsif !($text ~~ /^(\w|\:)+$/) {
181 $/.obs($bad, "$sigil\($text) for hard ref or $sigil\::($text) for symbolic ref");
182 }
183 }>
184 }
185
186 token rad_number {
187 :my $rad_digits := token rad_digits { <rad_digit>+ [ _ <rad_digit>+ ]* };
188 <O(|%methodcall)>
189 <O=.revO($<infixish>)>
190 <code=[A..Z]>
191 }
192
193 token pod_balanced_braces {
194 <?{ nqp::chars($<start>) == $*POD_ANGLE_COUNT || $*POD_ANGLE_COUNT < 0 }>
195 }
196
197 token routine_declarator:sym<macro> {
198 :my $*LINE_NO := HLL::Compiler.lineof(self.orig(), self.from(), :cache(1));
199 <!!{ nqp::rebless($/, self.slang_grammar('MAIN')); 1 }>
200 <sym> <.end_keyword> <macro_def()>
201 }
202
203 token routine_declarator:sym<macro> {
204 :my $*LINE_NO := HLL::Compiler.lineof(self.orig(), self.from(), :cache(1));
205 <sym> <.end_keyword> <macro_def()>
206 }
207
208 token integer {
209 <!!before ['.' <?before \s | ',' | '=' | ':' <!before <coloncircumfix <OPER=prefix> > > | <.terminator> | $ > <.typed_sorry: 'X::Syntax::Number::IllegalDecimal'>]? >
210 [ <?before '_' '_'+\d> <.sorry: "Only isolated underscores are allowed inside numbers"> ]?
211 }
212}
213
214say $match<block><pair>[0]<value>;
215# OUTPUT: 「value1」
216
217say $match<section>[0]<block><pair>[0]<value>;
218# OUTPUT: 「value3」
219
220# Promise
221my $promise = start {
222 my $i = 0;
223 for 1 .. 10 {
224 $i += $_
225 }
226 $i
227}
228my $result = await $promise;
229say $result;
230# OUTPUT: 55
231
232# Supply
233my $bread-supplier = Supplier.new;
234my $vegetable-supplier = Supplier.new;
235
236my $supply = supply {
237 whenever $bread-supplier.Supply {
238 emit("We've got bread: " ~ $_);
239 };
240 whenever $vegetable-supplier.Supply {
241 emit("We've got a vegetable: " ~ $_);
242 };
243}
244$supply.tap(-> $v { say "$v" });
245
246$vegetable-supplier.emit("Radish");
247# OUTPUT: «We've got a vegetable: Radish»
248$bread-supplier.emit("Thick sliced");
249# OUTPUT: «We've got bread: Thick sliced»
250$vegetable-supplier.emit("Lettuce");
251# OUTPUT: «We've got a vegetable: Lettuce»
252
253say (1, 2, 3) »+« (4, 5, 6);
254say (1, 2, 3, 4) »~» <a b>;
255say (&sin, &cos, &sqrt)».(0.5);
256say (<a b>, <c d e>)».&{ .elems };
257say << '>>' >>;
258say "stuff here!!!".subst(:g, /<</, '|');
259my @array = <<an 'array of' {"many"} items>>;
260
261say [1,2,3] (&) [2,5,7];
262say [1,2,3] ∩ [2,5,7];
263say 2 =:= 3;
264say [1,2,3] Z [4,5,6];
265[1,2,3] RZ[=>] [4,5,6];
266
267rx/:i
268 \w+ # some comment
269 'text'
270 \d ** 2..5
271 "double quotes!"
272 <alnum>
273 <:N + alpha>+
274 <{1 + 2}>
275 <:madeup>
276 '[' \w+ ']' || \S+ \s* '=' \s* \S*
277 || '[' \w+ ']'
278 || \S+ \s* '=' \s* \S*
279 <:Script('Latin')>
280 <:Block<Basic Latin>>
281 if | if <.ws> else $0
282 <:Lu+:N>
283 < f fo foo food >
284 <:L + :!N>
285 <[ a .. c 1 2 3 ]>*
286 @variable
287 <[\d] - [13579]>
288 <?before a $0> && .
289 <:Zs + [\x9] - [\xA0]>
290 ^ raku $
291 ^ raku$
292 [^raku$|something$]else$
293 <[ \x[00C0] .. \x[00C6] ]>*
294 two<|w>\-<|w>words
295 <[\c[GREEK SMALL LETTER ALPHA]..\c[GREEK SMALL LETTER GAMMA]]>*
296 two<!|w><!|w>words
297 << br >>
298 own »
299 ^^ <?alnum> \d+
300 ^^ \d+ <!:Script<Tamil>>
301 abc <?[ d..f ]>
302 abc <?@some-array>
303 <?after ^^ | "." \s+> <:Lu>\S+
304 [ ab || cbc ]
305 (a || b)+
306 [a||b] (c)
307 (\d) ($0)
308 (\d) {} :my $c = $0; ($c)
309 :my $counter = 0; ( \V* { ++$counter } ) *%% \n
310 (a) b {} :my $c2 = $/;
311 test
312 (a) {say "Check so far ", ~$/} b :my $c3 = ~$0;
313 { say 'hi' }
314
315 $<myname> = [ \w+ ]
316
317 <!!{ $*LANG := $*LEAF := $/.clone_braid_from(self); 1 }>
318
319 <?before <.[\)\]\}]>>
320
321 \/
322
323 $<string>=( [ $<part>=[abc] ]* % '-' )
324 $<variable>=\w+ '=' $<value>=\w+
325 a <( b )> c
326 <(a <( b )> c)>
327 '(' ~ ')' <expression>
328 $<OPEN> = '(' <SETGOAL: ')'> <some-expression> [ $GOAL || <FAILGOAL> ]
329 <?> ~ ')' \d+
330 '(' <-[()]>* ')' || '('[ <-[()]>* <~~> <-[()]>* ]* ')'
331 @<named-regex>=<.ident>
332 <.named-regex>
333 <&named-regex>
334 <someregex('a')>
335 <capture-name=named-regex>
336 <capture-name=named-regex(2)>
337 <$test>
338 <@test>
339 $(1 + 2; $test)
340 $pattern3.flip # Nil
341 "$pattern3.flip()"
342 $([~] $pattern3.comb.reverse)
343 @(%h.keys)
344 \d ** 1..3 <?{ $/.Int <= 255 && $/.Int >= 0 }>
345 $/;
346
347$<variable><key>;
348
349constant \something:some<adverb> = 'something';
350
351my %hash = %(
352 query => something,
353 qq => 'something',
354 :23year,
355 m => $test,
356 :yes,
357 :!yes,
358 (Less) => $test,
359);
360
361my %hash2 = %(
362 :query(something),
363 :qq<something>,
364 :m(something),
365 :23('a'),
366 :test<something>,
367 :query1{1 + 2}
368 :list[1,2,3]
369);
370
371=for comment
372some comment
373
374=table
375 |col|
376 |row|
377
378say Q[「some $text」];
379say qq「「some $regex text」」;
380say qww「「some $variable 'some text' text」」;
381say q:ww「「some $variable 'some text' text」」;
382say q:w「「some $variable 'some text' text」」;
383say qq:w「「some $regex 'some text' text」」;
384say Q:c「「some $regex 'some text' { 2 + 1 } text」」;
385say q:a「「some @array[2] 'some text' { 2 + 1 } text」」;
386say Q:a:c「「some @array 'some text' { 2 + 1 } text」」;
387Q[some \qq[$variable.method()] testing]
388Q:a:c[some \qq[$variable.method()] testing]
389say Q:c:h「Testing {'toasting'} %h<one>」;
390say Q:c:h「Testing {'toasting'} %h<one><two>」;
391say Q:c:h「Testing {'toasting'} %h<<one>>」;
392say Q:c:h「Testing {'toasting'} %h«one»」;
393say Q:b[Testing];
394'some \qq[$variable.method()] testing';
395'somes\' testing';
396"some \qq[$variable.method()] testing";
397"some $variable.method() testing";
398"some $variable:some<adverb>.method() testing";
399"some $variable:some('adverb').method() testing";
400"some func() testing";
401"some func:some<adverb>() testing";
402"some &func() testing";
403"some &func($test) testing";
404"some &func:some<adverb>() testing";
405say "Something foo(2) $a.succ(2+3, $some_variable) $a.some-method() @more $_.Str(2) $_: { $_ * 2 }";
406
407#`[[
408multiline comment]
409]]
410
411my $regex = /'match' \s* <[:-]> \s* \w 'something'/;
412
413say S/some/a/ given $text;
414say s/some/a/;
415say S%some%a% given $text;
416say $text ~~ s/(some) \d+ $<var>=<alnum>/a $0/;
417say $text ~~ s:Pos(2)/(some)/$0/;
418say $text ~~ s%some%a%;
419say $text ~~ s%s(.)me%a$0%;
420say $text ~~ s:r/some/a/;
421say $text ~~ m/^text [:i \d+]$/;
422say $text ~~ m:s/^<{2+5}>$/;
423say $text ~~ m%^text$%;
424say $text ~~ /^text$/;
425say $text ~~ tr/abcde/12345/;
426s{\w+} = 'test';
427
428say 1+1i;
429
430CATCH {
431 when X::AdHoc {}
432 when CX::Warn {}
433}
434
435say Q:heredoc「FINISH」;
436some long
437text
438here.
439FINISH
440
441$*IN.lines.first: { .say and .so with %a{.Int cmp n}}
442
443#| Grammar C<G>
444grammar G {
445 rule TOP { <function-define> }
446 rule function-define {
447 :my \var = 'something';
448 'sub' <identifier>
449 {
450 say "func " ~ $<identifier>.made;
451 make $<identifier>.made;
452 }
453 '(' <parameter> ')' '{' '}'
454 { say "end " ~ $/.made; }
455 }
456 token identifier { \w+ { make ~$/; } }
457 token parameter { \w+ { say "param " ~ $/; } }
458 token token { \w+ { say "param " ~ $/; } }
459}
460
461use Some::Module;
462use Some::Module:auth<author>:ver(v1.0.0);
463
464notes $trip: "Almost there";
465
466LABEL:
467for <a b c> {
468}
469
470#|[[
471multiline pod declaration]
472]]
473grammar Calculator {
474 token TOP { <calc-op> }
475
476 proto rule calc-op {*}
477 proto rule calc-op($a) {*}
478 rule calc-op:sym<add> { <num> '+' <num> }
479 rule calc-op:sym<add> { <num> ':' '+' <num> }
480 rule calc-op:sym<sub>($a) { <num> '-' <num> }
481
482 token num { \d+ }
483}
484
485class Calculations {
486 method TOP ($/) { make $<calc-op>.made; }
487 method calc-op:sym<add> ($/) { make [+] $<num>; }
488 method calc-op:sym<sub> ($/) { make [-] $<num>; }
489 method calc-op:sym<sub>($/) { make [-] $<num>; }
490 method calc-op:sym($/) { make [-] $<num>; }
491 method calc-op:sym<sub> { make [-] $<num>; }
492}
493
494say Calculator.parse('2 + 3', actions => Calculations).made;
495
496put Date.today.later(:2years).year;
497
498=finish
499C<say> Date.today.year;
500# Output: 2020
501B<say> Date.today.later(:2years).year;
View as plain text