line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Parse::Keyword; |
2
|
24
|
|
|
24
|
|
1180986
|
use strict; |
|
24
|
|
|
|
|
34
|
|
|
24
|
|
|
|
|
529
|
|
3
|
24
|
|
|
24
|
|
84
|
use warnings; |
|
24
|
|
|
|
|
36
|
|
|
24
|
|
|
|
|
428
|
|
4
|
24
|
|
|
24
|
|
341
|
use 5.014; |
|
24
|
|
|
|
|
52
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: DEPRECATED: write syntax extensions in perl |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
8
|
|
|
|
|
|
|
|
9
|
24
|
|
|
24
|
|
9069
|
use Devel::CallParser; |
|
24
|
|
|
|
|
47336
|
|
|
24
|
|
|
|
|
962
|
|
10
|
24
|
|
|
24
|
|
110
|
use XSLoader; |
|
24
|
|
|
|
|
25
|
|
|
24
|
|
|
|
|
2442
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
XSLoader::load( |
13
|
|
|
|
|
|
|
__PACKAGE__, |
14
|
|
|
|
|
|
|
exists $Parse::Keyword::{VERSION} ? ${ $Parse::Keyword::{VERSION} } : (), |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub import { |
20
|
25
|
|
|
25
|
|
220
|
my $package = shift; |
21
|
25
|
|
|
|
|
34
|
my ($keywords) = @_; |
22
|
|
|
|
|
|
|
|
23
|
25
|
|
|
|
|
179
|
my $caller = caller; |
24
|
|
|
|
|
|
|
|
25
|
25
|
|
|
|
|
60
|
for my $keyword (keys %$keywords) { |
26
|
27
|
|
|
|
|
28
|
my $sub = do { |
27
|
24
|
|
|
24
|
|
81
|
no strict 'refs'; |
|
24
|
|
|
|
|
27
|
|
|
24
|
|
|
|
|
1857
|
|
28
|
27
|
|
|
|
|
22
|
\&{ $caller . '::' . $keyword }; |
|
27
|
|
|
|
|
103
|
|
29
|
|
|
|
|
|
|
}; |
30
|
27
|
|
|
|
|
122
|
install_keyword_handler($sub, $keywords->{$keyword}); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
25
|
|
|
|
|
72
|
my @helpers = qw( |
34
|
|
|
|
|
|
|
lex_peek |
35
|
|
|
|
|
|
|
lex_read |
36
|
|
|
|
|
|
|
lex_read_space |
37
|
|
|
|
|
|
|
lex_stuff |
38
|
|
|
|
|
|
|
parse_block |
39
|
|
|
|
|
|
|
parse_stmtseq |
40
|
|
|
|
|
|
|
parse_fullstmt |
41
|
|
|
|
|
|
|
parse_barestmt |
42
|
|
|
|
|
|
|
parse_fullexpr |
43
|
|
|
|
|
|
|
parse_listexpr |
44
|
|
|
|
|
|
|
parse_termexpr |
45
|
|
|
|
|
|
|
parse_arithexpr |
46
|
|
|
|
|
|
|
compiling_package |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
25
|
|
|
|
|
33
|
for my $helper (@helpers) { |
50
|
24
|
|
|
24
|
|
83
|
no strict 'refs'; |
|
24
|
|
|
|
|
26
|
|
|
24
|
|
|
|
|
1852
|
|
51
|
325
|
|
|
|
|
197
|
*{ $caller . '::' . $helper } = \&{ __PACKAGE__ . '::' . $helper }; |
|
325
|
|
|
|
|
2048
|
|
|
325
|
|
|
|
|
390
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
__END__ |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=pod |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=encoding UTF-8 |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Parse::Keyword - DEPRECATED: write syntax extensions in perl |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 VERSION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
version 0.09 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use Parse::Keyword { try => \&try_parser }; |
75
|
|
|
|
|
|
|
use Exporter 'import'; |
76
|
|
|
|
|
|
|
our @EXPORT = 'try'; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub try { |
79
|
|
|
|
|
|
|
my ($try, $catch) = @_; |
80
|
|
|
|
|
|
|
&Try::Tiny::try($try, ($catch ? (&Try::Tiny::catch($catch)) : ())); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub try_parser { |
84
|
|
|
|
|
|
|
lex_read_space; |
85
|
|
|
|
|
|
|
die "syntax error" unless lex_peek eq '{'; |
86
|
|
|
|
|
|
|
my $try = parse_block; |
87
|
|
|
|
|
|
|
lex_read_space; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my $catch; |
90
|
|
|
|
|
|
|
if (lex_peek(6) =~ /^catch\b/) { |
91
|
|
|
|
|
|
|
lex_read(5); |
92
|
|
|
|
|
|
|
lex_read_space; |
93
|
|
|
|
|
|
|
die "syntax error" unless lex_peek eq '{'; |
94
|
|
|
|
|
|
|
$catch = parse_block; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
return (sub { ($try, $catch) }, 1); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 DESCRIPTION |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 DO NOT USE! |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This module has fundamental errors in the way it handles closures, which are |
105
|
|
|
|
|
|
|
not fixable. Runtime keywords will never be able to work properly with the |
106
|
|
|
|
|
|
|
current design of this module. There are certain cases where this module is |
107
|
|
|
|
|
|
|
still safe to use (keywords that only have effect at compile time, or keywords |
108
|
|
|
|
|
|
|
that never call any of the C<parse_*> functions), but that is limiting enough |
109
|
|
|
|
|
|
|
to make this module mostly worthless, and I likely won't be continuing to |
110
|
|
|
|
|
|
|
maintain it. Be warned! |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
B<< NOTE: The API of this module is still in flux. I may make |
113
|
|
|
|
|
|
|
backwards-incompatible changes as I figure out how it should look. >> |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This module allows you to write keyword-based syntax extensions without |
116
|
|
|
|
|
|
|
requiring you to write any C code yourself. It is similar to L<Devel::Declare>, |
117
|
|
|
|
|
|
|
except that it uses the Perl parser API introduced in Perl 5.14 in order to |
118
|
|
|
|
|
|
|
allow you to parse parts of things using perl's own parser, rather than having |
119
|
|
|
|
|
|
|
to fake it with balanced brace matching or other fragile things. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
To use this module, you should pass a hashref to the C<use> statement. The keys |
122
|
|
|
|
|
|
|
of this hashref are subroutines in the current package which should have |
123
|
|
|
|
|
|
|
special parsing behavior attached to them, and the values are coderefs which |
124
|
|
|
|
|
|
|
should be used to implement the custom parsing behavior. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
The parsing coderefs will be called when perl encounters a call to the keyword |
127
|
|
|
|
|
|
|
that you attached custom parsing to. The current parser state will be directly |
128
|
|
|
|
|
|
|
after parsing the keyword. The parser function will receive the name of the |
129
|
|
|
|
|
|
|
keyword as a parameter, and should return a coderef which, when called at |
130
|
|
|
|
|
|
|
runtime, will produce the arguments to the function. In addition, if your |
131
|
|
|
|
|
|
|
keyword should be parsed as a statement (for instance, if you don't want to |
132
|
|
|
|
|
|
|
require a trailing semicolon), you can return a second, true value. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
In order to actually handle the parsing itself, this module also exports |
135
|
|
|
|
|
|
|
various parsing functions, which you can call. See below for details. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 FUNCTIONS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 lex_peek($n) |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Returns a string consisting of the next C<$n> characters in the input (or next |
142
|
|
|
|
|
|
|
one character, if C<$n> isn't given). This string may be shorter than C<$n> |
143
|
|
|
|
|
|
|
characters if there are fewer than C<$n> characters remaining to read. The |
144
|
|
|
|
|
|
|
current position in the buffer to be parsed is not moved. See L<< |
145
|
|
|
|
|
|
|
perlapi/PL_parser->linestr >> and L<perlapi/lex_next_chunk> for more |
146
|
|
|
|
|
|
|
information. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
NOTE: This function currently only returns text that is on the current line, |
149
|
|
|
|
|
|
|
unless the current line has been fully read (via C<lex_read>). This is due to a |
150
|
|
|
|
|
|
|
bug in perl itself, and this restriction will hopefully be lifted in a future |
151
|
|
|
|
|
|
|
version of this module, so don't depend on it. See the L</BUGS> section for |
152
|
|
|
|
|
|
|
more information. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 lex_read($n) |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Moves the current position in the parsing buffer forward by C<$n> characters |
157
|
|
|
|
|
|
|
(or one character, if C<$n> isn't given). See L<perlapi/lex_read_to> for more |
158
|
|
|
|
|
|
|
details. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 lex_read_space |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Moves the current position in the parsing buffer forward past any whitespace or |
163
|
|
|
|
|
|
|
comments. See L<perlapi/lex_read_space> for more details. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 lex_stuff($str) |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Inserts C<$str> into the current parsing buffer at the current location, so |
168
|
|
|
|
|
|
|
that future calls to C<lex_peek> and such will see it. Note that this does not |
169
|
|
|
|
|
|
|
move the current position in the parsing buffer, so multiple calls to |
170
|
|
|
|
|
|
|
C<lex_stuff> at the same location will end up inserted into the buffer in |
171
|
|
|
|
|
|
|
reverse order. See L<perlapi/lex_stuff_sv> for more information. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 parse_block, parse_stmtseq, parse_fullstmt, parse_barestmt, |
174
|
|
|
|
|
|
|
parse_fullexpr, parse_listexpr, parse_termexpr, parse_arithexpr |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
These functions parse the specified amount of Perl code, and return a coderef |
177
|
|
|
|
|
|
|
which will evaluate that code when executed. They each take an optional boolean |
178
|
|
|
|
|
|
|
parameter that should be true if you are creating a subroutine which will be |
179
|
|
|
|
|
|
|
going in the symbol table, or in other more obscure situations involving |
180
|
|
|
|
|
|
|
closures (the CVf_ANON flag will be set on the created coderef if this is not |
181
|
|
|
|
|
|
|
passed - see C<t/unavailable.t> in this distribution). See |
182
|
|
|
|
|
|
|
L<perlapi/parse_block>, L<perlapi/parse_stmtseq>, L<perlapi/parse_fullstmt>, |
183
|
|
|
|
|
|
|
L<perlapi/parse_barestmt>, L<perlapi/parse_fullexpr>, L<parse_listexpr>, |
184
|
|
|
|
|
|
|
L<parse_termexpr>, and L<perlapi/parse_arithexpr> for more details. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 compiling_package |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Returns the name of the package that the keyword which is currently being |
189
|
|
|
|
|
|
|
parsed was called in. This should be used instead of C<caller> if you want to |
190
|
|
|
|
|
|
|
do something like install a subroutine in the calling package. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 BUGS |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Peeking into the next line is currently (as of 5.19.2) broken in perl if the |
195
|
|
|
|
|
|
|
current line hasn't been fully consumed. This module works around this by just |
196
|
|
|
|
|
|
|
not doing that. This shouldn't be an issue for the most part, since it will |
197
|
|
|
|
|
|
|
only come up if you need to conditionally parse something based on a token that |
198
|
|
|
|
|
|
|
can span multiple lines. Just keep in mind that if you're reading in a large |
199
|
|
|
|
|
|
|
chunk of text, you'll need to alternate between calling C<lex_peek> and |
200
|
|
|
|
|
|
|
C<lex_read>, or else you'll only be able to see text on the current line. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
This module also inherits the limitation from L<Devel::CallParser> that custom |
203
|
|
|
|
|
|
|
parsing is only triggered if the keyword is called by its unqualified name |
204
|
|
|
|
|
|
|
(C<try>, not C<Try::try>, for instance). |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
This module doesn't yet work with lexical subs, such as via |
207
|
|
|
|
|
|
|
L<Exporter::Lexical>. This will hopefully be fixed in the future, but will |
208
|
|
|
|
|
|
|
likely require modifications to perl. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 SEE ALSO |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
L<Devel::CallParser> |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
L<Keyword::API> |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
L<Devel::Declare> |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 SUPPORT |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
You can find this documentation for this module with the perldoc command. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
perldoc Parse::Keyword |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
You can also look for information at: |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=over 4 |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item * MetaCPAN |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
L<https://metacpan.org/release/Parse-Keyword> |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Parse-Keyword> |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item * Github |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
L<https://github.com/haarg/Parse-Keyword> |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=back |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=for Pod::Coverage install_keyword_handler |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head1 AUTHOR |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Jesse Luehrs <doy@tozt.net> |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
This software is Copyright (c) 2013 by Jesse Luehrs. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
This is free software, licensed under: |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
The MIT (X11) License |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=cut |