Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2017 00:12
Show Gist options
  • Save anonymous/76ed6fad77d42e8daf444a1bdb7c46fb to your computer and use it in GitHub Desktop.
Save anonymous/76ed6fad77d42e8daf444a1bdb7c46fb to your computer and use it in GitHub Desktop.
perl6 grammar
I'm trying to use a perl6 grammar inside another one. You will find my testcase below. Unfortunately, when running this code,I get the error message :
Cannot assign to a readonly variable or a value
in method TOP at ./sub021.pl6 line 41
in regex TOP at ./sub021.pl6 line 30
in block at /opt/rakudo-star-2016.10/share/perl6/site/sources/88A197C57F6C4623C1071F68DFC42E86CA5F4B4D (Grammar::Tracer) line 21
in block <unit> at ./sub021.pl6 line 53
I dont understand why my variable is read only...
#!/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;
coomplete execution results is :
TOP
| sql
| | sqlstart
| | * MATCH "EXEC SQL"
| | sqltext
| | * MATCH "select nvl(toto,titi) from dual "
| | sqlend
| | * MATCH "END-EXEC"
| * MATCH "EXEC SQL select nvl(toto,titi) from dual END-EXEC"
| TOP
| | main
| | | text
| | | * MATCH "select "
| | | nvl
| | | | expr1
| | | | * MATCH "t"
| | | | expr2
| | | | * MATCH "t"
| | | * MATCH "nvl(toto,titi)"
| | * MATCH "select nvl(toto,titi) "
| | main
| | | text
| | | * MATCH "from "
| | | nvl
| | | * FAIL
| | | nvl
| | | * FAIL
| | | nvl
| | | * FAIL
| | | nvl
| | | * FAIL
| | * FAIL
| | textfin
| | * MATCH "from dual "
| * MATCH "select nvl(toto,titi) from dual "
Cannot assign to a readonly variable or a value
in method TOP at ./sub021.pl6 line 41
in regex TOP at ./sub021.pl6 line 30
in block at /opt/rakudo-star-2016.10/share/perl6/site/sources/88A197C57F6C4623C1071F68DFC42E86CA5F4B4D (Grammar::Tracer) line 21
in block <unit> at ./sub021.pl6 line 53
MAny thanks for your help.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment