From ac1fddb62fa62609fc9d770e4cd97e0a024ac87c Mon Sep 17 00:00:00 2001 From: baichou Date: Wed, 15 Jun 2022 19:07:40 +0800 Subject: [PATCH] protoc IsSpaceAndEspecially --- parser/protoc.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/parser/protoc.go b/parser/protoc.go index f4ba852..d22fbef 100644 --- a/parser/protoc.go +++ b/parser/protoc.go @@ -4,6 +4,7 @@ import ( "fmt" "strconv" "strings" + "unicode" ) // ProtocFileParser 解释proto文件结构 @@ -99,7 +100,7 @@ func GetProtoFileParser(path string) (ProtocFileParser, error) { case wordT_word: switch work.Str { case "syntax": - d.Doc = lastDoc + d.Doc = doc(lastDoc) d.Syntax, offset = protoSyntax(l.list, offset) lastDoc = "" case "package": @@ -112,7 +113,7 @@ func GetProtoFileParser(path string) (ProtocFileParser, error) { lastDoc = "" case "option": var val Option - val.Doc = lastDoc + val.Doc = doc(lastDoc) val, offset = protoOption(l.list, offset) d.Option[val.Key] = val lastDoc = "" @@ -120,18 +121,18 @@ func GetProtoFileParser(path string) (ProtocFileParser, error) { var val Service val, offset = protoService(l.list, offset) val.Protoc = &d - val.Doc = lastDoc + val.Doc = doc(lastDoc) d.Services[val.Name] = val lastDoc = "" case "message": var val Message - val.Doc = lastDoc + val.Doc = doc(lastDoc) val, offset = protoMessage(l.list, offset) d.Messages[val.Name] = val lastDoc = "" case "enum": var val Enum - val.Doc = lastDoc + val.Doc = doc(lastDoc) val, offset = protoEnum(l.list, offset) d.Enums[val.Name] = val lastDoc = "" @@ -146,6 +147,23 @@ func GetProtoFileParser(path string) (ProtocFileParser, error) { return d, nil } +func doc(doc string) string { + doc = strings.TrimFunc(doc, IsSpaceAndEspecially) + return doc +} + +func IsSpaceAndEspecially(r rune) bool { + // This property isn't the same as Z; special-case it. + if uint32(r) <= unicode.MaxLatin1 { + switch r { + case '=', ';', '/', '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0: + return true + } + return false + } + return false +} + func protoSyntax(l []*word, offset int) (string, int) { name, i := GetFistWordBehindStr(l[offset:], "syntax") return name[1 : len(name)-1], offset + i @@ -293,6 +311,7 @@ func protoMessage(l []*word, offset int) (Message, int) { attr.Name, _ = GetFistWord(nl[offset+1:]) st, et := GetBrackets(nl[offset:], "{", "}") attr.Message = protoOtherMessage(attr.Name, nl[offset+st:offset+et+1]) + attr.Doc = doc(attr.Doc) got.Attr = append(got.Attr, attr) attr = Attr{} offset = offset + et + 1 @@ -314,6 +333,7 @@ func protoMessage(l []*word, offset int) (Message, int) { attr.Name = work.Str } else { attr.Num, _ = strconv.Atoi(work.Str) + attr.Doc = doc(attr.Doc) got.Attr = append(got.Attr, attr) attr = Attr{} } @@ -352,6 +372,7 @@ func protoOtherMessage(name string, l []*word) *Message { attr.Name, _ = GetFistWord(nl[offset+1:]) st, et := GetBrackets(nl[offset:], "{", "}") attr.Message = protoOtherMessage(attr.Name, nl[offset+st:offset+et+1]) + attr.Doc = doc(attr.Doc) got.Attr = append(got.Attr, attr) attr = Attr{} offset = offset + et + 1 @@ -373,6 +394,7 @@ func protoOtherMessage(name string, l []*word) *Message { attr.Name = work.Str } else { attr.Num, _ = strconv.Atoi(work.Str) + attr.Doc = doc(attr.Doc) got.Attr = append(got.Attr, attr) attr = Attr{} } @@ -401,6 +423,7 @@ func protoEnum(l []*word, offset int) (Enum, int) { attr.Name = work.Str } else { attr.Num, _ = strconv.Atoi(work.Str) + attr.Doc = doc(attr.Doc) got.Opt = append(got.Opt, attr) attr = Attr{} }