Skip to content

Commit

Permalink
Merge pull request #514 from MauricioFauth/lexer-parse-method
Browse files Browse the repository at this point in the history
Add Lexer::parse method to explicit call parser methods
  • Loading branch information
MauricioFauth committed Sep 26, 2023
2 parents 61baa39 + 147f470 commit a870bca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 104 deletions.
48 changes: 0 additions & 48 deletions psalm-baseline.xml
Expand Up @@ -587,49 +587,11 @@
<code><![CDATA[$this->last]]></code>
<code><![CDATA[$this->last]]></code>
</LoopInvalidation>
<MixedAssignment>
<code>$lastToken</code>
<code>$token</code>
</MixedAssignment>
<MixedOperand>
<code><![CDATA[$lastToken->flags]]></code>
<code><![CDATA[$lastToken->token]]></code>
<code><![CDATA[$lastToken->value]]></code>
<code><![CDATA[$this->str[$this->last]]]></code>
<code><![CDATA[$token->flags]]></code>
<code><![CDATA[$token->value]]></code>
</MixedOperand>
<MixedPropertyAssignment>
<code>$lastToken</code>
<code>$lastToken</code>
<code>$lastToken</code>
<code>$lastToken</code>
<code>$token</code>
<code>$token</code>
<code>$token</code>
<code>$token</code>
</MixedPropertyAssignment>
<MixedPropertyFetch>
<code><![CDATA[$lastToken->flags]]></code>
<code><![CDATA[$lastToken->token]]></code>
<code><![CDATA[$lastToken->type]]></code>
<code><![CDATA[$lastToken->type]]></code>
<code><![CDATA[$lastToken->value]]></code>
<code><![CDATA[$lastToken->value]]></code>
<code><![CDATA[$token->flags]]></code>
<code><![CDATA[$token->token]]></code>
<code><![CDATA[$token->token]]></code>
<code><![CDATA[$token->type]]></code>
<code><![CDATA[$token->type]]></code>
<code><![CDATA[$token->type]]></code>
<code><![CDATA[$token->value]]></code>
<code><![CDATA[$token->value]]></code>
</MixedPropertyFetch>
<MixedPropertyTypeCoercion>
<code><![CDATA[$list->tokens]]></code>
<code><![CDATA[$list->tokens]]></code>
<code><![CDATA[$list->tokens]]></code>
</MixedPropertyTypeCoercion>
<NullArgument>
<code>null</code>
</NullArgument>
Expand Down Expand Up @@ -690,16 +652,6 @@
<code><![CDATA[$next->value]]></code>
<code><![CDATA[$next->value]]></code>
</PossiblyNullPropertyFetch>
<PossiblyUnusedMethod>
<code>parseBool</code>
<code>parseComment</code>
<code>parseDelimiter</code>
<code>parseKeyword</code>
<code>parseLabel</code>
<code>parseNumber</code>
<code>parseOperator</code>
<code>parseSymbol</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Parser.php">
<InvalidPropertyAssignmentValue>
Expand Down
95 changes: 39 additions & 56 deletions src/Lexer.php
Expand Up @@ -28,46 +28,6 @@
*/
class Lexer extends Core
{
/**
* A list of methods that are used in lexing the SQL query.
*/
private const PARSER_METHODS = [
// It is best to put the parsers in order of their complexity
// (ascending) and their occurrence rate (descending).
//
// Conflicts:
//
// 1. `parseDelimiter`, `parseUnknown`, `parseKeyword`, `parseNumber`
// They fight over delimiter. The delimiter may be a keyword, a
// number or almost any character which makes the delimiter one of
// the first tokens that must be parsed.
//
// 1. `parseNumber` and `parseOperator`
// They fight over `+` and `-`.
//
// 2. `parseComment` and `parseOperator`
// They fight over `/` (as in ```/*comment*/``` or ```a / b```)
//
// 3. `parseBool` and `parseKeyword`
// They fight over `TRUE` and `FALSE`.
//
// 4. `parseKeyword` and `parseUnknown`
// They fight over words. `parseUnknown` does not know about
// keywords.

'parseDelimiter',
'parseWhitespace',
'parseNumber',
'parseComment',
'parseOperator',
'parseBool',
'parseString',
'parseSymbol',
'parseKeyword',
'parseLabel',
'parseUnknown',
];

/**
* A list of keywords that indicate that the function keyword
* is not used as a function
Expand Down Expand Up @@ -203,26 +163,11 @@ public function lex(): void

/**
* Last processed token.
*
* @var Token
*/
$lastToken = null;

for ($this->last = 0, $lastIdx = 0; $this->last < $this->len; $lastIdx = ++$this->last) {
/**
* The new token.
*
* @var Token
*/
$token = null;

foreach (self::PARSER_METHODS as $method) {
$token = $this->$method();

if ($token) {
break;
}
}
$token = $this->parse();

if ($token === null) {
// @assert($this->last === $lastIdx);
Expand Down Expand Up @@ -1043,4 +988,42 @@ public function parseDelimiter(): Token|null

return new Token($this->delimiter, TokenType::Delimiter);
}

private function parse(): Token|null
{
// It is best to put the parsers in order of their complexity
// (ascending) and their occurrence rate (descending).
//
// Conflicts:
//
// 1. `parseDelimiter`, `parseUnknown`, `parseKeyword`, `parseNumber`
// They fight over delimiter. The delimiter may be a keyword, a
// number or almost any character which makes the delimiter one of
// the first tokens that must be parsed.
//
// 1. `parseNumber` and `parseOperator`
// They fight over `+` and `-`.
//
// 2. `parseComment` and `parseOperator`
// They fight over `/` (as in ```/*comment*/``` or ```a / b```)
//
// 3. `parseBool` and `parseKeyword`
// They fight over `TRUE` and `FALSE`.
//
// 4. `parseKeyword` and `parseUnknown`
// They fight over words. `parseUnknown` does not know about
// keywords.

return $this->parseDelimiter()
?? $this->parseWhitespace()
?? $this->parseNumber()
?? $this->parseComment()
?? $this->parseOperator()
?? $this->parseBool()
?? $this->parseString()
?? $this->parseSymbol()
?? $this->parseKeyword()
?? $this->parseLabel()
?? $this->parseUnknown();
}
}

0 comments on commit a870bca

Please sign in to comment.