vis: move text object definitions to separate file
[vis.git] / lexers / sql.lua
blob7ea207f57732643b88c0c5bde4b05066678f11a7
1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- SQL LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
8 local M = {_NAME = 'sql'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local line_comment = (P('--') + '#') * l.nonnewline^0
15 local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
16 local comment = token(l.COMMENT, line_comment + block_comment)
18 -- Strings.
19 local sq_str = l.delimited_range("'")
20 local dq_str = l.delimited_range('"')
21 local bt_str = l.delimited_range('`')
22 local string = token(l.STRING, sq_str + dq_str + bt_str)
24 -- Numbers.
25 local number = token(l.NUMBER, l.float + l.integer)
27 -- Keywords.
28 local keyword = token(l.KEYWORD, word_match({
29 'add', 'all', 'alter', 'analyze', 'and', 'as', 'asc', 'asensitive', 'before',
30 'between', 'bigint', 'binary', 'blob', 'both', 'by', 'call', 'cascade',
31 'case', 'change', 'char', 'character', 'check', 'collate', 'column',
32 'condition', 'connection', 'constraint', 'continue', 'convert', 'create',
33 'cross', 'current_date', 'current_time', 'current_timestamp', 'current_user',
34 'cursor', 'database', 'databases', 'day_hour', 'day_microsecond',
35 'day_minute', 'day_second', 'dec', 'decimal', 'declare', 'default', 'delayed',
36 'delete', 'desc', 'describe', 'deterministic', 'distinct', 'distinctrow',
37 'div', 'double', 'drop', 'dual', 'each', 'else', 'elseif', 'enclosed',
38 'escaped', 'exists', 'exit', 'explain', 'false', 'fetch', 'float', 'for',
39 'force', 'foreign', 'from', 'fulltext', 'goto', 'grant', 'group', 'having',
40 'high_priority', 'hour_microsecond', 'hour_minute', 'hour_second', 'if',
41 'ignore', 'in', 'index', 'infile', 'inner', 'inout', 'insensitive', 'insert',
42 'int', 'integer', 'interval', 'into', 'is', 'iterate', 'join', 'key', 'keys',
43 'kill', 'leading', 'leave', 'left', 'like', 'limit', 'lines', 'load',
44 'localtime', 'localtimestamp', 'lock', 'long', 'longblob', 'longtext', 'loop',
45 'low_priority', 'match', 'mediumblob', 'mediumint', 'mediumtext', 'middleint',
46 'minute_microsecond', 'minute_second', 'mod', 'modifies', 'natural', 'not',
47 'no_write_to_binlog', 'null', 'numeric', 'on', 'optimize', 'option',
48 'optionally', 'or', 'order', 'out', 'outer', 'outfile', 'precision',
49 'primary', 'procedure', 'purge', 'read', 'reads', 'real', 'references',
50 'regexp', 'rename', 'repeat', 'replace', 'require', 'restrict', 'return',
51 'revoke', 'right', 'rlike', 'schema', 'schemas', 'second_microsecond',
52 'select', 'sensitive', 'separator', 'set', 'show', 'smallint', 'soname',
53 'spatial', 'specific', 'sql', 'sqlexception', 'sqlstate', 'sqlwarning',
54 'sql_big_result', 'sql_calc_found_rows', 'sql_small_result', 'ssl',
55 'starting', 'straight_join', 'table', 'terminated', 'text', 'then',
56 'tinyblob', 'tinyint', 'tinytext', 'to', 'trailing', 'trigger', 'true',
57 'undo', 'union', 'unique', 'unlock', 'unsigned', 'update', 'usage', 'use',
58 'using', 'utc_date', 'utc_time', 'utc_timestamp', 'values', 'varbinary',
59 'varchar', 'varcharacter', 'varying', 'when', 'where', 'while', 'with',
60 'write', 'xor', 'year_month', 'zerofill'
61 }, nil, true))
63 -- Identifiers.
64 local identifier = token(l.IDENTIFIER, l.word)
66 -- Operators.
67 local operator = token(l.OPERATOR, S(',()'))
69 M._rules = {
70 {'whitespace', ws},
71 {'keyword', keyword},
72 {'identifier', identifier},
73 {'string', string},
74 {'comment', comment},
75 {'number', number},
76 {'operator', operator},
79 return M