1
0
forked from x/ContextOS

mac add lex添加了词法解析

This commit is contained in:
shaoying 2017-11-28 21:32:27 +08:00
parent c98127bc1a
commit 52089121a8
4 changed files with 77 additions and 16 deletions

View File

@ -1,8 +1,14 @@
# @debug on @debug on
~aaa ~root aaa
# login root 94ca7394d007fa189cc4be0a2625d716 root
login root root login root root
~root lex
server start
~root cli
@lex lex
# login root 94ca7394d007fa189cc4be0a2625d716 root
# ~cli # ~cli
# remote slaver listen :9393 tcp # remote slaver listen :9393 tcp
@ -10,8 +16,8 @@ login root root
# login shy shy # login shy shy
# userinfo add context hi hello nice # userinfo add context hi hello nice
# userinfo add command hi context # userinfo add command hi context
~web # ~web
listen # listen
# ~demo # ~demo
# listen # listen
# ~home spawn test # ~home spawn test

View File

@ -27,6 +27,7 @@ type CLI struct {
next string next string
exit bool exit bool
login *ctx.Context login *ctx.Context
lex *ctx.Message
target *ctx.Context target *ctx.Context
*ctx.Context *ctx.Context
@ -113,11 +114,24 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
} }
back: back:
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if line[0] == '#' { if line[0] == '#' {
return true return true
} }
ls := strings.Split(line, " ") ls := strings.Split(line, " ")
if cli.lex != nil {
cli.lex.Cmd("split", line)
ls = cli.lex.Meta["result"]
for i := 0; i < len(ls); i++ {
if ls[i][0] == '"' {
ls[i] = ls[i][1 : len(ls[i])-1]
}
}
if len(ls) == 0 {
return true
}
}
msg := m.Spawn(cli.target) msg := m.Spawn(cli.target)
msg.Wait = make(chan bool) msg.Wait = make(chan bool)
@ -139,7 +153,9 @@ back:
} }
for i := 0; i < len(ls); i++ { for i := 0; i < len(ls); i++ {
ls[i] = strings.TrimSpace(ls[i]) if cli.lex == nil {
ls[i] = strings.TrimSpace(ls[i])
}
if ls[i][0] == '#' { if ls[i][0] == '#' {
break break
@ -181,6 +197,32 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
return x.Value return x.Value
}} }}
cli.Configs["lex"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "", Help: "屏蔽脚本输出的信息yes:屏蔽no:不屏蔽", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
if len(arg) > 0 {
cli, ok := m.Target.Server.(*CLI)
if !ok {
return ""
}
cli.lex = m.Find(arg[0], m.Target.Root)
if cli.lex == nil {
return ""
}
cli.lex.Target.Start(cli.lex)
cli.lex.Cmd("train", "[ \n\t]+", "1")
cli.lex.Cmd("train", "[a-zA-Z][a-zA-Z0-9]*", "2", "2")
cli.lex.Cmd("train", "0x[0-9]+", "3", "2")
cli.lex.Cmd("train", "[0-9]+", "3", "2")
cli.lex.Cmd("train", "\"[^\"]*\"", "4", "2")
cli.lex.Cmd("train", "'[^']*'", "4", "2")
cli.lex.Cmd("train", "[~!@#$&*:]", "4", "2")
}
return ""
}}
cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息yes:屏蔽no:不屏蔽"} cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息yes:屏蔽no:不屏蔽"}
cli.Configs["default"] = &ctx.Config{Name: "默认的搜索起点(root/back/home)", Value: "root", Help: "模块搜索的默认起点root:从根模块back:从父模块home:从当前模块"} cli.Configs["default"] = &ctx.Config{Name: "默认的搜索起点(root/back/home)", Value: "root", Help: "模块搜索的默认起点root:从根模块back:从父模块home:从当前模块"}
cli.Configs["PS1"] = &ctx.Config{Name: "命令行提示符(target/detail)", Value: "target", Help: "命令行提示符target:显示当前模块detail:显示详细信息", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string { cli.Configs["PS1"] = &ctx.Config{Name: "命令行提示符(target/detail)", Value: "target", Help: "命令行提示符target:显示当前模块detail:显示详细信息", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
@ -353,7 +395,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
if s := m.Search(target, m.Get("args")); len(s) > 0 { if s := m.Search(target, m.Get("args")); len(s) > 0 {
ms = append(ms, s...) ms = append(ms, s...)
arg = arg[1:] arg = arg[1:]
break
} }
fallthrough
default: default:
ms = append(ms, m.Spawn(target)) ms = append(ms, m.Spawn(target))
} }
@ -361,11 +405,9 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
for _, v := range ms { for _, v := range ms {
switch { switch {
case m.Has("start"): case m.Has("start"):
args := m.Meta["start"] v.Start(arg[0], arg[1:]...)
v.Start(arg[0], args[1:]...)
case m.Has("spawn"): case m.Has("spawn"):
args := m.Meta["spawn"] v.Target.Spawn(v, arg[0]).Begin(v)
v.Target.Spawn(v, args[0]).Begin(v)
cli.target = v.Target cli.target = v.Target
case m.Has("switch"): case m.Has("switch"):
cli.target = v.Target cli.target = v.Target

View File

@ -9,6 +9,7 @@ import ( // {{{
"os/exec" "os/exec"
"path" "path"
"regexp" "regexp"
"runtime"
"runtime/debug" "runtime/debug"
"strconv" "strconv"
"strings" "strings"
@ -689,10 +690,14 @@ func (m *Message) Search(c *Context, name string) []*Message { // {{{
} }
// }}} // }}}
func (m *Message) Find(name string) *Message { // {{{ func (m *Message) Find(name string, begin ...*Context) *Message { // {{{
ns := strings.Split(name, ".") ns := strings.Split(name, ".")
cs := m.Target.contexts target := m.Target
old := m.Target.Name if len(begin) > 0 {
target = begin[0]
}
cs := target.contexts
old := target.Name
for _, v := range ns { for _, v := range ns {
if x, ok := cs[v]; ok { if x, ok := cs[v]; ok {
@ -788,8 +793,8 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
} }
m.AssertOne(m, true, func(m *Message) { m.AssertOne(m, true, func(m *Message) {
m.Log("system", ":%v", arg) m.Log("system", ": %s %v", key, arg)
cmd := exec.Command(key, arg[1:]...) cmd := exec.Command(key, arg...)
v, e := cmd.CombinedOutput() v, e := cmd.CombinedOutput()
if e != nil { if e != nil {
m.Echo("%s\n", e) m.Echo("%s\n", e)
@ -1137,6 +1142,7 @@ var Index = &Context{Name: "ctx", Help: "根模块",
switch arg[0] { switch arg[0] {
case "start": case "start":
go m.Set("detail", arg[1:]...).Target.Start(m) go m.Set("detail", arg[1:]...).Target.Start(m)
runtime.Gosched()
case "stop": case "stop":
m.Set("detail", arg[1:]...).Target.Exit(m) m.Set("detail", arg[1:]...).Target.Exit(m)
case "switch": case "switch":
@ -1305,11 +1311,15 @@ var Index = &Context{Name: "ctx", Help: "根模块",
m.Target.BackTrace(func(s *Context) bool { m.Target.BackTrace(func(s *Context) bool {
switch len(arg) { switch len(arg) {
case 0: case 0:
target := m.Target
m.Target = s
for k, v := range s.Caches { for k, v := range s.Caches {
if m.Check(m.Target, "caches", k) { if m.Check(m.Target, "caches", k) {
m.Echo("%s(%s): %s\n", k, v.Value, v.Name) m.Echo("%s(%s): %s\n", k, m.Cap(k), v.Name)
} }
} }
m.Target = target
case 1: case 1:
if v, ok := s.Caches[arg[0]]; ok { if v, ok := s.Caches[arg[0]]; ok {
if m.Check(m.Target, "caches", arg[0]) { if m.Check(m.Target, "caches", arg[0]) {

View File

@ -11,6 +11,9 @@ import (
_ "context/ssh" _ "context/ssh"
_ "context/web" _ "context/web"
_ "context/lex"
"os" "os"
) )