• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include <iomanip>
10 #include <map>
11 
12 #include "token.h"
13 
14 using namespace OHOS::Hardware;
15 
16 static constexpr int WIDTH_ZERO = 0;
17 static constexpr int WIDTH_EIGHT = 8;
18 static constexpr int WIDTH_EIGHTEEN = 18;
19 static constexpr int WIDTH_TWENTY = 20;
20 
TokenType2String(int32_t type)21 std::string OHOS::Hardware::TokenType2String(int32_t type)
22 {
23     static std::map<int32_t, std::string> tokenTypeMap = {
24         {NUMBER,    "NUMBER"   },
25         {TEMPLATE,  "TEMPLATE" },
26         {LITERAL,   "LITERAL"  },
27         {ROOT,      "ROOT"     },
28         {INCLUDE,   "INCLUDE"  },
29         {DELETE,    "DELETE"   },
30         {STRING,    "STRING"   },
31         {REF_PATH,  "REF_PATH" },
32         {FILE_PATH, "FILE_PATH"}
33     };
34 
35     std::string str;
36     if (type < '~') {
37         str.push_back(static_cast<char>(type));
38         return str;
39     } else if (tokenTypeMap.find(type) != tokenTypeMap.end()) {
40         str = tokenTypeMap[type];
41     }
42 
43     return str;
44 }
45 
operator <<(std::ostream & stream,const OHOS::Hardware::Token & token)46 std::ostream &OHOS::Hardware::operator<<(std::ostream &stream, const OHOS::Hardware::Token &token)
47 {
48     stream << "Token: type: " << std::setw(WIDTH_EIGHT) << ::std::left << TokenType2String(token.type);
49     stream << " value: " << std::setw(WIDTH_EIGHT) << ::std::left;
50     token.type != NUMBER ? stream << std::setw(WIDTH_TWENTY) << token.strval
51                      : stream << std::setw(WIDTH_ZERO) << "0x" << std::setw(WIDTH_EIGHTEEN) <<
52                        std::hex << token.numval;
53     stream << " lineno:" << token.lineNo;
54     return stream;
55 }
56 
Token()57 Token::Token() : type(0), strval(), numval(0), src(), lineNo(0) {}
58 
operator ==(int32_t otherType) const59 bool Token::operator==(int32_t otherType) const
60 {
61     return otherType == type;
62 }
63 
operator !=(int32_t otherType) const64 bool Token::operator!=(int32_t otherType) const
65 {
66     return otherType != type;
67 }
operator ==(const Token & token) const68 bool Token::operator==(const Token &token) const
69 {
70     return token.type == type && token.numval == numval && token.strval == strval;
71 }
72 
operator !=(const Token & token) const73 bool Token::operator!=(const Token &token) const
74 {
75     return token.type != type || token.numval != numval || token.strval != strval;
76 }
77