123 lines
2.4 KiB
Go
123 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
var ReKeyDefined = regexp.MustCompile("var +([a-zA-Z_]+) +([a-zA-Z0-9]+) *(\\([0-9]+\\))")
|
|
|
|
type parseTest struct {
|
|
PS string
|
|
Result []string
|
|
}
|
|
|
|
func TestParseRe(t *testing.T) {
|
|
|
|
var parseCollection []parseTest
|
|
parseCollection = append(parseCollection,
|
|
parseTest{
|
|
PS: "var a int = 0; ($ >= 1 && $ <= 4)?$+1:$-1 ; $+=1",
|
|
Result: []string{"a", "int"},
|
|
},
|
|
parseTest{
|
|
PS: "var das_das int64; $ = 0 ; if $ >= 1 && $ <= 4 { push() }",
|
|
Result: []string{"das_das", "int64"},
|
|
},
|
|
parseTest{
|
|
PS: "var BitTkoen []text; BitTkoen = 0 ; BitTkoen >= 1 && BitTkoen <= 4",
|
|
Result: []string{"BitTkoen", "text"},
|
|
},
|
|
parseTest{
|
|
PS: "var BitTkoen [24]char; BitTkoen = 0 ; BitTkoen >= 1 && BitTkoen <= 4",
|
|
Result: []string{"BitTkoen", "text"},
|
|
},
|
|
)
|
|
|
|
field := &Field{}
|
|
|
|
for _, parse := range parseCollection {
|
|
codes := strings.Split(parse.PS, ";")
|
|
definestr := codes[0]
|
|
i := 0
|
|
|
|
// 检查定义头是否存在
|
|
for ; i < len(definestr); i++ {
|
|
if definestr[i] == 'v' && definestr[i+1] == 'a' && definestr[i+2] == 'r' {
|
|
i += 2
|
|
|
|
skipSpace(&definestr, &i)
|
|
|
|
GET_KEY:
|
|
for ; i < len(definestr); i++ {
|
|
switch c := definestr[i]; {
|
|
case (c <= 'Z' && c >= 'A' && c <= 'z' && c >= 'a') || c == '_':
|
|
field.Key = append(field.Key, c)
|
|
case c == ' ':
|
|
break GET_KEY
|
|
default:
|
|
t.Error("错误")
|
|
}
|
|
}
|
|
|
|
skipSpace(&definestr, &i)
|
|
|
|
for ; i < len(definestr); i++ {
|
|
switch c := definestr[i]; {
|
|
case c == ' ':
|
|
continue
|
|
case (c <= 'Z' && c >= 'A' && c <= 'z' && c >= 'a'):
|
|
|
|
var valueType []byte
|
|
for ; i < len(definestr); i++ {
|
|
cc := definestr[i]
|
|
if cc == ';' {
|
|
break
|
|
}
|
|
valueType = append(valueType, cc)
|
|
}
|
|
|
|
case c == '[':
|
|
i++
|
|
|
|
var arrayNum []byte
|
|
GET_ARRAY:
|
|
for ; i < len(definestr); i++ {
|
|
switch cc := definestr[i]; {
|
|
case cc == ']':
|
|
break GET_ARRAY
|
|
default:
|
|
arrayNum = append(arrayNum, cc)
|
|
|
|
}
|
|
}
|
|
|
|
if arrayNum == nil {
|
|
field.ArrayNumber = -1
|
|
} else {
|
|
num, err := strconv.Atoi(string(arrayNum))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
field.ArrayNumber = num
|
|
}
|
|
|
|
default:
|
|
t.Error("错误")
|
|
}
|
|
}
|
|
|
|
} else {
|
|
t.Error("错误")
|
|
break
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
t.Error(field)
|
|
|
|
}
|