| #ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_EXPRESSION_H_ |
| #define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_EXPRESSION_H_ |
| |
| #include <string> |
| |
| #include "absl/status/statusor.h" |
| #include "absl/strings/string_view.h" |
| |
| namespace milotic_tlbmc { |
| |
| // Evaluates a string expression with substitution. |
| // The function will replace all occurrences of the sub_key with the sub_value |
| // and then evaluate the expression starting at the first replaced value. |
| // After parsing, the evaluated expression result will be concatenated to the |
| // prefix before the first replaced value. |
| // |
| // Example output: |
| // EvaluateStrExpressionSubstitution("100 + $bus", "$bus", "10") -> "110" |
| // EvaluateStrExpressionSubstitution("prefix $bus - 5 * 3 + 55 suffix", |
| // "$bus", "6") -> "prefix 58 suffix" |
| absl::StatusOr<std::string> EvaluateStrExpressionSubstitution( |
| absl::string_view expression, absl::string_view sub_key, |
| absl::string_view sub_value); |
| |
| // Evaluates a given string expression from left to right if there are numeric |
| // tokens. |
| // The expression is a space-separated list of tokens. If the first token |
| // is an integer, the function will attempt to parse the expression and evaluate |
| // until it encounters a non-numeric token or reaches the end of the |
| // string. If the syntax is valid, the function will return the evaluated |
| // expression, with any remaining tokens appended as a suffix. |
| // |
| // For example, "10 + 5 - 6 * 3 suffix" is a valid expression and will return |
| // "27 suffix" |
| // |
| // If the first token is not an integer, the function will not attempt to parse |
| // the expression and will return the expression as is. |
| // |
| // For example, "something 10 + 5" will return "something 10 + 5" |
| // |
| // The function returns an error if the expression is ill-formed: |
| // - The expression is empty. |
| // - The expression has a hanging operator |
| // - The expression attempts to divide by zero |
| absl::StatusOr<std::string> EvaluateStrExpression(absl::string_view expression); |
| |
| } // namespace milotic_tlbmc |
| |
| #endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_EXPRESSION_H_ |