-
Notifications
You must be signed in to change notification settings - Fork 1
/
CodeProcessing.rakumod
executable file
·604 lines (466 loc) · 20.9 KB
/
CodeProcessing.rakumod
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#!/usr/bin/env perl6
# The initial version of the code was taken from : https://stackoverflow.com/a/57128623
use v6.d;
use Text::CodeProcessing::REPLSandbox;
unit module Text::CodeProcessing;
##===========================================================
## Code chunk known languages
##===========================================================
#| Known code chunk languages.
our @codeChuckLangs = <perl6 raku>;
#= This variable can be overwritten by other packages
#| Modules for know code chunk languages.
our %codeChunkLangModule;
#= One language one module.
#| Initialization for know code chunk languages.
our %codeChunkLangCaller;
#| Initialization for know code chunk languages.
our %codeChunkLangInit;
##===========================================================
## Code chunk parameters with their default values
##===========================================================
my %defaultChunkParams =
:echo,
errorPrompt => '#ERROR: ',
evaluate => 'TRUE',
lang => '',
name => '',
outputLang => '',
outputPrompt => '# ',
outputResults => 'markup';
##===========================================================
## Code chunk parameter extraction
##===========================================================
#| Extract parameters from Match object
sub CodeChunkParametersExtraction( Str $list-of-params, $/, %defaults --> Hash) {
my $name = $<name> ?? $<name>.Str !! '';
my $lang = $<lang> ?? $<lang>.Str !! '';
my $echo = True;
my $outputResults = 'markup';
my $outputLang = '';
my $evaluate = 'TRUE';
my $outputPrompt = %defaults<outputPrompt> // "# OUTPUT: ";
my $errorPrompt = %defaults<errorPrompt> // "# ERROR: ";
my %extra;
# If a list of parameters is specified extract values
if $<params>{$list-of-params} {
for $<params>{$list-of-params}.values -> $pair {
if $pair<param>.Str (elem) <echo> {
$echo = $pair<value>.Str.lc ∈ <false no f> ?? False !! True;
} elsif $pair<param>.Str (elem) <eval evaluate> {
$evaluate = $pair<value>.Str;
} elsif $pair<param>.Str eq 'outputLang' || $pair<param>.Str ~~ / output.lang / {
$outputLang = $pair<value>.Str;
if $outputLang eq 'NONE' || ! $outputLang.trim { $outputLang = ''; }
} elsif $pair<param>.Str eq 'outputPrompt' || $pair<param>.Str ~~ / output.prompt / {
$outputPrompt = $pair<value>.Str;
$outputPrompt =
do if $outputPrompt eq 'NONE' { '' }
elsif $outputPrompt (elem) <AUTO AUTOMATIC GLOBAL Whatever> { $outputPrompt }
elsif $outputPrompt eq 'DEFAULT' { '# ' }
else { $outputPrompt }
} elsif $pair<param>.Str eq 'errorPrompt' || $pair<param>.Str ~~ / error.prompt / {
$errorPrompt = $pair<value>.Str;
$errorPrompt =
do if $errorPrompt eq 'NONE' { '' }
elsif $errorPrompt (elem) <AUTO AUTOMATIC GLOBAL Whatever> { $errorPrompt }
elsif $errorPrompt eq 'DEFAULT' { '# ERR: ' }
else { $errorPrompt }
} elsif $pair<param>.Str eq 'results' {
$outputResults = $pair<value>.Str;
if $outputResults (elem) <AUTO AUTOMATIC Whatever> || ! $outputResults.trim { $outputResults = 'markup'; }
if $outputResults eq 'asis' { $outputPrompt = ''; }
} else {
%extra{$pair<param>} = $pair<value> // True;
}
}
}
return Hash( %defaults , %( :$echo, :$evaluate, :$name, :$lang, :$outputLang, :$outputPrompt, :$errorPrompt, :$outputResults ), %extra);
}
##===========================================================
## Markdown functions
##===========================================================
#| Markdown code chunk ticks
constant $mdTicks = '```';
#| Markdown pair assignment
my regex md-assign-pair { $<param>=([<.alpha> | '.' | '_' | '-']+) \h* '=' \h* $<value>=(<-[{}\s]>* | '{' ~ '}' <-[{}]>* ) }
#| Markdown list of assignments
my regex md-list-of-params { <md-assign-pair>+ % [ \h* ',' \h* ] }
#| Markdown code chunk search regex
my regex MarkdownSearch {
$<fence>=[ ['`' | '~'] ** 3..* ]
$<header>=('{'? \h* $<lang>=(@codeChuckLangs)
[ \h+ $<name>=(<alpha>+) ]?
[ \h* ',' \h* $<params>=(<md-list-of-params>) ]? \h* '}'? \h* \v )
$<code>=(.*?)
<?after \v> $<fence>
}
#| Markdown replace sub
sub MarkdownReplace ($sandbox, $/, Str :$evalOutputPrompt = '# ', Str :$evalErrorPrompt = '#ERROR: ', Bool :$promptPerLine = True) {
# Determine the code chunk parameters
my %params =
CodeChunkParametersExtraction( 'md-list-of-params', $<header>,
%( lang => 'raku',
evaluate => 'TRUE',
outputPrompt => $evalOutputPrompt.lc ∈ <auto whatever> ?? '# ' !! $evalOutputPrompt,
errorPrompt => $evalErrorPrompt.lc ∈ <auto whatever> ?? '#ERROR: ' !! $evalErrorPrompt,
format => 'JSON' ) );
my $outputLang = %params<outputLang> // '';
# To evaluate or not
my Bool $evalCode = %params<evaluate>.lc (elem) <true t yes>;
# Construct the replacement string
my $res = '';
if $evalCode {
$res = CodeChunkEvaluate($sandbox, $<code>, %params<outputPrompt>, %params<errorPrompt>,
lang => %params<lang>,
:$promptPerLine,
|%params.grep({ $_.key ∉ %defaultChunkParams.keys }).Hash);
}
# Compose result
my $origChunk = %params<echo> ?? $<fence> ~ $<header> ~ $<code> ~ $<fence> !! '';
return do given %params<outputResults> {
when 'asis' {
$origChunk ~ ($evalCode ?? "\n" ~ $res !! '');
}
when 'hide' {
$origChunk;
}
default {
$origChunk ~
($evalCode
?? "\n" ~ $mdTicks ~ $outputLang ~ "\n" ~ $res ~ $mdTicks
!! '');
}
}
}
##===========================================================
## Org-mode functions
##===========================================================
#| Org-mode code block opening
constant $orgBeginSrc = '#+BEGIN_SRC';
#| Org-mode code block closing
constant $orgEndSrc = '#+END_SRC';
#| Org-mode pair assignment
my regex org-assign-pair { ':' $<param>=([<.alpha> | '.' | '_' | '-']+) \h+ $<value>=(\S*) | ':' $<param>=(<.alpha>+) }
#| Org-mode list of assignments
my regex org-list-of-params { <org-assign-pair>+ % [ \h+ ] }
#| Org-mode code chunk search regex
my regex OrgModeSearch {
$<header>=( $orgBeginSrc \h* $<lang>=(@codeChuckLangs)
[ \h+ $<params>=(<org-list-of-params>) ]? \h* \v )
$<code>=[<!before $orgEndSrc> .]*
$orgEndSrc
}
#| Org-mode replace sub
sub OrgModeReplace ($sandbox, $/, Str :$evalOutputPrompt = ': ', Str :$evalErrorPrompt = ':ERROR: ', Bool :$promptPerLine = True) {
# Determine the code chunk parameters
my %params =
CodeChunkParametersExtraction( 'org-list-of-params', $<header>,
%( lang => 'raku',
evaluate => 'TRUE',
outputPrompt => $evalOutputPrompt.lc ∈ <auto whatever> ?? ': ' !! $evalOutputPrompt,
errorPrompt => $evalErrorPrompt.lc ∈ <auto whatever> ?? ':ERROR: ' !! $evalErrorPrompt,
format => 'JSON' ) );
# To evaluate or not
my Bool $evalCode = %params<evaluate>.lc (elem) <true t yes>;
# Construct the replacement string
my $res = '';
if $evalCode {
$res = CodeChunkEvaluate(
$sandbox, $<code>, %params<outputPrompt>, %params<errorPrompt>,
lang => %params<lang>,
:$promptPerLine,
|%params.grep({ $_.key ∉ %defaultChunkParams.keys }).Hash);
}
# Compose result
my $origChunk = %params<echo> ?? $<header> ~ $<code> ~ $orgEndSrc !! '';
return do given %params<outputResults> {
when 'asis' {
$origChunk ~
($evalCode ?? "\n" ~ $res !! '');
}
when 'hide' {
$origChunk;
}
default {
$origChunk ~
($evalCode ?? "\n" ~ "#+RESULTS:" ~ "\n" ~ $res !! "\n");
}
}
}
##===========================================================
## Pod6 functions
##===========================================================
#| Pod6 code block opening
constant $podBeginSrc = '=begin code';
#| Pod6 code block opening
constant $podEndSrc = '=end code';
#| Pod6 pair assignment
my regex pod-assign-pair { ':' $<param>=([<.alpha> | '.' | '_' | '-']+) '<' $<value>=(\S*) '>' | ':' $<param>=(<.alpha>+) }
#| Pod6 list of assignments
my regex pod-list-of-params { <pod-assign-pair>+ % [ \h+ ] }
#| Pod6 code chunk search regex
my regex Pod6Search {
$<header>=( $podBeginSrc [ \h+ ':lang<' @codeChuckLangs '>' ]?
[ \h+ $<params>=(<pod-list-of-params>) ]? \h* \v )
$<code>=[<!before $podEndSrc> .]*
$podEndSrc
}
#| Pod6 replace sub
sub Pod6Replace ($sandbox, $/, Str :$evalOutputPrompt = '# ', Str :$evalErrorPrompt = '#ERROR: ', Bool :$promptPerLine = True) {
# Determine the code chunk parameters
my %params =
CodeChunkParametersExtraction( 'pod-list-of-params', $<header>,
%( lang => 'raku',
evaluate => 'TRUE',
outputPrompt => $evalOutputPrompt.lc ∈ <auto whatever> ?? '# ' !! $evalOutputPrompt,
errorPrompt => $evalErrorPrompt.lc ∈ <auto whatever> ?? '#ERROR: ' !! $evalErrorPrompt,
format => 'JSON' ) );
my $outputLang = %params<outputLang> // '';
if $outputLang { $outputLang = ' :lang<' ~ $outputLang ~ '>'; }
# To evaluate or not
my Bool $evalCode = %params<evaluate>.lc (elem) <true t yes>;
# Construct the replacement string
my $res = '';
if $evalCode {
$res = CodeChunkEvaluate(
$sandbox, $<code>, %params<outputPrompt>, %params<errorPrompt>,
lang => %params<lang>,
:$promptPerLine,
|%params.grep({ $_.key ∉ %defaultChunkParams.keys }).Hash);
}
# Compose result
my $origChunk = %params<echo> ?? $<header> ~ $<code> ~ $podEndSrc !! '';
return do given %params<outputResults> {
when 'asis' {
$origChunk ~
"\n" ~
$res
}
when 'hide' {
$origChunk;
}
default {
$origChunk ~
"\n" ~
"=begin output" ~ $outputLang ~ "\n" ~
$res ~
"=end output";
}
}
}
##===========================================================
## Dictionaries of file-type => sub
##===========================================================
my %fileTypeToSearchSub =
markdown => &MarkdownSearch,
org-mode => &OrgModeSearch,
pod6 => &Pod6Search;
my %fileTypeToReplaceSub =
markdown => &MarkdownReplace,
org-mode => &OrgModeReplace,
pod6 => &Pod6Replace;
##===========================================================
## Evaluation
##===========================================================
#| Checks if a module is installed.
sub is-installed(Str $module-name) {
try {
require ::($module-name);
return True;
}
False;
}
#| Adds a prompt to multi-line text.
sub add-prompt( Str:D $prompt, Str:D $text, Bool :$promptPerLine = True) {
$prompt ~ ( $promptPerLine ?? $text.subst( "\n", "\n$prompt", :g) !! $text )
}
#| Evaluates a code chunk in a REPL sandbox.
sub CodeChunkEvaluate ($sandbox, $code, $evalOutputPrompt, $evalErrorPrompt,
Str :$lang = 'raku',
Bool :$promptPerLine = True,
*%params) is export {
# If DSL evaluation is specified change the code accordingly
my $code-to-eval = do given $lang {
when %codeChunkLangCaller{$_}:exists {
my $ps = %params.grep({ $_.key ∉ %defaultChunkParams.keys }).map({ "{$_.key} => {(+$_.value).defined ?? $_.value !! "'{$_.value}'"}" }).join(', ');
%codeChunkLangCaller{$_}.($code.Str, $ps)
}
default { $code.Str }
}
if %codeChunkLangModule{$lang}:exists && !(%codeChunkLangInit{$lang} // False) {
# Check if the required module(s) are installed
if ! is-installed(%codeChunkLangModule{$lang}) {
die "The module {%codeChunkLangModule{$lang}} has to be installed in order to use $lang code chunk evaluation.";
}
# Add package loading
$code-to-eval = "use {%codeChunkLangModule{$lang}};" ~ "\n" ~ $code-to-eval;
# Mark the DSL initialization
%codeChunkLangInit{$lang} = True;
}
## Redirecting stdout to a custom $out
my $out;
my $*OUT = $*OUT but role {
method print (*@args) {
$out ~= @args
}
}
## Redirecting stdout to a custom $err
my $err;
my $*ERR = $*ERR but role {
method print (*@args) {
$err ~= @args
}
}
# REPL sandbox execution
$sandbox.execution-count++;
my $p = $sandbox.eval($code-to-eval, :store($sandbox.execution-count));
## Result with prompts
($p.exception ?? add-prompt($evalErrorPrompt, $p.exception.Str.trim, :$promptPerLine) ~ "\n" !! '') ~
($err ?? add-prompt($evalErrorPrompt, $err.Str.trim, :$promptPerLine) ~ "\n" !! '') ~
add-prompt($evalOutputPrompt, ($out // $p.output).trim, :$promptPerLine) ~
"\n"
}
##===========================================================
## StringCodeChunksEvaluation
##===========================================================
#| Evaluates code chunks in a string.
sub StringCodeChunksEvaluation(Str:D $input,
Str:D $docType,
:$evalOutputPrompt is copy = Whatever,
:$evalErrorPrompt is copy = Whatever,
Bool :$promptPerLine = True) is export {
die "The second argument is expected to be one of {%fileTypeToReplaceSub.keys.join(', ')}."
unless $docType (elem) %fileTypeToReplaceSub.keys;
if $evalOutputPrompt.isa(Whatever) { $evalOutputPrompt = 'Whatever' }
die "The argument evalOutputPrompt is expected to be a string or Whatever."
unless $evalOutputPrompt ~~ Str;
if $evalErrorPrompt.isa(Whatever) { $evalErrorPrompt = 'Whatever' }
die "The argument evalErrorPrompt is expected to be a string or Whatever."
unless $evalErrorPrompt ~~ Str;
## Create a sandbox
my $sandbox = Text::CodeProcessing::REPLSandbox.new();
## Process code chunks (weave output)
$input.subst: %fileTypeToSearchSub{$docType}, -> $s { %fileTypeToReplaceSub{$docType}($sandbox, $s,
:$evalOutputPrompt,
:$evalErrorPrompt,
:$promptPerLine) }, :g;
}
##===========================================================
## StringCodeChunksExtraction
##===========================================================
#| Extracts code from code chunks in a string.
sub StringCodeChunksExtraction(Str:D $input,
Str:D $docType) is export {
die "The second argument is expected to be one of {%fileTypeToReplaceSub.keys}"
unless $docType (elem) %fileTypeToReplaceSub.keys;
## Process code chunks (weave output)
$input.match( %fileTypeToSearchSub{$docType}, :g).map({ trim($_.<code>) }).join("\n")
}
##===========================================================
## FileCodeChunksProcessing
##===========================================================
#| Evaluates code chunks in a file.
sub FileCodeChunksProcessing(Str $fileName,
:$outputFileName = Whatever,
:$evalOutputPrompt is copy = Whatever,
:$evalErrorPrompt is copy = Whatever,
Bool :$noteOutputFileName = False,
Bool :$promptPerLine = True,
Bool :$tangle = False) {
## Determine the output file name and type
my Str $fileNameNew;
my Str $fileType;
my Str $autoSuffix = $tangle ?? '_tangled' !! '_woven';
if $outputFileName.isa(Str) {
$fileNameNew = $outputFileName
} elsif $outputFileName.isa(Whatever) {
## If the input file name has extension that is one of <md MD Rmd org pod6>
## then insert "_weaved" before the extension.
if $fileName.match(/ :i .* \. [md | Rmd | qmd | org | pod6] $ /) {
$fileNameNew = $fileName.subst(/ $<name> = (.*) '.' $<ext> = (md | MD | Rmd | qmd | org | pod6) $ /, -> $/ { $<name> ~ $autoSuffix ~ '.' ~ $<ext> });
} else {
$fileNameNew = $fileName ~ $autoSuffix;
}
} else {
die 'The argument $outputFileName is expected to be string or Whatever.';
}
if $fileName.match(/ :i .* \. [md | Rmd | qmd] $ /) { $fileType = 'markdown' }
elsif $fileName.match(/ :i .* \. org $ /) { $fileType = 'org-mode' }
elsif $fileName.match(/ :i .* \. pod6 $ /) { $fileType = 'pod6' }
else {
die "Unknown file type (extension). The file type (extension) is expectecd to be one of {<md Rmd qmd org pod6>}.";
}
if $noteOutputFileName {
note "Output file is $fileNameNew" unless $outputFileName;
}
## Process output prompt
if $evalOutputPrompt.isa(Whatever) { $evalOutputPrompt = 'Whatever' }
die "The argument evalOutputPrompt is expected to be a string or Whatever."
unless $evalOutputPrompt ~~ Str;
## Process error prompt
if $evalErrorPrompt.isa(Whatever) { $evalErrorPrompt = 'Whatever' }
die "The argument evalErrorPrompt is expected to be a string or Whatever."
unless $evalErrorPrompt ~~ Str;
## Process code chunks (weave output) and spurt in a file
if $tangle {
spurt( $fileNameNew, StringCodeChunksExtraction(slurp($fileName), $fileType) )
} else {
spurt( $fileNameNew, StringCodeChunksEvaluation(slurp($fileName), $fileType, :$evalOutputPrompt, :$evalErrorPrompt, :$promptPerLine) )
}
}
##===========================================================
## FileCodeChunksEvaluation
##===========================================================
#| Evaluates code chunks in a file.
sub FileCodeChunksEvaluation(Str $fileName,
:$outputFileName = Whatever,
:$evalOutputPrompt = 'AUTO',
:$evalErrorPrompt = 'AUTO',
Bool :$noteOutputFileName = True,
Bool :$promptPerLine = True) is export {
FileCodeChunksProcessing( $fileName, :$outputFileName, :$evalOutputPrompt, :$evalErrorPrompt, :$noteOutputFileName, :$promptPerLine, :!tangle)
}
##===========================================================
## FileCodeChunksExtraction
##===========================================================
#| Extracts code from code chunks in a file.
sub FileCodeChunksExtraction(Str $fileName,
:$outputFileName = Whatever,
Bool :$noteOutputFileName = True) is export {
FileCodeChunksProcessing( $fileName, :$outputFileName, :$noteOutputFileName, :tangle)
}
##===========================================================
## Register code chunk lang
##===========================================================
#| Register code chunk language.
sub register-lang(Str :$lang!, Str :$module!, :&caller!) is export {
@codeChuckLangs.append($lang);
if $module { %codeChunkLangModule{$lang} = $module; }
%codeChunkLangCaller{$lang} = &caller;
}
##===========================================================
## Plug-in definition
##===========================================================
# Shell
register-lang(
lang => 'shell',
module => '',
caller => -> $code, $params {
'my $pCoDeXe832xereSWEiie3 = Q (' ~ $code.Str ~ '); my $proc = Proc.new(:out); $proc.shell($pCoDeXe832xereSWEiie3); my $captured-output = $proc.out.slurp: :close; $captured-output;'
} );
# DSL
register-lang(
lang => 'raku-dsl',
module => 'DSL::Shared::Utilities::ComprehensiveTranslation',
caller => -> $code, $params { 'ToDSLCode(Q「「「「' ~ $code.Str ~ '」」」」' ~ ($params ?? ", $params" !! '') ~ ')' } );
# OpenAI
register-lang(
lang => 'openai',
module => 'WWW::OpenAI',
caller => -> $code, $params {'openai-completion(Q「「「「' ~ $code.Str ~ '」」」」' ~ ($params ?? ", $params" !! '') ~ ')' });
# PaLM
register-lang(
lang => 'palm',
module => 'WWW::PaLM',
caller => -> $code, $params {'palm-generate-text(Q「「「「' ~ $code.Str~ '」」」」' ~ ($params ?? ", $params" !! '') ~ ')' });