cldf-scn.lua /size: 4924 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
cldf-scn
'
]
=
{
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
local
load
,
type
,
tostring
=
load
,
type
,
tostring
10 11
local
formatters
=
string
.
formatters
12
local
char
=
string
.
char
13
local
concat
=
table
.
concat
14 15
local
lpegmatch
=
lpeg
.
match
16
local
p_unquoted
=
lpeg
.
Cs
(
lpeg
.
patterns
.
unquoted
)
17 18
local
f_action_f
=
formatters
[
"
action%s(%s)
"
]
19
local
f_action_s
=
formatters
[
"
local action%s = action[%s]
"
]
20
local
f_command
=
formatters
[
"
local action = tokens._action\n%\nt\nreturn function(%s) return %s end
"
]
21 22
local
interfaces
=
interfaces
23
local
commands
=
commands
24
local
register
=
interfaces
.
registerscanner
25
local
knownscanner
=
interfaces
.
knownscanner
26 27
local
compile
=
tokens
.
compile
or
function
(
)
end
28
local
presets
=
tokens
.
presets
29 30
local
dummy
=
function
(
)
end
31 32
local
report
=
logs
.
reporter
(
"
interfaces
"
,
"
implementor
"
)
33 34
function
interfaces
.
implement
(
specification
)
35
local
name
=
specification
.
name
36
if
name
=
=
"
"
then
37
name
=
nil
38
end
39
local
actions
=
specification
.
actions
40
local
arguments
=
specification
.
arguments
41
local
private
=
specification
.
scope
=
=
"
private
"
42
local
onlyonce
=
specification
.
onlyonce
43
if
not
actions
then
44
if
name
then
45
report
(
"
error: no actions for %a
"
,
name
)
46
else
47
report
(
"
error: no actions and no name
"
)
48
end
49
return
50
end
51
local
p
=
arguments
and
presets
[
arguments
]
52
if
p
then
53
arguments
=
p
54
end
55
local
scanner
56
local
resetter
=
onlyonce
and
name
and
commands
.
ctxresetter
(
name
)
57
if
resetter
then
58
local
scan
=
compile
(
specification
)
59
if
private
then
60
scanner
=
function
(
)
61
resetter
(
)
62
return
scan
(
)
63
end
64
else
65
scanner
=
function
(
)
66
commands
[
name
]
=
dummy
67
resetter
(
)
68
return
scan
(
)
69
end
70
end
71
else
72
scanner
=
compile
(
specification
)
73
end
74
if
not
name
then
75
return
scanner
76
end
77
if
knownscanner
(
name
)
and
not
specification
.
overload
then
78
report
(
"
warning: interface scanner %a is overloaded
"
,
name
)
79
end
80
register
(
name
,
scanner
,
specification
.
protected
,
specification
.
public
,
specification
.
value
)
81
if
private
then
82
return
83
end
84
local
command
85
if
onlyonce
then
86
if
type
(
actions
)
=
=
"
function
"
then
87
actions
=
{
actions
}
88
elseif
#
actions
=
=
1
then
89
actions
=
{
actions
[
1
]
}
90
end
91
end
92
if
type
(
actions
)
=
=
"
function
"
then
93
command
=
actions
94
elseif
#
actions
=
=
1
then
95
command
=
actions
[
1
]
96
else
97
-- this one is not yet complete .. compare tokens
98
tokens
.
_action
=
actions
99
local
f
=
{
}
100
local
args
101
if
not
arguments
then
102
args
=
"
"
103
elseif
type
(
arguments
)
=
=
"
table
"
then
104
local
a
=
{
}
105
for
i
=
1
,
#
arguments
do
106
local
v
=
arguments
[
i
]
107
local
t
=
type
(
v
)
108
if
t
=
=
"
boolean
"
then
109
a
[
i
]
=
tostring
(
v
)
110
elseif
t
=
=
"
number
"
then
111
a
[
i
]
=
tostring
(
v
)
112
elseif
t
=
=
"
string
"
then
113
local
s
=
lpegmatch
(
p_unquoted
,
v
)
114
if
s
and
v
~
=
s
then
115
a
[
i
]
=
v
-- a string, given as "'foo'" or '"foo"'
116
else
117
a
[
i
]
=
char
(
96
+
i
)
118
end
119
else
120
-- nothing special for tables
121
a
[
i
]
=
char
(
96
+
i
)
122
end
123
end
124
args
=
concat
(
a
,
"
,
"
)
125
else
126
args
=
"
a
"
127
end
128
command
=
args
129
for
i
=
1
,
#
actions
do
130
command
=
f_action_f
(
i
,
command
)
131
f
[
i
]
=
f_action_s
(
i
,
i
)
132
end
133
command
=
f_command
(
f
,
args
,
command
)
134
command
=
load
(
command
)
135
if
command
then
136
if
resetter
then
137
local
cmd
=
command
(
)
138
command
=
function
(
)
139
commands
[
name
]
=
dummy
140
resetter
(
)
141
cmd
(
)
142
end
143
else
144
command
=
command
(
)
145
end
146
end
147
tokens
.
_action
=
nil
148
end
149
if
commands
[
name
]
and
not
specification
.
overload
then
150
report
(
"
warning: 'commands.%s' is redefined
"
,
name
)
151
end
152
commands
[
name
]
=
command
153
-- return scanner, command
154
end
155 156
-- it's convenient to have copies here:
157 158
interfaces
.
defined
=
tokens
.
defined
159 160
interfaces
.
setmacro
=
tokens
.
setters
.
macro
161
interfaces
.
setcount
=
tokens
.
setters
.
count
162
interfaces
.
setdimen
=
tokens
.
setters
.
dimen
163