diff options
author | Dan Ungureanu <udan1107@gmail.com> | 2015-07-15 00:19:14 +0300 |
---|---|---|
committer | Dan Ungureanu <udan1107@gmail.com> | 2015-07-15 00:19:14 +0300 |
commit | d8c6b9cc361a610ef04df9d86b61aabbb68eacb3 (patch) | |
tree | d9e6ab71497b777fc58fc1715315e56ccd934684 /src/Components/Array2d.php | |
parent | 380603a8bfebd612bbc70801d99eb727be3e36ce (diff) | |
download | sql-parser-d8c6b9cc361a610ef04df9d86b61aabbb68eacb3.zip sql-parser-d8c6b9cc361a610ef04df9d86b61aabbb68eacb3.tar.gz sql-parser-d8c6b9cc361a610ef04df9d86b61aabbb68eacb3.tar.bz2 |
Improved error messages.
Achieved 100% code coverage.
Some refactoring.
Diffstat (limited to 'src/Components/Array2d.php')
-rw-r--r-- | src/Components/Array2d.php | 85 |
1 files changed, 40 insertions, 45 deletions
diff --git a/src/Components/Array2d.php b/src/Components/Array2d.php index c521de2..90be806 100644 --- a/src/Components/Array2d.php +++ b/src/Components/Array2d.php @@ -26,39 +26,38 @@ class Array2d extends Component { /** - * An array with the values of the row to be inserted. - * - * @var array - */ - public $values; - - /** * @param Parser $parser The parser that serves as context. * @param TokensList $list The list of tokens that are being parsed. * @param array $options Parameters for parsing. * - * @return Array2d + * @return ArrayObj[] */ public static function parse(Parser $parser, TokensList $list, array $options = array()) { $ret = array(); - $expr = new Array2d(); - $value = ''; + /** + * Whether an array was parsed or not. To be a valid parsing, at least + * one array must be parsed after each comma. + * @var bool $parsed + */ + $parsed = false; + + /** + * The number of values in each set. + * @var int + */ + $count = -1; /** * The state of the parser. * * Below are the states of the parser. * - * 0 ------------------------[ ( ]-----------------------> 1 + * 0 ----------------------[ array ]---------------------> 1 * - * 1 ----------------------[ value ]---------------------> 2 - * - * 2 ------------------------[ , ]-----------------------> 1 - * 2 ------------------------[ ) ]-----------------------> 3 - * - * 3 ---------------------[ options ]--------------------> 4 + * 1 ------------------------[ , ]------------------------> 0 + * 1 -----------------------[ else ]----------------------> -1 * * @var int */ @@ -86,40 +85,36 @@ class Array2d extends Component break; } - if ($token->type === Token::TYPE_OPERATOR) { + if ($state === 0) { if ($token->value === '(') { - $state = 1; - continue; - } elseif ($token->value === ',') { - if ($state !== 3) { - $expr->values[] = $value; - $value = ''; - $state = 1; + $arr = ArrayObj::parse($parser, $list, $options); + $arrCount = count($arr->values); + if ($count === -1) { + $count = $arrCount; + } elseif ($arrCount != $count) { + $parser->error("Expected {$count} values, found {$arrCount}.", $token); } - continue; - } elseif ($token->value === ')') { - $state = 3; - $expr->values[] = $value; - $ret[] = $expr; - $value = ''; - $expr = new Array2d(); - continue; + $ret[] = $arr; + $parsed = true; + $state = 1; + } else { + break; + } + } elseif ($state === 1) { + if ($token->value === ',') { + $parsed = false; + $state = 0; + } else { + break; } - - // No other operator is expected. - break; - } - - if ($state === 1) { - $value .= $token->value; - $state = 2; } - } - // Last iteration was not saved. - if (!empty($expr->values)) { - $ret[] = $expr; + if (!$parsed) { + $parser->error( + "Expected open bracket followed by a set of values.", + $list->tokens[$list->idx] + ); } --$list->idx; |