diff --git a/tars/tools/tars2go/ast/ast.go b/tars/tools/tars2go/ast/ast.go index be145d24..62023820 100644 --- a/tars/tools/tars2go/ast/ast.go +++ b/tars/tools/tars2go/ast/ast.go @@ -37,8 +37,8 @@ func (a StructMemberSorter) Len() int { return len(a) } func (a StructMemberSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a StructMemberSorter) Less(i, j int) bool { return a[i].Tag < a[j].Tag } -// StructInfo record struct information. -type StructInfo struct { +// Struct record struct information. +type Struct struct { Name string OriginName string //original name Mb []StructMember @@ -46,28 +46,28 @@ type StructInfo struct { DependModuleWithJce map[string]string } -// ArgInfo record argument information. -type ArgInfo struct { +// Arg record argument information. +type Arg struct { Name string OriginName string //original name IsOut bool Type *VarType } -// FunInfo record function information. -type FunInfo struct { +// Func record function information. +type Func struct { Name string // after the uppercase converted name OriginName string // original name HasRet bool RetType *VarType - Args []ArgInfo + Args []Arg } -// InterfaceInfo record interface information. -type InterfaceInfo struct { +// Interface record interface information. +type Interface struct { Name string OriginName string // original name - Fun []FunInfo + Funcs []Func DependModule map[string]bool DependModuleWithJce map[string]string } @@ -80,79 +80,87 @@ type EnumMember struct { Name string //type 1 } -// EnumInfo record EnumMember information include name. -type EnumInfo struct { +// Enum record EnumMember information include name. +type Enum struct { Module string Name string OriginName string // original name Mb []EnumMember } -// ConstInfo record const information. -type ConstInfo struct { +// Const record const information. +type Const struct { Type *VarType Name string OriginName string // original name Value string } -// HashKeyInfo record hash key information. -type HashKeyInfo struct { +// HashKey record hash key information. +type HashKey struct { Name string Member []string } -type ModuleInfo struct { +type TarsFile struct { Source string // proto file name(not include .tars) - ProtoName string + ProtoName string + Module Module + + Include []string + // have parsed include file + IncTarsFile []*TarsFile +} + +type Module struct { Name string OriginName string - Include []string - Struct []StructInfo - HashKey []HashKeyInfo - Enum []EnumInfo - Const []ConstInfo - Interface []InterfaceInfo - - // have parsed include file - IncModule []*ModuleInfo + Struct []Struct + HashKey []HashKey + Enum []Enum + Const []Const + Interface []Interface } // Rename module -func (p *ModuleInfo) Rename(moduleUpper bool) { - p.OriginName = p.Name +func (m *Module) Rename(moduleUpper bool) { + m.OriginName = m.Name if moduleUpper { - p.Name = utils.UpperFirstLetter(p.Name) + m.Name = utils.UpperFirstLetter(m.Name) } } +func (tf *TarsFile) Rename(moduleUpper bool) { + tf.Module.Rename(moduleUpper) +} + // FindTNameType Looking for the true type of user-defined identifier -func (p *ModuleInfo) FindTNameType(tname string) (token.Type, string, string) { - for _, v := range p.Struct { - if p.Name+"::"+v.Name == tname { - return token.Struct, p.Name, p.ProtoName +func (tf *TarsFile) FindTNameType(tName string) (token.Type, string, string) { + for _, v := range tf.Module.Struct { + if tf.Module.Name+"::"+v.Name == tName { + return token.Struct, tf.Module.Name, tf.ProtoName } } - for _, v := range p.Enum { - if p.Name+"::"+v.Name == tname { - return token.Enum, p.Name, p.ProtoName + for _, v := range tf.Module.Enum { + if tf.Module.Name+"::"+v.Name == tName { + return token.Enum, tf.Module.Name, tf.ProtoName } } - for _, pInc := range p.IncModule { - ret, mod, protoName := pInc.FindTNameType(tname) + for _, tarsFile := range tf.IncTarsFile { + ret, mod, protoName := tarsFile.FindTNameType(tName) if ret != token.Name { return ret, mod, protoName } } // not find - return token.Name, p.Name, p.ProtoName + return token.Name, tf.Module.Name, tf.ProtoName } -func (p *ModuleInfo) FindEnumName(ename string, moduleCycle bool) (*EnumMember, *EnumInfo, error) { +func (tf *TarsFile) FindEnumName(ename string, moduleCycle bool) (*EnumMember, *Enum, error) { if strings.Contains(ename, "::") { vec := strings.Split(ename, "::") if len(vec) >= 2 { @@ -160,24 +168,24 @@ func (p *ModuleInfo) FindEnumName(ename string, moduleCycle bool) (*EnumMember, } } var cmb *EnumMember - var cenum *EnumInfo - for ek, enum := range p.Enum { + var cenum *Enum + for ek, enum := range tf.Module.Enum { for mk, mb := range enum.Mb { if mb.Key != ename { continue } if cmb == nil { cmb = &enum.Mb[mk] - cenum = &p.Enum[ek] + cenum = &tf.Module.Enum[ek] } else { return nil, nil, errors.New(ename + " name conflict [" + cenum.Name + "::" + cmb.Key + " or " + enum.Name + "::" + mb.Key) } } } var err error - for _, pInc := range p.IncModule { + for _, tarsFile := range tf.IncTarsFile { if cmb == nil { - cmb, cenum, err = pInc.FindEnumName(ename, moduleCycle) + cmb, cenum, err = tarsFile.FindEnumName(ename, moduleCycle) if err != nil { return cmb, cenum, err } @@ -187,17 +195,16 @@ func (p *ModuleInfo) FindEnumName(ename string, moduleCycle bool) (*EnumMember, } if cenum != nil && cenum.Module == "" { if moduleCycle { - cenum.Module = p.ProtoName + "_" + p.Name + cenum.Module = tf.ProtoName + "_" + tf.Module.Name } else { - cenum.Module = p.Name + cenum.Module = tf.Module.Name } } return cmb, cenum, nil } -// Rename struct -// struct Name { 1 require Mb type} -func (st *StructInfo) Rename() { +// Rename Struct Name { 1 require Mb type} +func (st *Struct) Rename() { st.OriginName = st.Name st.Name = utils.UpperFirstLetter(st.Name) for i := range st.Mb { @@ -206,17 +213,17 @@ func (st *StructInfo) Rename() { } } -// Rename interface -// interface Name { Fun } -func (itf *InterfaceInfo) Rename() { +// Rename Interface Name { Funcs } +func (itf *Interface) Rename() { itf.OriginName = itf.Name itf.Name = utils.UpperFirstLetter(itf.Name) - for i := range itf.Fun { - itf.Fun[i].Rename() + for i := range itf.Funcs { + itf.Funcs[i].Rename() } } -func (en *EnumInfo) Rename() { +// Rename Enum Name { Mb } +func (en *Enum) Rename() { en.OriginName = en.Name en.Name = utils.UpperFirstLetter(en.Name) for i := range en.Mb { @@ -224,20 +231,21 @@ func (en *EnumInfo) Rename() { } } -func (cst *ConstInfo) Rename() { +// Rename Const Name +func (cst *Const) Rename() { cst.OriginName = cst.Name cst.Name = utils.UpperFirstLetter(cst.Name) } -// Rename func -// type Fun (arg ArgType), in case keyword and name conflicts,argname need to capitalize. -// Fun (type int32) -func (fun *FunInfo) Rename() { +// Rename Func Name { Args } +// type Funcs (arg ArgType), in case keyword and name conflicts, arg name need to capitalize. +// Funcs (type int32) +func (fun *Func) Rename() { fun.OriginName = fun.Name fun.Name = utils.UpperFirstLetter(fun.Name) for i := range fun.Args { fun.Args[i].OriginName = fun.Args[i].Name - // func args donot upper firs - //fun.Args[i].Name = utils.UpperFirstLetter(fun.Args[i].Name) + // func args do not upper firs + // fun.Args[i].Name = utils.UpperFirstLetter(fun.Args[i].Name) } } diff --git a/tars/tools/tars2go/gencode/gen_go.go b/tars/tools/tars2go/gencode/gen_go.go index 07379d47..507aafbd 100755 --- a/tars/tools/tars2go/gencode/gen_go.go +++ b/tars/tools/tars2go/gencode/gen_go.go @@ -30,7 +30,7 @@ type GenGo struct { vc int // var count. Used to generate unique variable names filepath string prefix string - module *ast.ModuleInfo + tarsFile *ast.TarsFile // proto file name(not include .tars) ProtoName string @@ -78,7 +78,7 @@ func (g *GenGo) Gen() { } }() - g.module = parse.NewParse(g.opt, g.filepath, make([]string, 0)) + g.tarsFile = parse.NewParse(g.opt, g.filepath, make([]string, 0)) g.genAll() } @@ -96,34 +96,34 @@ func (g *GenGo) W(v ...string) { } func (g *GenGo) genAll() { - g.module.Rename(g.opt.ModuleUpper) - key := g.module.Source + ":" + g.module.Name + g.tarsFile.Rename(g.opt.ModuleUpper) + key := g.tarsFile.Source + ":" + g.tarsFile.Module.Name if _, ok := fileMap.Load(key); ok { // already compiled return } fileMap.Store(key, struct{}{}) - g.genInclude(g.module.IncModule) + g.genInclude(g.tarsFile.IncTarsFile) g.code.Reset() g.genHead() g.genPackage() - for _, v := range g.module.Enum { + for _, v := range g.tarsFile.Module.Enum { g.genEnum(&v) } - g.genConst(g.module.Const) + g.genConst(g.tarsFile.Module.Const) - for _, v := range g.module.Struct { + for _, v := range g.tarsFile.Module.Struct { g.genStruct(&v) } - if len(g.module.Enum) > 0 || len(g.module.Const) > 0 || len(g.module.Struct) > 0 { + if len(g.tarsFile.Module.Enum) > 0 || len(g.tarsFile.Module.Const) > 0 || len(g.tarsFile.Module.Struct) > 0 { g.saveToSourceFile(utils.Path2ProtoName(g.filepath) + ".go") } - for _, v := range g.module.Interface { + for _, v := range g.tarsFile.Module.Interface { g.genInterface(&v) } } @@ -156,9 +156,9 @@ func (g *GenGo) saveToSourceFile(filename string) { } else { var mkPath string if g.opt.ModuleCycle { - mkPath = prefix + g.ProtoName + "/" + g.module.Name + mkPath = prefix + g.ProtoName + "/" + g.tarsFile.Module.Name } else { - mkPath = prefix + g.module.Name + mkPath = prefix + g.tarsFile.Module.Name } err = os.MkdirAll(mkPath, 0766) if err != nil { @@ -181,20 +181,21 @@ func (g *GenGo) genVariableName(prefix, name string) string { func (g *GenGo) genHead() { g.P("// Code generated by tars2go ", version.VERSION, ", DO NOT EDIT.") - g.P("// This file was generated from ", filepath.Base(g.filepath)) - g.P("// Package ", g.module.Name, " comment") + g.P("// This file was generated from ", g.tarsFile.Source) + g.P("// Package ", g.tarsFile.Module.Name, " comment") } func (g *GenGo) genPackage() { - g.P("package ", g.module.Name) + g.P("package ", g.tarsFile.Module.Name) g.P() g.P("import (") g.P(strconv.Quote("fmt")) g.P() g.P(strconv.Quote(g.opt.TarsPath + "/protocol/codec")) + g.P() mImports := make(map[string]bool) - for _, st := range g.module.Struct { + for _, st := range g.tarsFile.Module.Struct { if g.opt.ModuleCycle { for k, v := range st.DependModuleWithJce { g.genStructImport(k, v, mImports) @@ -260,8 +261,8 @@ func (g *GenGo) genStructImport(module string, protoName string, mImports map[st mImports[moduleAlia+strconv.Quote(modulePath)] = true } -func (g *GenGo) genIFPackage(itf *ast.InterfaceInfo) { - g.P("package " + g.module.Name) +func (g *GenGo) genIFPackage(itf *ast.Interface) { + g.P("package " + g.tarsFile.Module.Name) g.P() g.P("import (") @@ -288,6 +289,7 @@ func (g *GenGo) genIFPackage(itf *ast.InterfaceInfo) { g.P("tarstrace ", strconv.Quote(tarsPath+"/util/trace")) } + g.P() if g.opt.ModuleCycle { for k, v := range itf.DependModuleWithJce { g.genIFImport(k, v) @@ -430,7 +432,7 @@ func (g *GenGo) genType(ty *ast.VarType) string { return ret } -func (g *GenGo) genStructDefine(st *ast.StructInfo) { +func (g *GenGo) genStructDefine(st *ast.Struct) { g.P("// ", st.Name, " struct implement") g.P("type ", st.Name, " struct {") for _, v := range st.Mb { @@ -444,7 +446,7 @@ func (g *GenGo) genStructDefine(st *ast.StructInfo) { g.P("}") } -func (g *GenGo) genFunResetDefault(st *ast.StructInfo) { +func (g *GenGo) genFunResetDefault(st *ast.Struct) { g.P("func (st *", st.Name, ") ResetDefault() {") for _, v := range st.Mb { if v.Type.CType == token.Struct { @@ -604,7 +606,7 @@ func (g *GenGo) genWriteVar(v *ast.StructMember, prefix string, hasRet bool) { } } -func (g *GenGo) genFunWriteBlock(st *ast.StructInfo) { +func (g *GenGo) genFunWriteBlock(st *ast.Struct) { // WriteBlock function head g.P("// WriteBlock encode struct") g.P("func (st *", st.Name, ") WriteBlock(buf *codec.Buffer, tag byte) error {") @@ -627,7 +629,7 @@ func (g *GenGo) genFunWriteBlock(st *ast.StructInfo) { g.P("}") } -func (g *GenGo) genFunWriteTo(st *ast.StructInfo) { +func (g *GenGo) genFunWriteTo(st *ast.Struct) { g.P("// WriteTo encode struct to buffer") g.P("func (st *", st.Name, ") WriteTo(buf *codec.Buffer) (err error) {") for _, v := range st.Mb { @@ -816,7 +818,7 @@ func (g *GenGo) genReadVar(v *ast.StructMember, prefix string, hasRet bool) { } } -func (g *GenGo) genFunReadFrom(st *ast.StructInfo) { +func (g *GenGo) genFunReadFrom(st *ast.Struct) { g.P(`// ReadFrom reads from readBuf and put into struct. func (st *`, st.Name, `) ReadFrom(readBuf *codec.Reader) error { var ( @@ -841,7 +843,7 @@ func (st *`, st.Name, `) ReadFrom(readBuf *codec.Reader) error { }`) } -func (g *GenGo) genFunReadBlock(st *ast.StructInfo) { +func (g *GenGo) genFunReadBlock(st *ast.Struct) { g.P(`// ReadBlock reads struct from the given tag , require or optional. func (st *`, st.Name, `) ReadBlock(readBuf *codec.Reader, tag byte, require bool) error { var ( @@ -875,7 +877,7 @@ func (st *`, st.Name, `) ReadBlock(readBuf *codec.Reader, tag byte, require bool }`) } -func (g *GenGo) genStruct(st *ast.StructInfo) { +func (g *GenGo) genStruct(st *ast.Struct) { g.vc = 0 st.Rename() @@ -889,11 +891,11 @@ func (g *GenGo) genStruct(st *ast.StructInfo) { g.genFunWriteBlock(st) } -func (g *GenGo) makeEnumName(en *ast.EnumInfo, mb *ast.EnumMember) string { +func (g *GenGo) makeEnumName(en *ast.Enum, mb *ast.EnumMember) string { return utils.UpperFirstLetter(en.Name) + "_" + utils.UpperFirstLetter(mb.Key) } -func (g *GenGo) genEnum(en *ast.EnumInfo) { +func (g *GenGo) genEnum(en *ast.Enum) { if len(en.Mb) == 0 { return } @@ -935,29 +937,29 @@ func (g *GenGo) genEnum(en *ast.EnumInfo) { g.P(")") } -func (g *GenGo) genConst(cst []ast.ConstInfo) { +func (g *GenGo) genConst(cst []ast.Const) { if len(cst) == 0 { return } g.P("//const as define in tars file") g.P("const (") - for _, v := range g.module.Const { + for _, v := range g.tarsFile.Module.Const { v.Rename() g.P(v.Name, " ", g.genType(v.Type), " = ", v.Value) } g.P(")") } -func (g *GenGo) genInclude(modules []*ast.ModuleInfo) { +func (g *GenGo) genInclude(modules []*ast.TarsFile) { for _, module := range modules { - genModule := NewGenGo(g.opt, module.Name+module.Source) - genModule.module = module + genModule := NewGenGo(g.opt, module.Module.Name+module.Source) + genModule.tarsFile = module genModule.genAll() } } -func (g *GenGo) genInterface(itf *ast.InterfaceInfo) { +func (g *GenGo) genInterface(itf *ast.Interface) { g.code.Reset() itf.Rename() @@ -975,7 +977,7 @@ func (g *GenGo) genInterface(itf *ast.InterfaceInfo) { g.saveToSourceFile(itf.Name + ".tars.go") } -func (g *GenGo) genIFProxy(itf *ast.InterfaceInfo) { +func (g *GenGo) genIFProxy(itf *ast.Interface) { g.P("// ", itf.Name, " struct") g.P("type ", itf.Name, ` struct { servant model.Servant @@ -1025,14 +1027,14 @@ func (obj *`, itf.Name, `) AddServantWithContext(imp `, itf.Name, `ServantWithCo }`) } - for _, v := range itf.Fun { + for _, v := range itf.Funcs { g.genIFProxyFun(itf.Name, &v, false, false) g.genIFProxyFun(itf.Name, &v, true, false) g.genIFProxyFun(itf.Name, &v, true, true) } } -func (g *GenGo) genIFProxyFun(interfName string, fun *ast.FunInfo, withContext bool, isOneWay bool) { +func (g *GenGo) genIFProxyFun(interfName string, fun *ast.Func, withContext bool, isOneWay bool) { if withContext { if isOneWay { g.P("// ", fun.Name, "OneWayWithContext is the proxy function for the method defined in the tars file, with the context") @@ -1230,7 +1232,7 @@ if ok && trace.Call() { g.P("}") } -func (g *GenGo) genArgs(args []ast.ArgInfo) { +func (g *GenGo) genArgs(args []ast.Arg) { for _, arg := range args { g.W(arg.Name, " ") if arg.IsOut || arg.Type.CType == token.Struct { @@ -1240,7 +1242,7 @@ func (g *GenGo) genArgs(args []ast.ArgInfo) { } } -func (g *GenGo) genCallArgs(args []ast.ArgInfo) { +func (g *GenGo) genCallArgs(args []ast.Arg) { for _, arg := range args { if arg.IsOut || arg.Type.CType == token.Struct { g.W("&", arg.Name, ",") @@ -1250,23 +1252,23 @@ func (g *GenGo) genCallArgs(args []ast.ArgInfo) { } } -func (g *GenGo) genIFServer(itf *ast.InterfaceInfo) { +func (g *GenGo) genIFServer(itf *ast.Interface) { g.P("type ", itf.Name, "Servant interface {") - for _, v := range itf.Fun { + for _, v := range itf.Funcs { g.genIFServerFun(&v) } g.P("}") } -func (g *GenGo) genIFServerWithContext(itf *ast.InterfaceInfo) { +func (g *GenGo) genIFServerWithContext(itf *ast.Interface) { g.P("type ", itf.Name, "ServantWithContext interface {") - for _, v := range itf.Fun { + for _, v := range itf.Funcs { g.genIFServerFunWithContext(&v) } g.P("}") } -func (g *GenGo) genIFServerFun(fun *ast.FunInfo) { +func (g *GenGo) genIFServerFun(fun *ast.Func) { g.W(fun.Name, "(") g.genArgs(fun.Args) g.W(") (") @@ -1277,7 +1279,7 @@ func (g *GenGo) genIFServerFun(fun *ast.FunInfo) { g.P("err error)") } -func (g *GenGo) genIFServerFunWithContext(fun *ast.FunInfo) { +func (g *GenGo) genIFServerFunWithContext(fun *ast.Func) { g.W(fun.Name, "(tarsCtx context.Context, ") g.genArgs(fun.Args) g.W(") (") @@ -1288,7 +1290,7 @@ func (g *GenGo) genIFServerFunWithContext(fun *ast.FunInfo) { g.P("err error)") } -func (g *GenGo) genIFDispatch(itf *ast.InterfaceInfo) { +func (g *GenGo) genIFDispatch(itf *ast.Interface) { g.P("// Dispatch is used to call the server side implement for the method defined in the tars file. withContext shows using context or not. ") g.P("func(obj *", itf.Name, `) Dispatch(tarsCtx context.Context, val interface{}, tarsReq *requestf.RequestPacket, tarsResp *requestf.ResponsePacket, withContext bool) (err error) { var ( @@ -1298,7 +1300,7 @@ func (g *GenGo) genIFDispatch(itf *ast.InterfaceInfo) { )`) var param bool - for _, v := range itf.Fun { + for _, v := range itf.Funcs { if len(v.Args) > 0 { param = true break @@ -1312,7 +1314,7 @@ func (g *GenGo) genIFDispatch(itf *ast.InterfaceInfo) { } g.P(`buf := codec.NewBuffer() switch tarsReq.SFuncName {`) - for _, v := range itf.Fun { + for _, v := range itf.Funcs { g.genSwitchCase(itf.Name, &v) } @@ -1349,7 +1351,7 @@ func (g *GenGo) genIFDispatch(itf *ast.InterfaceInfo) { }`) } -func (g *GenGo) genSwitchCase(tname string, fun *ast.FunInfo) { +func (g *GenGo) genSwitchCase(tname string, fun *ast.Func) { g.P("case ", strconv.Quote(fun.OriginName), ":") inArgsCount := 0 outArgsCount := 0 diff --git a/tars/tools/tars2go/parse/parse.go b/tars/tools/tars2go/parse/parse.go index ae588d97..26c29e41 100644 --- a/tars/tools/tars2go/parse/parse.go +++ b/tars/tools/tars2go/parse/parse.go @@ -17,12 +17,12 @@ import ( // Parse record information of parse file. type Parse struct { - opt *options.Options - Module *ast.ModuleInfo + opt *options.Options - lex *lexer.LexState - tk *token.Token - lastTk *token.Token + lex *lexer.LexState + tk *token.Token + lastTk *token.Token + tarsFile *ast.TarsFile // jce include chain IncChain []string @@ -32,7 +32,7 @@ type Parse struct { } // NewParse parse a file,return grammar tree. -func NewParse(opt *options.Options, filePath string, incChain []string) *ast.ModuleInfo { +func NewParse(opt *options.Options, filePath string, incChain []string) *ast.TarsFile { if _, err := os.Stat(filePath); os.IsNotExist(err) { // 查找tars文件路径 filename := path.Base(filePath) @@ -53,7 +53,7 @@ func NewParse(opt *options.Options, filePath string, incChain []string) *ast.Mod p := newParse(opt, filePath, b, incChain) p.parse() - return p.Module + return p.tarsFile } func newParse(opt *options.Options, source string, data []byte, incChain []string) *Parse { @@ -67,7 +67,7 @@ func newParse(opt *options.Options, source string, data []byte, incChain []strin p := &Parse{ opt: opt, - Module: &ast.ModuleInfo{ + tarsFile: &ast.TarsFile{ Source: source, ProtoName: utils.Path2ProtoName(source), }, @@ -84,7 +84,7 @@ func (p *Parse) parseErr(err string) { line = strconv.Itoa(p.tk.Line) } - panic(p.Module.Source + ": " + line + ". " + err) + panic(p.tarsFile.Source + ": " + line + ". " + err) } func (p *Parse) next() { @@ -141,10 +141,10 @@ func (p *Parse) parseType() *ast.VarType { } func (p *Parse) parseEnum() { - enum := ast.EnumInfo{} + enum := ast.Enum{} p.expect(token.Name) enum.Name = p.tk.S.S - for _, v := range p.Module.Enum { + for _, v := range p.tarsFile.Module.Enum { if v.Name == enum.Name { p.parseErr(enum.Name + " Redefine.") } @@ -191,7 +191,7 @@ LFOR: } } p.expect(token.Semi) - p.Module.Enum = append(p.Module.Enum, enum) + p.tarsFile.Module.Enum = append(p.tarsFile.Module.Enum, enum) } func (p *Parse) parseStructMemberDefault(m *ast.StructMember) { @@ -290,7 +290,7 @@ func (p *Parse) parseStructMember() *ast.StructMember { return m } -func (p *Parse) checkTag(st *ast.StructInfo) { +func (p *Parse) checkTag(st *ast.Struct) { set := make(map[int32]bool) for _, v := range st.Mb { if set[v.Tag] { @@ -300,15 +300,15 @@ func (p *Parse) checkTag(st *ast.StructInfo) { } } -func (p *Parse) sortTag(st *ast.StructInfo) { +func (p *Parse) sortTag(st *ast.Struct) { sort.Sort(ast.StructMemberSorter(st.Mb)) } func (p *Parse) parseStruct() { - st := ast.StructInfo{} + st := ast.Struct{} p.expect(token.Name) st.Name = p.tk.S.S - for _, v := range p.Module.Struct { + for _, v := range p.tarsFile.Module.Struct { if v.Name == st.Name { p.parseErr(st.Name + " Redefine.") } @@ -327,11 +327,11 @@ func (p *Parse) parseStruct() { p.checkTag(&st) p.sortTag(&st) - p.Module.Struct = append(p.Module.Struct, st) + p.tarsFile.Module.Struct = append(p.tarsFile.Module.Struct, st) } -func (p *Parse) parseInterfaceFun() *ast.FunInfo { - fun := &ast.FunInfo{} +func (p *Parse) parseInterfaceFun() *ast.Func { + fun := &ast.Func{} p.next() if p.tk.T == token.BraceRight { return nil @@ -360,7 +360,7 @@ func (p *Parse) parseInterfaceFun() *ast.FunInfo { } for { - arg := &ast.ArgInfo{} + arg := &ast.Arg{} if p.tk.T == token.Out { arg.IsOut = true p.next() @@ -390,10 +390,10 @@ func (p *Parse) parseInterfaceFun() *ast.FunInfo { } func (p *Parse) parseInterface() { - itf := &ast.InterfaceInfo{} + itf := &ast.Interface{} p.expect(token.Name) itf.Name = p.tk.S.S - for _, v := range p.Module.Interface { + for _, v := range p.tarsFile.Module.Interface { if v.Name == itf.Name { p.parseErr(itf.Name + " Redefine.") } @@ -405,14 +405,14 @@ func (p *Parse) parseInterface() { if fun == nil { break } - itf.Fun = append(itf.Fun, *fun) + itf.Funcs = append(itf.Funcs, *fun) } p.expect(token.Semi) //semicolon at the end of struct. - p.Module.Interface = append(p.Module.Interface, *itf) + p.tarsFile.Module.Interface = append(p.tarsFile.Module.Interface, *itf) } func (p *Parse) parseConst() { - m := ast.ConstInfo{} + m := ast.Const{} // type p.next() @@ -460,11 +460,11 @@ func (p *Parse) parseConst() { } p.expect(token.Semi) - p.Module.Const = append(p.Module.Const, m) + p.tarsFile.Module.Const = append(p.tarsFile.Module.Const, m) } func (p *Parse) parseHashKey() { - hashKey := ast.HashKeyInfo{} + hashKey := ast.HashKey{} p.expect(token.SquareLeft) p.expect(token.Name) hashKey.Name = p.tk.S.S @@ -477,7 +477,7 @@ func (p *Parse) parseHashKey() { switch t.T { case token.SquarerRight: p.expect(token.Semi) - p.Module.HashKey = append(p.Module.HashKey, hashKey) + p.tarsFile.Module.HashKey = append(p.tarsFile.Module.HashKey, hashKey) return case token.Comma: default: @@ -516,43 +516,43 @@ func (p *Parse) parseModule() { p.expect(token.Name) // 解决一个tars文件中定义多个module - if p.Module.Name != "" { - name := p.Module.ProtoName + "_" + p.tk.S.S + ".tars" - newp := newParse(p.opt, p.Module.Source, nil, nil) - newp.Module.Name = p.tk.S.S - newp.Module.Include = p.Module.Include - m := *p.Module - newp.Module.IncModule = append(newp.Module.IncModule, &m) + if p.tarsFile.Module.Name != "" { + name := p.tarsFile.ProtoName + "_" + p.tk.S.S + ".tars" + newp := newParse(p.opt, p.tarsFile.Source, nil, nil) + newp.tarsFile.Module.Name = p.tk.S.S + newp.tarsFile.Include = p.tarsFile.Include + tf := *p.tarsFile + newp.tarsFile.IncTarsFile = append(newp.tarsFile.IncTarsFile, &tf) newp.lex = p.lex newp.parseModuleSegment() newp.analyzeDepend() if p.fileNames[name] { // merge - for _, module := range p.Module.IncModule { - if module.Name == newp.Module.Name { - module.Struct = append(module.Struct, newp.Module.Struct...) - module.Interface = append(module.Interface, newp.Module.Interface...) - module.Enum = append(module.Enum, newp.Module.Enum...) - module.Const = append(module.Const, newp.Module.Const...) - module.HashKey = append(module.HashKey, newp.Module.HashKey...) + for _, tarsFile := range p.tarsFile.IncTarsFile { + if tarsFile.Module.Name == newp.tarsFile.Module.Name { + tarsFile.Module.Struct = append(tarsFile.Module.Struct, newp.tarsFile.Module.Struct...) + tarsFile.Module.Interface = append(tarsFile.Module.Interface, newp.tarsFile.Module.Interface...) + tarsFile.Module.Enum = append(tarsFile.Module.Enum, newp.tarsFile.Module.Enum...) + tarsFile.Module.Const = append(tarsFile.Module.Const, newp.tarsFile.Module.Const...) + tarsFile.Module.HashKey = append(tarsFile.Module.HashKey, newp.tarsFile.Module.HashKey...) break } } } else { // 增加已经解析的module - p.Module.IncModule = append(p.Module.IncModule, newp.Module) + p.tarsFile.IncTarsFile = append(p.tarsFile.IncTarsFile, newp.tarsFile) p.fileNames[name] = true } p.lex = newp.lex } else { - p.Module.Name = p.tk.S.S + p.tarsFile.Module.Name = p.tk.S.S p.parseModuleSegment() } } func (p *Parse) parseInclude() { p.expect(token.String) - p.Module.Include = append(p.Module.Include, p.tk.S.S) + p.tarsFile.Include = append(p.tarsFile.Include, p.tk.S.S) } func addToSet(m *map[string]bool, module string) { @@ -573,17 +573,17 @@ func (p *Parse) checkDepTName(ty *ast.VarType, dm *map[string]bool, dmj *map[str if ty.Type == token.Name { name := ty.TypeSt if strings.Count(name, "::") == 0 { - name = p.Module.Name + "::" + name + name = p.tarsFile.Module.Name + "::" + name } mod := "" protoName := "" - ty.CType, mod, protoName = p.Module.FindTNameType(name) + ty.CType, mod, protoName = p.tarsFile.FindTNameType(name) if ty.CType == token.Name { p.parseErr(ty.TypeSt + " not find define") } if p.opt.ModuleCycle { - if mod != p.Module.Name || protoName != p.Module.ProtoName { + if mod != p.tarsFile.Module.Name || protoName != p.tarsFile.ProtoName { var modStr string if p.opt.ModuleUpper { modStr = utils.UpperFirstLetter(mod) @@ -602,7 +602,7 @@ func (p *Parse) checkDepTName(ty *ast.VarType, dm *map[string]bool, dmj *map[str ty.TypeSt = strings.Replace(ty.TypeSt, mod+"::", "", 1) } } else { - if mod != p.Module.Name { + if mod != p.tarsFile.Module.Name { addToSet(dm, mod) } else { // the same Module ,do not add self. @@ -619,31 +619,31 @@ func (p *Parse) checkDepTName(ty *ast.VarType, dm *map[string]bool, dmj *map[str // analysis custom type,whether have definition func (p *Parse) analyzeTName() { - for i, v := range p.Module.Struct { + for i, v := range p.tarsFile.Module.Struct { for _, v := range v.Mb { ty := v.Type - p.checkDepTName(ty, &p.Module.Struct[i].DependModule, &p.Module.Struct[i].DependModuleWithJce) + p.checkDepTName(ty, &p.tarsFile.Module.Struct[i].DependModule, &p.tarsFile.Module.Struct[i].DependModuleWithJce) } } - for i, v := range p.Module.Interface { - for _, v := range v.Fun { + for i, v := range p.tarsFile.Module.Interface { + for _, v := range v.Funcs { for _, v := range v.Args { ty := v.Type - p.checkDepTName(ty, &p.Module.Interface[i].DependModule, &p.Module.Interface[i].DependModuleWithJce) + p.checkDepTName(ty, &p.tarsFile.Module.Interface[i].DependModule, &p.tarsFile.Module.Interface[i].DependModuleWithJce) } if v.RetType != nil { - p.checkDepTName(v.RetType, &p.Module.Interface[i].DependModule, &p.Module.Interface[i].DependModuleWithJce) + p.checkDepTName(v.RetType, &p.tarsFile.Module.Interface[i].DependModule, &p.tarsFile.Module.Interface[i].DependModuleWithJce) } } } } func (p *Parse) analyzeDefault() { - for _, v := range p.Module.Struct { + for _, v := range p.tarsFile.Module.Struct { for i, r := range v.Mb { if r.Default != "" && r.DefType == token.Name { - mb, enum, err := p.Module.FindEnumName(r.Default, p.opt.ModuleCycle) + mb, enum, err := p.tarsFile.FindEnumName(r.Default, p.opt.ModuleCycle) if err != nil { p.parseErr(err.Error()) } @@ -653,9 +653,9 @@ func (p *Parse) analyzeDefault() { defValue := enum.Name + "_" + utils.UpperFirstLetter(mb.Key) var currModule string if p.opt.ModuleCycle { - currModule = p.Module.ProtoName + "_" + p.Module.Name + currModule = p.tarsFile.ProtoName + "_" + p.tarsFile.Module.Name } else { - currModule = p.Module.Name + currModule = p.tarsFile.Module.Name } if len(enum.Module) > 0 && currModule != enum.Module { defValue = enum.Module + "." + defValue @@ -672,11 +672,11 @@ func (p *Parse) analyzeHashKey() { } func (p *Parse) analyzeDepend() { - for _, v := range p.Module.Include { - relativePath := path.Dir(p.Module.Source) + for _, v := range p.tarsFile.Include { + relativePath := path.Dir(p.tarsFile.Source) dependFile := relativePath + "/" + v pInc := NewParse(p.opt, dependFile, p.IncChain) - p.Module.IncModule = append(p.Module.IncModule, pInc) + p.tarsFile.IncTarsFile = append(p.tarsFile.IncTarsFile, pInc) log.Println("parse include: ", v) }