Mysql Tokenizer
Splits strings into tokens using the syntax of Mysql 8.
const tokenize = ; const tokens = console;/* Output:["SELECT"," ","0",",","0x1",","," ","'2'",","," ","X'33'",";"]*/
Basic Usage
const tokenize = ; const tokens = ; console; /* Output:mysql: ["SELECT"," ","tbl",".","x","->","'$'",","," ","1","-","-","2","=","<=>","-","1"," ","FROM"," ","tbl",";"]*/
Comment Syntax
Comments are supported using --
, #
and /*...*/
syntaxes.
By default, comments using --
must start with a space, or come at the end of a line, as in Mysql.
const tokenize = ; const tokens = console;/* Output:["SELECT"," ","1","-","-","2","=","3"," ","-- One less minus two equals three"]*/
To implement the SQL standard, which does not require following whitespace,
include { regExp: require('mysql-tokenizer/lib/regexp-sql92') }
in the options.
Options
The following options are supported:
options.operators
: Array
Specify the list of valid operator tokens. If not specified, all operators from Mysql 8 are supported.
If this is the only option required, options can be specified as an Array.
Alternatively, specify options.regExp.Token.Operator
.
options.regExp.Initial
: Object
Override the parser's built-in regular expressions for character parsing.
Initial RegExps must start with ^
and match at most one character.
declare
options.regExp.Token
: Object
Token RegExps must have the 'g' and 'y' flags set.
Token.Operator
is optional. Without it, the parser treats punctuation as invalid characters.
declare
options.commentHandlers
: Object
Override the parser's built-in parsing of comments.
After a successful match on Token.StartComment
, the
matching string is used to look up a parsing function.
The default comment object is
comment = '/*': ParserprototypestarComment '--': ParserprototypesingleLineComment '#': ParserprototypesingleLineComment
Example: Comparing Mysql and SQL-92 Operator and Comment Parsing
const tokenizerFactory = ; const tokenizeMysql = ; const tokensMysql = ; console; /* Output:Mysql: ["SELECT"," ","tbl",".","x","->","'$'",","," ","-","1","<=>","1","-","-","2"," ","FROM"," ","tbl",";"] */ const sql92Operators = const sql92RegExp = ;const tokenizeSQL92 = ;const tokensSQL92 = ;console;/* Output:SQL92: ["SELECT"," ","tbl",".","x","-",">","'$'",","," ","-","1","<=",">","1","--2 FROM tbl;"] */