1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 16:58:06 +08:00

mac pro lex

This commit is contained in:
shaoying 2018-07-23 09:16:24 +08:00
parent 8345bca7c7
commit 0b446010cf
4 changed files with 33 additions and 112 deletions

View File

@ -140,6 +140,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
}
m.Options("scan_end", false)
m.Optionv("ps_target", cli.target)
m.Option("prompt", m.Conf("prompt"))
m.Cap("stream", m.Spawn(yac.Target()).Call(func(cmd *ctx.Message) *ctx.Message {
if !m.Caps("parse") {
@ -160,6 +161,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Options("scan_end", true)
m.Target().Close(m.Spawn())
}
m.Optionv("ps_target", cli.target)
return nil
}, "parse", arg[1]).Target().Name)
@ -235,6 +237,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
}},
"target": &ctx.Command{Name: "target module", Help: "设置当前模块, module: 模块全名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if cli, ok := m.Target().Server.(*CLI); m.Assert(ok) { // {{{
if len(arg) == 0 {
m.Echo("%s", m.Cap("ps_target"))
return
}
if msg := m.Find(arg[0]); msg != nil {
cli.target = msg.Target()
m.Cap("ps_target", cli.target.Name)
@ -726,10 +732,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
}
// }}}
}},
"demo": &ctx.Command{Name: "demo word", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Append("hi", "hello", "world")
m.Echo("nice")
}},
},
}

View File

@ -225,50 +225,7 @@ func (lex *LEX) train(page int, hash int, seed []byte) int { // {{{
}
// }}}
func (lex *LEX) parse(page int, line []byte) (hash int, rest []byte, word []byte) { // {{{
pos := 0
for star, s := 0, page; s != 0 && pos < len(line); pos++ {
c := line[pos]
if c == '\\' && pos < len(line)-1 { //跳过转义
pos++
c = lex.charset(line[pos])[0]
}
if c > 127 { //跳过中文
word = append(word, c)
continue
}
state := lex.mat[s][c]
lex.Log("debug", nil, "(%d,%d): %v", s, c, state)
if state == nil {
s, star, pos = star, 0, pos-1
continue
}
word = append(word, c)
if state.star {
star = s
} else if x, ok := lex.mat[star][c]; !ok || !x.star {
star = 0
}
if s, hash = state.next, state.hash; s == 0 {
s, star = star, 0
}
}
if hash == 0 {
pos, word = 0, word[:0]
}
rest = line[pos:]
return
}
// }}}
func (lex *LEX) scan(m *ctx.Message, page int, line []byte) (hash int, rest []byte, word []byte) { // {{{
func (lex *LEX) parse(m *ctx.Message, page int, line []byte) (hash int, rest []byte, word []byte) { // {{{
pos := 0
for star, s := 0, page; s != 0 && pos < len(line); pos++ {
@ -378,7 +335,6 @@ func (lex *LEX) Close(m *ctx.Message, arg ...string) bool { // {{{
case m.Target():
case m.Source():
}
return false
return true
}
@ -415,19 +371,7 @@ var Index = &ctx.Context{Name: "lex", Help: "词法中心",
page = lex.index("npage", arg[1])
}
hash, rest, word := lex.parse(page, []byte(arg[0]))
m.Result(0, hash, string(rest), string(word))
lex.Log("debug", nil, "\033[31m[%v]\033[0m %d [%v]", string(word), hash, string(rest))
} // }}}
}},
"scan": &ctx.Command{Name: "scan line [page]", Help: "解析单词", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if lex, ok := m.Target().Server.(*LEX); m.Assert(ok) { // {{{
page := 1
if len(arg) > 1 {
page = lex.index("npage", arg[1])
}
hash, rest, word := lex.scan(m, page, []byte(arg[0]))
hash, rest, word := lex.parse(m, page, []byte(arg[0]))
m.Result(0, hash, string(rest), string(word))
} // }}}
}},
@ -449,9 +393,9 @@ var Index = &ctx.Context{Name: "lex", Help: "词法中心",
}
rest := []byte(arg[0])
_, _, rest = lex.parse(help, []byte(rest))
_, _, rest = lex.parse(void, []byte(rest))
hash, word, rest := lex.parse(page, []byte(rest))
_, _, rest = lex.parse(m, help, []byte(rest))
_, _, rest = lex.parse(m, void, []byte(rest))
hash, word, rest := lex.parse(m, page, []byte(rest))
m.Add("result", fmt.Sprintf("%d", hash), string(word), string(rest))
} // }}}
}},
@ -502,11 +446,6 @@ var Index = &ctx.Context{Name: "lex", Help: "词法中心",
} // }}}
}},
},
Index: map[string]*ctx.Context{
"void": &ctx.Context{Name: "void", Help: "void",
Commands: map[string]*ctx.Command{"parse": &ctx.Command{}},
},
},
}
func init() {

View File

@ -3,11 +3,11 @@ package nfs // {{{
import ( // {{{
"contexts"
"encoding/json"
"errors"
"github.com/nsf/termbox-go"
"github.com/skip2/go-qrcode"
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
@ -25,11 +25,12 @@ import ( // {{{
var FileNotExist = errors.New("file not exist")
type NFS struct {
in *os.File
out *os.File
history []string
pages []string
width, height int
in *os.File
out *os.File
history []string
pages []string
width int
height int
paths []string
@ -470,8 +471,7 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
case termbox.KeyCtrlK:
if len(rest) > 0 {
back = back[:0]
back = append(back, rest...)
back = append([]rune{}, rest...)
}
rest = rest[:0]
@ -487,16 +487,17 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
if len(tab) == 0 {
tabi = 0
prefix := string(buf)
msg := nfs.Message.Spawn(nfs.cli.Target())
target := msg.Cmd("target").Data["target"].(*ctx.Context)
msg.Spawn(target).BackTrace(func(msg *ctx.Message) bool {
for k, _ := range msg.Target().Commands {
target := nfs.Message.Target()
nfs.Message.Target(nfs.Optionv("ps_target").(*ctx.Context))
nfs.Message.BackTrace(func(m *ctx.Message) bool {
for k, _ := range m.Target().Commands {
if strings.HasPrefix(k, prefix) {
tab = append(tab, k[len(prefix):])
}
}
return true
})
nfs.Message.Target(target)
}
if tabi >= 0 && tabi < len(tab) {
@ -619,8 +620,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
nfs.print(v)
}
if msg.Append("file_pos0") != "" {
i = msg.Appendi("file_pos0")
i--
i = msg.Appendi("file_pos0") - 1
}
}
line = ""
@ -1401,18 +1401,6 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
} // }}}
}},
},
Index: map[string]*ctx.Context{
"void": &ctx.Context{Name: "void",
Commands: map[string]*ctx.Command{
"scan": &ctx.Command{},
"open": &ctx.Command{},
"save": &ctx.Command{},
"load": &ctx.Command{},
"genqr": &ctx.Command{},
"write": &ctx.Command{},
},
},
},
}
func init() {

View File

@ -192,7 +192,7 @@ func (yac *YAC) parse(m *ctx.Message, out *ctx.Message, page int, void int, line
for star, s := 0, page; s != 0 && len(line) > 0; {
//解析空白
lex := m.Sesss("lex")
lex.Cmd("scan", line, yac.name(void))
lex.Cmd("parse", line, yac.name(void))
if lex.Result(0) == "-1" {
break
}
@ -200,7 +200,7 @@ func (yac *YAC) parse(m *ctx.Message, out *ctx.Message, page int, void int, line
//解析单词
line = lex.Result(1)
lex = m.Sesss("lex")
lex.Cmd("scan", line, yac.name(s))
lex.Cmd("parse", line, yac.name(s))
if lex.Result(0) == "-1" {
break
}
@ -257,7 +257,6 @@ func (yac *YAC) parse(m *ctx.Message, out *ctx.Message, page int, void int, line
func (yac *YAC) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
yac.Message = m
c.Caches = map[string]*ctx.Cache{}
c.Configs = map[string]*ctx.Config{}
@ -357,9 +356,7 @@ func (yac *YAC) Start(m *ctx.Message, arg ...string) (close bool) { // {{{
func (yac *YAC) Close(m *ctx.Message, arg ...string) bool { // {{{
switch yac.Context {
case m.Target():
return true
case m.Source():
return false
}
return true
}
@ -373,15 +370,15 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
Configs: map[string]*ctx.Config{
"ncell": &ctx.Config{Name: "词法上限", Value: "128", Help: "词法集合的最大数量"},
"nlang": &ctx.Config{Name: "语法上限", Value: "32", Help: "语法集合的最大数量"},
"name": &ctx.Config{Name: "name", Value: "parse", Help: "模块名", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
"label": &ctx.Config{Name: "嵌套标记", Value: "####################", Help: "嵌套层级日志的标记"},
"yac_name": &ctx.Config{Name: "yac_name", Value: "parse", Help: "模块名", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
if len(arg) > 0 { // {{{
return arg[0]
}
return fmt.Sprintf("%s%d", x.Value, m.Capi("nparse", 1))
// }}}
}},
"help": &ctx.Config{Name: "help", Value: "解析模块", Help: "模块帮助"},
"label": &ctx.Config{Name: "嵌套标记", Value: "####################", Help: "嵌套层级日志的标记"},
"yac_help": &ctx.Config{Name: "yac_help", Value: "解析模块", Help: "模块帮助"},
},
Commands: map[string]*ctx.Command{
"init": &ctx.Command{Name: "init [ncell [nlang]]", Help: "初始化语法矩阵", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -455,22 +452,17 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
// }}}
}},
"parse": &ctx.Command{
Name: "parse filename [name [help]] [line line] [void void]",
Help: "解析文件, filename: name:模块名, help:模块帮助, 文件名, line: 默认语法, void: 默认空白",
Name: "parse filename [yac_name [help]] [line line] [void void]",
Help: "解析文件, filename: yac_name:模块名, yac_help:模块帮助, 文件名, line: 默认语法, void: 默认空白",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
m.Optioni("page", yac.page["line"])
m.Optioni("void", yac.page["void"])
m.Start(m.Confx("name", arg, 1), m.Confx("help", arg, 2), key, arg[0])
m.Start(m.Confx("yac_name", arg, 1), m.Confx("yac_help", arg, 2), key, arg[0])
}
// }}}
}},
},
Index: map[string]*ctx.Context{
"void": &ctx.Context{Name: "void", Help: "void",
Commands: map[string]*ctx.Command{"parse": &ctx.Command{}},
},
},
}
func init() {