123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- /**
- * 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(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());
- }
- /**
- *
- * @param fileName
- * @return
- */
- string getFileName(const string &fileName);
- /**
- * replace ext
- * @param fileName
- * @param ext
- * @return
- */
- string replaceFileName(const string &fileName, const string &ext);
- /**
- * get absolute filename
- * @param baseDir
- * @param fileName
- * @return
- */
- string getAbsoluteFileName(const string &baseDir, const string &fileName);
- 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
|