Skip to content

Commit

Permalink
分词修复转义符
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Apr 22, 2022
1 parent 096925b commit 77b6f25
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
GOBIN := $(shell go env GOBIN)
ATDIR := $(shell pwd)

# mac 系统更新path可能不全
export PATH := $(GOBIN):$(PATH)

build:
go build -ldflags="-w -s" -o $(GOBIN)/toolset ./

11 changes: 10 additions & 1 deletion console/commands/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func (BeanCommand) Configure() command.Configure {
},
},
Option: []command.ArgParam{
{
Name: "name",
Description: "跳过目录",
Default: "New{name}",
},
{
Name: "scan",
Description: "扫码目录下的源码; shell(pwd)",
Expand All @@ -40,7 +45,11 @@ func (BeanCommand) Configure() command.Configure {
}
}

var newName = "New{name}"

func (BeanCommand) Execute(input command.Input) {
newName = input.GetOption("name")

root := getRootPath()
scan := input.GetOption("scan")
scan = strings.Replace(scan, "@root", root, 1)
Expand Down Expand Up @@ -208,7 +217,7 @@ func genInitializeNewStr(name string) string {
name = name[1:]
}

return "New" + name
return strings.Replace(newName, "{name}", name, 1)
}

// 生成 import => alias
Expand Down
13 changes: 6 additions & 7 deletions console/commands/protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@ func (ProtocCommand) Configure() command.Configure {
{
Name: "proto_path",
Description: "protoc后面拼接的proto_path, 可以传入多个",
Default: "@root/protobuf/common/http",
Default: "@root/protobuf/common",
},
{
Name: "go_out",
Description: "生成文件到指定目录",
Default: "@root/generate/proto",
},
{
Name: "show",
Description: "是否打印protoc命令",
Default: "false",
},
},
},
}
Expand All @@ -51,7 +46,7 @@ func (ProtocCommand) Configure() command.Configure {
var show = false

func (ProtocCommand) Execute(input command.Input) {
show = input.GetOption("show") != "false"
show = input.GetOption("debug") != "false"
root := getRootPath()
_, err := exec.LookPath("protoc")
if err != nil {
Expand All @@ -71,6 +66,10 @@ func (ProtocCommand) Execute(input command.Input) {
for _, s := range input.GetOptions("proto_path") {
s = strings.Replace(s, "@root", root, 1)
pps = append(pps, "--proto_path="+s)
// 子目录也加入进来
for _, dir := range parser.GetChildrenDir(s) {
pps = append(pps, "--proto_path="+dir.Path)
}
}
// path/*.proto 不是protoc命令提供的, 如果这里执行需要每一个文件一个命令
for _, dir := range parser.GetChildrenDir(path) {
Expand Down
7 changes: 7 additions & 0 deletions console/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func (k *Kernel) Run() {
return val, true
},
})
app.AddBaseOption(command.ArgParam{
Name: "debug",
Description: "是否显示明细",
Call: func(val string, c *command.Console) (string, bool) {
return "true", true
},
})

for _, provider := range commands.GetAllProvider() {
if v, ok := provider.(command.Command); ok {
Expand Down
28 changes: 23 additions & 5 deletions parser/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"fmt"
"os"
"strings"
)

Expand Down Expand Up @@ -115,7 +116,8 @@ func GetFileParser(path string) (GoFileParser, error) {
for _, w := range nl {
str += w.Str
}
fmt.Println("文件块作用域似乎解析有错误", path, offset, str)
fmt.Println("文件块作用域似乎解析有错误\n", path, "\n", offset, "\n", str)
os.Exit(1)
}
}
}
Expand Down Expand Up @@ -224,11 +226,13 @@ func (d GoDoc) GetAlias() string {
if w.Ty == wordT_word {
if w.Str == "Bean" {
if l[i-1].Str == "@" {
num = 1
num = i
}
} else if num == 1 {
} else if num == -1 {
return w.Str[1 : len(w.Str)-1]
}
} else if num == -i && w.Str == "(" {
num = -1
}
}
return ""
Expand Down Expand Up @@ -386,6 +390,7 @@ func handleFunds(l []*word, offset int) (GoFunc, int) {
interCount++
}
}

if interCount != 0 {
for i := 0; i <= interCount; i++ {
_, et := GetBrackets(l[offset:], "{", "}")
Expand All @@ -394,7 +399,8 @@ func handleFunds(l []*word, offset int) (GoFunc, int) {
} else {
offset = offset + et
}
return GoFunc{Name: name}, offset

return GoFunc{Name: name}, offset + 1
}
func handleCosts(l []*word, offset int) (map[string]string, int) {
return handleVars(l, offset)
Expand All @@ -411,7 +417,11 @@ func handleVars(l []*word, offset int) (map[string]string, int) {
nl := l[offset+last-1:]
offset = offset + last - 1
_, et := GetBrackets(nl, "{", "}")
return nil, offset + et + 1
offset = offset + et + 1
// 检查是否 struct 实例化
_, et = GetBrackets(l[offset:], "{", "}")
offset = offset + et + 1
return nil, offset
} else {
nl := l[offset:]
_, et := GetBrackets(nl, "{", "}")
Expand All @@ -428,3 +438,11 @@ func handleVars(l []*word, offset int) (map[string]string, int) {
return nil, offset + et + 1
}
}

func toStr(l []*word) string {
s := ""
for _, w := range l {
s += w.Str
}
return s
}
4 changes: 2 additions & 2 deletions parser/help_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func getWordsWitchFile(path string) GoWords {
case scannerStatus_quote:
work = work + str
stop = true
if str == "\"" && !HasSuffix(work, "\\\"") {
if str == "\"" && (!HasSuffix(work, "\\\"") || HasSuffix(work, "\\\\\"")) {
got.list = append(got.list, &word{
Str: work,
Ty: wordT_word,
Expand All @@ -211,7 +211,7 @@ func getWordsWitchFile(path string) GoWords {
case scannerStatus_quote2:
work = work + str
stop = true
if str == "'" {
if str == "'" && (!HasSuffix(work, "\\'") || HasSuffix(work, "\\\\'")) {
got.list = append(got.list, &word{
Str: work,
Ty: wordT_word,
Expand Down

0 comments on commit 77b6f25

Please sign in to comment.