1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "lexer/token.h"
17
18 #include <unordered_map>
19
20 #include "util/common.h"
21 #include "util/file.h"
22 #include "util/string_builder.h"
23 #include "util/string_helper.h"
24
25 namespace OHOS {
26 namespace Idl {
Dump()27 std::string Token::Dump()
28 {
29 StringBuilder sb;
30 sb.AppendFormat("{kind:%u, row:%u, col:%u, value:%s}",
31 static_cast<size_t>(kind), location.row, location.col, value.c_str());
32 return sb.ToString();
33 }
34
LocInfo(const Token & token)35 std::string LocInfo(const Token &token)
36 {
37 size_t index = token.location.filePath.rfind(SEPARATOR);
38 std::string fileName =
39 (index == std::string::npos) ? token.location.filePath : token.location.filePath.substr(index + 1);
40 return StringHelper::Format("%s:%zu:%zu", fileName.c_str(), token.location.row, token.location.col);
41 }
42 } // namespace Idl
43 } // namespace OHOS