123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861 |
- /**
- * Tencent is pleased to support the open source community by making Tars available.
- *
- * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
- *
- * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * https://opensource.org/licenses/BSD-3-Clause
- *
- * Unless required by applicable law or agreed to in writing, software distributed
- * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
- #ifndef _ELEMENT_H
- #define _ELEMENT_H
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <iterator>
- #include <map>
- #include <stack>
- #include <sstream>
- #include "errno.h"
- #include "util/tc_autoptr.h"
- using namespace std;
- void yyerror(char const *msg);
- /**
- * 语法元素基类
- */
- class GrammarBase : virtual public tars::TC_HandleBase
- {
- public:
- virtual ~GrammarBase(){};
- };
- typedef tars::TC_AutoPtr<GrammarBase> GrammarBasePtr;
- /**
- * 解析过程中的字符串
- */
- class StringGrammar : public GrammarBase
- {
- public:
- StringGrammar() { }
- string v;
- };
- typedef tars::TC_AutoPtr<StringGrammar> StringGrammarPtr;
- /**
- * 解析过程中的整形数
- */
- class IntergerGrammar : public GrammarBase
- {
- public:
- IntergerGrammar():v(0) { }
- long long v;
- };
- typedef tars::TC_AutoPtr<IntergerGrammar> IntergerGrammarPtr;
- /**
- * 解析过程中的浮点数
- */
- class FloatGrammar : public GrammarBase
- {
- public:
- FloatGrammar():v(0.0f) { }
- double v;
- };
- typedef tars::TC_AutoPtr<FloatGrammar> FloatGrammarPtr;
- /**
- * 解析过程中的bool类型
- */
- class BoolGrammar : public GrammarBase
- {
- public:
- BoolGrammar():v(false) { }
- bool v;
- };
- typedef tars::TC_AutoPtr<BoolGrammar> BoolGrammarPtr;
- /**
- * 解析过程中的常类型
- */
- class ConstGrammar : public GrammarBase
- {
- public:
- ConstGrammar():t(VALUE) { }
- enum
- {
- VALUE,
- STRING,
- BOOL,
- ENUM
- };
- int t;
- string v;
- };
- typedef tars::TC_AutoPtr<ConstGrammar> ConstGrammarPtr;
- ///////////////////////////////////////////////
- /**
- * 类型基类
- */
- class Type : virtual public GrammarBase
- {
- public:
- /**
- * 构造函数
- * @param Type
- */
- Type():_size(0),_array(false),_pointer(false){};
-
- //是否简单类型
- virtual bool isSimple() const { return false;}
- //是否数组类型
- virtual bool isArray() const { return _array;}
- virtual void setArray(int size) {_array = true;_size = size;}
- virtual int getSize() const {return _size;};
- //是否指针类型
- virtual bool isPointer() const { return _pointer;}
- virtual void setPointer(bool b) {_pointer = b;}
- public:
- int _size;
- bool _array;
- bool _pointer;
- };
- typedef tars::TC_AutoPtr<Type> TypePtr;
- /**
- * 内建类型
- */
- class Builtin : public Type
- {
- public:
- enum Kind
- {
- KindVoid,
- KindBool,
- KindByte,
- KindShort,
- KindInt,
- KindLong,
- KindFloat,
- KindDouble,
- KindString,
- KindVector,
- KindMap
- };
- /**
- * 构造函数
- * @param kind
- */
- Builtin(Kind kind,bool isUnsigned);
- /**
- * 类型
- *
- * @return Kind
- */
- Kind kind() const;
- /**
- * 是否是简单类型
- *
- * @return bool
- */
- bool isSimple() const;
- /**
- * 是否是unsined类型
- *
- * @return bool
- */
- bool isUnsigned() const {return _isUnsigned;}
- /**
- * 设置是unsined类型
- *
- * @return bool
- */
- void setUnsigned(bool isUnsigned = false) {_isUnsigned = isUnsigned;}
- /**
- * 缺省值
- *
- * @return string
- */
- string def() const;
- /**
- * 字符串标示
- */
- static const char* builtinTable[];
- protected:
- Kind _kind;
- bool _isUnsigned;
- };
- typedef tars::TC_AutoPtr<Builtin> BuiltinPtr;
- /**
- * Vector类型
- */
- class Vector : public Type
- {
- public:
- /**
- * 构造函数
- * @param ptr
- */
- Vector(const TypePtr& ptr);
- /**
- * 获取类型
- *
- * @return TypePtr&
- */
- TypePtr& getTypePtr() {return _ptr;}
- protected:
- TypePtr _ptr;
- };
- typedef tars::TC_AutoPtr<Vector> VectorPtr;
- /**
- * Map类型
- */
- class Map : public Type
- {
- public:
- /**
- * 构造函数
- * @param pleft
- * @param pright
- */
- Map(const TypePtr& pleft, const TypePtr& pright);
- /**
- * 获取左类型
- *
- * @return TypePtr&
- */
- TypePtr& getLeftTypePtr() {return _pleft;}
- /**
- * 获取右类型
- *
- * @return TypePtr&
- */
- TypePtr& getRightTypePtr() {return _pright;}
- protected:
- TypePtr _pleft;
- TypePtr _pright;
- };
- typedef tars::TC_AutoPtr<Map> MapPtr;
- /**
- * 变量定义
- */
- class TypeId : public GrammarBase
- {
- public:
- /**
- * 构造函数
- * @param ptr
- * @param id
- */
- TypeId(const TypePtr& ptr, const string& id);
- /**
- * 变量名称
- *
- * @return string
- */
- string getId() const { return _id; }
- /**
- * 变量类型
- *
- * @return Type*
- */
- TypePtr& getTypePtr() { return _ptr;}
- /**
- * 是否需要该参数
- *
- * @return bool
- */
- bool isRequire() const { return _bRequire; }
- /**
- * 设置必选字段
- * @param tag
- */
- void setRequire(int tag);
- /**
- * 设置可选字段
- * 只有基本类型才有缺省值
- */
- void setDefault(const string &def);
- /**
- *
- * 只有基本类型才有缺省值
- */
- void disableDefault();
- /**
- * 设置可选字段
- * @param tag
- */
- void setOptional(int tag);
- /**
- * tag标识
- */
- int getTag() const { return _tag; }
- /**
- * 缺省值
- *
- * @return string
- */
- string def() const { return _default; }
- /**
- * 是否有缺省值
- *
- * @return bool
- */
- bool hasDefault() const { return _bHasDefault; }
- protected:
- TypePtr _ptr;
- string _id;
- bool _bRequire;
- int _tag;
- bool _bHasDefault;
- string _default;
- public:
- int _size;
- bool _array;
- };
- typedef tars::TC_AutoPtr<TypeId> TypeIdPtr;
- ////////////////////////////////////////////////////
- //
- class Namespace;
- typedef tars::TC_AutoPtr<Namespace> NamespacePtr;
- /**
- * 容器基类, 所有可以包含其他元素的元素都从该基类继承
- */
- class Container : virtual public GrammarBase
- {
- public:
- /**
- * 构造函数
- * @param id
- */
- Container(const string &id) : _id(id)
- {
- }
- /**
- * 生成名字空间
- * @param id
- *
- * @return NamespacePtr
- */
- NamespacePtr createNamespace(const string &id);
- /**
- * 获取ID
- *
- * @return string
- */
- string getId() const { return _id;}
- /**
- * 获取所有的名字空间
- *
- * @return vector<NamespacePtr>&
- */
- vector<NamespacePtr> &getAllNamespacePtr() { return _ns; }
- protected:
- string _id;
- vector<NamespacePtr> _ns;
- };
- typedef tars::TC_AutoPtr<Container> ContainerPtr;
- ////////////////////////////////////////////////////
- //
- class Const : public GrammarBase
- {
- public:
- /**
- *
- * @param tPtr
- * @param cPtr
- */
- Const(TypeIdPtr &tPtr, ConstGrammarPtr &cPtr) : _tPtr(tPtr), _cPtr(cPtr)
- {
- }
- /**
- *
- *
- * @return TypePtr&
- */
- TypeIdPtr &getTypeIdPtr() { return _tPtr; }
- /**
- *
- *
- * @return ConstGrammarPtr
- */
- ConstGrammarPtr getConstGrammarPtr() { return _cPtr; }
- protected:
- TypeIdPtr _tPtr;
- ConstGrammarPtr _cPtr;
- };
- typedef tars::TC_AutoPtr<Const> ConstPtr;
- /////////////////////////////////////////////////////////
- //
- class Enum : virtual public Container, virtual public Type
- {
- public:
- Enum(const string &id, const string &sid) : Container(id), _sid(sid)
- {
- }
- /**
- * 添加成员变量
- * @param ptid
- */
- void addMember(const TypeIdPtr &ptr);
- /**
- * 获取所有的成员变量
- *
- * @return vector<TypeIdPtr>&
- */
- vector<TypeIdPtr>& getAllMemberPtr() {return _members;}
- /**
- * 获取结构的名称
- *
- * @return string
- */
- string getSid() const { return _sid; }
- /**
- * 是否是简单类型
- *
- * @return bool
- */
- virtual bool isSimple() const { return true; }
- protected:
- /**
- * 每个变量名称
- */
- vector<TypeIdPtr> _members;
- /**
- * 包含名字的空间的名称
- */
- string _sid;
- };
- typedef tars::TC_AutoPtr<Enum> EnumPtr;
- ////////////////////////////////////////////////////
- //
- /**
- * 结构
- */
- class Struct : virtual public Container, virtual public Type
- {
- public:
- /**
- * 构造函数
- * @param id
- */
- Struct(const string& id, const string &sid) : Container(id), _sid(sid)
- {
- }
- /**
- * 添加成员变量
- * @param ptid
- */
- void addTypeId(const TypeIdPtr &ptr);
- /**
- * 获取所有的成员变量
- *
- * @return vector<TypeIdPtr>&
- */
- vector<TypeIdPtr>& getAllMemberPtr() {return _members;}
- /**
- * 获取结构的名称
- *
- * @return string
- */
- string getSid() const { return _sid; }
- /**
- * 增加小于memeber
- * @param member
- */
- void addKey(const string &member);
- vector<string> getKey() { return _key; }
- protected:
- vector<TypeIdPtr> _members;
- vector<string> _key;
- string _sid;
- };
- typedef tars::TC_AutoPtr<Struct> StructPtr;
- ////////////////////////////////////////////////////
- //
- /**
- * 参数描述
- */
- class ParamDecl : public GrammarBase
- {
- public:
- /**
- * 构造
- * @param typeIdPtr
- * @param v
- */
- ParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k)
- : _typeIdPtr(typeIdPtr), _v(v), _k(k)
- {
- }
- /**
- * 变量声明
- *
- * @return TypeIdPtr&
- */
- TypeIdPtr& getTypeIdPtr() { return _typeIdPtr; }
- /**
- * 是否是输出参数
- *
- * @return bool
- */
- bool isOut() const { return _v; }
- /**
- * 是否是需要路由的字段
- *
- * @return bool
- */
- bool isRouteKey() const { return _k; }
- protected:
- TypeIdPtr _typeIdPtr;
- bool _v;
- bool _k;
- };
- typedef tars::TC_AutoPtr<ParamDecl> ParamDeclPtr;
- ///////////////////////////////////////////////////////
- //
- /**
- * 操作类
- */
- class Operation : public Container
- {
- public:
- /**
- * 构造函数
- * @param id
- * @param typePtr
- */
- Operation(const string &id, const TypePtr &typePtr) : Container(id), _itag(0)
- {
- _retPtr = new TypeId(typePtr, "_ret");
- _retPtr->setRequire(_itag);
- }
- /**
- * 生成一个参数
- * @param typeIdPtr
- * @param v
- * @param k
- *
- * @return ParamDeclPtr
- */
- ParamDeclPtr createParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k);
- /**
- * 获取返回值类型
- *
- * @return TypePtr&
- */
- TypeIdPtr &getReturnPtr() { return _retPtr; }
- /**
- * 获取所有参数
- *
- * @return vector<ParamDeclPtr>&
- */
- vector<ParamDeclPtr> &getAllParamDeclPtr() { return _ps; }
- protected:
- int _itag;
- TypeIdPtr _retPtr;
- vector<ParamDeclPtr> _ps;
- };
- typedef tars::TC_AutoPtr<Operation> OperationPtr;
- ///////////////////////////////////////////////////////
- //
- /**
- * 接口描述
- */
- class Interface : public Container
- {
- public:
- /**
- * 构造
- * @param id
- */
- Interface(const string &id) : Container(id)
- {
- }
- /**
- * 生成一个操作
- * @param id
- * @param typePtr
- *
- * @return OperationPtr
- */
- OperationPtr createOperation(const string &id, const TypePtr &typePtr);
- /**
- * 获取所有操作
- *
- * @return vector<OperationPtr>&
- */
- vector<OperationPtr> &getAllOperationPtr() { return _ops; }
- protected:
- vector<OperationPtr> _ops;
- };
- typedef tars::TC_AutoPtr<Interface> InterfacePtr;
- /////////////////////////////////////////////////////////
- //
- /**
- * 名字空间
- */
- class Namespace : public Container
- {
- public:
- /**
- * 构造函数
- * @param id
- */
- Namespace(const string &id) : Container(id)
- {
- }
- /**
- * 生成接口
- * @param id
- *
- * @return InterfacePtr
- */
- InterfacePtr createInterface(const string &id);
- /**
- * 生成结构
- * @param id
- *
- * @return StructPtr
- */
- StructPtr createStruct(const string& id);
- /**
- * 生成枚举类型
- * @param id
- *
- * @return EnumPtr
- */
- EnumPtr createEnum(const string &id);
- /**
- *
- * @param pPtr
- * @param cPtr
- *
- * @return ConstPtr
- */
- ConstPtr createConst(TypeIdPtr &pPtr, ConstGrammarPtr &cPtr);
- /**
- * 是否有接口
- *
- * @return bool
- */
- bool hasInterface() const { return !_is.empty(); }
- /**
- * 获取所有的接口
- *
- * @return vector<InterfacePtr>&
- */
- vector<InterfacePtr> &getAllInterfacePtr() { return _is; }
- /**
- * 获取所有的结构
- *
- * @return vector<StructPtr>&
- */
- vector<StructPtr> &getAllStructPtr() { return _ss; }
- /**
- * 生成枚举类型
- *
- * @return vector<EnumPtr>&
- */
- vector<EnumPtr> &getAllEnumPtr() { return _es; }
- /**
- * 常量类型
- *
- * @return vector<ConstPtr>&
- */
- vector<ConstPtr> &getAllConstPtr() { return _cs; }
- protected:
- vector<InterfacePtr> _is;
- vector<StructPtr> _ss;
- vector<EnumPtr> _es;
- vector<ConstPtr> _cs;
- };
- /////////////////////////////////////////////////////////////////
- /**
- * 上下文
- */
- class Context : public tars::TC_HandleBase
- {
- public:
- /**
- * 构造函数
- */
- Context(const string &file) : _currline(1), _filename(file)
- {
- }
- /**
- * 下一行
- */
- void nextLine() { _currline++; }
- /**
- * 目前的行
- *
- * @return size_t
- */
- size_t getCurrLine() { return _currline; }
- /**
- * 当前文件名
- *
- * @return string
- */
- string getFileName() { return _filename; }
- /**
- * 添加include的文件
- * @param incl
- */
- void addInclude(const string &incl);
- /**
- * 添加属于这个文件的名字空间
- * @param c
- */
- void addNamespacePtr(const NamespacePtr &c)
- {
- _namespaces.push_back(c);
- }
- /**
- * 获取includes的文件
- *
- * @return vector<string>
- */
- vector<string> getIncludes() { return _includes; }
- /**
- * 获取名字空间
- *
- * @return vector<NamespacePtr>
- */
- vector<NamespacePtr> getNamespaces() { return _namespaces; }
- protected:
- size_t _currline;
- string _filename;
- vector<string> _includes;
- vector<NamespacePtr> _namespaces;
- };
- typedef tars::TC_AutoPtr<Context> ContextPtr;
- #endif
|