Skip to content

Commit

Permalink
pref(parser/xini): strengthen parameter analysis and fix scope closur…
Browse files Browse the repository at this point in the history
…e errors
  • Loading branch information
conero committed Oct 12, 2023
1 parent 8195103 commit e708016
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
20 changes: 20 additions & 0 deletions parser/xini/example/hello.ini
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ include in-hello-conf/unique.ini
name = "sys-name"
age = 30

; 节内部attr
attr = {
debug = False
log_file = runtime/logs.log
width = 1042px
height = 782px

; 头部高度
header-width = 23.87
header-height = 87
}

; 字符串数组
list = {
"x+y = 10;"
"2x-4.5y = 18;"
"x = ?;"
"y = ?;"
}

[base]
; 文件引入
include in-hello-conf/base.ini
Expand Down
41 changes: 23 additions & 18 deletions parser/xini/fileParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (p *baseFileParse) loadFile(flPath string, target *map[string]any) bool {
}

// 文件引入
// @todo 应该记载载入了那些文件,以及对应文件的大小等信息(可选)
func (p *baseFileParse) include(ln string, target *map[string]any) (loadOk bool, isIcl bool) {
ln = strings.TrimSpace(ln)
isIclReg := getRegByKey("reg_include_smbl")
Expand Down Expand Up @@ -290,6 +291,7 @@ func (p *baseFileParse) read(filename string) *baseFileParse {
// 作用域结束
if str == scopeSml[1] {
handlerScope()
isScope = false
return
}

Expand Down Expand Up @@ -325,19 +327,23 @@ func (p *baseFileParse) read(filename string) *baseFileParse {
} else if isIcl {
return
}
// 赋值
idx := strings.Index(str, baseEqualToken)
if idx == -1 { // 非法行自动过滤
// kv-键值对解析
dkvp := DecKvPairs(str)
if dkvp.isString { // 此行为字符串
if isScope {
scopeValStrSlice = append(scopeValStrSlice, dkvp.value)
return
}
}
if !dkvp.isKv {
// 非法行自动过滤,不是有效的等式时
return
}

key := strings.TrimSpace(str[:idx])
value := lnTrim(str[idx+1:])

// 长字符串,`"""|'''`
if strings.Index(value, mt1) == 0 || strings.Index(value, mt2) == 0 {
if strings.Index(dkvp.value, mt1) == 0 || strings.Index(dkvp.value, mt2) == 0 {
isMutLineStr = true
mutLineStr[0] = key
mutLineStr[0] = dkvp.key

mlsIdx := strings.Index(line, mt1)
if mlsIdx == -1 {
Expand All @@ -349,17 +355,17 @@ func (p *baseFileParse) read(filename string) *baseFileParse {
}

// 作用域, `{}`
if strings.Index(value, scopeSml[0]) == 0 {
if strings.Index(dkvp.value, scopeSml[0]) == 0 {
isScope = true
scopeValue = map[string]any{}
scopeKey = key
scopeKey = dkvp.key

// 变量处理
value = strings.TrimSpace(value[1:])
value := strings.TrimSpace(dkvp.value[1:])
if value != "" {
kp := DecKvPairs(value)
if kp.isString {
scopeValStrSlice = append(scopeValStrSlice, value)
scopeValStrSlice = append(scopeValStrSlice, kp.value)
} else if kp.isKv {
scopeValue[kp.key] = parseValue(kp.value)
}
Expand All @@ -369,18 +375,17 @@ func (p *baseFileParse) read(filename string) *baseFileParse {
}

// 变量命令替换
value = p.supportVariable(value)
value := p.supportVariable(dkvp.value)
vAny := parseValue(value)
// 赋值
if isScope {
scopeValue[key] = vAny
scopeValue[dkvp.key] = vAny
} else if isSecMk {
secTmpDd[key] = vAny
secTmpDd[dkvp.key] = vAny
} else {
p.data[key] = vAny
p.data[dkvp.key] = vAny
}

p.rawData[key] = value
p.rawData[dkvp.key] = value
})

// section 加到 data 中
Expand Down
4 changes: 4 additions & 0 deletions parser/xini/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (c *Encoder) putStruct(v any) {

// 返回渲染的内容,以及返回内容是否多行
func marshalToString(v reflect.Value, parentKey string) (string, bool) {
if !v.IsValid() {
return "", false
}

if v.Kind() == reflect.Interface {
v = reflect.ValueOf(v.Interface())
}
Expand Down
4 changes: 3 additions & 1 deletion parser/xini/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,17 @@ func DecKvPairs(s string) *KvPairs {
raw: s,
}
ln := strings.TrimSpace(s)
ln = lnTrim(ln) // 行内注释处理
strSymbol := getRegByKey("reg_str_symbol")
if strSymbol != nil && strSymbol.MatchString(ln) {
kp.isString = true
kp.value = s[1 : len(s)-1]
} else {
eqlIdx := strings.Index(ln, baseEqualToken)
if eqlIdx > -1 {
kp.isKv = true
kp.key = ln[:eqlIdx]
kp.value = ln[eqlIdx:]
kp.value = strings.TrimSpace(ln[eqlIdx+1:])
}
}
return kp
Expand Down

0 comments on commit e708016

Please sign in to comment.