diff options
author | Michal Čihař <michal@cihar.com> | 2016-03-01 16:31:08 +0100 |
---|---|---|
committer | Michal Čihař <michal@cihar.com> | 2016-12-21 15:02:38 +0100 |
commit | 26f97bce06a7a857e456df75dff899c4d5d5df55 (patch) | |
tree | d279841e85da19669c6f069cc4f25bfc47512ebb | |
parent | b084e76773685e1000c45aa9f610fd496123b521 (diff) | |
download | sql-parser-26f97bce06a7a857e456df75dff899c4d5d5df55.zip sql-parser-26f97bce06a7a857e456df75dff899c4d5d5df55.tar.gz sql-parser-26f97bce06a7a857e456df75dff899c4d5d5df55.tar.bz2 |
Share error handling in one base class
Signed-off-by: Michal Čihař <michal@cihar.com>
-rw-r--r-- | src/Core.php | 52 | ||||
-rw-r--r-- | src/Lexer.php | 29 | ||||
-rw-r--r-- | src/Parser.php | 29 |
3 files changed, 56 insertions, 54 deletions
diff --git a/src/Core.php b/src/Core.php new file mode 100644 index 0000000..ecca4dc --- /dev/null +++ b/src/Core.php @@ -0,0 +1,52 @@ +<?php + +/** + * Defines the core helper infrastructure of the library. + * + * @package SqlParser + */ +namespace SqlParser; + + +class Core +{ + + /** + * Whether errors should throw exceptions or just be stored. + * + * @var bool + * + * @see static::$errors + */ + public $strict = false; + + /** + * List of errors that occurred during lexing. + * + * Usually, the lexing does not stop once an error occurred because that + * error might be false positive or a partial result (even a bad one) + * might be needed. + * + * @var Exception[] + * + * @see Core::error() + */ + public $errors = array(); + + /** + * Creates a new error log. + * + * @param Exception $error The error exception. + * + * @throws Exception Throws the exception, if strict mode is enabled. + * + * @return void + */ + public function error($error) + { + if ($this->strict) { + throw $error; + } + $this->errors[] = $error; + } +} diff --git a/src/Lexer.php b/src/Lexer.php index 9de2a7a..1a706d2 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -41,7 +41,7 @@ if (!defined('USE_UTF_STRINGS')) { * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ * @see Context */ -class Lexer +class Lexer extends Core { /** @@ -80,15 +80,6 @@ class Lexer ); /** - * Whether errors should throw exceptions or just be stored. - * - * @var bool - * - * @see static::$errors - */ - public $strict = false; - - /** * The string to be parsed. * * @var string|UtfString @@ -146,19 +137,6 @@ class Lexer public $delimiterLen; /** - * List of errors that occurred during lexing. - * - * Usually, the lexing does not stop once an error occurred because that - * error might be false positive or a partial result (even a bad one) - * might be needed. - * - * @var LexerException[] - * - * @see Lexer::error() - */ - public $errors = array(); - - /** * Gets the tokens list parsed by a new instance of a lexer. * * @param string|UtfString $str The query to be lexed. @@ -375,10 +353,7 @@ class Lexer public function error($msg = '', $str = '', $pos = 0, $code = 0) { $error = new LexerException($msg, $str, $pos, $code); - if ($this->strict) { - throw $error; - } - $this->errors[] = $error; + parent::error($error); } /** diff --git a/src/Parser.php b/src/Parser.php index ddefc7e..b18b02d 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -23,7 +23,7 @@ use SqlParser\Statements\TransactionStatement; * @package SqlParser * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ -class Parser +class Parser extends Core { /** @@ -319,28 +319,6 @@ class Parser public $list; /** - * Whether errors should throw exceptions or just be stored. - * - * @var bool - * - * @see static::$errors - */ - public $strict = false; - - /** - * List of errors that occurred during parsing. - * - * Usually, the parsing does not stop once an error occurred because that - * error might be a false positive or a partial result (even a bad one) - * might be needed. - * - * @var ParserException[] - * - * @see Parser::error() - */ - public $errors = array(); - - /** * List of statements parsed. * * @var Statement[] @@ -599,9 +577,6 @@ class Parser public function error($msg = '', Token $token = null, $code = 0) { $error = new ParserException($msg, $token, $code); - if ($this->strict) { - throw $error; - } - $this->errors[] = $error; + parent::error($error); } } |