From b132d964423c77dad486bc965279f68a9abdc1cd Mon Sep 17 00:00:00 2001 From: xubo Date: Thu, 2 Dec 2021 17:02:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8Dulexer=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/golang/api.go | 2 +- api/golang/text_parser.go | 56 +++++++++++++++++++++++++++++++++------ go.mod | 7 ++--- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/api/golang/api.go b/api/golang/api.go index 4808a16..f5d5b80 100644 --- a/api/golang/api.go +++ b/api/golang/api.go @@ -69,7 +69,7 @@ func UnmarshalText(s string, obj interface{}) error { return nil }) - err := lex.Run(func(lex *ulexer.Lexer) { + err := ulexer.Try(lex, func(lex *ulexer.Lexer) { parseStruct(lex, tObj, vObj, "") diff --git a/api/golang/text_parser.go b/api/golang/text_parser.go index 104b1d8..7abbaa8 100644 --- a/api/golang/text_parser.go +++ b/api/golang/text_parser.go @@ -3,8 +3,48 @@ package ppgo import ( "github.com/davyxu/ulexer" "reflect" + "strings" ) +type MatchAction func(tk *ulexer.Token) + +func selectAction(self *ulexer.Lexer, mlist []ulexer.Matcher, alist []MatchAction) { + + if len(mlist) != len(alist) { + panic("Matcher list should equal to Action list length") + } + + var hit bool + for index, m := range mlist { + tk := self.Read(m) + + if tk != ulexer.EmptyToken { + + action := alist[index] + if action != nil { + action(tk) + } + hit = true + break + } + } + + if !hit { + + var sb strings.Builder + + for index, m := range mlist { + if index > 0 { + sb.WriteString(" ") + } + sb.WriteString(m.TokenType()) + } + + self.Error("Expect %s", sb.String()) + } + +} + func detectEnd(lex *ulexer.Lexer, literal string) bool { if literal != "" { if tk := lex.Read(ulexer.Contain(literal)); tk.String() == literal { @@ -22,12 +62,12 @@ func parseStruct(lex *ulexer.Lexer, tObj reflect.Type, vObj reflect.Value, endLi break } - fieldTk := lex.Expect(ulexer.Identifier()) + fieldTk := ulexer.Expect(lex, ulexer.Identifier()) tField, fieldExists := tObj.FieldByName(fieldTk.String()) vField := vObj.FieldByName(fieldTk.String()) - lex.Expect(ulexer.Contain(":")) + ulexer.Expect(lex, ulexer.Contain(":")) parseMultiValue(lex, []ulexer.Matcher{ulexer.String(), ulexer.Numeral(), @@ -68,20 +108,20 @@ func parseStruct(lex *ulexer.Lexer, tObj reflect.Type, vObj reflect.Value, endLi } } -func parseMultiValue(lex *ulexer.Lexer, mlist []ulexer.Matcher, action ulexer.MatchAction) { +func parseMultiValue(lex *ulexer.Lexer, mlist []ulexer.Matcher, action MatchAction) { - alist := make([]ulexer.MatchAction, 0, len(mlist)) + alist := make([]MatchAction, 0, len(mlist)) for i := 0; i < len(mlist); i++ { alist = append(alist, action) } - lex.SelectAction( + selectAction(lex, mlist, alist, ) } -func parseElement(lex *ulexer.Lexer, endLiteral string, m ulexer.Matcher, onValue ulexer.MatchAction, onEnd func()) { +func parseElement(lex *ulexer.Lexer, endLiteral string, m ulexer.Matcher, onValue MatchAction, onEnd func()) { for { if detectEnd(lex, endLiteral) { @@ -89,7 +129,7 @@ func parseElement(lex *ulexer.Lexer, endLiteral string, m ulexer.Matcher, onValu break } - tk := lex.Expect(m) + tk := ulexer.Expect(lex, m) onValue(tk) } @@ -190,7 +230,7 @@ func parseArray(lex *ulexer.Lexer, tField reflect.Type, vObj reflect.Value, endL break } - lex.Expect(ulexer.Contain("{")) + ulexer.Expect(lex, ulexer.Contain("{")) element := reflect.New(tField.Elem()).Elem() diff --git a/go.mod b/go.mod index 2e7d595..d0dcb3f 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,10 @@ go 1.12 require ( // 测试用 - //github.com/davyxu/cellnet v4.1.0+incompatible github.com/davyxu/golexer v0.1.1-0.20200202091144-a15ddde83f6a github.com/davyxu/ulexer v0.0.0-20200713054812-c9bb8db3521f - //github.com/stretchr/testify v1.7.0 ) replace ( -// github.com/davyxu/cellnet => ../cellnet -// github.com/davyxu/x => ../x -) \ No newline at end of file + github.com/davyxu/ulexer => ../ulexer +)