1 /**
2 * Copyright (c) 2024-2025 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 "ThrowingTypedParser.h"
17 #include "lexer/lexer.h"
18
19 namespace ark::es2panda::parser {
20
ThrowUnexpectedToken(lexer::TokenType tokenType) const21 void ThrowingTypedParser::ThrowUnexpectedToken(lexer::TokenType tokenType) const
22 {
23 ThrowSyntaxError({"Unexpected token '", lexer::TokenToString(tokenType), "'."});
24 }
25
ThrowSyntaxError(std::string_view errorMessage) const26 void ThrowingTypedParser::ThrowSyntaxError(std::string_view errorMessage) const
27 {
28 ThrowSyntaxError(errorMessage, Lexer()->GetToken().Start());
29 }
30
ThrowSyntaxError(std::initializer_list<std::string_view> list) const31 void ThrowingTypedParser::ThrowSyntaxError(std::initializer_list<std::string_view> list) const
32 {
33 ThrowSyntaxError(list, Lexer()->GetToken().Start());
34 }
35
ThrowSyntaxError(std::initializer_list<std::string_view> list,const lexer::SourcePosition & pos) const36 void ThrowingTypedParser::ThrowSyntaxError(std::initializer_list<std::string_view> list,
37 const lexer::SourcePosition &pos) const
38 {
39 std::stringstream ss;
40 for (const auto &it : list) {
41 ss << it;
42 }
43
44 std::string err = ss.str();
45 ThrowSyntaxError(std::string_view {err}, pos);
46 }
47
ThrowSyntaxError(std::string_view errorMessage,const lexer::SourcePosition & pos) const48 void ThrowingTypedParser::ThrowSyntaxError(std::string_view errorMessage, const lexer::SourcePosition &pos) const
49 {
50 lexer::LineIndex index(GetProgram()->SourceCode());
51 lexer::SourceLocation loc = index.GetLocation(pos);
52
53 DiagnosticEngine().ThrowSyntaxError(errorMessage, GetProgram()->SourceFilePath().Utf8(), loc.line, loc.col);
54 }
55
56 } // namespace ark::es2panda::parser
57