summaryrefslogtreecommitdiffstats
path: root/src/Parser.php
blob: a5fe2335632187b768c5da705b48548f1f024589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php

namespace SqlParser;

use SqlParser\Exceptions\ParserException;

/**
 * Takes multiple tokens (contained in a Lexer instance) as input and builds a
 * parse tree.
 *
 * @category Parser
 * @package  SqlParser
 * @author   Dan Ungureanu <udan1107@gmail.com>
 * @license  http://opensource.org/licenses/GPL-2.0 GNU Public License
 */
class Parser
{

    /**
     * Array of classes that are used in parsing the SQL statements.
     *
     * @var array
     */
    public static $STATEMENT_PARSERS = array(

        // Data Definition Statements.
        // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-definition.html
        'ALTER'         => '',
        'CREATE'        => 'SqlParser\\Statements\\CreateStatement',
        'DROP'          => '',
        'RENAME'        => 'SqlParser\\Statements\\RenameStatement',
        'TRUNCATE'      => '',

        // Data Manipulation Statements.
        // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-data-manipulation.html
        'CALL'          => 'SqlParser\\Statements\\CallStatement',
        'DELETE'        => 'SqlParser\\Statements\\DeleteStatement',
        'DO'            => '',
        'HANDLER'       => '',
        'INSERT'        => 'SqlParser\\Statements\\InsertStatement',
        'LOAD'          => '',
        'REPLACE'       => 'SqlParser\\Statements\\ReplaceStatement',
        'SELECT'        => 'SqlParser\\Statements\\SelectStatement',
        'UPDATE'        => 'SqlParser\\Statements\\UpdateStatement',

        // Prepared Statements.
        // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html
        'PREPARE'       => '',
        'EXECUTE'       => '',
    );

    /**
     * Array of classes that are used in parsing SQL fragments.
     *
     * @var array
     */
    public static $KEYWORD_PARSERS = array(

        'CALL'          => array(
            'class'     => 'SqlParser\\Fragments\\CallKeyword',
            'field'     => 'call',
        ),
        'FROM'          => array(
            'class'     => 'SqlParser\\Fragments\\FromKeyword',
            'field'     => 'from',
        ),
        'GROUP BY'      => array(
            'class'     => 'SqlParser\\Fragments\\OrderKeyword',
            'field'     => 'group',
        ),
        'HAVING'        => array(
            'class'     => 'SqlParser\\Fragments\\WhereKeyword',
            'field'     => 'having',
        ),
        'INTO'          => array(
            'class'     => 'SqlParser\\Fragments\\IntoKeyword',
            'field'     => 'into',
        ),
        'JOIN'          => array(
            'class'     => 'SqlParser\\Fragments\\JoinKeyword',
            'field'     => 'join',
        ),
        'LIMIT'         => array(
            'class'     => 'SqlParser\\Fragments\\LimitKeyword',
            'field'     => 'limit',
        ),
        'ORDER BY'      => array(
            'class'     => 'SqlParser\\Fragments\\OrderKeyword',
            'field'     => 'order',
        ),
        'PARTITION'     => array(
            'class'     => 'SqlParser\\Fragments\\ArrayFragment',
            'field'     => 'partition',
        ),
        'RENAME'        => array(
            'class'     => 'SqlParser\\Fragments\\RenameKeyword',
            'field'     => 'renames',
        ),
        'SET'           => array(
            'class'     => 'SqlParser\\Fragments\\SetKeyword',
            'field'     => 'set',
        ),
        'SELECT'        => array(
            'class'     => 'SqlParser\\Fragments\\SelectKeyword',
            'field'     => 'expr',
        ),
        'UPDATE'        => array(
            'class'     => 'SqlParser\\Fragments\\FromKeyword',
            'field'     => 'from',
        ),
        'VALUE'         => array(
            'class'     => 'SqlParser\\Fragments\\ValuesKeyword',
            'field'     => 'values',
        ),
        'VALUES'        => array(
            'class'     => 'SqlParser\\Fragments\\ValuesKeyword',
            'field'     => 'values',
        ),
        'WHERE'         => array(
            'class'     => 'SqlParser\\Fragments\\WhereKeyword',
            'field'     => 'where',
        ),

    );

    /**
     * The list of tokens that are parsed.
     *
     * @var TokensList
     */
    public $list;

    /**
     * Whether errors should throw exceptions or just be stored.
     *
     * @var bool
     *
     * @see static::$errors
     */
    public $strict = false;

    /**
     * List of errors that occured during parsing.
     *
     * Usually, the parsing does not stop once an error occured because that
     * error might be misdetected or a partial result (even a bad one) might be
     * needed.
     *
     * @var ParserException[]
     *
     * @see Parser::error()
     */
    public $errors = array();

    /**
     * List of statements parsed.
     *
     * @var Statement[]
     */
    public $statements = array();

    /**
     * Constructor.
     *
     * @param mixed $list   The list of tokens to be parsed.
     * @param bool  $strict Whether strict mode should be enabled or not.
     */
    public function __construct($list = null, $strict = false)
    {
        if ((is_string($list)) || ($list instanceof UtfString)) {
            $lexer = new Lexer($list, $strict);
            $this->list = $lexer->tokens;
        } elseif ($list instanceof TokensList) {
            $this->list = $list;
        }

        $this->strict = $strict;

        if ($list !== null) {
            $this->parse();
        }
    }

    /**
     * Builds the parse trees.
     *
     * @return void
     */
    public function parse()
    {
        $tokens = &$this->list->tokens;
        $count = &$this->list->count;
        $last = &$this->list->idx;

        for (; $last < $count; ++$last) {

            /**
             * Token parsed at this moment.
             * @var Token
             */
            $token = $tokens[$last];

            // Statements can start with keywords only.
            // Comments, whitespaces, etc. are ignored.
            if ($token->type !== Token::TYPE_KEYWORD) {
                continue;
            }

            // Checking if it is a known statement that can be parsed.
            if (empty(static::$STATEMENT_PARSERS[$token->value])) {
                $this->error(
                    'Unrecognized statement type "' . $token->value . '".',
                    $token
                );
                // TODO: Skip to first delimiter.
                continue;
            }

            /**
             * The name of the class that is used for parsing.
             * @var string
             */
            $class = static::$STATEMENT_PARSERS[$token->value];

            /**
             * Processed statement.
             * @var Statement
             */
            $stmt = new $class();

            $stmt->parse($this, $this->list);
            $this->statements[] = $stmt;
        }
    }

    /**
     * Creates a new error log.
     *
     * @param string $msg   The error message.
     * @param Token  $token The token that produced the error.
     * @param int    $code  The code of the error.
     */
    public function error($msg = '', Token $token = null, $code = 0)
    {
        $error = new ParserException($msg, $token, $code);
        if ($this->strict) {
            throw $error;
        }
        $this->errors[] = $error;
    }
}