Parser

An LL(*) recursive-descent parser for MySQL CREATE TABLE statements.

Parses a CREATE TABLE statement, reports any errors in it, and generates an AST.

Tags
todo

mark as internal/final

Table of Contents

Properties

$lexer  : Lexer
$statement  : string

Methods

__construct()  : mixed
Creates a new statement parser object.
createStatement()  : AbstractCreateStatement
CreateStatement ::= CREATE [TEMPORARY] TABLE Abstraction to allow for support of other schema objects like views in the future.
free()  : void
Frees this parser, enabling it to be reused.
getAST()  : AbstractCreateStatement
Parses and builds AST for the given Query.
getLexer()  : Lexer
Gets the lexer used by the parser.
indexName()  : Identifier
Return the name of an index. No name has been supplied if the next token is USING which defines the index type.
indexOptions()  : array<string|int, mixed>
IndexOptions ::= KEY_BLOCK_SIZE [=] value | index_type | WITH PARSER parser_name | COMMENT 'string'
indexType()  : string
IndexType ::= USING { BTREE | HASH }
match()  : mixed
Attempts to match the given token with the current lookahead token.
parse()  : array<string|int, Table>
Parses a statement string.
queryLanguage()  : AbstractCreateStatement
queryLanguage ::= CreateTableStatement
semanticalError()  : void
Generates a new semantical error.
syntaxError()  : void
Generates a new syntax error.
characterDataTypeOptions()  : array<string|int, mixed>
Parse common options for numeric datatypes
columnDataType()  : AbstractDataType
DataType ::= BIT[(length)] | TINYINT[(length)] [UNSIGNED] [ZEROFILL] | SMALLINT[(length)] [UNSIGNED] [ZEROFILL] | MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL] | INT[(length)] [UNSIGNED] [ZEROFILL] | INTEGER[(length)] [UNSIGNED] [ZEROFILL] | BIGINT[(length)] [UNSIGNED] [ZEROFILL] | REAL[(length,decimals)] [UNSIGNED] [ZEROFILL] | DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL] | FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL] | DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL] | NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL] | DATE | TIME[(fsp)] | TIMESTAMP[(fsp)] | DATETIME[(fsp)] | YEAR | CHAR[(length)] [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | VARCHAR(length) [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | BINARY[(length)] | VARBINARY(length) | TINYBLOB | BLOB | MEDIUMBLOB | LONGBLOB | TINYTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | MEDIUMTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | LONGTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | ENUM(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] | SET(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] | JSON
columnDefaultValue()  : mixed
DefaultValue::= DEFAULT default_value
createColumnDefinitionItem()  : CreateColumnDefinitionItem
CreateColumnDefinitionItem ::= col_name column_definition
createDefinition()  : CreateDefinition
Parses the table field/index definition
createDefinitionItem()  : AbstractCreateDefinitionItem
Parse the definition of a single column or index
createForeignKeyDefinitionItem()  : CreateForeignKeyDefinitionItem
Parses a foreign key definition item contained in the create definition
createIndexDefinitionItem()  : CreateIndexDefinitionItem
Parses an index definition item contained in the create definition
createTableClause()  : CreateTableClause
CreateTableClause ::= CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
createTableStatement()  : CreateTableStatement
CreateTableStatement ::= CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [tbl_options]
dataTypeLength()  : int
Determine length parameter of a column field definition, i.E. INT(11) or VARCHAR(255)
enumerationDataTypeOptions()  : array<string|int, mixed>
Parse shared options for enumeration datatypes (ENUM and SET)
fractionalSecondsPart()  : int
Determine the fractional seconds part support for TIME, DATETIME and TIMESTAMP columns
indexColumnName()  : IndexColumnName
IndexColumnName ::= col_name [(length)] [ASC | DESC]
numericDataTypeOptions()  : array<string|int, mixed>
Parse common options for numeric datatypes
peekBeyondClosingParenthesis()  : Token
Peeks beyond the matched closing parenthesis and returns the first token after that one.
referenceDefinition()  : ReferenceDefinition
ReferenceDefinition ::= REFERENCES tbl_name (index_col_name,...) [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE] [ON DELETE reference_option] [ON UPDATE reference_option]
referenceOption()  : string
ReferenceOption ::= RESTRICT | CASCADE | SET NULL | NO ACTION
schemaObjectName()  : Identifier
Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers.
tableOptions()  : array<string|int, mixed>
Parse MySQL table options
tableOptionValue()  : mixed
Return the value of an option, skipping the optional equal sign.
valueList()  : array<string|int, mixed>
Return all defined values for an enumeration datatype (ENUM, SET)
valueListItem()  : string
Return a value list item for an enumeration set

Properties

$statement

protected string $statement = ''

Methods

__construct()

Creates a new statement parser object.

public __construct(string $statement) : mixed
Parameters
$statement : string

The statement to parse.

free()

Frees this parser, enabling it to be reused.

public free([bool $deep = false ][, int $position = 0 ]) : void
Parameters
$deep : bool = false

Whether to clean peek and reset errors.

$position : int = 0

Position to reset.

getLexer()

Gets the lexer used by the parser.

public getLexer() : Lexer
Tags
todo

unused. drop after recheck.

Return values
Lexer

indexOptions()

IndexOptions ::= KEY_BLOCK_SIZE [=] value | index_type | WITH PARSER parser_name | COMMENT 'string'

public indexOptions() : array<string|int, mixed>
Tags
throws
StatementException
Return values
array<string|int, mixed>

match()

Attempts to match the given token with the current lookahead token.

public match(int $token) : mixed

If they match, updates the lookahead token; otherwise raises a syntax error.

Parameters
$token : int

The token type.

Tags
throws
StatementException

If the tokens don't match.

parse()

Parses a statement string.

public parse() : array<string|int, Table>
Tags
throws
SchemaException
throws
RuntimeException
throws
InvalidArgumentException
throws
StatementException
Return values
array<string|int, Table>

semanticalError()

Generates a new semantical error.

public semanticalError([string $message = '' ][, Token|null $token = null ]) : void
Parameters
$message : string = ''

Optional message.

$token : Token|null = null

Optional token.

Tags
throws
StatementException

syntaxError()

Generates a new syntax error.

public syntaxError([string $expected = '' ][, Token|null $token = null ]) : void
Parameters
$expected : string = ''

Expected string.

$token : Token|null = null

Got token.

Tags
throws
StatementException

characterDataTypeOptions()

Parse common options for numeric datatypes

protected characterDataTypeOptions() : array<string|int, mixed>
Tags
throws
StatementException
Return values
array<string|int, mixed>

columnDataType()

DataType ::= BIT[(length)] | TINYINT[(length)] [UNSIGNED] [ZEROFILL] | SMALLINT[(length)] [UNSIGNED] [ZEROFILL] | MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL] | INT[(length)] [UNSIGNED] [ZEROFILL] | INTEGER[(length)] [UNSIGNED] [ZEROFILL] | BIGINT[(length)] [UNSIGNED] [ZEROFILL] | REAL[(length,decimals)] [UNSIGNED] [ZEROFILL] | DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL] | FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL] | DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL] | NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL] | DATE | TIME[(fsp)] | TIMESTAMP[(fsp)] | DATETIME[(fsp)] | YEAR | CHAR[(length)] [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | VARCHAR(length) [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | BINARY[(length)] | VARBINARY(length) | TINYBLOB | BLOB | MEDIUMBLOB | LONGBLOB | TINYTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | MEDIUMTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | LONGTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] | ENUM(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] | SET(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] | JSON

protected columnDataType() : AbstractDataType
Tags
throws
StatementException
Return values
AbstractDataType

createColumnDefinitionItem()

CreateColumnDefinitionItem ::= col_name column_definition

protected createColumnDefinitionItem() : CreateColumnDefinitionItem

column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] [COMMENT 'string'] [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}] [STORAGE {DISK|MEMORY|DEFAULT}] [reference_definition]

Tags
throws
StatementException
Return values
CreateColumnDefinitionItem

createDefinition()

Parses the table field/index definition

protected createDefinition() : CreateDefinition

createDefinition ::= ( col_name column_definition | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) [index_option] ... | {INDEX|KEY} [index_name] [index_type] (index_col_name,...) [index_option] ... | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) [index_option] ... | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ... | [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition | CHECK (expr) )

Tags
throws
StatementException
Return values
CreateDefinition

dataTypeLength()

Determine length parameter of a column field definition, i.E. INT(11) or VARCHAR(255)

protected dataTypeLength([bool $required = false ]) : int
Parameters
$required : bool = false
Tags
throws
StatementException
Return values
int

enumerationDataTypeOptions()

Parse shared options for enumeration datatypes (ENUM and SET)

protected enumerationDataTypeOptions() : array<string|int, mixed>
Tags
throws
StatementException
Return values
array<string|int, mixed>

fractionalSecondsPart()

Determine the fractional seconds part support for TIME, DATETIME and TIMESTAMP columns

protected fractionalSecondsPart() : int
Tags
throws
StatementException
Return values
int

numericDataTypeOptions()

Parse common options for numeric datatypes

protected numericDataTypeOptions() : array<string|int, mixed>
Tags
throws
StatementException
Return values
array<string|int, mixed>

peekBeyondClosingParenthesis()

Peeks beyond the matched closing parenthesis and returns the first token after that one.

protected peekBeyondClosingParenthesis([bool $resetPeek = true ]) : Token
Parameters
$resetPeek : bool = true

Reset peek after finding the closing parenthesis.

Return values
Token

referenceOption()

ReferenceOption ::= RESTRICT | CASCADE | SET NULL | NO ACTION

protected referenceOption() : string
Tags
throws
StatementException
Return values
string

schemaObjectName()

Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers.

protected schemaObjectName() : Identifier
Tags
throws
StatementException
Return values
Identifier

tableOptions()

Parse MySQL table options

protected tableOptions() : array<string|int, mixed>

ENGINE [=] engine_name | AUTO_INCREMENT [=] value | AVG_ROW_LENGTH [=] value | [DEFAULT] CHARACTER SET [=] charset_name | CHECKSUM [=] {0 | 1} | [DEFAULT] COLLATE [=] collation_name | COMMENT [=] 'string' | COMPRESSION [=] {'ZLIB'|'LZ4'|'NONE'} | CONNECTION [=] 'connect_string' | DATA DIRECTORY [=] 'absolute path to directory' | DELAY_KEY_WRITE [=] {0 | 1} | ENCRYPTION [=] {'Y' | 'N'} | INDEX DIRECTORY [=] 'absolute path to directory' | INSERT_METHOD [=] { NO | FIRST | LAST } | KEY_BLOCK_SIZE [=] value | MAX_ROWS [=] value | MIN_ROWS [=] value | PACK_KEYS [=] {0 | 1 | DEFAULT} | PASSWORD [=] 'string' | ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT} | STATS_AUTO_RECALC [=] {DEFAULT|0|1} | STATS_PERSISTENT [=] {DEFAULT|0|1} | STATS_SAMPLE_PAGES [=] value | TABLESPACE tablespace_name | UNION [=] (tbl_name[,tbl_name]...)

Tags
throws
StatementException
Return values
array<string|int, mixed>

tableOptionValue()

Return the value of an option, skipping the optional equal sign.

protected tableOptionValue() : mixed
Tags
throws
StatementException

valueList()

Return all defined values for an enumeration datatype (ENUM, SET)

protected valueList() : array<string|int, mixed>
Tags
throws
StatementException
Return values
array<string|int, mixed>

valueListItem()

Return a value list item for an enumeration set

protected valueListItem() : string
Tags
throws
StatementException
Return values
string

        
On this page

Search results