Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation of captures into error messages #82

Open
bpj opened this issue Dec 5, 2021 · 0 comments
Open

Interpolation of captures into error messages #82

bpj opened this issue Dec 5, 2021 · 0 comments

Comments

@bpj
Copy link

bpj commented Dec 5, 2021

I wish there was a way to interpolate captures into error messages.

As an example my DSL supports == <= => comparison but not any kind of not-equal operator because the comparison is always followed by an operator which indicates what to do if the comparison succeeds or fails — think ternary except that either the if-true part or the if-false part may be left out. I want to throw an error if the user tries to use a not-equal operator, but I also want to detect more than one (im)possible not-equal operator (!= <> ~=). So basically I want to do something like this:

not_equal:
  / ( [ BANG TILDE ] EQUAL | LANGLE RANGLE ) /
  `Not-equal operator ('$1') not supported.`

instead of this:

not_equal:
    / BANG EQUAL /
    `Not-equal operator ('!=') not supported.`
  | / TILDE EQUAL /
    `Not-equal operator ('~=') not supported.`
  | / LANGLE RANGLE /
    `Not-equal operator ('<>') not supported.`

As a compromise I do this:

not_equal:
  / (! [ BANG TILDE ] EQUAL | LANGLE RANGLE ) /
  `Not-equal operator not supported.`

but it is not really satisfactory because I want to show the offender in the error message.

So I'm thinking a simple syntax like $n or ${n} to interpolate a capture and $$ to get a single dollar:

$message =~ s{ \$ (\$) | \$\{ (?! 0 ) (\d+) \} | \$ (?! 0 ) (\d+) \b }{
    if ( defined $1 ) { $1 }
    elsif ( defined $captures[$+ - 1] ) { $captures[$+ - 1] }
    else { croak "No capture \$$+ for error message `$message`" }
}egx;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant