doy / parse-keyword Goto Github PK
View Code? Open in Web Editor NEWDEPRECATED: write syntax extensions in perl
Home Page: http://metacpan.org/release/Parse-Keyword
DEPRECATED: write syntax extensions in perl
Home Page: http://metacpan.org/release/Parse-Keyword
It would be nice if the parse_*
functions accepted a second parameter allowing subroutine attributes (such as :lvalue
) to be set for the coderef that gets returned.
While some subroutine attributes can be set via attributes.pm at run-time, some attributes (such as :lvalue
) need to be set while the CV is being built, or else they don't work properly.
That is, ideally Parse::Keyword's parser_fn
function should pass the subroutine attributes as the 4th (counting from 1, not 0) parameter to newATTRSUB
.
I don't know how much work this request would represent, but it would really be handy for me. It strikes me as something that could be useful for the p5-mop method
keyword too.
Given the following, the warning "GOT foo" is printed twice. I'd expect "GOT foo" and then "GOT bar". Am I doing something wrong?
use strict;
use warnings;
BEGIN {
package PerlX::Switch;
$INC{'PerlX/Switch.pm'} = __FILE__;
use Exporter 'import';
our @EXPORT = qw( switch );
use Parse::Keyword { switch => \&_parse_switch };
use match::simple qw( match );
sub switch
{
my ($expr, $cases, $default) = @_;
local $_ = $expr->();
warn "GOT $_";
CASE: for my $case ( @$cases )
{
my ($type, $condition, $block) = @$case;
my $matched = 0;
if ($type eq 'block')
{
$matched = !!$condition->();
}
else
{
my @terms = $condition->();
TERM: for my $term (@terms)
{
match($_, $term) ? (++$matched && last TERM) : next TERM;
}
}
return $block->() if $matched;
}
return $default->() if $default;
return;
}
sub _parse_switch
{
my ($expr);
lex_read_space;
die "syntax error; expected open parenthesis" unless lex_peek eq '(';
lex_read(1);
lex_read_space;
$expr = parse_fullexpr;
lex_read_space;
die "syntax error; expected close parenthesis" unless lex_peek eq ')';
lex_read(1);
lex_read_space;
die "syntax error; expected block" unless lex_peek eq '{';
lex_read(1);
lex_read_space;
my @cases;
while ( lex_peek(4) eq 'case' )
{
lex_read(4);
push @cases, _parse_case();
lex_read_space;
}
my $default;
if ( lex_peek(4) eq 'else' )
{
lex_read(4);
lex_read_space;
$default = _parse_consequence();
lex_read_space;
}
die "syntax error; expected end of switch block" unless lex_peek eq '}';
lex_read(1);
return (
sub { ($expr, \@cases, $default) },
1,
);
}
sub _parse_case
{
my ($expr, $type);
lex_read_space;
if (lex_peek eq '(')
{
lex_read(1);
$type = 'term';
$expr = parse_fullexpr;
lex_read_space;
die "syntax error; expected close parenthesis" unless lex_peek eq ')';
lex_read(1);
lex_read_space;
}
elsif (lex_peek eq '{')
{
$type = 'block';
$expr = parse_block;
lex_read_space;
}
else
{
$type = 'simple-term';
$expr = parse_fullexpr;
lex_read_space;
}
my $block = _parse_consequence();
return [ $type, $expr, $block ];
}
sub _parse_consequence
{
my ($expr, $type);
lex_read_space;
my $block;
if (lex_peek eq ':')
{
lex_read(1);
lex_read_space;
$block = parse_fullexpr;
lex_read_space;
die "syntax error; expected semicolon" unless lex_peek eq ';' || lex_peek eq '}';
lex_read(1) && lex_read_space while lex_peek eq ';';
}
else
{
$block = parse_block;
lex_read_space;
lex_read(1) && lex_read_space while lex_peek eq ';';
}
return $block;
}
};
use v5.14;
use PerlX::Switch;
sub xyz
{
my $var = shift;
switch ( $var ) {
case "foo": 3;
case ("bar") { say "here"; 1 }
case { $_ eq "baz" } { say "there"; 2 }
else { say "everywhere"; 99 }
}
}
xyz("foo");
xyz("bar");
When using Parse::Keyword on the expression level, the custom parser runs as expected. However, a nondescriptive syntax error is thrown afterwards. Code to reproduce:
# tested under perl 5.18.1
BEGIN {
use Parse::Keyword {
foo => sub {
warn "parser runs all right";
return sub { 42 }, 1;
},
};
sub foo { print @_, "\n" }
}
#1. this works fine:
foo;
#2. this fails:
my $x = foo;
#3. but this works, of course:
my $y = do { foo };
The error message in the 2nd case is:
syntax error at - line 16, near "= foo"
Execution of - aborted due to compilation errors.
Can this error be avoided?
If not, can a better error message be displayed when Parse::Keyword is used on the expression level? Adding this to the “Limitations” section of the documentation might be helpful as well.
As per my previous issue, parsed keywords do need to be exported. However, exporting them as lexical subs does not seem to be sufficient; they seem to need to be exported into the compiling package.
Related to https://rt.cpan.org/Ticket/Display.html?id=89370.
With perl 5.21.4 the error.t test fails:
# Failed test at t/error.t line 62.
# got: 'Global symbol "$baz" requires explicit package name (did you forget to declare "my $baz"?) at t/error.pl line 9.
# Execution of t/error.pl aborted due to compilation errors.
# '
# expected: 'Global symbol "$baz" requires explicit package name at t/error.pl line 9.
# Execution of t/error.pl aborted due to compilation errors.
# '
# Looks like you failed 1 test of 21.
t/error.t .....................
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/21 subtests
It should include something like:
use Exporter 'import';
our @EXPORT = qw(try);
See here https://travis-ci.org/doy/parse-keyword/jobs/59163143
Looks like it can't find dzil...
I assume this is related to my locale, note the "" instead of the tested '' around the abc in the second line or the error:
# Failed test at t/fun/name.t line 32.
# 'abc 123 at t/fun/name.t line 22.
# Foo::foo("abc", 123) called at t/fun/name.t line 26
# eval {...} called at t/fun/name.t line 25
# '
# doesn't match '(?^:^abc 123 at t/fun/name.t line 22\.?\n\tFoo::foo\('abc', 123\) called at t/fun/name.t line 26)'
# Looks like you failed 1 test of 2.
t/fun/name.t ..................
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/2 subtests
brewed Perl 5.18.1 -Duserelocatableinc -Dusethreads
[ahartmai@ahartmai-nb:~/perl5/git/p5-mop-redux$ (master)]$ env | grep LC
LC_PAPER=de_AT.UTF-8
LC_ADDRESS=de_AT.UTF-8
LC_MONETARY=de_AT.UTF-8
LC_NUMERIC=de_AT.UTF-8
LC_TELEPHONE=de_AT.UTF-8
LC_IDENTIFICATION=de_AT.UTF-8
LC_MEASUREMENT=de_AT.UTF-8
LC_TIME=de_AT.UTF-8
LC_NAME=de_AT.UTF-8
cpanm (App::cpanminus) 1.6005 on perl 5.016003 built for MSWin32-x64-multi-thread
You have make C:\strawberry5.16\c\bin\dmake.exe
You have LWP 6.04
You have "C:\Program Files (x86)\Git\bin\tar.exe", "C:\Program Files (x86)\Git\bin\gzip.exe" and "C:\Program Files (x86)\Git\bin\bzip2.exe"
You have "C:\Program Files (x86)\Git\bin\unzip.exe"
Searching Parse::Keyword on cpanmetadb ...
--> Working on Parse::Keyword
Fetching http://www.cpan.org/authors/id/D/DO/DOY/Parse-Keyword-0.05.tar.gz
-> OK
Unpacking Parse-Keyword-0.05.tar.gz
Entering Parse-Keyword-0.05
Checking configure dependencies from META.json
Checking if you have ExtUtils::MakeMaker 6.30 ... Yes (6.64)
Checking if you have Devel::CallParser 0 ... Yes (0.001)
Configuring Parse-Keyword-0.05
Running Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for Parse::Keyword
Writing MYMETA.yml and MYMETA.json
-> OK
Checking dependencies from MYMETA.json ...
Checking if you have Test::More 0.88 ... Yes (0.98)
Checking if you have File::Find 0 ... Yes (1.20)
Checking if you have File::Temp 0 ... Yes (0.22)
Checking if you have Exporter 0 ... Yes (5.67)
Checking if you have if 0 ... Yes (0.0602)
Checking if you have Carp 0 ... Yes (1.26)
Checking if you have Test::More 0.88 ... Yes (0.98)
Checking if you have File::Find 0 ... Yes (1.20)
Checking if you have File::Temp 0 ... Yes (0.22)
Checking if you have Exporter 0 ... Yes (5.67)
Checking if you have if 0 ... Yes (0.0602)
Checking if you have Carp 0 ... Yes (1.26)
Checking if you have warnings 0 ... Yes (1.13)
Checking if you have strict 0 ... Yes (1.07)
Checking if you have XSLoader 0 ... Yes (0.16)
Checking if you have Devel::CallParser 0 ... Yes (0.001)
Building and testing Parse-Keyword-0.05
cp lib/Parse/Keyword.pm blib\lib\Parse\Keyword.pm
C:\strawberry5.16\perl\bin\perl.exe C:\strawberry5.16\perl\lib\ExtUtils\xsubpp -typemap C:\strawberry5.16\perl\lib\ExtUtils\typemap Keyword.xs > Keyword.xsc && C:\strawberry5.16\perl\bin\perl.exe -MExtUtils::Command -e mv -- Keyword.xsc Keyword.c
gcc -c -s -O2 -DWIN32 -DWIN64 -DCONSERVATIVE -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -mms-bitfields -s -O2 -DVERSION=\"0.05\" -DXS_VERSION=\"0.05\" "-IC:\strawberry5.16\perl\lib\CORE" Keyword.c
Running Mkbootstrap for Parse::Keyword ()
C:\strawberry5.16\perl\bin\perl.exe -MExtUtils::Command -e chmod -- 644 Keyword.bs
C:\strawberry5.16\perl\bin\perl.exe -MExtUtils::Mksymlists \
-e "Mksymlists('NAME'=>\"Parse::Keyword\", 'DLBASE' => 'Keyword', 'DL_FUNCS' => { }, 'FUNCLIST' => [], 'IMPORTS' => { }, 'DL_VARS' => []);"
dlltool --def Keyword.def --output-exp dll.exp
g++ -o blib\arch\auto\Parse\Keyword\Keyword.dll -Wl,--base-file -Wl,dll.base -mdll -s -L"C:\strawberry5.16\perl\lib\CORE" -L"C:\strawberry5.16\c\lib" Keyword.o C:\strawberry5.16\perl\lib\CORE\libperl516.a -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 dll.exp
Keyword.o:Keyword.c:(.text+0x16b3): undefined reference to `__imp_C8K61oRQKxigiqmUlVdk_scp1'
Keyword.o:Keyword.c:(.text+0x1836): undefined reference to `__imp_Perl_scalar'
c:/strawberry5.16/c/bin/../lib/gcc/x86_64-w64-mingw32/4.6.3/../../../../x86_64-w64-mingw32/bin/ld.exe: Keyword.o: bad reloc address 0x0 in section `.pdata'
c:/strawberry5.16/c/bin/../lib/gcc/x86_64-w64-mingw32/4.6.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2: ld returned 1 exit status
dmake.exe: Error code 129, while making 'blib\arch\auto\Parse\Keyword\Keyword.dll'
-> FAIL Installing Parse::Keyword failed. See C:\Users\Andre\.cpanm\build.log for details.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.