Skip to content

Commit

Permalink
Add runtime error when attempting to unpack a non unpackable Ast node
Browse files Browse the repository at this point in the history
Ex.:

```
Error unpacking a non unpackable Ast node on `$(binary_xor ...? {` at line 29 with context: [
    "^"
]

Hint: use a non ellipsis expansion as in `$(binary_xor ? {`
```

cc ircmaxell/php-compiler#29
  • Loading branch information
marcioAlmada committed Apr 17, 2019
1 parent 6c13805 commit 7b68d38
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Expansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ private function mutate(TokenStream $ts, Ast $context, Engine $engine) : TokenSt

$context = $context->unwrap();

if (! is_array($context))
$this->fail(
"Error unpacking a non unpackable Ast node on `$(%s ...%s {` at line %d with context: %s\n\n%s",
$result->{'* label name'}->token(),
$result->optional,
$result->{'* label name'}->token()->line(),
json_encode([$context], self::PRETTY_PRINT),
sprintf("Hint: use a non ellipsis expansion as in `$(%s %s {`", $result->{'* label name'}->token(), $result->optional)
);

$delimiters = $result->{'delimiters'};

// normalize associative arrays
Expand Down
51 changes: 51 additions & 0 deletions tests/phpt/issues/ircmaxell-php-compiler#29.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--TEST--
Test for bug found at https://github.com/ircmaxell/php-compiler/pull/29 --pretty-print
--FILE--
<?php

$(macro :unsafe :recursive) {
$(optional(buffer('unsigned')) as unsigned) compile {
$(
repeat(
either(
chain(
T_VARIABLE as result,
token('='),
either(
chain(
T_VARIABLE as binary_left,
either(token('^') as binary_xor) as binary_op,
either(T_VARIABLE as binary_variable, T_LNUMBER as binary_number) as binary_right
) as binary
),
token(';')
) as assignop
)
) as stmts
)
}
} >> {
$(stmts ... {
$(assignop ? ... {
$(binary ? ... {
$(binary_op ... {
$(binary_xor ? ... {
$(result) = $this->context->builder->bitwiseXor($(binary_left), $__right);
})
})
})
})
})
}

compile {
$result = $value ^ 1;
}

?>
--EXPECTF--
Error unpacking a non unpackable Ast node on `$(binary_xor ...? {` at line 29 with context: [
"^"
]

Hint: use a non ellipsis expansion as in `$(binary_xor ? {`

0 comments on commit 7b68d38

Please sign in to comment.