Skip to content

Commit

Permalink
Enabled lint for console.
Browse files Browse the repository at this point in the history
Updated sql-parser library to phpmyadmin/sql-parser@5e554b3.

Signed-off-by: Dan Ungureanu <udan1107@gmail.com>
  • Loading branch information
Dan Ungureanu committed Jul 19, 2015
1 parent 8a9a6fd commit d1f4942
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 44 deletions.
14 changes: 12 additions & 2 deletions js/console.js
Expand Up @@ -553,7 +553,12 @@ var PMA_consoleInput = {
mode: 'text/x-sql',
lineWrapping: true,
extraKeys: {"Ctrl-Space": "autocomplete"},
hintOptions: {"completeSingle": false, "completeOnSingleClick": true}
hintOptions: {"completeSingle": false, "completeOnSingleClick": true},
gutters: ["CodeMirror-lint-markers"],
lint: {
"getAnnotations": CodeMirror.sqlLint,
"async": true,
}
});
PMA_consoleInput._inputs.console.on("inputRead", codemirrorAutocompleteOnInputRead);
PMA_consoleInput._inputs.console.on("keydown", function(instance, event) {
Expand All @@ -565,7 +570,12 @@ var PMA_consoleInput = {
mode: 'text/x-sql',
lineWrapping: true,
extraKeys: {"Ctrl-Space": "autocomplete"},
hintOptions: {"completeSingle": false, "completeOnSingleClick": true}
hintOptions: {"completeSingle": false, "completeOnSingleClick": true},
gutters: ["CodeMirror-lint-markers"],
lint: {
"getAnnotations": CodeMirror.sqlLint,
"async": true,
}
});
PMA_consoleInput._inputs.bookmark.on("inputRead", codemirrorAutocompleteOnInputRead);
}
Expand Down
11 changes: 1 addition & 10 deletions libraries/sql-parser/src/Components/Array2d.php
Expand Up @@ -36,13 +36,6 @@ public static function parse(Parser $parser, TokensList $list, array $options =
{
$ret = array();

/**
* 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
Expand Down Expand Up @@ -95,22 +88,20 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$parser->error("{$count} values were expected, but found {$arrCount}.", $token);
}
$ret[] = $arr;
$parsed = true;
$state = 1;
} else {
break;
}
} elseif ($state === 1) {
if ($token->value === ',') {
$parsed = false;
$state = 0;
} else {
break;
}
}
}

if (!$parsed) {
if ($state === 0) {
$parser->error(
'An opening bracket followed by a set of values was expected.',
$list->tokens[$list->idx]
Expand Down
18 changes: 16 additions & 2 deletions libraries/sql-parser/src/Components/Expression.php
Expand Up @@ -239,6 +239,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =

if ($alias) {
// An alias is expected (the keyword `AS` was previously found).
if (!empty($ret->alias)) {
$parser->error('An alias was previously found.', $token);
}
$ret->alias = $token->value;
$alias = 0;
} else {
Expand All @@ -265,12 +268,18 @@ public static function parse(Parser $parser, TokensList $list, array $options =
break;
}

// Parsing aliases without `AS` keyword and any whitespace.
// Parsing aliases without `AS` keyword and any
// whitespace.
// Example: SELECT 1`foo`
if (($token->type === Token::TYPE_STRING)
|| (($token->type === Token::TYPE_SYMBOL)
&& ($token->flags & Token::FLAG_SYMBOL_BACKTICK))
) {
if (!empty($ret->alias)) {
$parser->error(
'An alias was previously found.', $token
);
}
$ret->alias = $token->value;
}
} else {
Expand All @@ -285,6 +294,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =
if (($token->type === Token::TYPE_NONE) || ($token->type === Token::TYPE_STRING)
|| (($token->type === Token::TYPE_SYMBOL) && ($token->flags & Token::FLAG_SYMBOL_BACKTICK))
) {
if (!empty($ret->alias)) {
$parser->error(
'An alias was previously found.', $token
);
}
$ret->alias = $token->value;
continue;
}
Expand All @@ -301,8 +315,8 @@ public static function parse(Parser $parser, TokensList $list, array $options =
} else {
$prev = null;
}

}

if ($alias === 2) {
$parser->error('An alias was expected.', $list->tokens[$list->idx - 1]);
}
Expand Down
35 changes: 28 additions & 7 deletions libraries/sql-parser/src/Components/ExpressionArray.php
Expand Up @@ -38,6 +38,20 @@ public static function parse(Parser $parser, TokensList $list, array $options =

$expr = null;

/**
* The state of the parser.
*
* Below are the states of the parser.
*
* 0 ----------------------[ array ]---------------------> 1
*
* 1 ------------------------[ , ]------------------------> 0
* 1 -----------------------[ else ]----------------------> -1
*
* @var int
*/
$state = 0;

for (; $list->idx < $list->count; ++$list->idx) {
/**
* Token parsed at this moment.
Expand All @@ -60,20 +74,27 @@ public static function parse(Parser $parser, TokensList $list, array $options =
break;
}

if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
$ret[] = $expr;
} else {
if ($state === 0) {
$expr = Expression::parse($parser, $list, $options);
if ($expr === null) {
break;
}
$ret[] = $expr;
$state = 1;
} elseif ($state === 1) {
if ($token->value === ',') {
$state = 0;
} else {
break;
}
}

}

// Last iteration was not processed.
if ($expr !== null) {
$ret[] = $expr;
if ($state === 0) {
$parser->error(
'An expression was expected.',
$list->tokens[$list->idx]
);
}

--$list->idx;
Expand Down
12 changes: 1 addition & 11 deletions libraries/sql-parser/src/Components/RenameOperation.php
Expand Up @@ -52,13 +52,6 @@ public static function parse(Parser $parser, TokensList $list, array $options =

$expr = new RenameOperation();

/**
* Whether an operation was parsed or not. To be a valid parsing, at
* least one operation must be parsed after each comma.
* @var bool $parsed
*/
$parsed = false;

/**
* The state of the parser.
*
Expand Down Expand Up @@ -129,21 +122,18 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$parser->error('The new name of the table was expected.', $token);
}
$state = 3;
$parsed = true;
} elseif ($state === 3) {
if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
$ret[] = $expr;
$expr = new RenameOperation();
$state = 0;
// Found a comma, looking for another operation.
$parsed = false;
} else {
break;
}
}
}

if (!$parsed) {
if ($state !== 3) {
$parser->error('A rename operation was expected.', $list->tokens[$list->idx - 1]);
}

Expand Down
28 changes: 16 additions & 12 deletions libraries/sql-parser/src/Parser.php
Expand Up @@ -327,6 +327,13 @@ public function parse()
// Statements can start with keywords only.
// Comments, whitespaces, etc. are ignored.
if ($token->type !== Token::TYPE_KEYWORD) {
if (($token->type !== TOKEN::TYPE_COMMENT)
&& ($token->type !== Token::TYPE_WHITESPACE)
&& ($token->type !== Token::TYPE_OPERATOR) // `(` and `)`
&& ($token->type !== Token::TYPE_DELIMITER)
) {
$this->error('Unexpected beginning of statement.', $token);
}
continue;
}

Expand All @@ -337,10 +344,7 @@ public function parse()

// Checking if it is a known statement that can be parsed.
if (empty(static::$STATEMENT_PARSERS[$token->value])) {
$this->error(
'Unrecognized statement type.',
$token
);
$this->error('Unrecognized statement type.', $token);
// Skipping to the end of this statement.
$list->getNextOfType(Token::TYPE_DELIMITER);
//
Expand All @@ -356,35 +360,35 @@ public function parse()

/**
* Processed statement.
* @var Statement $stmt
* @var Statement $statement
*/
$stmt = new $class($this, $this->list);
$statement = new $class($this, $this->list);

// The first token that is a part of this token is the next token
// unprocessed by the previous statement.
// There might be brackets around statements and this shouldn't
// affect the parser
$stmt->first = $prevLastIdx + 1;
$statement->first = $prevLastIdx + 1;

// Storing the index of the last token parsed and updating the old
// index.
$stmt->last = $list->idx;
$statement->last = $list->idx;
$prevLastIdx = $list->idx;

// Finally, storing the statement.
if (($inUnion)
&& ($lastStatement instanceof SelectStatement)
&& ($stmt instanceof SelectStatement)
&& ($statement instanceof SelectStatement)
) {
/**
* Last SELECT statement.
* @var SelectStatement $lastStatement
*/
$lastStatement->union[] = $stmt;
$lastStatement->union[] = $statement;
$inUnion = false;
} else {
$this->statements[] = $stmt;
$lastStatement = $stmt;
$this->statements[] = $statement;
$lastStatement = $statement;
}

}
Expand Down
5 changes: 5 additions & 0 deletions libraries/sql-parser/src/Statement.php
Expand Up @@ -195,6 +195,11 @@ public function parse(Parser $parser, TokensList $list)
// Only keywords are relevant here. Other parts of the query are
// processed in the functions below.
if ($token->type !== Token::TYPE_KEYWORD) {
if (($token->type !== TOKEN::TYPE_COMMENT)
&& ($token->type !== Token::TYPE_WHITESPACE)
) {
$parser->error('Unexpected token.', $token);
}
continue;
}

Expand Down

0 comments on commit d1f4942

Please sign in to comment.