tars2node/third_partly/tarsparse/parse.h

369 lines
6.9 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 __TARS_PARSE_H_
#define __TARS_PARSE_H_
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>
#include <stack>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include "element.h"
#include "util/tc_common.h"
#include "util/tc_autoptr.h"
using namespace std;
/**
* Tars文件解析类
*
*/
class TarsParse : public tars::TC_HandleBase
{
public:
/**
*
*/
TarsParse();
/**
* tars开头的标识
* @param bWithTars
*/
void setTars(bool bWithTars);
/**
*
* @param sHeader
*/
void setHeader(const string &sHeader);
/**
* 使tars文件
* @param bWithTars
*/
void setCurrentPriority(bool bFlag);
/**
*
*/
string getHeader();
/**
*
* @param sFileName
*/
void parse(const string &sFileName);
/**
*
* @param msg
*/
void error(const string &msg);
/**
*
* @param s
*
* @return int
*/
int checkKeyword(const string &s);
/**
*
*/
void nextLine();
/**
*
*
* @return string
*/
string getCurrFileName();
/**
* tab
*
* @return string
*/
string getTab();
/**
* tab数
*/
void incTab() { _itab++; }
/**
* tab数
*/
void delTab() { _itab--; }
/**
*
* @param file
*/
void pushFile(const string &file);
/**
*
*/
ContextPtr popFile();
/**
*
*
* @return std::vector<ContextPtr>
*/
std::vector<ContextPtr> getContexts() { return _vcontexts; }
/**
*
*
* @return ContainerPtr
*/
ContainerPtr currentContainer();
/**
* push容器
* @param c
*/
void pushContainer(const ContainerPtr &c);
/**
*
*
* @return ContextPtr
*/
ContextPtr currentContextPtr();
/**
*
*
* @return ContainerPtr
*/
ContainerPtr popContainer();
/**
* Builtin元素
* @param kind
*
* @return BuiltinPtr
*/
BuiltinPtr createBuiltin(Builtin::Kind kind,bool isUnsigned = false);
/**
* Vector元素
* @param ptr
*
* @return VectorPtr
*/
VectorPtr createVector(const TypePtr &ptr);
/**
* Map元素
* @param pleft
* @param pright
*
* @return MapPtr
*/
MapPtr createMap(const TypePtr &pleft, const TypePtr &pright);
/**
*
* @param sPtr
*/
void addStructPtr(const StructPtr &sPtr);
/**
*
* @param id
*
* @return StructPtr
*/
StructPtr findStruct(const string &sid);
/**
*
* @param ePtr
*/
void addEnumPtr(const EnumPtr &ePtr);
/**
*
* @param id
*
* @return EnumPtr
*/
EnumPtr findEnum(const string &sid);
/**
*
* @param id
*/
void checkConflict(const string &sid);
/**
*
* @param sid
*
* @return TypePtr
*/
TypePtr findUserType(const string &sid);
/**
*
* @param id
*
* @return NamespacePtr
*/
NamespacePtr findNamespace(const string &id);
/**
*
*
* @return NamespacePtr
*/
NamespacePtr currentNamespace();
/**
* tag的合法性
* @param i
*/
void checkTag(int i);
/**
* szie的合法性
* @param i
*/
void checkSize(int i);
/**
* array的合法性
* @param i
*/
void checkArrayVaid(TypePtr &tPtr,int size);
/**
* pointer的合法性
* @param i
*/
void checkPointerVaid(TypePtr &tPtr);
/**
*
* @param c
* @param b
*/
void checkConstValue(TypeIdPtr &tPtr, int b);
/**
*
* @param s
*
* @return string
*/
bool getFilePath(const string &s, string &file);
void setKeyStruct(const StructPtr& key)
{
_key = key;
}
StructPtr getKeyStruct()
{
return _key;
}
/**
*
*/
string printHeaderRemark();
/**
*
*/
bool checkEnum(const string &idName);
/**
* include的tars文件全部从当前文件搜寻
*/
void setUseCurrentPath(const bool & bEnable) { _bUseCurrentPath = bEnable; }
/**
* tars文件时,include路径
*/
void addIncludePath(const string &include)
{
vector<string> v = tars::TC_Common::sepstr<string>(include, "; ,", false);
_vIncludePath.insert(_vIncludePath.end(), v.begin(), v.end());
}
protected:
/**
*
* @param nPtr
*/
void addNamespacePtr(const NamespacePtr &nPtr);
/**
*
*/
void initScanner();
/**
*
*/
void clear();
protected:
bool _bWithTars;
std::map<std::string, int> _keywordMap;
int _itab;
StructPtr _key;
std::stack<ContextPtr> _contexts;
std::stack<ContainerPtr> _contains;
std::vector<ContextPtr> _vcontexts;
std::vector<StructPtr> _structs;
std::vector<EnumPtr> _enums;
std::vector<NamespacePtr> _namespaces;
string _sHeader;
bool _bUseCurrentPath;
bool _bUseCurrentPathFirst;
std::vector<string> _vIncludePath;
};
extern int yyparse();
extern int yylex();
extern FILE *yyin, *yyout;
typedef tars::TC_AutoPtr<TarsParse> TarsParsePtr;
extern TarsParsePtr g_parse;
#endif