tars2node/third_partly/tarsparse/element.h

862 lines
15 KiB
C
Raw Normal View History

2018-07-27 12:33:14 +08:00
/**
* 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