vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

sql.lua

(3361B)


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