1local info = {
2 version = 1.002,
3 comment = "scintilla lpeg lexer for plain text (with spell checking)",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files",
7}
8
9local P, S, Cmt, Cp = lpeg.P, lpeg.S, lpeg.Cmt, lpeg.Cp
10local find, match = string.find, string.match
11
12local lexers = require("scite-context-lexer")
13
14local patterns = lexers.patterns
15local token = lexers.token
16local styleofword = lexers.styleofword
17local setwordlist = lexers.setwordlist
18
19local textlexer = lexers.new("txt","scite-context-lexer-txt")
20local textwhitespace = textlexer.whitespace
21
22local space = patterns.space
23local any = patterns.any
24local wordtoken = patterns.wordtoken
25local wordpattern = patterns.wordpattern
26
27
28local validwords = false
29local validminimum = 3
30
31
32
33local p_preamble = Cmt((S("#!-%") * P(" ") + P(true)), function(input,i)
34 validwords = false
35 validminimum = 3
36 local s, e, line = find(input,"^[#!%-%%](.+)[\n\r]",1)
37 if line then
38 local language = match(line," language=([a-z]+)")
39 if language then
40 validwords, validminimum = setwordlist(language)
41 end
42 end
43 return false
44end)
45
46local t_preamble =
47 token("preamble", p_preamble)
48
49local t_word =
50 C(wordpattern) * Cp() / function(s,p) return styleofword(validwords,validminimum,s,p) end
51
52local t_text =
53 token("default", wordtoken^1)
54
55local t_rest =
56 token("default", (1-wordtoken-space)^1)
57
58local t_spacing =
59 token(textwhitespace, space^1)
60
61textlexer.rules = {
62 { "whitespace", t_spacing },
63 { "preamble", t_preamble },
64 { "word", t_word },
65 { "text", t_text },
66 { "rest", t_rest },
67}
68
69return textlexer
70 |