forked from Raku/roast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.t
77 lines (60 loc) · 2.99 KB
/
inheritance.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
76
77
use v6;
use Test;
plan 31;
# L<S05/Grammars/"Like classes, grammars can inherit">
# tests namespace, inheritance and override
grammar Grammar::Foo {
token TOP { <foo> };
token foo { 'foo' };
};
#?niecza skip 'Cannot dispatch to a method on Foo because it is not inherited or done by Cursor'
is(~('foo' ~~ /^<Grammar::Foo::foo>$/), 'foo', 'got right match (foo)');
ok Grammar::Foo.parse('foo'), 'got the right match through .parse TOP';
ok Grammar::Foo.parse('foo', :rule<foo>), 'got the right match through .parse foo';
grammar Grammar::Bar is Grammar::Foo {
token TOP { <any> };
token bar { 'bar' };
token any { <foo> | <bar> };
};
isa_ok Grammar::Foo, Grammar, 'grammar isa Grammar';
isa_ok Grammar::Bar, Grammar, 'inherited grammar still isa Grammar';
isa_ok Grammar::Bar, Grammar::Foo, 'child isa parent';
#?niecza 4 skip 'Cannot dispatch to a method on Bar because it is not inherited or done by Cursor'
is(~('bar' ~~ /^<Grammar::Bar::bar>$/), 'bar', 'got right match (bar)');
is(~('foo' ~~ /^<Grammar::Bar::foo>$/), 'foo', 'got right match (foo)');
is(~('foo' ~~ /^<Grammar::Bar::any>$/), 'foo', 'got right match (any)');
is(~('bar' ~~ /^<Grammar::Bar::any>$/), 'bar', 'got right match (any)');
ok Grammar::Bar.parse('foo'), 'can parse foo through .parsed and inhertied subrule';
ok Grammar::Bar.parse('bar', :rule<bar>), 'got right match (bar)';
ok Grammar::Bar.parse('foo', :rule<foo>), 'got right match (foo)';
ok Grammar::Bar.parse('bar', :rule<any>), 'got right match (any)';
ok Grammar::Bar.parse('foo', :rule<any>), 'got right match (any)';
nok Grammar::Bar.parse('boo', :rule<any>), 'No match for bad input (any)';
grammar Grammar::Baz is Grammar::Bar {
token baz { 'baz' };
token any { <foo> | <bar> | <baz> };
};
#?niecza 6 skip 'Cannot dispatch to a method on Baz because it is not inherited or done by Cursor'
is(~('baz' ~~ /^<Grammar::Baz::baz>$/), 'baz', 'got right match');
is(~('foo' ~~ /^<Grammar::Baz::foo>$/), 'foo', 'got right match');
is(~('bar' ~~ /^<Grammar::Baz::bar>$/), 'bar', 'got right match');
is(~('foo' ~~ /^<Grammar::Baz::any>$/), 'foo', 'got right match');
is(~('bar' ~~ /^<Grammar::Baz::any>$/), 'bar', 'got right match');
is(~('baz' ~~ /^<Grammar::Baz::any>$/), 'baz', 'got right match');
ok Grammar::Baz.parse('baz', :rule<baz>), 'got right match (baz)';
ok Grammar::Baz.parse('foo', :rule<foo>), 'got right match (foo)';
ok Grammar::Baz.parse('bar', :rule<bar>), 'got right match (bar)';
ok Grammar::Baz.parse('baz', :rule<any>), 'got right match (any)';
ok Grammar::Baz.parse('foo', :rule<any>), 'got right match (any)';
ok Grammar::Baz.parse('bar', :rule<any>), 'got right match (any)';
nok Grammar::Baz.parse('boo', :rule<any>), 'No match for bad input (any)';
{
class A { };
grammar B is A { };
#?rakudo todo 'automatic Grammar superclass'
#?niecza todo 'automatic Grammar superclass'
isa_ok B, Grammar, 'A grammar isa Grammar, even if inherting from a class';
}
is(Grammar.WHAT.gist,"(Grammar)", "Grammar.WHAT.gist = Grammar()");
done;
# vim: ft=perl6