cldf-prs.lua /size: 2807 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['cldf-bas'] = {
2    version   = 1.001,
3    comment   = "companion to cldf-ini.mkiv",
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-- used in chem-ini.lua
10
11local lpegmatch, patterns = lpeg.match, lpeg.patterns
12local P, R, V, Cc, Cs = lpeg.P, lpeg.R, lpeg.V, lpeg.Cc, lpeg.Cs
13local format = string.format
14
15local cpatterns     = patterns.context or { }
16patterns.context    = cpatterns
17
18local utf8character = patterns.utf8character
19local cardinal      = patterns.cardinal
20
21local leftbrace     = P("{")
22local rightbrace    = P("}")
23local backslash     = P("\\")
24local csname        = backslash * P(1) * (1-backslash-leftbrace)^0 * P(" ")^0
25local sign          = P("+") / "\\textplus "
26                    + P("-") / "\\textminus "
27local nested        = P { leftbrace * (V(1) + (1-rightbrace))^0 * rightbrace } -- we already have this
28local subscript     = P("_")
29local superscript   = P("^")
30
31local scripted      = Cs { "start",
32                          start     = (V("csname") + V("nested") + V("lowfirst") + V("highfirst") + V("character"))^1,
33                          rest      = V("csname") + V("nested") + V("lowfirst") + V("highfirst"),
34                          csname    = csname,
35                          character = utf8character,
36                          nested    = leftbrace * (V("rest") + (V("character")-rightbrace))^0 * rightbrace,
37                          -- why does this fail:
38                       -- nested    = leftbrace * (V("start") - rightbrace)^0 * rightbrace,
39                       -- nested    = lpeg.patterns.nested, -- leftbrace * (V("start") - rightbrace)^0 * rightbrace,
40                       -- content   = Cs(V("nested") + sign^-1 * (cardinal + V("character"))),
41                          content   = V("nested") + sign^-1 * (cardinal + V("character")) + sign,
42                          lowfirst  = (subscript  /"") * ( Cc("\\lohi{") * V("content") * Cc("}{") * (superscript/"") + Cc("\\low{" ) ) * V("content") * Cc("}"),
43                          highfirst = (superscript/"") * ( Cc("\\hilo{") * V("content") * Cc("}{") * (subscript  /"") + Cc("\\high{") ) * V("content") * Cc("}"),
44                      }
45
46cpatterns.csname    = csname
47cpatterns.scripted  = scripted
48cpatterns.nested    = nested
49
50-- print(lpegmatch(scripted,"^4_2He"))
51-- print(lpegmatch(scripted,"^{4}_{2}He"))
52-- print(lpegmatch(scripted,"^{4}He"))
53-- print(lpegmatch(scripted,"\\L {C_5}"))
54-- print(lpegmatch(scripted,"\\SL{}"))
55-- print(lpegmatch(scripted,"\\SL{C_5}"))
56-- print(lpegmatch(scripted,"\\SL{C_5}"))
57-- print(lpegmatch(scripted,"{C_5}"))
58-- print(lpegmatch(scripted,"{\\C_5}"))
59-- print(lpegmatch(scripted,"10^-a"))
60