-
Notifications
You must be signed in to change notification settings - Fork 6
/
text.go
56 lines (49 loc) · 1.21 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package lyml
import (
"strings"
"get.pme.sh/pmesh/luae"
lua "github.com/yuin/gopher-lua"
)
// BalancedBrackets returns the body of the first balanced pair of brackets in s, and the rest of s after the closing bracket.
func balancedBrackets(s string, b, e rune) (body string, rest string, ok bool) {
count := 1
for i, c := range s {
if c == b {
count++
} else if c == e {
count--
}
if count == 0 {
return s[:i], s[i+1:], true
}
}
return "", s, false
}
// EscapeSubstitute replaces all occurrences of $(...) in s with the result of lookup(body).
func escapeSubstitute(s string, lookup func(string) (string, error)) (result string) {
for {
i := strings.Index(s, "$(")
if i == -1 {
return result + s
}
result += s[:i]
body, rest, ok := balancedBrackets(s[i+2:], '(', ')')
if !ok {
return result + s
}
eval, err := lookup(body)
if err != nil {
result += "$(" + body + ")"
} else {
result += eval
}
s = rest
}
}
// EvalEscape evaluates all occurrences of $(...) in s as Lua expressions.
func evalEscape(s string, state *lua.LState) (result string) {
return escapeSubstitute(s, func(s string) (res string, err error) {
err = luae.EvalLua(state, s, &res)
return
})
}