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 & t)46 std::ostream &OHOS::Hardware::operator<<(std::ostream &stream, const OHOS::Hardware::Token &t)
47 {
48 stream << "Token: type: " << std::setw(WIDTH_EIGHT) << ::std::left << TokenType2String(t.type).data();
49 stream << " value: " << std::setw(WIDTH_EIGHT) << ::std::left;
50 t.type != NUMBER ? stream << std::setw(WIDTH_TWENTY) << t.strval.data()
51 : stream << std::setw(WIDTH_ZERO) << "0x" << std::setw(WIDTH_EIGHTEEN) <<
52 std::hex << t.numval;
53 stream << " lineno:" << t.lineNo;
54 return stream;
55 }
56
operator ==(int32_t t) const57 bool Token::operator==(int32_t t) const
58 {
59 return t == type;
60 }
61
operator !=(int32_t t) const62 bool Token::operator!=(int32_t t) const
63 {
64 return t != type;
65 }
operator ==(Token & t) const66 bool Token::operator==(Token &t) const
67 {
68 return t.type == type && t.numval == numval && t.strval == strval;
69 }
70
operator !=(Token & t) const71 bool Token::operator!=(Token &t) const
72 {
73 return t.type != type || t.numval != numval || t.strval != strval;
74 }
75