/
Grammar.pm
349 lines (291 loc) · 8.28 KB
/
Grammar.pm
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use v6;
grammar Perlito6::Grammar {
use Perlito6::Expression;
use Perlito6::Grammar::Regex;
use Perlito6::Grammar::Control;
token is_newline {
| \c10 \c13?
| \c13 \c10?
}
token not_newline {
<!before \n> .
}
token ident {
<!before \d > <.word>+
}
token full_ident {
<.ident> [ '::' <.ident> ]*
}
token namespace_before_ident {
<.ident> <before '::'> [ '::' <.ident> <before '::'> ]*
}
token optional_namespace_before_ident {
| <namespace_before_ident> '::'
{ make ~$<namespace_before_ident> }
| ''
{ make '' }
}
token pod_begin {
| \n '=end' \N*
| . \N* <.pod_begin>
}
token ws {
[
| '#' \N*
| \n [
| '=begin' <.pod_begin>
| '=for' <.pod_begin> # fixme
| ''
]
| \s
]+
}
token opt_ws { <.ws>? }
token opt_ws2 { <.ws>? }
token opt_ws3 { <.ws>? }
token grammar {
<full_ident> <.ws>?
'{'
<.ws>?
<exp_stmts>
<.ws>?
'}'
{
make CompUnit.new(
name => $$<full_ident>,
body => $$<exp_stmts>,
)
}
}
token declarator {
'my' | 'state' | 'has'
}
token exp_stmts2 { <exp_stmts> { make $$<exp_stmts> } }
token exp {
<Perlito6::Expression.exp_parse>
{ make $$<Perlito6::Expression.exp_parse> }
}
token exp2 {
<Perlito6::Expression.exp_parse>
{ make $$<Perlito6::Expression.exp_parse> }
}
token opt_ident {
| <ident> { make $$<ident> }
| '' { make 'postcircumfix:<( )>' }
}
token opt_type {
| '::'? <full_ident> { make $$<full_ident> }
| '' { make '' }
}
token var_sigil { \$ |\% |\@ |\& }
token var_twigil { [ \. | \! | \^ | \* ]? }
token var_name { <full_ident> | '/' | <digit> }
token var_ident {
<var_sigil> <var_twigil> <optional_namespace_before_ident> <var_name>
{
make Var.new(
sigil => ~$<var_sigil>,
twigil => ~$<var_twigil>,
namespace => $$<optional_namespace_before_ident>,
name => ~$<var_name>,
)
}
}
token exponent {
[ 'e' | 'E' ] [ '+' | '-' | '' ] \d+
}
token val_num {
[ \. \d+ <.exponent>?
| \d+ [ <.exponent> | \. \d+ <.exponent>? ]
]
{ make Val::Num.new( num => ~$/ ) }
}
token char_any {
.
}
token char_any_single_quote {
<!before \' > .
[ <!before [ \' | \\ ] > . ]*
}
token single_quoted_unescape {
| \\ \\ <single_quoted_unescape>
{ make "\\" ~ $<single_quoted_unescape> }
| \\ \' <single_quoted_unescape>
{ make '\'' ~ $<single_quoted_unescape> }
| \\ <single_quoted_unescape>
{ make "\\" ~ $<single_quoted_unescape> }
| <char_any_single_quote> <single_quoted_unescape>
{ make $<char_any_single_quote> ~ $<single_quoted_unescape> }
| ''
}
token char_any_double_quote {
<!before [ \" | \$ | \@ | \% | \{ ] > .
[ <!before [ \" | \$ | \@ | \% | \{ | \\ ] > . ]*
}
token double_quoted_unescape {
| \\
[ c
[ \[ <digits> \]
{ make chr( $<digits> ) }
| <digits>
{ make chr( $<digits> ) }
]
| e
{ make chr(27) }
| n
{ make "\n" }
| t
{ make chr(9) }
| <char_any>
{ make ~$<char_any> }
]
| <char_any_double_quote>
{ make ~$<char_any_double_quote> }
}
token double_quoted_buf {
| <before \$ >
[ <before \$ <.var_twigil> <.ident> > <Perlito6::Expression.operator>
{ make ($$<Perlito6::Expression.operator>)[1] }
| <char_any>
{ make Val::Buf.new( buf => ~$<char_any> ) }
]
| <before \@ >
[ <before \@ <.var_twigil> <.ident> > <Perlito6::Expression.operator> '[]'
{ make ($$<Perlito6::Expression.operator>)[1] }
| <char_any>
{ make Val::Buf.new( buf => ~$<char_any> ) }
]
| <before \% >
[ <before \% <.var_twigil> <.ident> > <Perlito6::Expression.operator> '{}'
{ make ($$<Perlito6::Expression.operator>)[1] }
| <char_any>
{ make Val::Buf.new( buf => ~$<char_any> ) }
]
| \{ <exp_stmts> \}
{ make Do.new( block => Lit::Block.new( stmts => $$<exp_stmts> ) ) }
| <double_quoted_unescape>
{ make Val::Buf.new( buf => $$<double_quoted_unescape> ) }
}
token val_buf {
| \" <double_quoted_buf>* \"
{
my $args = $<double_quoted_buf>;
if !$args {
make Val::Buf.new( buf => '' )
}
else {
make Apply.new(
namespace => '',
code => 'list:<~>',
arguments => ($<double_quoted_buf>)>>.capture,
)
}
}
| \' <single_quoted_unescape> \'
{ make Val::Buf.new( buf => $$<single_quoted_unescape> ) }
}
token digits {
\d+
}
token val_int {
\d+
{ make Val::Int.new( int => ~$/ ) }
}
token exp_stmts {
<Perlito6::Expression.delimited_statement>*
{ make $<Perlito6::Expression.delimited_statement>>>.capture }
}
token opt_name { <ident>? }
token var_invocant {
| <var_ident> \: { make $$<var_ident> }
| { make Var.new(
sigil => '$',
twigil => '',
name => 'self',
)
}
}
token args_sig {
<var_invocant>
<.opt_ws>
# TODO - Perlito6::Expression.list_parse / exp_mapping == positional / named
<Perlito6::Expression.list_parse>
{
# say ' invocant: ', ($$<var_invocant>).perl;
# say ' positional: ', ($$<>).perl;
make Sig.new(
invocant => $$<var_invocant>,
positional => Perlito6::Expression::expand_list(($$<Perlito6::Expression.list_parse>){'exp'}),
named => { } );
}
}
token method_sig {
| <.opt_ws> \( <.opt_ws> <args_sig> <.opt_ws> \)
{ make $$<args_sig> }
| { make Sig.new(
invocant => Var.new(
sigil => '$',
twigil => '',
name => 'self' ),
positional => [ ],
named => { } ) }
}
token method_def {
<opt_name> <.opt_ws>
<method_sig>
<.opt_ws> \{ <.opt_ws>
<exp_stmts>
<.opt_ws>
[ \} | { die 'Syntax Error in method \'.', $$<name>, '\' near pos=', $/.to; } ]
{
# say ' block: ', ($$<exp_stmts>).perl;
make Method.new( name => $$<opt_name>, sig => $$<method_sig>, block => $$<exp_stmts> );
}
}
token sub_def {
<opt_name> <.opt_ws>
<method_sig>
<.opt_ws> \{ <.opt_ws>
<exp_stmts> <.opt_ws>
[ \} | { die 'Syntax Error in sub \'', $$<name>, '\''; } ]
{ make Sub.new( name => $$<opt_name>, sig => $$<method_sig>, block => $$<exp_stmts> ) }
}
token token {
<opt_name> <.opt_ws> \{
<Perlito6::Grammar::Regex.rule>
\}
{
#say 'Token was compiled into: ', ($$<Perlito6::Grammar::Regex.rule>).perl;
my $source = $<opt_name> ~ ' ( $grammar: $str, $pos ) { ' ~
'my $MATCH; $MATCH = Perlito6::Match.new( str => $str, from => $pos, to => $pos, bool => 1 ); ' ~
'$MATCH.bool = ( ' ~
($$<Perlito6::Grammar::Regex.rule>).emit_perl6() ~
'); ' ~
'$MATCH }';
#say 'Intermediate code: ', $source;
my $ast = Perlito6::Grammar.method_def( $source, 0 );
# say 'Intermediate ast: ', $$ast.perl;
make $$ast;
}
}
}
=begin
=head1 NAME
Perlito6::Grammar - Grammar for Perlito
=head1 SYNOPSIS
my $match = $source.parse;
($$match).perl; # generated Perlito AST
=head1 DESCRIPTION
This module generates a syntax tree for the Perlito compiler.
=head1 AUTHORS
Flavio Soibelmann Glock <fglock@gmail.com>.
The Pugs Team E<lt>perl6-compiler@perl.orgE<gt>.
=head1 SEE ALSO
The Perl 6 homepage at L<http://dev.perl.org/perl6>.
The Pugs homepage at L<http://pugscode.org/>.
=head1 COPYRIGHT
Copyright 2006, 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock, Audrey Tang and others.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See L<http://www.perl.com/perl/misc/Artistic.html>
=end