我使用模块Parse :: RecDescent
并尝试构建语法来捕获perl子名称
为什么在我的代码中
https://gist.github.com/1595532
不工作语法 get_sub: NOWORD TEST NOWORD
on'> test1 $'? (variabble $ perl_code1)
use Modern::Perl;
use Parse::RecDescent;
my $perl_code1 = q{my >test1$ string3 = $object->oldSub(6,7);};
my $perl_code2 = q{test1$ string3 = $object->oldSub(6,7);};
my $perl_code3 = q{my test1$ string3 = $object->oldSub(6,7);};
my @perl_lines = ( $perl_code1, $perl_code2, $perl_code3 );
my $var = 'test1';
my $sub_grammar = q{
get_sub:
NOWORD TEST NOWORD
{print $item[2]}
|TEST NOWORD
{print $item[1]}
get_sub2:
WORD TEST NOWORD
{print $item[2]}
|TEST NOWORD
{print $item[1]}
WORD:
/\w+/
NOWORD:
/\W+/
TEST:
/} . $var . q{/
};
my $sub_parse = new Parse::RecDescent($sub_grammar);
say 'example1:';
for my $string1 (@perl_lines) {
say "Valid sub:$string1\n" if $sub_parse->get_sub($string1);
}
say 'example2:';
for my $string2 (@perl_lines) {
say " Valid sub:$string2\n" if $sub_parse->get_sub2($string2);
}
答案 0 :(得分:2)
您的$perl_code1
以字符串my
开头,与规则不匹配。你需要在语法中遵循以下内容:
get_sub:
WORD NOWORD TEST NOWORD
...