1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-25 17:18:05 +08:00
This commit is contained in:
IT 老营长 @云轩领航-创始人 2023-12-03 10:51:48 +08:00
parent 6166a5b0af
commit 7d76a316b4
6 changed files with 6 additions and 129 deletions

View File

@ -7,5 +7,3 @@ const LEX = "lex"
var Index = &ice.Context{Name: LEX, Help: "词法模块"}
func init() { ice.Index.Register(Index, nil, SPLIT) }
// func init() { ice.Index.Register(Index, nil) }

View File

@ -34,6 +34,7 @@ func _split_list(m *ice.Message, file string, arg ...string) ice.Map {
const INDENT = "_indent"
stack, indent := []int{}, 0
list, line := kit.List(kit.Data(INDENT, -1)), ""
err := false
m.Cmd(nfs.CAT, file, func(text string) {
if strings.TrimSpace(text) == "" {
return
@ -47,6 +48,9 @@ func _split_list(m *ice.Message, file string, arg ...string) ice.Map {
stack, indent = _split_deep(stack, text)
data := kit.Data(INDENT, indent)
ls := kit.Split(text, m.Option(SPLIT_SPACE), m.Option(SPLIT_BLOCK), m.Option(SPLIT_QUOTE), m.Option(SPLIT_TRANS))
if err {
return
}
switch cb := m.OptionCB(SPLIT).(type) {
case func(int, []string):
cb(indent, ls)
@ -69,6 +73,7 @@ func _split_list(m *ice.Message, file string, arg ...string) ice.Map {
cb(indent, text, ls)
case nil:
default:
err = true
m.ErrorNotImplement(cb)
}
kit.For(arg, func(k string) {

View File

@ -1,9 +0,0 @@
package lex
import ice "shylinux.com/x/icebergs"
const LEX = "lex"
var Index = &ice.Context{Name: LEX, Help: "词法模块"}
func init() { ice.Index.Register(Index, nil, SPLIT) }

View File

@ -1,11 +0,0 @@
package lex
const (
PATTERN = "pattern"
REGEXP = "regexp"
PREFIX = "prefix"
SUFFIX = "suffix"
SPACE = "space"
OPERATOR = "operator"
)

View File

@ -1,106 +0,0 @@
package lex
import (
"strings"
ice "shylinux.com/x/icebergs"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs"
kit "shylinux.com/x/toolkits"
)
func _split_tab(text string) (tab int) {
for _, c := range text {
switch c {
case '\t':
tab += 4
case ' ':
tab++
default:
break
}
}
return
}
func _split_deep(stack []int, text string) ([]int, int) {
tab := _split_tab(text)
for i := len(stack) - 1; i >= 0; i-- {
kit.If(tab <= stack[i], func() { stack = stack[:len(stack)-1] })
}
stack = append(stack, tab)
return stack, len(stack)
}
func _split_list(m *ice.Message, file string, arg ...string) ice.Map {
const INDENT = "_indent"
stack, indent := []int{}, 0
list, line := kit.List(kit.Data(INDENT, -1)), ""
m.Cmd(nfs.CAT, file, func(text string) {
if strings.TrimSpace(text) == "" {
return
}
if line += text; strings.Count(text, "`")%2 == 1 {
return
}
if strings.HasPrefix(strings.TrimSpace(text), "# ") {
return
}
stack, indent = _split_deep(stack, text)
data := kit.Data(INDENT, indent)
ls := kit.Split(text, m.Option(SPLIT_SPACE), m.Option(SPLIT_BLOCK), m.Option(SPLIT_QUOTE), m.Option(SPLIT_TRANS))
switch cb := m.OptionCB(SPLIT).(type) {
case func(int, []string):
cb(indent, ls)
case func(int, []string) []string:
ls = cb(indent, ls)
case func(int, []string, ice.Map, ice.Map):
root, _ := kit.Value(list[0], "list.0").(ice.Map)
cb(indent, ls, data, root)
case func(int, []string, ice.Map) []string:
ls = cb(indent, ls, data)
case func([]string, ice.Map) []string:
ls = cb(ls, data)
case func([]string) []string:
ls = cb(ls)
case func([]string):
cb(ls)
case nil:
default:
m.ErrorNotImplement(cb)
}
kit.For(arg, func(k string) {
kit.Value(data, kit.Keym(k), kit.Select("", ls, 0))
kit.If(len(ls) > 0, func() { ls = ls[1:] })
})
kit.For(ls, func(k, v string) { kit.Value(data, kit.Keym(k), v) })
for i := len(list) - 1; i >= 0; i-- {
if indent > kit.Int(kit.Value(list[i], kit.Keym(INDENT))) {
kit.Value(list[i], kit.Keys(mdb.LIST, "-2"), data)
list = append(list, data)
break
}
list = list[:len(list)-1]
}
line = ""
})
return list[0].(ice.Map)
}
const (
SPLIT_SPACE = "split.space"
SPLIT_BLOCK = "split.block"
SPLIT_QUOTE = "split.quote"
SPLIT_TRANS = "split.trans"
)
const SPLIT = "split"
func init() {
Index.MergeCommands(ice.Commands{
SPLIT: {Name: "split path key auto", Help: "分词", Hand: func(m *ice.Message, arg ...string) {
if len(arg) == 0 || strings.HasSuffix(arg[0], nfs.PS) {
m.Cmdy(nfs.DIR, arg)
return
}
m.Echo(kit.Format(_split_list(m, arg[0], kit.Split(kit.Join(arg[1:]))...)))
}},
})
}

View File

@ -9,12 +9,12 @@ import (
"shylinux.com/x/icebergs/base/aaa"
"shylinux.com/x/icebergs/base/cli"
"shylinux.com/x/icebergs/base/ctx"
"shylinux.com/x/icebergs/base/lex"
"shylinux.com/x/icebergs/base/log"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs"
"shylinux.com/x/icebergs/base/tcp"
"shylinux.com/x/icebergs/base/web"
"shylinux.com/x/icebergs/misc/lex"
kit "shylinux.com/x/toolkits"
)