packages/sql-semantics-mysql/src/Rules/Noise/LeafNoise.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Rules\Noise;
6
7/**
8 * The noise and synonym token positions of the shared leaf productions: names, optional keywords and variables.
9 *
10 * Only a token with no influence on meaning in its production may be listed
11 * as noise, and only terminals the manual defines as synonyms may share a
12 * key. Every entry is listed in the method documentation with its reason and
13 * the manual page that states it.
14 *
15 * @visibility SqlSemantics\Platform\MySql
16 */
17final class LeafNoise
18{
19    /**
20     * Answers the noise positions by production signature.
21     *
22     * - `opt_as: AS`: the keyword before the query of CREATE TABLE ... SELECT
23     *   (5.6, 5.7) is optional and changes nothing
24     *   (https://dev.mysql.com/doc/refman/5.7/en/create-table-select.html: "[AS] query_expression").
25     *   Before a table alias (8.0 and later) the model keeps the keyword as
26     *   written (AliasMark), since it can be part of the text MySQL names an
27     *   unaliased select list expression after.
28     * - `equal: EQ`, `equal: SET_VAR`: an option is written `name [=] value`, and
29     *   in SET and UPDATE assignments `:=` and `=` are the same assignment
30     *   operator; where the rule is mandatory the token cannot be left out, so
31     *   dropping its key loses nothing
32     *   (https://dev.mysql.com/doc/refman/8.4/en/create-table.html,
33     *   https://dev.mysql.com/doc/refman/8.4/en/assignment-operators.html).
34     * - `opt_default: DEFAULT`, `opt_default: DEFAULT_SYM`: `[DEFAULT] CHARACTER
35     *   SET`, `[DEFAULT] COLLATE` and `[DEFAULT] ENCRYPTION` are the same option
36     *   with or without the keyword
37     *   (https://dev.mysql.com/doc/refman/8.4/en/create-database.html,
38     *   https://dev.mysql.com/doc/refman/8.4/en/create-table.html).
39     * - `opt_wild: . *`: `tbl_name.*` in a multiple-table DELETE is the table
40     *   name; the suffix is accepted for compatibility with Access
41     *   (https://dev.mysql.com/doc/refman/8.4/en/delete.html).
42     * - `opt_comma: ,`: options in an option list are separated by an optional
43     *   comma (https://dev.mysql.com/doc/refman/8.4/en/create-table.html:
44     *   "table_option [[,] table_option]").
45     * - `opt_storage: STORAGE_SYM`: `[STORAGE] ENGINE` and `SHOW [STORAGE]
46     *   ENGINES` (https://dev.mysql.com/doc/refman/8.4/en/show-engines.html,
47     *   https://dev.mysql.com/doc/refman/8.4/en/create-tablespace.html).
48     * - `opt_table: TABLE_SYM`: `TRUNCATE [TABLE] tbl_name`, and TABLE is the
49     *   default object type of GRANT and REVOKE
50     *   (https://dev.mysql.com/doc/refman/8.4/en/truncate-table.html,
51     *   https://dev.mysql.com/doc/refman/8.4/en/grant.html).
52     * - `table_ident: . ident` and `field_ident: . ident`, position 0: a
53     *   leading dot names the default database, as an unqualified name does
54     *   (https://dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html).
55     *   A table of a FROM clause and a column of an expression keep the dot
56     *   (TableReference, ColumnUse), since it is part of the text MySQL names
57     *   an unaliased select list expression after.
58     * - `charset: CHAR_SYM SET` and `character_set: CHAR_SYM SET_SYM`, position
59     *   1: with the synonym key of position 0, the two keywords are the one
60     *   keyword CHARSET (https://dev.mysql.com/doc/refman/8.4/en/create-table.html:
61     *   "CHARSET is a synonym for CHARACTER SET"). The character set attribute
62     *   of a type keeps the two-word form (CharsetForm::CharacterSet), since a
63     *   cast target is part of the text MySQL names an unaliased select list
64     *   expression after.
65     *
66     * @return array<string, list<int>>
67     */
68    public static function positions(): array
69    {
70        return [
71            'opt_as: AS' => [0],
72            'equal: EQ' => [0],
73            'equal: SET_VAR' => [0],
74            'opt_default: DEFAULT' => [0],
75            'opt_default: DEFAULT_SYM' => [0],
76            'opt_wild: . *' => [0, 1],
77            'opt_comma: ,' => [0],
78            'opt_storage: STORAGE_SYM' => [0],
79            'opt_table: TABLE_SYM' => [0],
80            'table_ident: . ident' => [0],
81            'field_ident: . ident' => [0],
82            'charset: CHAR_SYM SET' => [1],
83            'character_set: CHAR_SYM SET_SYM' => [1],
84        ];
85    }
86
87    /**
88     * Answers the key of each synonym position by production signature.
89     *
90     * - `charset: CHAR_SYM SET`, `character_set: CHAR_SYM SET_SYM`: CHARACTER SET
91     *   is CHARSET (https://dev.mysql.com/doc/refman/8.4/en/create-table.html).
92     * - `key_or_index: KEY_SYM`: KEY is a synonym for INDEX
93     *   (https://dev.mysql.com/doc/refman/8.4/en/create-table.html: "KEY is
94     *   normally a synonym for INDEX").
95     * - `keys_or_index: KEYS`, `keys_or_index: INDEX_SYM`: SHOW INDEX, SHOW
96     *   INDEXES and SHOW KEYS are one statement
97     *   (https://dev.mysql.com/doc/refman/8.4/en/show-index.html).
98     * - `table_or_tables: TABLES`: `{TABLE | TABLES}`
99     *   (https://dev.mysql.com/doc/refman/8.4/en/lock-tables.html,
100     *   https://dev.mysql.com/doc/refman/8.4/en/flush.html).
101     * - `not: NOT2_SYM`: under HIGH_NOT_PRECEDENCE the lexer reads every NOT as
102     *   this terminal; outside an expression, as in IF NOT EXISTS and NOT NULL,
103     *   it is the keyword NOT
104     *   (https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_high_not_precedence).
105     * - `opt_no_write_to_binlog: LOCAL_SYM`: LOCAL is an alias of
106     *   NO_WRITE_TO_BINLOG (https://dev.mysql.com/doc/refman/8.4/en/analyze-table.html).
107     * - the `LOCAL_SYM .` alternatives of the scope rules: LOCAL is a synonym of
108     *   SESSION (https://dev.mysql.com/doc/refman/8.4/en/using-system-variables.html).
109     * - `lvalue_variable: DEFAULT_SYM . ident`: `DEFAULT.name` names the
110     *   instance `default` of a structured system variable
111     *   (https://dev.mysql.com/doc/refman/8.4/en/structured-system-variables.html).
112     * - the BINARY alternatives of the character set and collation name rules:
113     *   the keyword is the name `binary`
114     *   (https://dev.mysql.com/doc/refman/8.4/en/charset-binary-set.html).
115     *
116     * @return array<string, array<int, string>>
117     */
118    public static function synonyms(): array
119    {
120        return [
121            'charset: CHAR_SYM SET' => [0 => 'CHARSET'],
122            'character_set: CHAR_SYM SET_SYM' => [0 => 'CHARSET'],
123            'key_or_index: KEY_SYM' => [0 => 'INDEX_SYM'],
124            'keys_or_index: KEYS' => [0 => 'INDEXES'],
125            'keys_or_index: INDEX_SYM' => [0 => 'INDEXES'],
126            'table_or_tables: TABLES' => [0 => 'TABLE_SYM'],
127            'not: NOT2_SYM' => [0 => 'NOT_SYM'],
128            'opt_no_write_to_binlog: LOCAL_SYM' => [0 => 'NO_WRITE_TO_BINLOG'],
129            'opt_var_ident_type: LOCAL_SYM .' => [0 => 'SESSION_SYM'],
130            'opt_rvalue_system_variable_type: LOCAL_SYM .' => [0 => 'SESSION_SYM'],
131            'opt_set_var_ident_type: LOCAL_SYM .' => [0 => 'SESSION_SYM'],
132            'lvalue_variable: DEFAULT_SYM . ident' => [0 => 'name:default'],
133            'charset_name: BINARY' => [0 => 'name:binary'],
134            'charset_name: BINARY_SYM' => [0 => 'name:binary'],
135            'old_or_new_charset_name: BINARY' => [0 => 'name:binary'],
136            'old_or_new_charset_name: BINARY_SYM' => [0 => 'name:binary'],
137            'collation_name: BINARY_SYM' => [0 => 'name:binary'],
138        ];
139    }
140}
141