scite-context-lexer-web.lua /size: 3451 b    last modification: 2021-10-28 13:49
1local info = {
2    version   = 1.003,
3    comment   = "scintilla lpeg lexer for web",
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, R, S = lpeg.P, lpeg.R, lpeg.S
10
11local lexers        = require("scite-context-lexer")
12
13local patterns      = lexers.patterns
14local token         = lexers.token
15
16local weblexer      = lexers.new("web","scite-context-lexer-web")
17local webwhitespace = weblexer.whitespace
18
19local space       = patterns.space -- S(" \n\r\t\f\v")
20local any         = patterns.any
21local restofline  = patterns.restofline
22local eol         = patterns.eol
23
24local period      = P(".")
25local percent     = P("%")
26
27local spacing     = token(webwhitespace, space^1)
28local rest        = token("default", any)
29
30local eop         = P("@>")
31local eos         = eop * P("+")^-1 * P("=")
32
33-- we can put some of the next in the web-snippets file
34-- is f okay here?
35
36-- This one is hard to handle partial because trailing spaces are part of the tex part as well
37-- as the c part so they are bound to that. We could have some special sync signal like a label
38-- with space-like properties (more checking then) or styles that act as boundary (basically any
39-- style + 128 or so). A sunday afternoon challenge. Maybe some newline trickery? Or tag lines
40-- which is possible in scite. Or how about a function hook: foolexer.backtracker(str) where str
41-- matches at the beginning of a line: foolexer.backtracker("@ @c") or a pattern, maybe even a
42-- match from start.
43
44-- local backtracker = ((lpeg.Cp() * lpeg.P("@ @c")) / function(p) n = p end + lpeg.P(1))^1
45-- local c = os.clock() print(#s) print(lpeg.match(backtracker,s)) print(n) print(c)
46
47-- local backtracker = (lpeg.Cmt(lpeg.P("@ @c"),function(_,p) n = p end) + lpeg.P(1))^1
48-- local c = os.clock() print(#s) print(lpeg.match(backtracker,s)) print(n) print(c)
49
50----- somespace   = spacing
51----- somespace   = token("whitespace",space^1)
52local somespace   = space^1
53
54local texpart     = token("label",P("@")) * #somespace
55                  + token("label",P("@") * P("*")^1) * token("function",(1-period)^1) * token("label",period)
56local midpart     = token("label",P("@d")) * #somespace
57                  + token("label",P("@f")) * #somespace
58local cpppart     = token("label",P("@c")) * #somespace
59                  + token("label",P("@p")) * #somespace
60                  + token("label",P("@") * S("<(")) * token("function",(1-eop)^1) * token("label",eos)
61
62local anypart     = P("@") * ( P("*")^1 + S("dfcp") + space^1 + S("<(") * (1-eop)^1 * eos )
63local limbo       = 1 - anypart - percent
64
65weblexer.backtracker =                 eol^1 * P("@ @c")
66-- weblexer.foretracker = (space-eol)^0 * eol^1 * P("@") * space + anypart
67weblexer.foretracker = anypart
68
69local texlexer    = lexers.load("scite-context-lexer-tex-web")
70local cpplexer    = lexers.load("scite-context-lexer-cpp-web")
71
72-- local texlexer    = lexers.load("scite-context-lexer-tex")
73-- local cpplexer    = lexers.load("scite-context-lexer-cpp")
74
75lexers.embed(weblexer, texlexer, texpart + limbo,   #anypart)
76lexers.embed(weblexer, cpplexer, cpppart + midpart, #anypart)
77
78local texcomment = token("comment", percent * restofline^0)
79
80weblexer.rules = {
81    { "whitespace", spacing    },
82    { "texcomment", texcomment }, -- else issues with first tex section
83    { "rest",       rest       },
84}
85
86return weblexer
87