/
my.t
75 lines (63 loc) 路 2.04 KB
/
my.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use v6;
use Test;
plan 24;
# L<S05/Modifiers/Any grammar regex is really just a kind of method>
grammar DeclaratorTest1 {
token TOP {
|| a <a>
|| b <b>
|| c <c>
|| d <d>
}
token a {
:constant $x = 'foo';
$x
}
token b {
:my $y = ' yack';
$y $y
}
token c {
:state $z++;
$z
}
token d {
:our $our = 'zho';
$our
}
}
ok DeclaratorTest1.parse( 'afoo' ), 'can declare :constant in regex';
is ~$/, 'afoo', '... and it matched the constant';
ok !DeclaratorTest1.parse( 'abar' ), 'does not work with wrong text';
ok DeclaratorTest1.parse( 'b yack yack' ), 'can declare :my in regex';
is ~$/, 'b yack yack', 'correct match with "my" variable';
ok !DeclaratorTest1.parse('b yack shaving'), 'does not work with wrong text';
ok DeclaratorTest1.parse('c1'), ':state in regex (match) (1)';
is ~$/, 'c1', ':state in regex ($/) (1)';
ok DeclaratorTest1.parse('c2'), ':state in regex (match) (2)';
is ~$/, 'c2', ':state in regex ($/) (2)';
ok !DeclaratorTest1.parse('c3'), ':state in regex (no match)';
ok DeclaratorTest1.parse('dzho'), ':our in regex (match)';
is ~$/, 'dzho', ':our in regex ($/)';
is $DeclaratorTest1::our, 'zho', 'can access our variable from the outside';
{
my $a = 1;
regex ta { :temp $a = 5; <ma> };
regex ma { $a $a };
ok '11' ~~ m/ ^ <ma> $ /, "can access variables in regex (not temp'ed)";
ok '55' !~~ m/ ^ <ma> $ /, "(-) not temp'ed";
is $a, 1, "temp'ed variable still 1";
ok '55' ~~ m/ ^ <ta> $ /, "can access temp'ed variable in regex (+)";
ok '11' !~~ m/ ^ <ta> $ /, "(-) temp'ed";
is $a, 1, "temp'ed variable again 1";
}
{
my $a = 1;
regex la { :let $a = 5; <lma> };
regex lma { $a $a };
ok '23' !~~ m/ ^ <la> $ /, 'can detect a non-match with :let';
is $a, 1, 'unsuccessful match did not affect :let variable';
ok '55' ~~ m/ ^ <la> $ /, 'can match changed :let variable';
is $a, 5, 'successful match preserves new :let value';
}
# vim: ft=perl6 sw=4 ts=4 expandtab