/
Grammar.pm6
230 lines (190 loc) · 4.57 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
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
=begin pod
=head1 ANTLR4::Grammar
C<ANTLR4::Grammar> generates a perl6 representation of an ANTLR4 AST.
=head1 Synopsis
use ANTLR4::Grammar;
my $ag = ANTLR4::Grammar.new;
say $ag.to-string('grammar Minimal { identifier : [A-Z][A-Za-z]+ ; }');
say $ag.file-to-string('ECMAScript.g4');
=head1 Documentation
In its simplest form, just use the .to-string method on an existing grammar
text to get back its closest Perl 6 representation.
=head1 Extension
Suppose you don't like how the module formats the ANTLR grammar. Subclass this
module and override the C<to-lines> methods I've provided, or go all the way
back to the top level and replace the C<to-lines( Grammar $g )> with your own
inheritance hierarchy.
Maybe you want to add a way to create a bare-bones action class to go along
with your resulting grammar - override the C<to-string> method, you've got the
C<$grammar> value that you can walk through, and do your own thing.
=end pod
use v6;
use JSON::Tiny;
use ANTLR4::Grammar::Parser;
use ANTLR4::Actions::Perl6;
my role Indenting {
method indent-line( $line ) {
if $line {
return "\t" ~ $line
}
return ''
}
method indent( *@lines ) {
map { self.indent-line( $_ ) }, grep { /\S/ }, @lines
}
}
my role Formatting {
also does Indenting;
sub greed-to-string( $a ) {
$a.greed ?? '?' !! ''
}
sub modifier-to-string( $a ) {
( $a.modifier // '' ) ~ greed-to-string( $a )
}
multi method to-lines( Any $a ) {
die "Unknown type, this should not get triggred"
}
multi method to-lines( Action $a ) {
return (
q{#|} ~
$a.name ~
greed-to-string( $a )
)
}
multi method to-lines( Token $t ) {
my $lc-name = lc( $t.name );
return (
"token {$t.name} \{",
self.indent-line(
'||' ~ self.indent-line( "'$lc-name'" )
),
"\}"
)
}
multi method to-lines( Terminal $t ) {
return (
q{'} ~ $t.name ~ q{'} ~
modifier-to-string( $t )
)
}
multi method to-lines( Wildcard $w ) {
return (
"." ~
modifier-to-string( $w )
)
}
multi method to-lines( Grouping $g ) {
my @child;
for $g.child {
@child.append( self.to-lines( $_ ) );
}
return (
"\(" ~ self.indent-line( @child.shift ),
self.indent( @child ),
"\)" ~
modifier-to-string( $g )
).flat
}
multi method to-lines( EOF $e ) {
return (
'$' ~
modifier-to-string( $e )
)
}
multi method to-lines( Nonterminal $n ) {
return (
q{<} ~
( $n.negated ?? '!' !! '' ) ~
( $n.alias ?? ( $n.alias ~ '=' ) !! '' ) ~
$n.name ~
q{>} ~
modifier-to-string( $n )
)
}
multi method to-lines( CharacterRange $r ) {
return (
"{$r.from} .. {$r.to}"
)
}
multi method to-lines( Character $c ) {
return $c.name
}
multi method to-lines( CharacterSet $c ) {
my $negated = $c.negated ?? '-' !! '';
my @child;
for $c.child {
@child.append( self.to-lines( $_ ) )
}
return (
"<{$negated}[ {@child} ]>" ~
modifier-to-string( $c )
)
}
multi method to-lines( Concatenation $c ) {
my @child;
for $c.child {
@child.append( self.to-lines( $_ ) )
}
@child
}
multi method to-lines( Alternation $a ) {
my @child;
for $a.child {
# XXX These should always be objects...
next unless $_;
my @lines = self.indent( self.to-lines( $_ ) );
if @lines {
@lines[0] = '||' ~ @lines[0];
@child.append( @lines );
}
}
@child
}
multi method to-lines( Rule $r ) {
my @child;
for $r.child {
@child.append( self.to-lines( $_ ) );
}
return (
"token {$r.name} \{",
self.indent( @child ),
"\}"
).flat
}
multi method to-lines( Grammar $g ) {
my @token;
my @rule;
my $json-str;
my %json;
@token.append( self.to-lines( $_ ) ) for $g.token;
@rule.append( self.to-lines( $_ ) ) for $g.rule;
%json<type> = $g.type if $g.type;
%json<option> = $g.option if keys $g.option;
%json<import> = $g.import if keys $g.import;
%json<action> = $g.action if keys $g.action;
$json-str = q{#|} ~ to-json( %json ) if keys %json;
return (
$json-str // (),
"grammar {$g.name} \{",
self.indent( @token ),
self.indent( @rule ),
"\}"
).flat
}
}
class ANTLR4::Grammar:ver<0.6.2> {
also does Formatting;
method to-string( Str $string ) {
my $p = ANTLR4::Grammar::Parser.new;
my $a = ANTLR4::Actions::Perl6.new;
my $ast = $p.parse( $string, :actions( $a ) ).ast;
return self.to-lines( $ast ).join( "\n" ) ~ "\n";
}
method file-to-string( Str $filename ) {
my $p = ANTLR4::Grammar::Parser.new;
my $a = ANTLR4::Actions::Perl6.new;
my $ast = $p.parsefile( $filename, :actions( $a ) ).ast;
return self.to-lines( $ast ).join( "\n" ) ~ "\n";
}
}
# vim: ft=perl6