diff options
author | Dan Ungureanu <udan1107@gmail.com> | 2015-07-09 00:50:28 +0300 |
---|---|---|
committer | Dan Ungureanu <udan1107@gmail.com> | 2015-07-09 00:50:28 +0300 |
commit | 9478c9a1d7eae3283bc5aae248d9d23b4a9f7bc6 (patch) | |
tree | c1f22f43adda2376fbd0a5638e57426b4d6009bd | |
parent | 0bff48ae488e651c37d84ba0a9aa8ff57ae8a43e (diff) | |
download | sql-parser-9478c9a1d7eae3283bc5aae248d9d23b4a9f7bc6.zip sql-parser-9478c9a1d7eae3283bc5aae248d9d23b4a9f7bc6.tar.gz sql-parser-9478c9a1d7eae3283bc5aae248d9d23b4a9f7bc6.tar.bz2 |
Added utilities for parsing errors.
-rw-r--r-- | src/Utils/Error.php | 94 | ||||
-rw-r--r-- | tests/Lexer/ContextTest.php | 2 | ||||
-rw-r--r-- | tests/Utils/ErrorTest.php | 34 |
3 files changed, 129 insertions, 1 deletions
diff --git a/src/Utils/Error.php b/src/Utils/Error.php new file mode 100644 index 0000000..c1bfee1 --- /dev/null +++ b/src/Utils/Error.php @@ -0,0 +1,94 @@ +<?php + +/** + * Error related utilities. + * + * @package SqlParser + * @subpackage Utils + */ +namespace SqlParser\Utils; + +use SqlParser\Lexer; +use SqlParser\Parser; + +/** + * Error related utilities. + * + * @category Exceptions + * @package SqlParser + * @subpackage Utils + * @author Dan Ungureanu <udan1107@gmail.com> + * @license http://opensource.org/licenses/GPL-2.0 GNU Public License + */ +class Error +{ + + /** + * Gets the errors of a lexer and a parser. + * + * @param array $objs + * + * @return array Each element of the array represents an error. + * `$err[0]` holds the error message. + * `$err[1]` holds the error code. + * `$err[2]` holds the string that caused the issue. + * `$err[3]` holds the position of the string. + * (i.e. `array($msg, $code, $str, $pos)`) + */ + public static function get($objs) + { + $ret = array(); + + foreach ($objs as $obj) { + if ($obj instanceof Lexer) { + foreach ($obj->errors as $err) { + $ret[] = array( + $err->getMessage(), + $err->getCode(), + $err->ch, + $err->pos + ); + } + } elseif ($obj instanceof Parser) { + foreach ($obj->errors as $err) { + $ret[] = array( + $err->getMessage(), + $err->getCode(), + $err->token->token, + $err->token->position + ); + } + } + } + + return $ret; + } + + /** + * Formats the specified errors + * + * @param array $errors The errors to be formatted. + * @param string $format The format of an error. + * '$1$d' is replaced by the position of this error. + * '$2$s' is replaced by the error message. + * '$3$d' is replaced by the error code. + * '$4$s' is replaced by the string that caused the + * issue. + * '$5$d' is replaced by the position of the string. + * @return array + */ + public static function format( + $errors, $format = '#%1$d: %2$s (near "%4$s" at position %5$d)' + ) { + $ret = array(); + + $i = 0; + foreach ($errors as $key => $err) { + $ret[$key] = sprintf( + $format, ++$i, $err[0], $err[1], $err[2], $err[3] + ); + } + + return $ret; + } +} diff --git a/tests/Lexer/ContextTest.php b/tests/Lexer/ContextTest.php index 50e500a..dca3dec 100644 --- a/tests/Lexer/ContextTest.php +++ b/tests/Lexer/ContextTest.php @@ -30,7 +30,7 @@ class ContextTest extends TestCase /** * @expectedException Exception - * @expectedExceptionMessage Specified context ("\SqlParser\Contexts\ContextFoo") doesn't exist. + * @expectedExceptionMessage Specified context ("\SqlParser\Contexts\ContextFoo") does not exist. */ public function testLoadError() { diff --git a/tests/Utils/ErrorTest.php b/tests/Utils/ErrorTest.php new file mode 100644 index 0000000..25e65f4 --- /dev/null +++ b/tests/Utils/ErrorTest.php @@ -0,0 +1,34 @@ +<?php + +namespace SqlParser\Tests\Utils; + +use SqlParser\Lexer; +use SqlParser\Parser; +use SqlParser\Utils\Error; + +use SqlParser\Tests\TestCase; + +class ErrorTest extends TestCase +{ + + public function testGet() + { + $lexer = new Lexer('SELECT * FROM db..tbl $'); + $parser = new Parser($lexer->list); + $this->assertEquals( + array( + array('Unexpected character.', 0, '$', 22), + array('Unexpected dot.', 0, '.', 17), + ), + Error::get(array($lexer, $parser)) + ); + } + + public function testFormat() + { + $this->assertEquals( + array('#1: error msg (near "token" at position 100)'), + Error::format(array(array('error msg', 42, 'token', 100))) + ); + } +} |