develooper Front page | perl.perl5.porters | Postings from July 2019

Equivalent of yyerror() in XS

From:
Paul "LeoNerd" Evans
Date:
July 23, 2019 08:41
Subject:
Equivalent of yyerror() in XS
Message ID:
20190723094129.0924c1a3@shy.leonerd.org.uk
Bits of perl core parser make use of yyerror() to report syntax errors,
so the output includes source snippets:

 $ perl -Mexperimental=signatures -Mfeature=signatures \
   -E 'sub foo(@x,@y) { return 1 }'
  Multiple slurpy parameters not allowed at -e line 1, near "@y) "
  Execution of -e aborted due to compilation errors.

If I apply Evil Magic in my XS module, I can see the Perl_yyerror()
function on ELF platforms (e.g. my Linux box):

  #define yyerror(s)  Perl_yyerror(aTHX_ s)

and get to use the same in Future::AsyncAwait:

  $ perl -Mblib -MFuture::AsyncAwait -Mexperimental=signatures \
    -Mfeature=signatures -E 'async sub foo(@x, @y) { return 1 }'
  Multiple slurpy parameters not allowed at -e line 1, near "async sub
  foo(@x, @y"
  syntax error at -e line 1, near "async sub foo(@x, @y) "
  Execution of -e aborted due to compilation errors.

Admittedly I get two levels of error not just one, but still... the
output is clear enough in meaning.

However this doesn't work for non-ELF platforms like MSWin32 because
the actual symbol isn't really visible at link time.

As an alternative to yyerror(), I could just croak(), but then I don't
get such nice output:

  $ perl -Mblib -MFuture::AsyncAwait -Mexperimental=signatures \
    -Mfeature=signatures -E 'async sub foo(@x, @y) { return 1 }'
  Multiple slurpy parameters not allowed at -e line 1.

no source snippet in output.

Any suggestions on how I can get this result?

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About