Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
mei-rune committed Jun 24, 2024
1 parent 4281982 commit 0a6bf65
Showing 1 changed file with 80 additions and 31 deletions.
111 changes: 80 additions & 31 deletions core/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,46 @@ const (
exprTokIdent
exprTokOp
exprTokStr
exprTokFloat
exprTokInt
)

func readToken(txt []rune) (tok int, end int) {
func isExprIdent(c rune) bool {
return unicode.IsDigit(c) ||
('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
c == '.' || c == '_'
}

// func isExprIdentFirst(c rune) bool {
// return unicode.IsDigit(c) ||
// ('a' <= c && c <= 'z') ||
// ('A' <= c && c <= 'Z') ||
// c == '_'
// }

func readExprString(txt []rune) int {
quote := txt[0]
isEscape := false
for i := 1; i < len(txt); i++ {
c := txt[i]
if c == '\\' {
isEscape = !isEscape
continue
}
if c == quote {
if isEscape {
isEscape = false
continue
}
return i + 1
}
isEscape = false
}
return len(txt)
}

func readExprToken(txt []rune) (tok int, end int) {
c := txt[0]
if unicode.IsSpace(c) {
for i := 1; i < len(txt); i++ {
Expand All @@ -24,45 +61,57 @@ func readToken(txt []rune) (tok int, end int) {
return exprTokWS, len(txt)
}

if unicode.IsDigit(c) ||
('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
c == '.' ||
c == '_' {
/////// begin
// 我们无需区分 number 和 ident, 所以下面删除了

// if unicode.IsDigit(c) {
// i := 1
// for ; ; i++ {
// if i >= len(txt) {
// return exprTokInt, len(txt)
// }
// if !unicode.IsDigit(txt[i]) {
// break
// }
// }
// if txt[i] == '.' {
// for ; ; i++ {
// if i >= len(txt) {
// return exprTokFloat, len(txt)
// }
// if !unicode.IsDigit(txt[i]) {
// break
// }
// }
// return exprTokFloat, len(txt)
// }
// return exprTokInt, len(txt)
// }
// if c == '.' {
// for i := 1; i < len(txt); i++ {
// if !unicode.IsDigit(txt[i]) {
// return exprTokIdent, i
// }
// }
// return exprTokFloat, len(txt)
// }

// if isExprIdentFirst(c) {
/////// end

if isExprIdent(c) {
for i := 1; i < len(txt); i++ {
c = txt[i]
if !unicode.IsDigit(c) &&
!('a' <= c && c <= 'z') &&
!('A' <= c && c <= 'Z') &&
c != '.' &&
c != '_' {

if !isExprIdent(c) {
return exprTokIdent, i
}
}
return exprTokIdent, len(txt)
}

if c == '\'' || c == '"' {
quote := c
isEscape := false
for i := 1; i < len(txt); i++ {
c = txt[i]
if c == '\\' {
isEscape = !isEscape
continue
}
if c == quote {
if isEscape {
isEscape = false
continue
}
return exprTokStr, i + 1
}
isEscape = false
}
return exprTokStr, len(txt)
end := readExprString(txt)
return exprTokStr, end
}

return exprTokOp, 1
Expand All @@ -73,7 +122,7 @@ func replaceAndOr(s string) string {
var sb strings.Builder

for len(runes) > 0 {
tok, next := readToken(runes)
tok, next := readExprToken(runes)

// fmt.Println(tok, string(runes[:next]))
if tok != exprTokIdent {
Expand Down

0 comments on commit 0a6bf65

Please sign in to comment.