diff --git a/bin/compiler.php b/bin/compiler.php index 365062ae..d3248409 100755 --- a/bin/compiler.php +++ b/bin/compiler.php @@ -3,6 +3,7 @@ require __DIR__ . '/bootstrap.php'; use PhpAot\Php\FileScanner; +use PhpAot\Php\SyntaxError; use PhpAot\Php\Translator; use PhpAot\Php\Unsupported; @@ -41,6 +42,8 @@ foreach ($list as $k => $file) { echo " unsupported syntax: " . $e->getMessage() . "\n"; echo " skip: " . $file . "\n"; unset($list[$k]); + } catch (SyntaxError $e) { + unset($list[$k]); } } } diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index d5a31070..92ea303c 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -85,7 +85,12 @@ class Preprocessor extends CompilerBase $phpCode = $this->loadFile($file); $this->climate->info('prepare: ' . $this->file); - $ast = $this->parser->parse($phpCode); + try { + $ast = $this->parser->parse($phpCode); + } catch (\PhpParser\Error $e) { + $this->climate->red("Fatal error: {$e->getMessage()} in {$this->file}"); + throw new SyntaxError($e->getMessage(), $e->getCode()); + } $traverser = new NodeTraverser(); $traverser->addVisitor(new Visitor()); diff --git a/src/Php/SyntaxError.php b/src/Php/SyntaxError.php new file mode 100644 index 00000000..689a8cb6 --- /dev/null +++ b/src/Php/SyntaxError.php @@ -0,0 +1,8 @@ +flags; $type = $v->type ? $this->getTypeFromZendType($this->parseIdentifier($v->type)) : self::TYPE_VAR; foreach ($v->consts as $const) { - $constInfo = new ConstantDef($this->parseIdentifier($const->name), $flags, $type, $this->parseIdentifier($const->value)); + $constName = $this->parseIdentifier($const->name); + if (isset($this->classDef->constants[$constName])) { + $this->fatalError($const, 'Cannot redefine class constant ' . $this->class . '::' . $constName); + } + $constInfo = new ConstantDef($constName, $flags, $type, $this->parseIdentifier($const->value)); $this->classDef->constants[$constInfo->name] = $constInfo; } } diff --git a/tests/core/classes/constants_error_001.phpt b/tests/core/classes/constants_error_001.phpt new file mode 100644 index 00000000..13428675 --- /dev/null +++ b/tests/core/classes/constants_error_001.phpt @@ -0,0 +1,18 @@ +--TEST-- +Error case: duplicate class constant definition +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Fatal error: Cannot redefine class constant myclass::myConst in %s on line 5 diff --git a/tests/core/classes/constants_error_002.phpt b/tests/core/classes/constants_error_002.phpt new file mode 100644 index 00000000..32aba740 --- /dev/null +++ b/tests/core/classes/constants_error_002.phpt @@ -0,0 +1,13 @@ +--TEST-- +Error case: class constant as an array +--FILE-- + +--EXPECT-- diff --git a/tests/core/classes/constants_error_005.phpt b/tests/core/classes/constants_error_005.phpt new file mode 100644 index 00000000..9c31e147 --- /dev/null +++ b/tests/core/classes/constants_error_005.phpt @@ -0,0 +1,16 @@ +--TEST-- +Error case: class constant as an encapsed containing a variable +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Fatal error: Constant expression contains invalid operations in %s on line %d