1local info = {
2 version = 1.002,
3 comment = "scintilla lpeg lexer for lua",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files",
7}
8
9
10
11
12local P, R, S, C, Cmt, Cp = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Cmt, lpeg.Cp
13local match, find = string.match, string.find
14local setmetatable = setmetatable
15
16local lexer = require("scite-context-lexer")
17local context = lexer.context
18local patterns = context.patterns
19
20local token = lexer.token
21local exact_match = lexer.exact_match
22local just_match = lexer.just_match
23
24local lualexer = lexer.new("lua","scite-context-lexer-lua")
25local whitespace = lualexer.whitespace
26
27local stringlexer = lexer.load("scite-context-lexer-lua-longstring")
28
29
30local directives = { }
31
32
33
34
35
36
37local keywords = {
38 "and", "break", "do", "else", "elseif", "end", "false", "for", "function",
39 "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true",
40 "until", "while",
41}
42
43local functions = {
44 "assert", "collectgarbage", "dofile", "error", "getmetatable",
45 "ipairs", "load", "loadfile", "module", "next", "pairs",
46 "pcall", "print", "rawequal", "rawget", "rawset", "require",
47 "setmetatable", "tonumber", "tostring", "type", "unpack", "xpcall", "select",
48
49 "string", "table", "coroutine", "debug", "file", "io", "lpeg", "math", "os", "package", "bit32", "utf8",
50}
51
52local constants = {
53 "_G", "_VERSION", "_M", "...", "_ENV",
54
55 "__add", "__call", "__concat", "__div", "__idiv", "__eq", "__gc", "__index",
56 "__le", "__lt", "__metatable", "__mode", "__mul", "__newindex",
57 "__pow", "__sub", "__tostring", "__unm", "__len",
58 "__pairs", "__ipairs",
59 "__close",
60 "NaN",
61 "<const>", "<toclose>",
62}
63
64
65
66
67
68
69
70local internals = {
71 "add", "call", "concat", "div", "idiv", "eq", "gc", "index",
72 "le", "lt", "metatable", "mode", "mul", "newindex",
73 "pow", "sub", "tostring", "unm", "len",
74 "pairs", "ipairs",
75 "close",
76}
77
78local depricated = {
79 "arg", "arg.n",
80 "loadstring", "setfenv", "getfenv",
81 "pack",
82}
83
84local csnames = {
85 "commands",
86 "context",
87
88
89 "metafun",
90 "metapost",
91}
92
93local level = nil
94local setlevel = function(_,i,s) level = s return i end
95
96local equals = P("=")^0
97
98local longonestart = P("[[")
99local longonestop = P("]]")
100local longonestring = (1-longonestop)^0
101
102local longtwostart = P("[") * Cmt(equals,setlevel) * P("[")
103local longtwostop = P("]") * equals * P("]")
104
105local sentinels = { } setmetatable(sentinels, { __index = function(t,k) local v = "]" .. k .. "]" t[k] = v return v end })
106
107local longtwostring = P(function(input,index)
108 if level then
109
110 local sentinel = sentinels[level]
111 local _, stop = find(input,sentinel,index,true)
112 return stop and stop + 1 - #sentinel or #input + 1
113 end
114end)
115
116 local longtwostring_body = longtwostring
117
118 local longtwostring_end = P(function(input,index)
119 if level then
120
121 local sentinel = sentinels[level]
122 local _, stop = find(input,sentinel,index,true)
123 return stop and stop + 1 or #input + 1
124 end
125 end)
126
127local longcomment = Cmt(#("[[" + ("[" * C(equals) * "[")), function(input,index,level)
128
129 local sentinel = sentinels[level]
130 local _, stop = find(input,sentinel,index,true)
131 return stop and stop + 1 or #input + 1
132end)
133
134local space = patterns.space
135local any = patterns.any
136local eol = patterns.eol
137
138local squote = P("'")
139local dquote = P('"')
140local escaped = P("\\") * P(1)
141local dashes = P("--")
142
143local spacing = token(whitespace, space^1)
144local rest = token("default", any)
145
146local shortcomment = token("comment", dashes * (1-eol)^0)
147local longcomment = token("comment", dashes * longcomment)
148
149
150
151
152local shortstring = token("quote", dquote)
153 * token("string", (escaped + (1-dquote))^0)
154 * token("quote", dquote)
155 + token("quote", squote)
156 * token("string", (escaped + (1-squote))^0)
157 * token("quote", squote)
158
159
160
161
162
163
164
165
166local string = shortstring
167
168
169lexer.embed_lexer(lualexer, stringlexer, token("quote",longtwostart), token("string",longtwostring_body) * token("quote",longtwostring_end))
170
171local integer = P("-")^-1 * (patterns.hexadecimal + patterns.decimal)
172local number = token("number", patterns.float + integer)
173 * (token("error",R("AZ","az","__")^1))^0
174
175
176
177
178
179local utf8character = P(1) * R("\128\191")^1
180local validword = (R("AZ","az","__") + utf8character) * (R("AZ","az","__","09") + utf8character)^0
181local validsuffix = (R("AZ","az") + utf8character) * (R("AZ","az","__","09") + utf8character)^0
182
183local identifier = token("default",validword)
184
185
186
187
188local operator = token("special", S('+-*/%^#=<>;:,{}[]().|~'))
189
190local structure = token("special", S('{}[]()'))
191
192local optionalspace = spacing^0
193local hasargument = #S("{([")
194
195
196
197local gotokeyword = token("keyword", P("goto"))
198 * spacing
199 * token("grouping",validword)
200local gotolabel = token("keyword", P("::"))
201 * (spacing + shortcomment)^0
202 * token("grouping",validword)
203 * (spacing + shortcomment)^0
204 * token("keyword", P("::"))
205
206
207
208
209
210
211
212local p_finish = #(1-R("az","AZ","__"))
213local p_keywords = lexer.helpers.utfchartabletopattern(keywords) * p_finish
214local p_functions = lexer.helpers.utfchartabletopattern(functions) * p_finish
215local p_constants = lexer.helpers.utfchartabletopattern(constants) * p_finish
216local p_internals = P("__")
217 * lexer.helpers.utfchartabletopattern(internals) * p_finish
218
219local p_csnames = lexer.helpers.utfchartabletopattern(csnames)
220local p_ctnames = P("ctx") * R("AZ","az","__")^0
221local keyword = token("keyword", p_keywords)
222local builtin = token("plain", p_functions)
223local constant = token("data", p_constants)
224local internal = token("data", p_internals)
225local csname = token("user", p_csnames + p_ctnames)
226 * p_finish * optionalspace * (
227 hasargument
228 + ( token("special", S(".:")) * optionalspace * token("user", validword) )^1
229 )^-1
230
231
232
233local identifier = token("default", validword)
234 * ( optionalspace * token("special", S(".:")) * optionalspace * (
235 token("warning", p_keywords) +
236 token("data", p_internals) +
237 token("default", validword )
238 ) )^0
239
240
241
242
243
244
245
246
247
248
249
250lualexer._rules = {
251 { "whitespace", spacing },
252 { "keyword", keyword },
253
254 { "function", builtin },
255 { "constant", constant },
256
257 { "csname", csname },
258 { "goto", gotokeyword },
259 { "identifier", identifier },
260 { "string", string },
261 { "number", number },
262 { "longcomment", longcomment },
263 { "shortcomment", shortcomment },
264 { "label", gotolabel },
265 { "operator", operator },
266 { "rest", rest },
267}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315lualexer._tokenstyles = context.styleset
316
317
318
319lualexer._foldpattern = (P("end") + P("if") + P("do") + P("function") + P("repeat") + P("until")) * P(#(1 - R("az")))
320 + S("{}[]")
321
322lualexer._foldsymbols = {
323 _patterns = {
324 "[a-z][a-z]+",
325 "[{}%[%]]",
326 },
327 ["keyword"] = {
328 ["if"] = 1,
329 ["do"] = 1,
330 ["function"] = 1,
331 ["repeat"] = 1,
332 ["until"] = -1,
333 ["end"] = -1,
334 },
335 ["comment"] = {
336 ["["] = 1, ["]"] = -1,
337 },
338
339
340
341 ["special"] = {
342
343 ["{"] = 1, ["}"] = -1,
344 },
345}
346
347
348
349local cstoken = R("az","AZ","\127\255") + S("@!?_")
350local texcsname = P("\\") * cstoken^1
351local commentline = P("%") * (1-S("\n\r"))^0
352
353local texcomment = token("comment", Cmt(commentline, function() return directives.cld_inline end))
354
355local longthreestart = P("\\!!bs")
356local longthreestop = P("\\!!es")
357local longthreestring = (1-longthreestop)^0
358
359local texstring = token("quote", longthreestart)
360 * token("string", longthreestring)
361 * token("quote", longthreestop)
362
363
364local texcommand = token("warning", texcsname)
365
366
367
368
369
370
371
372
373lualexer._directives = directives
374
375lualexer._rules_cld = {
376 { "whitespace", spacing },
377 { "texstring", texstring },
378 { "texcomment", texcomment },
379 { "texcommand", texcommand },
380
381 { "keyword", keyword },
382 { "function", builtin },
383 { "csname", csname },
384 { "goto", gotokeyword },
385 { "constant", constant },
386 { "identifier", identifier },
387 { "string", string },
388 { "longcomment", longcomment },
389 { "shortcomment", shortcomment },
390 { "number", number },
391 { "label", gotolabel },
392 { "operator", operator },
393 { "rest", rest },
394}
395
396return lualexer
397 |