/
Plosurin.pm
228 lines (210 loc) · 6.62 KB
/
Plosurin.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
use v6;
my $VERSION = '0.01';
class Template {
has $.namespace ;
has @.params;
has $.name;
has $.comment;
has $.body;
}
class Param {
has Bool $!requred = False;
has $!name='';
has $!comment='';
}
sub line-and-column(Match $m) {
my $line = ($m.orig.substr(0, $m.from).split("\n")).elems;
# RAKUDO workaround for RT #70003, $m.orig.rindex(...) directly fails
my $column = $m.from - ('' ~ $m.orig).rindex("\n", $m.from);
$line, $column;
}
grammar Plosurin::Template {
token TOP { <content>+ }
token content { $<cnt>=[<struct_if> || <struct_switch> || <struct_foreach> || <raw_text> || <tag> ] }
token plain_content { [<raw_text> || <tag> ]+}
rule raw_text { <-[{ }]>+ }
rule tag {'{' ~ '}' [ <command_import> || <command_print> ] }
rule command_print { 'print' <variable>}
rule struct_if { '{' ~ '}' <command_if>
<plain_content>
[ '{' ~ '}' <command_elseif>
<plain_content>
]*
[ '{' ~ '}' <command_else>
<plain_content>
]?
'{' ~ '}' <command_endif>
}
rule command_if {'if' <expression>}
rule command_else {'else' }
rule command_elseif {'elseif' <expression>}
rule command_endif {'/if'}
rule struct_switch { '{' ~ '}' <command_switch>
[ '{' ~ '}' <command_case>
<plain_content> ]+
[ '{' ~ '}' <command_default>
<plain_content> ]?
'{' ~ '}' <command_endswitch>
}
rule command_switch {'switch' <expression> }
rule command_endswitch {'/switch'}
rule command_case {'case' <expression_list>}
rule command_default {'default'}
rule struct_foreach { '{' <command_foreach> <expression_foreach> '}'
<content>
[ '{' <command_ifempty> '}'
<content>
]?
'{' <command_endforeach> '}'
}
rule command_foreach {'foreach'}
rule command_endforeach {'/foreach'}
rule command_ifempty {'ifempty'}
rule expression_foreach { <variable> 'in' <variable>}
rule command_import {
'import' <attribute> ** 1..2 }
rule expression { [ \w+ ['=='||'<'||'>'] \w+ || <variable>] }
rule expression_list { [\w+] ** ',' }
token variable { '$' \w+ }
# rule pair { <string> ':' <value> }
rule attribute { (\w+) '=' '"' (<-['"']>+) '"' }
}
class Plo::Node {
has Plo::Node @!childs;
has $!name;
method dumper {
return { self.WHAT.perl =>[ @!childs».dumper]};
}
}
class Plo::raw_text is Plo::Node {
has $.raw_text;
method export_perl {
return '\'' ~ $.raw_text ~ '\'';
}
}
class Plo::command_print is Plo::Node {
has $.raw_text;
}
class Plo::command_import is Plo::Node {
has $!file;
has $!rule;
method dumper {
return { self.WHAT.perl =>{ file=> $!file, rule=>$!rule}};
}
method export_perl {
my $body = qqx% pod6xhtml $!file%;
return $body
}
}
class Plosurin::TActions {
method TOP ($/) { make [ $<content>».ast]}
method content ($/) {
make $/.values.[0].ast;
}
method raw_text ($/) {
make Plo::raw_text.new(:raw_text($/));
}
method command_print ($/) {
make Plo::command_print.new(:raw_text($/));
}
method attribute ($/) {
my ($key,$val) = $/[0..1];
make ~$key => ~$val;
}
method command_import ($/) {
my %attr = $/<attribute>».ast.hash;
unless ( %attr{'file'}) {
die "Bad attr at: " ~ $/.CURSOR.pos ;
}
make Plo::command_import.new( :file( %attr{'file'}), :rule(%attr{'rule'}));
}
method tag ($/) {
make $/.values.[0].ast;
}
}
class Plosurin::Actions {
method TOP ($/) {
my $namespace = $<namespace><nsname>;
my @arr = ();
for $<def_template>.list -> $template {
my $tcomment = $template<header><h_comment>;
my $tparams = $template<header><h_param>;
my $tmplname = $template<template><template_name>;
my $tmplbody = $template<template><tmpl_content>;
@arr.push(Template.new(
:namespace($namespace),
:params($tparams),
:name($tmplname),
:comment($tcomment),
:body($tmplbody)
));
}
make @arr;
}
method namespace ($/) {
make $/.values.[0].ast
}
method h_comment ($/) {
make $<comment>.ast
}
method h_param ($/) {
my Bool $requred = "?" ne $<is_notrequired>;
make Param.new( requred=>$requred , name => $<paramname>, comment=>$<comment> )
}
}
grammar Plosurin::Grammar {
token TOP {<namespace> [ <def_template> ]+}
token namespace {'{namespace' \s* $<nsname>=[\w.]+ '}'}
rule def_template { <header>
<template> }
rule header { '/**'\s+
<h_comment>?
<h_param>*
'*/'||{
unless $/.CURSOR.pos ~~ $/.orig.chars {
my $m = $/;
my ($line, $column ) = line-and-column($/);
my $near_text = $/.orig.substr( $/.CURSOR.pos -1 , 5);
die "bad template header at line: $line : pos $column " ~" near:"
~ $/.orig.substr($/.CURSOR.pos-1, 5) ~ "< "}
#return 2
1;}}
rule h_comment { '*' <!before '@'>$<comment>=[\N]+}
rule h_param { '*' '@param'$<is_notrequired>=['?']? $<paramname>=[\w+] $<comment>=[\N]+}
rule template {
'{template' $<template_name>=<tmpl_name>'}'
$<tmpl_content>=[ .*? ]
'{/template}'}
rule tmpl_name { \.? \w+}
}
class Plosurin {
has $!type='perl5';
has $!package='MyApp::Template';
has $!parsed;
method parse ( $txt) {
my $res = Plosurin::Grammar.parse($txt, :actions(Plosurin::Actions.new ));
$!parsed = $/;
self;
}
multi method out_perl { self.out_perl( $!parsed.ast.values)}
multi method out_perl ( @templates ) {
my $output = "
# this is generated code
# Plosurin ver. $VERSION
package $!package;
";
for @templates -> $tmpl {
my $sub_name = $!package ~ '::' ~ ($tmpl.namespace ~ $tmpl.name).subst(rx/\./, '_', :g);
my $res = Plosurin::Template.parse($tmpl.body, :actions(Plosurin::TActions.new));
my $body = "";
for $/.ast.values -> $cnt {
$body ~= $cnt.export_perl()
}
$output ~= "sub $sub_name \{
return q!$body!;
\}
";
}
return $output;
}
}