Skip to content

Commit

Permalink
适配ulexer修改
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Dec 2, 2021
1 parent 2f39919 commit b132d96
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/golang/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")

Expand Down
56 changes: 48 additions & 8 deletions api/golang/text_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand Down Expand Up @@ -68,28 +108,28 @@ 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) {
onEnd()
break
}

tk := lex.Expect(m)
tk := ulexer.Expect(lex, m)
onValue(tk)
}

Expand Down Expand Up @@ -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()

Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
github.com/davyxu/ulexer => ../ulexer
)

0 comments on commit b132d96

Please sign in to comment.