TODO: 解析的正则

This commit is contained in:
huangsimin 2020-03-12 18:27:51 +08:00
parent a0677f2451
commit 28fce16157
2 changed files with 56 additions and 1 deletions

View File

@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
@ -55,6 +57,10 @@ type Metadata struct {
type ValueType int
const (
VT_BOOL ValueType = 00000 + iota
)
const (
VT_INT ValueType = 10000 + iota
VT_INT8
@ -94,7 +100,9 @@ type Field struct {
// Parse 从字符串中解析 var a int; a = 0; a < 0 && a > 5 ;
func (field *Field) Parse(code string) {
codes := strings.Split(code, ";")
re := regexp.MustCompile("var +([a-zA-Z_]+) +[a-zA-Z]+[8|16|32|64]{0,1}")
re.FindStringSubmatch(codes[0])
}
// Table 表结构

47
table_test.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"regexp"
"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; a = 0 ; a >= 1 && a <= 4",
Result: []string{"a", "int"},
},
parseTest{
PS: "var das_das int64; das_das = 0 ; das_das >= 1 && das_das <= 4",
Result: []string{"das_das", "int64"},
},
parseTest{
PS: "var BitTkoen text; BitTkoen = 0 ; BitTkoen >= 1 && BitTkoen <= 4",
Result: []string{"BitTkoen", "text"},
},
parseTest{
PS: "var BitTkoen char(24); BitTkoen = 0 ; BitTkoen >= 1 && BitTkoen <= 4",
Result: []string{"BitTkoen", "text"},
},
)
for _, parse := range parseCollection {
codes := strings.Split(parse.PS, ";")
result := ReKeyDefined.FindStringSubmatch(codes[0])
if len(result) >= 3 && !(result[1] == parse.Result[0] && result[2] == parse.Result[1]) {
t.Error(result)
}
t.Error(result)
}
}