Skip to content

Commit a870bca

Browse files
Merge pull request #514 from MauricioFauth/lexer-parse-method
Add Lexer::parse method to explicit call parser methods
2 parents 61baa39 + 147f470 commit a870bca

File tree

2 files changed

+39
-104
lines changed

2 files changed

+39
-104
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -587,49 +587,11 @@
587587
<code><![CDATA[$this->last]]></code>
588588
<code><![CDATA[$this->last]]></code>
589589
</LoopInvalidation>
590-
<MixedAssignment>
591-
<code>$lastToken</code>
592-
<code>$token</code>
593-
</MixedAssignment>
594590
<MixedOperand>
595-
<code><![CDATA[$lastToken->flags]]></code>
596-
<code><![CDATA[$lastToken->token]]></code>
597591
<code><![CDATA[$lastToken->value]]></code>
598592
<code><![CDATA[$this->str[$this->last]]]></code>
599-
<code><![CDATA[$token->flags]]></code>
600593
<code><![CDATA[$token->value]]></code>
601594
</MixedOperand>
602-
<MixedPropertyAssignment>
603-
<code>$lastToken</code>
604-
<code>$lastToken</code>
605-
<code>$lastToken</code>
606-
<code>$lastToken</code>
607-
<code>$token</code>
608-
<code>$token</code>
609-
<code>$token</code>
610-
<code>$token</code>
611-
</MixedPropertyAssignment>
612-
<MixedPropertyFetch>
613-
<code><![CDATA[$lastToken->flags]]></code>
614-
<code><![CDATA[$lastToken->token]]></code>
615-
<code><![CDATA[$lastToken->type]]></code>
616-
<code><![CDATA[$lastToken->type]]></code>
617-
<code><![CDATA[$lastToken->value]]></code>
618-
<code><![CDATA[$lastToken->value]]></code>
619-
<code><![CDATA[$token->flags]]></code>
620-
<code><![CDATA[$token->token]]></code>
621-
<code><![CDATA[$token->token]]></code>
622-
<code><![CDATA[$token->type]]></code>
623-
<code><![CDATA[$token->type]]></code>
624-
<code><![CDATA[$token->type]]></code>
625-
<code><![CDATA[$token->value]]></code>
626-
<code><![CDATA[$token->value]]></code>
627-
</MixedPropertyFetch>
628-
<MixedPropertyTypeCoercion>
629-
<code><![CDATA[$list->tokens]]></code>
630-
<code><![CDATA[$list->tokens]]></code>
631-
<code><![CDATA[$list->tokens]]></code>
632-
</MixedPropertyTypeCoercion>
633595
<NullArgument>
634596
<code>null</code>
635597
</NullArgument>
@@ -690,16 +652,6 @@
690652
<code><![CDATA[$next->value]]></code>
691653
<code><![CDATA[$next->value]]></code>
692654
</PossiblyNullPropertyFetch>
693-
<PossiblyUnusedMethod>
694-
<code>parseBool</code>
695-
<code>parseComment</code>
696-
<code>parseDelimiter</code>
697-
<code>parseKeyword</code>
698-
<code>parseLabel</code>
699-
<code>parseNumber</code>
700-
<code>parseOperator</code>
701-
<code>parseSymbol</code>
702-
</PossiblyUnusedMethod>
703655
</file>
704656
<file src="src/Parser.php">
705657
<InvalidPropertyAssignmentValue>

src/Lexer.php

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,6 @@
2828
*/
2929
class Lexer extends Core
3030
{
31-
/**
32-
* A list of methods that are used in lexing the SQL query.
33-
*/
34-
private const PARSER_METHODS = [
35-
// It is best to put the parsers in order of their complexity
36-
// (ascending) and their occurrence rate (descending).
37-
//
38-
// Conflicts:
39-
//
40-
// 1. `parseDelimiter`, `parseUnknown`, `parseKeyword`, `parseNumber`
41-
// They fight over delimiter. The delimiter may be a keyword, a
42-
// number or almost any character which makes the delimiter one of
43-
// the first tokens that must be parsed.
44-
//
45-
// 1. `parseNumber` and `parseOperator`
46-
// They fight over `+` and `-`.
47-
//
48-
// 2. `parseComment` and `parseOperator`
49-
// They fight over `/` (as in ```/*comment*/``` or ```a / b```)
50-
//
51-
// 3. `parseBool` and `parseKeyword`
52-
// They fight over `TRUE` and `FALSE`.
53-
//
54-
// 4. `parseKeyword` and `parseUnknown`
55-
// They fight over words. `parseUnknown` does not know about
56-
// keywords.
57-
58-
'parseDelimiter',
59-
'parseWhitespace',
60-
'parseNumber',
61-
'parseComment',
62-
'parseOperator',
63-
'parseBool',
64-
'parseString',
65-
'parseSymbol',
66-
'parseKeyword',
67-
'parseLabel',
68-
'parseUnknown',
69-
];
70-
7131
/**
7232
* A list of keywords that indicate that the function keyword
7333
* is not used as a function
@@ -203,26 +163,11 @@ public function lex(): void
203163

204164
/**
205165
* Last processed token.
206-
*
207-
* @var Token
208166
*/
209167
$lastToken = null;
210168

211169
for ($this->last = 0, $lastIdx = 0; $this->last < $this->len; $lastIdx = ++$this->last) {
212-
/**
213-
* The new token.
214-
*
215-
* @var Token
216-
*/
217-
$token = null;
218-
219-
foreach (self::PARSER_METHODS as $method) {
220-
$token = $this->$method();
221-
222-
if ($token) {
223-
break;
224-
}
225-
}
170+
$token = $this->parse();
226171

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

1044989
return new Token($this->delimiter, TokenType::Delimiter);
1045990
}
991+
992+
private function parse(): Token|null
993+
{
994+
// It is best to put the parsers in order of their complexity
995+
// (ascending) and their occurrence rate (descending).
996+
//
997+
// Conflicts:
998+
//
999+
// 1. `parseDelimiter`, `parseUnknown`, `parseKeyword`, `parseNumber`
1000+
// They fight over delimiter. The delimiter may be a keyword, a
1001+
// number or almost any character which makes the delimiter one of
1002+
// the first tokens that must be parsed.
1003+
//
1004+
// 1. `parseNumber` and `parseOperator`
1005+
// They fight over `+` and `-`.
1006+
//
1007+
// 2. `parseComment` and `parseOperator`
1008+
// They fight over `/` (as in ```/*comment*/``` or ```a / b```)
1009+
//
1010+
// 3. `parseBool` and `parseKeyword`
1011+
// They fight over `TRUE` and `FALSE`.
1012+
//
1013+
// 4. `parseKeyword` and `parseUnknown`
1014+
// They fight over words. `parseUnknown` does not know about
1015+
// keywords.
1016+
1017+
return $this->parseDelimiter()
1018+
?? $this->parseWhitespace()
1019+
?? $this->parseNumber()
1020+
?? $this->parseComment()
1021+
?? $this->parseOperator()
1022+
?? $this->parseBool()
1023+
?? $this->parseString()
1024+
?? $this->parseSymbol()
1025+
?? $this->parseKeyword()
1026+
?? $this->parseLabel()
1027+
?? $this->parseUnknown();
1028+
}
10461029
}

0 commit comments

Comments
 (0)