-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathexpression_parser.cpp
47 lines (39 loc) · 2.14 KB
/
expression_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#include <cppast/cpp_expression.hpp>
#include "parse_functions.hpp"
using namespace cppast;
std::unique_ptr<cpp_expression> detail::parse_expression(const detail::parse_context& context,
const CXCursor& cur)
{
auto kind = clang_getCursorKind(cur);
DEBUG_ASSERT(clang_isExpression(kind), detail::assert_handler{});
detail::cxtokenizer tokenizer(context.tu, context.file, cur);
detail::cxtoken_stream stream(tokenizer, cur);
auto type = parse_type(context, cur, clang_getCursorType(cur));
auto expr = to_string(stream, stream.end(), false);
if (kind == CXCursor_CallExpr && (expr.empty() || expr.back().spelling != ")"))
{
// we have a call expression that doesn't end in a closing parentheses
// this means default constructor, don't parse it at all
// so, for example a variable doesn't have a default value
return nullptr;
}
else if (kind == CXCursor_CharacterLiteral || kind == CXCursor_CompoundLiteralExpr
|| kind == CXCursor_FloatingLiteral || kind == CXCursor_ImaginaryLiteral
|| kind == CXCursor_IntegerLiteral || kind == CXCursor_StringLiteral
|| kind == CXCursor_CXXBoolLiteralExpr || kind == CXCursor_CXXNullPtrLiteralExpr)
return cpp_literal_expression::build(std::move(type), expr.as_string());
else
return cpp_unexposed_expression::build(std::move(type), std::move(expr));
}
std::unique_ptr<cpp_expression> detail::parse_raw_expression(const parse_context&,
cxtoken_stream& stream,
cxtoken_iterator end,
std::unique_ptr<cpp_type> type)
{
if (stream.done())
return nullptr;
auto expr = to_string(stream, std::prev(end)->value() == ";" ? std::prev(end) : end, false);
return cpp_unexposed_expression::build(std::move(type), std::move(expr));
}