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
25 public:
33 FunctionCallInfo(std::string function_name, std::vector<std::vector<Token>> arguments, std::size_t start_index)
34 : function_name_{std::move(function_name)}, arguments_{std::move(arguments)}, start_index_{start_index}
35 {
36 }
37
43 [[nodiscard]] const std::string &function_name() const
44 {
45 return function_name_;
46 }
47
57 [[nodiscard]] const std::vector<std::vector<Token>> &arguments() const
58 {
59 return arguments_;
60 }
61
67 [[nodiscard]] std::size_t argument_count() const
68 {
69 return arguments_.size();
70 }
71
79 [[nodiscard]] const std::vector<Token> &argument_at(std::size_t index) const
80 {
81 return arguments_.at(index);
82 }
83
89 [[nodiscard]] std::size_t start_index() const
90 {
91 return start_index_;
92 }
93
103 [[nodiscard]] std::string reconstruct_call_text() const
104 {
105 std::string result = function_name_ + "(";
106 for (std::size_t arg_idx = 0; arg_idx < arguments_.size(); ++arg_idx) {
107 if (arg_idx > 0) {
108 result += ", ";
109 }
110 const auto &arg_tokens = arguments_[arg_idx];
111 for (std::size_t tok_idx = 0; tok_idx < arg_tokens.size(); ++tok_idx) {
112 if (tok_idx > 0) {
113 bool suppress_space =
114 arg_tokens[tok_idx - 1].is_any_of(TokenType::left_paren, TokenType::left_bracket) ||
115 arg_tokens[tok_idx].is_any_of(TokenType::right_paren, TokenType::right_bracket);
116 if (!suppress_space) {
117 result += " ";
118 }
119 }
120 result += arg_tokens[tok_idx].text();
121 }
122 }
123 result += ")";
124 return result;
125 }
126
127 private:
128 std::string function_name_;
129 std::vector<std::vector<Token>> arguments_;
130 std::size_t start_index_;
131};
132
148[[nodiscard]] std::vector<FunctionCallInfo>
149find_function_calls(const std::vector<Token> &tokens, const std::string &target_function_name);
150
161[[nodiscard]] std::vector<FunctionCallInfo> find_all_function_calls(const std::vector<Token> &tokens);
162
163} // 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.