packages/sql-parser/src/MySql/Lexer/MySqlLexer.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlParser\MySql\Lexer;
6
7use SqlParser\Lexer\Cursor;
8use SqlParser\Lexer\Lexeme;
9use SqlParser\Lexer\LexicalException;
10use SqlParser\Lexer\ParameterSyntax;
11use SqlParser\MySql\MySqlVersion;
12use SqlParser\MySql\SqlMode;
13
14/**
15 * Reads MySQL text into the terminals of its grammar, as `sql_lex.cc` does.
16 *
17 * The lexer is a port of MySQL's own state machine: the state a token leaves
18 * behind decides how the next one is read, `WITH ROLLUP` is one token, and
19 * the input ends with an explicit end-of-input terminal.
20 *
21 * @visibility root
22 */
23final class MySqlLexer
24{
25 /**
26 * @param KeywordTable $keywords Keywords of the release
27 * @param MySqlVersion $version Release the text is read for
28 * @param SqlMode $mode Mode the text is read under
29 * @param TriviaScanner $trivia Skips whitespace and comments
30 * @param QuotedScanner $quoted Reads quoted tokens
31 * @param NumberScanner $numbers Reads numbers
32 * @param WordScanner $words Reads words
33 * @param OperatorScanner $operators Reads operators
34 * @param VariableScanner $variables Reads what follows an at sign
35 * @param ParameterSyntax $parameters Which parameter markers are read
36 */
37 public function __construct(
38 private readonly KeywordTable $keywords,
39 private readonly MySqlVersion $version,
40 private readonly SqlMode $mode = new SqlMode(),
41 private readonly TriviaScanner $trivia = new TriviaScanner(),
42 private readonly QuotedScanner $quoted = new QuotedScanner(),
43 private readonly NumberScanner $numbers = new NumberScanner(),
44 private readonly WordScanner $words = new WordScanner(),
45 private readonly OperatorScanner $operators = new OperatorScanner(),
46 private readonly VariableScanner $variables = new VariableScanner(),
47 private readonly ParameterSyntax $parameters = ParameterSyntax::Native,
48 ) {
49 }
50
51 /**
52 * Reads SQL text into lexemes, the end-of-input terminal last.
53 *
54 * @param string $sql The SQL text
55 *
56 * @return list<Lexeme> The lexemes in text order
57 *
58 * @throws LexicalException When the text holds something no token starts with
59 */
60 public function scan(string $sql): array
61 {
62 $scan = new Scan(new Cursor($sql), $this->keywords, $this->mode, $this->version, $this->parameters);
63 $lexemes = [];
64 while (true) {
65 if ($scan->next === LexerState::Start) {
66 $this->trivia->skip($scan);
67 if ($scan->cursor->eof()) {
68 break;
69 }
70 }
71 $lexemes[] = $this->next($scan);
72 }
73 $lexemes[] = new Lexeme('END_OF_INPUT', '', strlen($sql));
74
75 return $this->merged($lexemes, $sql);
76 }
77
78 /**
79 * Reads one lexeme in the state the previous one left behind.
80 *
81 * @param Scan $scan The tokenization in progress
82 *
83 * @return Lexeme The lexeme
84 *
85 * @throws LexicalException When the text holds something no token starts with
86 */
87 public function next(Scan $scan): Lexeme
88 {
89 $state = $scan->next;
90 $scan->next = LexerState::Start;
91 $start = $scan->cursor->offset();
92
93 return match ($state) {
94 LexerState::Hostname => $this->variables->hostname($scan),
95 LexerState::SystemVariable => $this->variables->systemVariable($scan),
96 LexerState::IdentifierOrKeyword => $this->variables->identifierOrKeyword($scan, $this->words),
97 LexerState::IdentifierSeparator => $this->operators->separator($scan),
98 LexerState::IdentifierStart => $this->words->identifier($scan, $start),
99 LexerState::Start => $scan->cursor->peek() === '@'
100 ? $this->variables->at($scan)
101 : $this->quoted->scan($scan)
102 ?? $this->numbers->scan($scan, $this->words)
103 ?? $this->words->scan($scan)
104 ?? $this->operators->scan($scan),
105 };
106 }
107
108 /**
109 * Joins `WITH ROLLUP`, and `WITH CUBE` where the release has it, into one lexeme.
110 *
111 * @param list<Lexeme> $lexemes Lexemes as read
112 * @param string $sql The SQL text the joined lexeme spans
113 *
114 * @return list<Lexeme> Lexemes with the pairs joined
115 */
116 public function merged(array $lexemes, string $sql): array
117 {
118 $merged = [];
119 for ($index = 0, $count = count($lexemes); $index < $count; $index++) {
120 $lexeme = $lexemes[$index];
121 $following = $lexemes[$index + 1] ?? null;
122 if ($lexeme->name === 'WITH' && $following !== null) {
123 $joined = match (true) {
124 $following->name === 'ROLLUP_SYM' => 'WITH_ROLLUP_SYM',
125 $following->name === 'CUBE_SYM' && $this->version->mergesWithCube() => 'WITH_CUBE_SYM',
126 default => null,
127 };
128 if ($joined !== null) {
129 $merged[] = new Lexeme($joined, substr($sql, $lexeme->offset, $following->end() - $lexeme->offset), $lexeme->offset);
130 $index++;
131 continue;
132 }
133 }
134 $merged[] = $lexeme;
135 }
136
137 return $merged;
138 }
139}
140