Skip to content

Interpolation of captures into error messages #82

Open
@bpj

Description

@bpj

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions