Skip to content

Commit

Permalink
Share error handling in one base class
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Čihař <michal@cihar.com>
  • Loading branch information
nijel committed Dec 21, 2016
1 parent b084e76 commit 26f97bc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
52 changes: 52 additions & 0 deletions 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;
}
}
29 changes: 2 additions & 27 deletions src/Lexer.php
Expand Up @@ -41,7 +41,7 @@
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
* @see Context
*/
class Lexer
class Lexer extends Core
{

/**
Expand Down Expand Up @@ -79,15 +79,6 @@ class Lexer
'parseSymbol', 'parseKeyword', 'parseLabel', 'parseUnknown'
);

/**
* Whether errors should throw exceptions or just be stored.
*
* @var bool
*
* @see static::$errors
*/
public $strict = false;

/**
* The string to be parsed.
*
Expand Down Expand Up @@ -145,19 +136,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.
*
Expand Down Expand Up @@ -375,10 +353,7 @@ public function lex()
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);
}

/**
Expand Down
29 changes: 2 additions & 27 deletions src/Parser.php
Expand Up @@ -23,7 +23,7 @@
* @package SqlParser
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
*/
class Parser
class Parser extends Core
{

/**
Expand Down Expand Up @@ -318,28 +318,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.
*
Expand Down Expand Up @@ -599,9 +577,6 @@ public function parse()
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);
}
}

0 comments on commit 26f97bc

Please sign in to comment.