-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_fb_sql.lua
executable file
·133 lines (108 loc) · 3.8 KB
/
build_fb_sql.lua
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env lua
-- Builds Vim keywords lists from keyword data file
-- Location of keyword data
local in_path = arg[1] or [[firebird_sql_keywords.csv]]
local out_path = arg[2] or [[sqlfirebird_body]]
-- Mapping of keyword data to Vim types
local type_map = {
keyword = "sqlKeyword ",
key = "sqlKeyword ",
op = "sqlOperator ",
["type"] = "sqlType ",
func = "sqlFunction ",
const = "sqlConst ",
state = "sqlStatement",
type_mod = "sqlTypeMod ",
object = "sqlObject ",
group_op = "sqlFunction ",
}
-- Splits a string according to CSV rules
-- params:
-- s : The string to split
-- comma : (optional) The character to use for field seperation
-- quote : (optional) The character to use for quoting field text
-- returns: Table containing the split data, or nil + error message
local function split_csv(s, comma, quote)
comma = comma or ','
quote = quote or '"'
local comma_inv = '^'..comma
local quote_inv = '^'..quote
local quote_match = quote.."("..quote.."?)"
s = s .. comma -- ending comma
local t = {} -- table to collect fields
local fieldstart = 1
repeat
-- next field is quoted? (start with `"'?)
if string.find(s, quote_inv, fieldstart) then
local a, c
local i = fieldstart
repeat
-- find closing quote
a, i, c = string.find(s, quote_match, i+1)
until c ~= quote -- quote not followed by quote?
if not i then
return nil, "Error : unmatched "..quote
end
local f = string.sub(s, fieldstart+1, i-1)
table.insert(t, (string.gsub(f, quote..quote, quote)))
fieldstart = string.find(s, comma, i) + 1
else -- unquoted; find next comma
local nexti = string.find(s, comma, fieldstart)
table.insert(t, string.sub(s, fieldstart, nexti-1))
fieldstart = nexti + 1
end
until fieldstart > string.len(s)
return t
end
-- Parses a CSV file
-- params:
-- path : The path of the file to parse
-- comma : (optional) The character to use for field seperation
-- quote : (optional) The character to use for quoting field text
-- returns: An iterator function that returns parsed CSV rows
-- usage:
-- for row in read_csv("test.csv") do
-- print(row[1], row[2])
-- end
local function read_csv(path, comma, quote)
local input = io.lines(path)
return function()
local l = input()
if not l then return nil; end
local row = split_csv(l, comma, quote)
while not row do
l = l.."\n"..input()
row = split_csv(l, comma, quote)
end
return row
end
end
---------------------------------------
local lib = {}
for row in read_csv(in_path) do
if row[2] == "" then row[2] = "keyword"; end
local tmp = lib[row[2]] or {}
lib[row[2]] = tmp
tmp[#tmp+1] = row[1]:lower()
end
local function write_keyword(f, name, items)
local function write_kw_start()
f:write("syn keyword ", name, "\t")
end
local a,i = 1,1
while i <= #items do
while (i <= #items) and (table.concat(items, " ", a,i):len() < 48) do
i = i + 1
end
write_kw_start()
f:write(table.concat(items, " ", a,i-1), "\n")
a = i
end
f:write("\n")
end
local f = io.open(out_path, "w+")
for t, items in pairs(lib) do
local name = type_map[t]
if name then write_keyword(f, name, items); end
end
f:close()