-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.def
297 lines (251 loc) · 7.95 KB
/
output.def
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
include: macros_util/debug.def
#---- common features -----------
#- include: macros_output/case.def
#- include: macros_output/for.def
#- include: macros_output/scope.def
#- include: macros_output/sumcode.def
subcode: output_main
$global $debug=0
$global $out
$global $mode
$global $page
$call @package_globals
$sub get_interface
return (\&init_page, \&parsecode, \&set_output, \&modeswitch, \&dumpout);
$sub init_page($t_page)
$page=$t_page
# ---- Set output file extension
$(if:ext)
MyDef::set_page_extension("$(ext)")
$(else)
MyDef::set_page_extension(undef)
$(if:init_mode)
my $init_mode="$(init_mode)"
$(else)
my $init_mode="sub"
$call @on_init_page # perpage global variable initializations
$call @init_page # page logic initializations
return $init_mode
$sub set_output($newout)
$out = $newout
# ---- www has php, html and js mode ----------
$sub modeswitch($mode, $in)
$call @modeswitch
# ---- All specialized parsing goes here.
$sub parsecode($l)
$if $debug eq "parse"
$call dump_line, parsecode
#-------------------
$call parsecode_debug
$call parsecode_eval
$call parsecode
# ---- Final output, allows for 2nd pass translation
$sub dumpout($f, $out)
my $dump={out=>$out,f=>$f}
$call @dumpout
MyDef::dumpout::dumpout($dump);
$call single_blocks
$call @support_subs
1;
#----------------------------------------
subcode: inherit(M)
$use MyDef::output_$(M)
$global $out
$(export:super=MyDef::output_$(M)::$1)
$call @package_globals
$sub get_interface
return (\&init_page, \&parsecode, \&set_output, \&modeswitch, \&dumpout);
$sub set_output($newout)
$out = $newout
$(super:set_output)($newout)
$sub modeswitch($mode, $in)
$call @on_modeswitch
$sub init_page($t_page)
my $page=$t_page
# ---- Set output file extension
$(if:ext)
MyDef::set_page_extension("$(ext)")
$(else)
MyDef::set_page_extension(undef)
$call @on_init_pre
my $init_mode = $(super:init_page)(@_)
$call @on_init
return $init_mode
$sub parsecode($l)
$call parsecode_debug
$call parsecode_eval
$call @on_parsecode
return $(super:parsecode)($l)
$sub dumpout($f, $out)
$call @on_dumpout
$(super:dumpout)($f, $out)
$call @support_subs
1;
# -------------------------
subcode:@ parsecode
push @$out, $l
subcode: parsecode_eval
$case $l=~/^\$eval\s+(\w+)(.*)/
my ($codename, $param)=($1, $2)
$param=~s/^\s*,\s*//
$call parsecode_run_eval, $codename
return
subcode: parsecode_debug
$case $l=~/^DEBUG (\w+)/
$global $debug
$if $1 eq "OFF"
$debug=0
$else
$debug=$1
$(if:super)
return $(super:parsecode)($l)
$(else)
return
#----------------------------------------
subcode: parsecode_common
# print "parsecode_common: input [$l]\n"
$if $l=~/^DUMP_STUB\s/
push @$out, $l
return
$call @parsecode_special
$elif $l=~/^\s*\$(\w+)\((.*?)\)\s+(.*?)\s*$/
# $function(param1) param2 ----
my ($func, $param1, $param2)=($1, $2, $3)
$case $func eq "plugin"
# $call $param2, ...
$call add_plugin, $param1, $param2
return
$call @parsecode_func_param
$elif $l=~/^\s*\$(\w+)\s*(.*)$/ -> $func, $param
# $function param ----
$if $param !~ /^=/
$case $func eq "plugin"
$foreach $p in split /,\s*/, $param
$if $p=~/^&(.+)/
# &call $1, ...
$call add_plugin, $1, $p
$else
# $call $p, ...
$call add_plugin, $p, $p
return
$call parsecode_check_plugin # run
$call @parsecode_func
$elif $l=~/^CALLBACK\s+(\w+)\s*(.*)/
# $function param ----
# callback
my ($func, $param)=($1, $2)
my $codelist=$MyDef::compileutil::named_blocks{"last_grab"}
$call @parsecode_callback
return
$call @parsecode_other
# print "parsecode_common: push out [$l]\n"
push @$out, $l
subcode: single_blocks
$sub single_block($t1, $t2, $scope)
my @src
push @src, "$t1";
push @src, "INDENT";
push @src, "BLOCK";
push @src, "DEDENT";
push @src, "$t2";
MyDef::compileutil::set_named_block("NEWBLOCK", \@src)
$if $scope
return "NEWBLOCK-$scope"
$else
return "NEWBLOCK";
subcode: return_newblock(scope)
my @src
BLOCK
MyDef::compileutil::set_named_block("NEWBLOCK", \@src)
return "NEWBLOCK-$(scope)"
subcode: push_indented_block
push @src, "INDENT";
push @src, "BLOCK";
push @src, "DEDENT";
subcode: push_single_block(pre, post)
push @src, $(pre);
push @src, "INDENT";
push @src, "BLOCK";
push @src, "DEDENT";
push @src, $(post);
# ------------------------------------------
subcode: add_plugin(p1, p2)
$global %plugin_statement
$global %plugin_condition
$if $(p2)=~/_condition$/
$plugin_condition{$(p1)}=$(p2)
$else
$plugin_statement{$(p1)}=$(p2)
subcode: parsecode_check_plugin
$(set:C=$plugin_statement{$func})
$case $(C)
my $c= $(C)
$if $c=~/^&(.+)/
return "PARSE:\&call $1, $param"
$else
MyDef::compileutil::call_sub("$c, $param")
# $call parsecode_run_eval, $(C)
return
subcode: parse_condition_check_plugin
$if $plugin_condition{$func}
my $condition
$call parsecode_run_eval, $plugin_condition{$func}
return $condition
# -------------------------------
subcode: parsecode_run_eval(code)
$(if:code!=$codename)
my $codename=$(code)
# print "parsecode_run_eval - $codename - ", __PACKAGE__, "\n"
my $t=MyDef::compileutil::eval_sub($codename)
eval $t
$if $@ and !$MyDef::compileutil::eval_sub_error{$codename}
$MyDef::compileutil::eval_sub_error{$codename}=1
print "evalsub - $codename\n"
print "[$t]\n"
print "eval error: [$@] package [", __PACKAGE__, "]\n"
#----------------------------------------
#- utility subcode
macros:
hascode: $MyDef::def->{codes}->{"$1"} or $MyDef::page->{codes}->{"$1"}
callsub: MyDef::compileutil::callsub($1)
getmacro: MyDef::compileutil::get_macro_word("$1", 1)
subcode: warn(@msg)
$(if:msg~")
$(set:msg=$(msg:strip))
my $curfile=MyDef::compileutil::curfile_curline()
$print "[$curfile]$yellow $(msg)"
subcode: set_macro(name, value)
MyDef::compileutil::set_current_macro($(name), $(value))
subcode: replace_output(name)
my $old_out=MyDef::compileutil::set_output($(name))
BLOCK
MyDef::compileutil::set_output($old_out)
subcode: push_indent_block
push @$out, "INDENT"
BLOCK
push @$out, "DEDENT"
subcode: inherit_macros(code)
my $macro={}
$if $(code)->{macros}
$foreach %{$(code)->{macros}}
$macro->{$k}=$v
$if $(code)->{codes}
$macro->{"codes"}=$(code)->{codes}
push @{$MyDef::compileutil::deflist}, $macro
BLOCK
pop @{$MyDef::compileutil::deflist}
#--------
subcode: each_subcode(type)
my $codes=$MyDef::def->{codes}
$call iter_subcodes, $(type)
# output_python.def uses iter_subcodes directly ...
subcode: iter_subcodes(type)
my @tlist
$while my ($k, $v)= each %$codes
$if $v->{type} eq "$(type)"
push @tlist, $k
$if @tlist
@tlist=sort { $codes->{$a}->{index} <=> $codes->{$b}->{index} } @tlist
$foreach $name in @tlist
my $code=$codes->{$name}
BLOCK