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
  • Loading branch information
marcioAlmada committed Apr 17, 2019
1 parent 6c13805 commit 70edd18
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Expansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ private function mutate(TokenStream $ts, Ast $context, Engine $engine) : TokenSt

$context = $context->unwrap();

if (! is_array($context))
$this->fail(
"Error unpacking non unpackable Ast node on `$(%s ...%s {` at line %d with context: %s",
$result->{'* label name'}->token(),
$result->optional ? ' ?' : '',
$result->{'* label name'}->token()->line(),
json_encode($context, self::PRETTY_PRINT)
);

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

// normalize associative arrays
Expand Down
47 changes: 47 additions & 0 deletions tests/phpt/issues/ircmaxell-php-compiler#29.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--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 non unpackable Ast node on `$(binary_xor ... ? {` at line 29 with context: "^"

0 comments on commit 70edd18

Please sign in to comment.