Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2017 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/710b65b90fbc8244d39cb6086d151fdc to your computer and use it in GitHub Desktop.
Save anonymous/710b65b90fbc8244d39cb6086d151fdc to your computer and use it in GitHub Desktop.
#!/opt/rakudo-star-2016.10/bin/perl6
use v6;
use Grammar::Tracer;
grammar NVL {
rule TOP { [<main>]* <textfin> }
rule main { <text> <nvl> }
regex text {:s .+? }
rule nvl {:i 'nvl' '(' <expr1> ',' <expr2> ')'}
regex expr1 { .+? }
regex expr2 { .+? }
regex textfin { .+ }
}
class Nvl {
method TOP ($/) { my $result ~= make $_.made for $<main>;
make $result ~= $<textfin>; }
method main ($/) { make $<text> ~ $<nvl>.made ; }
method nvl ($/) { make "coalesce("~$<expr1>~','~$<expr2> ~ ")" ;}
}
grammar TEST {
rule TOP { <sql> }
rule sql { \s* <sqlstart> <sqltext> <sqlend> \s* }
token sqlstart {'EXEC SQL'}
token sqlend {'END-EXEC'}
regex sqltext { .+? <before 'END-EXEC'> }
}
class Test {
method TOP ($/) { my $sql=$<sql><sqltext>.made;
my $act = Nvl.new;
my $result= NVL.parse($sql, :act).made ;
}
method sql ($/) { $/.make: (~$/) }
method sqltext ($/) { $/.make: (~$/) }
}
my $cbl = "EXEC SQL select nvl(toto,titi) from dual END-EXEC";
say TEST.parse($cbl, actions => Test).made;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment