Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2017 22:32
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/fca80723f43a7b76ad80a3c674c50469 to your computer and use it in GitHub Desktop.
Save anonymous/fca80723f43a7b76ad80a3c674c50469 to your computer and use it in GitHub Desktop.
#!/opt/rakudo-star-2016.10/bin/perl6
use v6;
#use Grammar::Tracer;
#use Grammar::Debugger;
# Question is about regex expr definition (see line 72/73):
# When expr is defined as "regex expr {:s {;}<mot1>* ", AST is
#「select decode(i_r_p_a,'O',0,nvl(dual.dummy,0) ) from dual 」
# main => 「select decode(i_r_p_a,'O',0,nvl(dual.dummy,0) ) 」
# text => 「select 」
# decode => 「decode(i_r_p_a,'O',0,nvl(dual.dummy,0) ) 」
# decodeinstr => 「decode」
# paraIn => 「(」
# val => 「i_r_p_a」
# expr => 「i_r_p_a」
# mot1 => 「i_r_p_a」
# whenthen => 「,'O',0」
# comma => 「,」
# when => 「'O'」
# expr => 「'O'」
# mot1 => 「'」
# mot1 => 「O」
# mot1 => 「'」
# comma => 「,」
# then => 「0」
# expr => 「0」
# mot1 => 「0」
# comma => 「,」
# else => 「nvl(dual.dummy,0) 」
# expr => 「nvl(dual.dummy,0) 」
# mot1 => 「nvl」
# paraIn => 「(」
# mot => 「dual」
# mot => 「.」
# mot => 「dummy」
# mot => 「,」
# mot => 「0」
# paraOut => 「) 」
# paraOut => 「) 」
# textfin => 「from dual 」
# When expr is defined as "regex expr {:s <mot1>* ", AST is
#「select decode(i_r_p_a,'O',0,nvl(dual.dummy,0) ) from dual 」
# textfin => 「select decode(i_r_p_a,'O',0,nvl(dual.dummy,0) ) from dual 」
# I don't understand why I have to add {;} to get the "correct" AST. Any idea ?
grammar DECODE {
rule TOPDECODE { [<main>]* <textfin>? }
rule main { <text>? <decode> }
rule decode {:i <decodeinstr> <paraIn> <val> <whenthen>+ [<comma> <else>] <paraOut>
| <decodeinstr> <paraIn> <val> <whenthen>+ <paraOut> }
regex decodeinstr {:i 'decode'\s* }
regex val {:s <decode>
|<expr> }
regex whenthen {:i <comma> <when> <comma> <then> }
regex when {:s <decode>
| <expr> }
regex then {:s <decode>
|<expr> }
regex else {:s <decode>
|<expr> }
regex text {:s .+? }
regex textfin {:s .+ }
regex comma {\s*','\s* }
regex paraIn {\s*'('\s*}
regex paraOut {\s*')'\s*}
token ws { <!ww> }
######## EXPR DEFINITION CHOOSE TO COMMENT LINE 72 or 73
#regex expr {:s {;}<mot1>*
regex expr {:s <mot1>*
| [<mot1><paraIn><mot>*<paraOut>]*
| [<paraIn><mot>*<paraOut>]*
}
rule mot { <-[()]>+? }
rule mot1 { <-[(),]>+? }
}
my $sqlStmt="select decode(i_r_p_a,'O',0,nvl(dual.dummy,0) ) from dual ";
say DECODE.parse($sqlStmt, rule => 'TOPDECODE');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment