/
Grammar.pm6
69 lines (65 loc) · 2.07 KB
/
Grammar.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
57
58
59
60
61
62
63
64
65
66
67
68
69
my class Grammar is Match {
method parse(\target, :$rule, :$args, Mu :$actions, :$filename) is raw {
my $*LINEPOSCACHE;
nqp::stmts(
(my $grammar := self.new(:orig(target), |%_).set_actions($actions)),
nqp::decont(nqp::getlexcaller('$/') =
nqp::if(
(my $cursor := nqp::if(
$rule,
nqp::if(
$args,
$grammar."$rule"(|$args.Capture),
$grammar."$rule"()
),
nqp::if(
$args,
$grammar.TOP(|$args.Capture),
$grammar.TOP()
),
)),
nqp::stmts(
(my $match := $cursor.MATCH),
nqp::while(
$match && nqp::isne_i(
nqp::getattr_i(($match := $cursor.MATCH),Match,'$!pos'),
target.chars
),
$match := ($cursor := $cursor.'!cursor_next'()).MATCH
),
$match || Nil
),
Nil
)
)
)
}
method subparse(\target, :$rule, :$args, :$actions) is raw {
nqp::stmts(
(my $grammar := self.new(:orig(target), |%_).set_actions($actions)),
nqp::decont(nqp::getlexcaller('$/') =
nqp::if(
$rule,
nqp::if(
$args,
$grammar."$rule"(|$args.Capture).MATCH,
$grammar."$rule"().MATCH,
),
nqp::if(
$args,
$grammar.TOP(|$args.Capture).MATCH,
$grammar.TOP().MATCH
),
)
)
)
}
method parsefile(Str(Cool) $filename, :$enc) is raw {
nqp::decont(nqp::getlexcaller('$/') = nqp::if(
nqp::elems(nqp::getattr(%_,Map,'$!storage')),
self.parse($filename.IO.slurp(:$enc), :$filename, |%_),
self.parse($filename.IO.slurp(:$enc), :$filename)
))
}
}
# vim: ft=perl6 expandtab sw=4