diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Components/Array2d.php | 6 | ||||
-rw-r--r-- | src/Components/OptionsArray.php | 6 | ||||
-rw-r--r-- | src/Lexer.php | 26 | ||||
-rw-r--r-- | src/Parser.php | 22 | ||||
-rw-r--r-- | src/Statement.php | 5 | ||||
-rw-r--r-- | src/Statements/CreateStatement.php | 2 |
6 files changed, 55 insertions, 12 deletions
diff --git a/src/Components/Array2d.php b/src/Components/Array2d.php index 64dea40..1660959 100644 --- a/src/Components/Array2d.php +++ b/src/Components/Array2d.php @@ -85,7 +85,11 @@ class Array2d extends Component if ($count === -1) { $count = $arrCount; } elseif ($arrCount != $count) { - $parser->error("{$count} values were expected, but found {$arrCount}.", $token); + $parser->error( + '%1$d values were expected, but found %2$d.', + $token, + array($count, $arrCount) + ); } $ret[] = $arr; $state = 1; diff --git a/src/Components/OptionsArray.php b/src/Components/OptionsArray.php index 487eefd..d1b9952 100644 --- a/src/Components/OptionsArray.php +++ b/src/Components/OptionsArray.php @@ -137,7 +137,11 @@ class OptionsArray extends Component // real options (e.g. if there are 5 options, the first // fake ID is 6). if (isset($ret->options[$lastOptionId])) { - $parser->error('This option conflicts with \'' . $ret->options[$lastOptionId] . '\'.', $token); + $parser->error( + 'This option conflicts with "%1$s".', + $token, + array($ret->options[$lastOptionId]) + ); $lastOptionId = $lastAssignedId++; } } else { diff --git a/src/Lexer.php b/src/Lexer.php index aae95f1..d6c3784 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -13,6 +13,16 @@ namespace SqlParser; use SqlParser\Exceptions\LexerException; +if (!defined('USE_GETTEXT')) { + + /** + * Whether `gettext` function should be used to translate error messages + * before throwing exceptions. + * @var bool + */ + define('USE_GETTEXT', function_exists('gettext')); +} + if (!defined('USE_UTF_STRINGS')) { /** @@ -302,14 +312,22 @@ class Lexer * @param string $msg The error message. * @param string $str The character that produced the error. * @param int $pos The position of the character. + * @param array $args The arguments to be replaced in the message. * @param int $code The code of the error. * * @throws LexerException Throws the exception, if strict mode is enabled. * * @return void */ - public function error($msg = '', $str = '', $pos = 0, $code = 0) - { + public function error( + $msg = '', $str = '', $pos = 0, $args = null, $code = 0 + ) { + if (USE_GETTEXT) { + $msg = gettext($msg); + } + if (!empty($args)) { + $msg = vsprintf($msg, $args); + } $error = new LexerException($msg, $str, $pos, $code); if ($this->strict) { throw $error; @@ -652,7 +670,9 @@ class Lexer } if (($this->last >= $this->len) || ($this->str[$this->last] !== $quote)) { - $this->error('Ending quote ' . $quote . ' was expected.', '', $this->last); + $this->error( + 'Ending quote %1$s was expected.', '', $this->last, array($quote) + ); } else { $token .= $this->str[$this->last]; } diff --git a/src/Parser.php b/src/Parser.php index 888a9d4..2fdd27c 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -12,6 +12,16 @@ namespace SqlParser; use SqlParser\Statements\SelectStatement; use SqlParser\Exceptions\ParserException; +if (!defined('USE_GETTEXT')) { + + /** + * Whether `gettext` function should be used to translate error messages + * before throwing exceptions. + * @var bool + */ + define('USE_GETTEXT', function_exists('gettext')); +} + /** * Takes multiple tokens (contained in a Lexer instance) as input and builds a * parse tree. @@ -399,14 +409,22 @@ class Parser * * @param string $msg The error message. * @param Token $token The token that produced the error. + * @param array $args The arguments to be replaced in the message. * @param int $code The code of the error. * * @throws ParserException Throws the exception, if strict mode is enabled. * * @return void */ - public function error($msg = '', Token $token = null, $code = 0) - { + public function error( + $msg = '', Token $token = null, $args = null, $code = 0 + ) { + if (USE_GETTEXT) { + $msg = gettext($msg); + } + if (!empty($args)) { + $msg = vsprintf($msg, $args); + } $error = new ParserException($msg, $token, $code); if ($this->strict) { throw $error; diff --git a/src/Statement.php b/src/Statement.php index 277942d..5e15d1e 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -248,10 +248,7 @@ abstract class Statement } elseif ($class === null) { // There is no parser for this keyword and isn't the beginning // of a statement (so no options) either. - $parser->error( - 'Unrecognized keyword.', - $token - ); + $parser->error('Unrecognized keyword.', $token); continue; } diff --git a/src/Statements/CreateStatement.php b/src/Statements/CreateStatement.php index afabc6a..6bc1c98 100644 --- a/src/Statements/CreateStatement.php +++ b/src/Statements/CreateStatement.php @@ -326,7 +326,7 @@ class CreateStatement extends Statement $token = $list->getNextOfType(Token::TYPE_KEYWORD); if ($token->value !== 'RETURNS') { $parser->error( - 'A \'RETURNS\' keyword was expected.', + 'A "RETURNS" keyword was expected.', $token ); } else { |