/
custom_processor.go
164 lines (155 loc) · 4.8 KB
/
custom_processor.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package jsonte
import (
"github.com/Bedrock-OSS/go-burrito/burrito"
"github.com/MCDevKit/jsonte/jsonte/json"
"github.com/MCDevKit/jsonte/jsonte/types"
"github.com/gammazero/deque"
"strings"
)
// ProcessLangFile processes a lang file replacing all the jsonte expressions with their values
func ProcessLangFile(input string, scope *types.JsonObject) (string, error) {
str, err := json.ConvertToUTF8([]byte(input))
if err != nil {
return input, burrito.PassError(err)
}
lines := strings.Split(string(str), "\n")
for i, line := range lines {
lines[i], err = ProcessString(line, scope, "##", "")
if err != nil {
return "", burrito.WrapErrorf(err, "Failed to process line %d", i+1)
}
}
return strings.Join(lines, "\n"), nil
}
// ProcessMCFunction processes a file replacing all the jsonte expressions with their values
func ProcessMCFunction(input string, scope *types.JsonObject) (string, error) {
str, err := json.ConvertToUTF8([]byte(input))
if err != nil {
return input, burrito.PassError(err)
}
output, err := ProcessString(string(str), scope, "#", "")
if err != nil {
return "", burrito.PassError(err)
}
return output, nil
}
// ProcessString processes a string replacing all the jsonte expressions with their values
func ProcessString(input string, scope *types.JsonObject, startToken, endToken string) (string, error) {
runes := []rune(input)
templateMatches, err := FindTemplateMatches(input, startToken, endToken)
if err != nil {
return "", burrito.PassError(err)
}
globalScope := deque.Deque[*types.JsonObject]{}
globalScope.PushBack(scope)
var sb strings.Builder
lastMatchEnd := 0
for _, match := range templateMatches {
if match.Start > lastMatchEnd {
sb.WriteString(string(runes[lastMatchEnd:match.Start]))
}
result, err := Eval(match.Match, globalScope, "#")
if err != nil {
return "", burrito.WrapErrorf(err, "Failed to evaluate expression '%s'", match.EscapedMatch)
}
if result.Value == nil {
return "", burrito.WrappedErrorf("The expression '%s' evaluated to null.", match.EscapedMatch)
}
if result.Action == types.Value {
sb.WriteString(types.ToString(result.Value))
lastMatchEnd = match.Start + match.Length + 1
} else {
return "", burrito.WrappedErrorf("The expression '%s' evaluated to an action.", match.EscapedMatch)
}
}
if lastMatchEnd < len(runes) {
sb.WriteString(string(runes[lastMatchEnd:]))
}
return sb.String(), nil
}
type TemplateMatch struct {
Match string
EscapedMatch string
Start int
Length int
}
// FindTemplateMatches processes a string replacing all the jsonte expressions with their values
func FindTemplateMatches(input, startToken, endToken string) ([]TemplateMatch, error) {
inputRunes := []rune(input)
startTokenRunes := []rune(startToken)
endTokenRunes := []rune(endToken)
inputLen := len(inputRunes)
startLen := len(startTokenRunes)
endLen := len(endTokenRunes)
matches := make([]TemplateMatch, 0)
started := false
bracketCount := 0
startIndex := 0
var currentMatch strings.Builder
var debugMatch strings.Builder
outerFor:
for i := 0; i < inputLen; i++ {
char := inputRunes[i]
if !started {
if i+startLen < inputLen && inputRunes[i+startLen] == '{' {
for j := 0; j < startLen; j++ {
if inputRunes[i+j] != startTokenRunes[j] {
continue outerFor
}
}
started = true
bracketCount = 1
startIndex = i
i += startLen
currentMatch.Reset()
debugMatch.Reset()
continue
}
} else {
if char == '"' || char == '\'' {
currentMatch.WriteRune(char)
debugMatch.WriteRune(char)
i++
ended, debug := UnescapeStringToBuffer(inputRunes, ¤tMatch, &i, char)
if !ended {
return matches, burrito.WrappedErrorf("The string '%s' is not closed.", debug)
}
debugMatch.WriteString(debug)
currentMatch.WriteRune(char)
debugMatch.WriteRune(char)
} else if char == '{' {
bracketCount++
currentMatch.WriteRune(char)
debugMatch.WriteRune(char)
} else if char == '}' {
bracketCount--
if bracketCount == 0 && i+endLen < inputLen {
for j := 0; j < endLen; j++ {
if inputRunes[i+j] != endTokenRunes[j] {
return matches, burrito.WrappedErrorf("The expression '%s' is not closed.", debugMatch.String())
}
}
started = false
i += endLen
match := currentMatch.String()
matches = append(matches, TemplateMatch{
Match: match,
EscapedMatch: startToken + "{" + debugMatch.String() + "}" + endToken,
Start: startIndex,
Length: i - startIndex,
})
} else {
currentMatch.WriteRune(char)
debugMatch.WriteRune(char)
}
} else {
currentMatch.WriteRune(char)
debugMatch.WriteRune(char)
}
}
}
if started {
return matches, burrito.WrappedErrorf("The expression '%s' is not closed.", debugMatch.String())
}
return matches, nil
}