aboutsummaryrefslogtreecommitdiff
path: root/language_psql.lua
blob: 7c6c4b72bb954359f2598b670a5bf3fbc5172391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
local syntax = require "core.syntax"

-- In sql symbols can be lower case and upper case
local keywords = {
  "CREATE", "SELECT", "INSERT", "INTO", "UPDATE",
  "DELETE", "TABLE", "DROP", "VALUES", "NOT",
  "NULL", "PRIMARY", "KEY", "REFERENCES",
  "DEFAULT", "UNIQUE", "CONSTRAINT", "CHECK",
  "ON", "EXCLUDE", "WITH", "USING", "WHERE",
  "GROUP", "BY", "HAVING", "DISTINCT", "LIMIT",
  "OFFSET", "ONLY", "CROSS", "JOIN", "INNER",
  "LEFT", "RIGHT", "FULL", "OUTER", "NATURAL",
  "AND", "OR", "AS", "ORDER", "ORDINALITY",
  "UNNEST", "FROM", "VIEW", "RETURNS", "SETOF",
  "LANGUAGE", "SQL", "LIKE", "LATERAL",
  "INTERVAL", "PARTITION", "UNION", "INTERSECT",
  "EXCEPT", "ALL", "ASC", "DESC", "NULLS",
  "FIRST", "LAST", "IN", "RECURSIVE", "ARRAY",
  "RETURNING", "SET", "ALSO", "INSTEAD",
  "ALTER", "SEQUENCE", "OWNED", "AT", "ZONE",
  "WITHOUT", "TO", "TIMEZONE", "TYPE", "ENUM",
  "DOCUMENT", "XMLPARSE", "XMLSERIALIZE",
  "CONTENT", "OPTION", "INDEX", "ANY",
  "EXTENSION", "ISNULL", "NOTNULL", "UNKNOWN",
  "CASE", "THEN", "WHEN", "ELSE", "END",
  "ROWS", "BETWEEN", "UNBOUNDED", "PRECEDING",
  "UNBOUNDED", "FOLLOWING", "EXISTS", "SOME",
  "COLLATION", "FOR", "TRIGGER", "BEFORE",
  "EACH", "ROW", "EXECUTE", "PROCEDURE",
  "FUNCTION", "DECLARE", "BEGIN", "LOOP",
  "RAISE", "NOTICE", "LOOP", "EVENT",
  "OPERATOR", "DOMAIN", "VARIADIC", "FOREIGN"
}

local types = {
  "BIGINT", "INT8", "BIGSERIAL", "SERIAL8",
  "BIT", "VARBIT", "BOOLEAN", "BOOL", "BOX",
  "BYTEA", "CHARACTER", "CHAR", "VARCHAR",
  "CIDR", "CIRCLE", "DATE", "DOUBLE",
  "PRECISION", "FLOAT8", "INET", "INTEGER",
  "INT", "INT4", "INTERVAL", "JSON", "JSONB",
  "LINE", "LSEG", "MACADDR", "MONEY", "NUMERIC",
  "DECIMAL", "PATH", "POINT", "POLYGON", "REAL",
  "FLOAT4", "INT2", "SMALLINT", "SMALLSERIAL",
  "SERIAL2", "SERIAL", "SERIAL4", "TEXT",
  "TIME", "TIMEZ", "TIMESTAMP", "TIMESTAMPZ",
  "TSQUERY", "TSVECTOR", "TXID_SNAPSHOT",
  "UUID", "XML", "INT4RANGE", "INT8RANGE",
  "NUMRANGE", "TSRANGE", "TSTZRANGE",
  "DATERANGE", "PG_LSN"
}

local literals = {
  "FALSE", "TRUE", "CURRENT_TIMESTAMP",
  "CURRENT_TIME", "CURRENT_DATE", "LOCALTIME",
  "LOCALTIMESTAMP"
}

local symbols = {}
for _, keyword in ipairs(keywords) do
  symbols[keyword:lower()] = "keyword"
  symbols[keyword] = "keyword"
end

for _, type in ipairs(types) do
  symbols[type:lower()] = "keyword2"
  symbols[type] = "keyword2"
end

for _, literal in ipairs(literals) do
  symbols[literal:lower()] = "literal"
  symbols[literal] = "literal"
end

syntax.add {
  files = { "%.sql$", "%.psql$" },
  comment = "--",
  patterns = {
    { pattern = "%-%-.-\n",                type = "comment"  },
    { pattern = { "/%*", "%*/" },          type = "comment"  },
    { pattern = { "'", "'", '\\' },        type = "string"   },
    { pattern = "-?%d+[%d%.eE]*f?",        type = "number"   },
    { pattern = "-?%.?%d+f?",              type = "number"   },
    { pattern = "[%+%-=/%*%%<>!~|&@%?$#]", type = "operator" },
    { pattern = "[%a_][%w_]*%f[(]",        type = "function" },
    { pattern = "[%a_][%w_]*",             type = "symbol"   },
  },
  symbols = symbols,
}