-
Notifications
You must be signed in to change notification settings - Fork 0
/
Moritz.pm6
56 lines (50 loc) · 1.32 KB
/
Moritz.pm6
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
grammar S-Expression {
proto token atom {*}
token atom:sym<integer> { <[-+]>?\d+ }
token atom:sym<string> { '"' ( <-["\\]>+ | "\\" . )* '"' }
token atom:sym<identifier> { .+ }
rule TOP { <atom> }
}
class Identifier {
has $.str;
}
class S-Actions {
method atom:sym<identifier>($/) { make Identifier.new(str => ~$/) }
}
use Test;
my %atoms =
integer => ('1', '01234', '-23', '+12'),
identifier => ('abc', '=', '*_*'),
string => ('""', '"abc"', Q'"abc\"def"', Q'"\\"'),
;
my %not-atoms =
identifier => ('', '_'),
string => ('', '"""', Q'"\"',),
;
for %atoms.keys.sort -> $atom {
for %atoms{$atom}.list -> $test {
ok S-Expression.parse($test,
rule => "atom:sym<$atom>"),
"Parsing '$test' as atom $atom";
}
}
for %not-atoms.keys.sort -> $atom {
for %not-atoms{$atom}.list -> $test {
nok try {S-Expression.parse($test,
rule => "atom:sym<$atom>") },
"Not parsing '$test' as atom $atom";
}
}
my @tests = '()', '(abc)', ' (abc) ', '( abc )',
'(1)', '(+1)', '(-1)', '( () ( ) )';
for @tests -> $t {
ok S-Expression.parse($t), "can parse '$t'";
}
my $m = S-Expression.parse(
Q'((a "b") 23 "ab \\cd")',
actions => S-Actions.new,
);
ok $m, 'Can parse S-Expression with action method';
is-deeply $m.made,
[[[Identifier.new(str => "a"), "b"], 23, "ab \\cd"],],
"correct data extracted";