Extract return type, parameter list and body from lambda text.
Requires a trailing return type: the return type is read from between the arrow and the opening brace. A lambda without one yields an empty return_type, which valid() reports as a parse failure.
48 {
49 FieldSource fs;
50 fs.name = std::move(fn_name);
51
52 const auto bracket = text.find('[');
53 const auto paren_open = text.find('(', bracket);
54 if (paren_open == std::string_view::npos)
55 return fs;
56
57 std::size_t
depth = 1;
58 std::size_t paren_close = paren_open + 1;
59 while (paren_close < text.size() &&
depth > 0) {
60 if (text[paren_close] == '(') {
62 } else if (text[paren_close] == ')') {
64 }
65 ++paren_close;
66 }
67 --paren_close;
68
69 fs.params = std::string(text.substr(paren_open + 1, paren_close - paren_open - 1));
70
71 const auto arrow = text.find("->", paren_close);
72 const auto brace_open = text.find('{', paren_close);
73 if (brace_open == std::string_view::npos)
74 return fs;
75
76 if (arrow != std::string_view::npos && arrow < brace_open) {
77 auto rt = text.substr(arrow + 2, brace_open - arrow - 2);
78 const auto b = rt.find_first_not_of(
" \t\n\r");
79 const auto e = rt.find_last_not_of(" \t\n\r");
80 if (
b != std::string_view::npos)
81 fs.return_type = std::string(rt.substr(
b, e -
b + 1));
82 }
83
85 std::size_t brace_close = brace_open + 1;
86 while (brace_close < text.size() &&
depth > 0) {
87 if (text[brace_close] == '{') {
89 } else if (text[brace_close] == '}') {
91 }
92 ++brace_close;
93 }
94 --brace_close;
95
96 fs.body = std::string(text.substr(brace_open + 1, brace_close - brace_open - 1));
97 return fs;
98 }