-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathumono-lang.go
195 lines (138 loc) · 4.35 KB
/
umono-lang.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package umonolang
import (
"errors"
"fmt"
"regexp"
"strings"
"unicode/utf8"
"github.com/umono-cms/umono-lang/components"
"github.com/umono-cms/umono-lang/interfaces"
ustrings "github.com/umono-cms/umono-lang/utils/strings"
)
type UmonoLang struct {
converter interfaces.Converter
globalComps []interfaces.Component
}
func New(converter interfaces.Converter) *UmonoLang {
return &UmonoLang{
converter: converter,
globalComps: []interfaces.Component{},
}
}
func (ul *UmonoLang) Convert(raw string) string {
content := raw
localComps := []interfaces.Component{}
firstCompDefIndex := ul.findFirstCompDefIndex(raw)
if firstCompDefIndex != -1 {
content = raw[:firstCompDefIndex]
localComps = ul.readLocalComponents(raw[firstCompDefIndex:])
}
comps := builtInComps()
comps = overrideComps(comps, ul.globalComps)
comps = overrideComps(comps, localComps)
cursor := 0
preConverted := ul.converter.Convert(ul.handleComps(comps, content, 1, cursor))
return ul.convert(comps, preConverted)
}
func (ul *UmonoLang) SetGlobalComponent(name, content string) error {
if !ustrings.IsNumericScreamingSnakeCase(name) {
return errors.New("SYNTAX_ERROR: Component names have to be SCREAMING_SNAKE_CASE.")
}
ul.globalComps = overrideComps(ul.globalComps, []interfaces.Component{components.NewCustom(name, content)})
return nil
}
func (ul *UmonoLang) RemoveGlobalComponent(name string) error {
if !ustrings.IsNumericScreamingSnakeCase(name) {
return errors.New("SYNTAX_ERROR: Component names have to be SCREAMING_SNAKE_CASE.")
}
index, found := findCompByName(ul.globalComps, name)
if found == nil {
return fmt.Errorf("NOT_FOUND: The global component named '%s' not found.", name)
}
ul.globalComps = append(ul.globalComps[:index], ul.globalComps[index+1:]...)
return nil
}
func (ul *UmonoLang) findFirstCompDefIndex(raw string) int {
re := regexp.MustCompile(`\n~\s+[A-Z0-9]+(?:_[A-Z0-9]+)*\s*\n`)
match := re.FindStringIndex(raw)
if match != nil {
return match[0]
}
return -1
}
func (ul *UmonoLang) readLocalComponents(localeCompsRaw string) []interfaces.Component {
localeCompsIndexes := ustrings.IndexesByRegex(localeCompsRaw, `\n~\s+[A-Z0-9_]+(?:_[A-Z0-9]+)*\s*\n`)
comps := []interfaces.Component{}
re := regexp.MustCompile(`(?s)^~\s*|\s*\n$`)
for i := 0; i < len(localeCompsIndexes); i++ {
var compRaw string
if i == len(localeCompsIndexes)-1 {
compRaw = localeCompsRaw[localeCompsIndexes[i]:]
} else {
compRaw = localeCompsRaw[localeCompsIndexes[i]:localeCompsIndexes[i+1]]
}
trimmed := strings.TrimSpace(compRaw)
endOfCompName := strings.Index(trimmed, "\n")
var compNameRaw string
var compContentRaw string
if endOfCompName == -1 {
compNameRaw = trimmed
compContentRaw = ""
} else {
compNameRaw = trimmed[0:endOfCompName]
compContentRaw = trimmed[endOfCompName:]
}
compName := re.ReplaceAllString(compNameRaw, "")
comps = append(comps, components.NewCustom(compName, strings.TrimSpace(compContentRaw)))
}
return comps
}
func (ul *UmonoLang) handleComps(comps []interfaces.Component, content string, deep int, cursor int) string {
if deep == 20 {
return ""
}
calls := readCalls(content, comps)
handled := content
for _, call := range calls {
if call.Component().NeedToConvert() {
continue
}
handled = ustrings.ReplaceSubstring(handled, ul.handleComps(comps, call.Component().RawContent(), deep+1, cursor), call.Start()+cursor, call.End()+cursor)
rawContentLen := utf8.RuneCountInString(call.Component().RawContent())
callLen := call.End() - call.Start()
abs := rawContentLen - callLen
if abs < 0 {
abs = -abs
}
if callLen < rawContentLen {
cursor += abs
} else {
cursor -= abs
}
}
return strings.TrimSpace(handled)
}
func (ul *UmonoLang) convert(comps []interfaces.Component, handled string) string {
converted := handled
calls := readCalls(converted, comps)
cursor := 0
for _, call := range calls {
if !call.Component().NeedToConvert() {
continue
}
output := ul.converter.ConvertBuiltInComp(call)
converted = ustrings.ReplaceSubstring(converted, output, call.Start()+cursor, call.End()+cursor)
convertedLen := utf8.RuneCountInString(output)
callLen := call.End() - call.Start()
abs := convertedLen - callLen
if abs < 0 {
abs = -abs
}
if callLen < convertedLen {
cursor += abs
} else {
cursor -= abs
}
}
return converted
}