Porytiles
Loading...
Searching...
No Matches
function_call_info.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5
7
8namespace porytiles {
9
23 public:
29 FunctionCallInfo(std::string function_name, std::vector<std::vector<Token>> arguments, std::size_t start_index)
30 : function_name_{std::move(function_name)}, arguments_{std::move(arguments)}, start_index_{start_index}
31 {
32 }
33
37 [[nodiscard]] const std::string &function_name() const
38 {
39 return function_name_;
40 }
41
49 [[nodiscard]] const std::vector<std::vector<Token>> &arguments() const
50 {
51 return arguments_;
52 }
53
57 [[nodiscard]] std::size_t argument_count() const
58 {
59 return arguments_.size();
60 }
61
67 [[nodiscard]] const std::vector<Token> &argument_at(std::size_t index) const
68 {
69 return arguments_.at(index);
70 }
71
75 [[nodiscard]] std::size_t start_index() const
76 {
77 return start_index_;
78 }
79
87 [[nodiscard]] std::string reconstruct_call_text() const
88 {
89 std::string result = function_name_ + "(";
90 for (std::size_t arg_idx = 0; arg_idx < arguments_.size(); ++arg_idx) {
91 if (arg_idx > 0) {
92 result += ", ";
93 }
94 const auto &arg_tokens = arguments_[arg_idx];
95 for (std::size_t tok_idx = 0; tok_idx < arg_tokens.size(); ++tok_idx) {
96 if (tok_idx > 0) {
97 bool suppress_space =
98 arg_tokens[tok_idx - 1].is_any_of(TokenType::left_paren, TokenType::left_bracket) ||
99 arg_tokens[tok_idx].is_any_of(TokenType::right_paren, TokenType::right_bracket);
100 if (!suppress_space) {
101 result += " ";
102 }
103 }
104 result += arg_tokens[tok_idx].text();
105 }
106 }
107 result += ")";
108 return result;
109 }
110
111 private:
112 std::string function_name_;
113 std::vector<std::vector<Token>> arguments_;
114 std::size_t start_index_;
115};
116
130[[nodiscard]] std::vector<FunctionCallInfo>
131find_function_calls(const std::vector<Token> &tokens, const std::string &target_function_name);
132
141[[nodiscard]] std::vector<FunctionCallInfo> find_all_function_calls(const std::vector<Token> &tokens);
142
143} // namespace porytiles
Represents a parsed function call expression within a token stream.
const std::vector< Token > & argument_at(std::size_t index) const
Returns the argument at the specified index.
std::string reconstruct_call_text() const
Reconstructs a human-readable call expression from this function call.
std::size_t start_index() const
Returns the token index where this call begins.
const std::vector< std::vector< Token > > & arguments() const
Returns the arguments as token sequences.
FunctionCallInfo(std::string function_name, std::vector< std::vector< Token > > arguments, std::size_t start_index)
Constructs a FunctionCallInfo.
std::size_t argument_count() const
Returns the number of arguments.
const std::string & function_name() const
Returns the function name.
std::vector< FunctionCallInfo > find_all_function_calls(const std::vector< Token > &tokens)
Finds all function call expressions within a token stream.
std::vector< FunctionCallInfo > find_function_calls(const std::vector< Token > &tokens, const std::string &target_function_name)
Finds all calls to a specific function within a token stream.