code_template.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #pragma once
  2. #include <c10/util/irange.h>
  3. #include <sstream>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <vector>
  7. namespace at::jit {
  8. // A template environment is a mapping from template variable names, e.g.,
  9. // identifier (corresponding to $identifier) to their expansions.
  10. //
  11. // This template environment supports storing strings, numbers and lists
  12. // of strings, and can be chained together (so that lookup proceeds in
  13. // in the top level environment, and then recurses into a parent
  14. // environment if the key is not found.)
  15. struct TemplateEnv {
  16. TemplateEnv() = default;
  17. TemplateEnv(TemplateEnv& parent) : parent(&parent) {}
  18. TemplateEnv(TemplateEnv&&) = delete;
  19. TemplateEnv& operator=(const TemplateEnv& parent) = delete;
  20. TemplateEnv& operator=(TemplateEnv&& parent) = delete;
  21. ~TemplateEnv() = default;
  22. using string_list = std::vector<std::string>;
  23. // Add a string 'v' to the map at key 'k'.
  24. void s(const std::string& k, const std::string& v) {
  25. strings_[k] = v;
  26. lists_.erase(k);
  27. }
  28. // Add a number 'v' to the map at key 'k'
  29. template <typename T>
  30. void d(const std::string& k, const T& v) {
  31. strings_[k] = std::to_string(v);
  32. lists_.erase(k);
  33. }
  34. // Retrieve the string representation of the value stored at 'k' from the map.
  35. // Raises an exception if the key is not found.
  36. const std::string& s(const std::string& k) const {
  37. if (strings_.count(k) == 0) {
  38. if (parent) {
  39. return parent->s(k);
  40. }
  41. notFound(k);
  42. }
  43. return strings_.at(k);
  44. }
  45. // Store a list of strings 'v' in the map at 'k'.
  46. void v(const std::string& k, const string_list& v) {
  47. lists_[k] = v;
  48. strings_.erase(k);
  49. }
  50. // Retrieve a list of strings stored at 'k' from the map.
  51. // Raises an exception if the key is not found.
  52. const string_list& v(const std::string& k) const {
  53. if (lists_.count(k) == 0) {
  54. if (parent) {
  55. return parent->v(k);
  56. }
  57. notFound(k);
  58. }
  59. return lists_.at(k);
  60. }
  61. // Test if a string 'k' is a string (as opposed to a list.)
  62. bool keyIsString(const std::string& k) const {
  63. if (strings_.count(k) > 0)
  64. return true;
  65. if (lists_.count(k) > 0)
  66. return false;
  67. if (parent)
  68. return parent->keyIsString(k);
  69. notFound(k);
  70. }
  71. private:
  72. [[noreturn]] void notFound(const std::string& k) const {
  73. std::stringstream ss;
  74. ss << "key not found: " << k;
  75. throw std::logic_error(ss.str());
  76. }
  77. std::unordered_map<std::string, std::string> strings_;
  78. std::unordered_map<std::string, string_list> lists_;
  79. TemplateEnv* parent{nullptr};
  80. };
  81. /*
  82. # Match $identifier or ${identifier} and replace with the value in env.
  83. # If this identifier is at the beginning of whitespace on a line
  84. # and its value is a list then it is treated as
  85. # block substitution by indenting all lines of all elements.
  86. # If the identifier is on a line starting with non-whitespace and a list
  87. # then it is comma separated. ${,foo} will insert a comma before the list
  88. # if this list is not empty and ${foo,} will insert one after.
  89. */
  90. struct CodeTemplate {
  91. /* implicit */ CodeTemplate(std::string t) : template_text(std::move(t)) {}
  92. std::string format(const TemplateEnv& env) const {
  93. std::stringstream out;
  94. size_t pos = 0;
  95. size_t indent = 0;
  96. bool all_whitespace = true;
  97. while (pos < template_text.size()) {
  98. char c = template_text[pos];
  99. if (c == '$') {
  100. std::stringstream kss;
  101. bool comma_before = false;
  102. bool comma_after = false;
  103. size_t new_pos = parseKey(pos, kss, comma_before, comma_after);
  104. std::string k = kss.str();
  105. bool is_string = env.keyIsString(k);
  106. if (all_whitespace) {
  107. if (is_string)
  108. emitStringWithIndents(out, indent, env.s(k));
  109. else
  110. emitLinesIndented(out, indent, env.v(k));
  111. } else {
  112. if (is_string)
  113. out << env.s(k);
  114. else
  115. emitCommaSeparatedList(out, env.v(k), comma_before, comma_after);
  116. }
  117. all_whitespace = false;
  118. pos = new_pos;
  119. } else {
  120. out << c;
  121. if (!isspace(c))
  122. all_whitespace = false;
  123. indent++;
  124. if (c == '\n') {
  125. indent = 0;
  126. all_whitespace = true;
  127. }
  128. pos++;
  129. }
  130. }
  131. return out.str();
  132. }
  133. private:
  134. using string_list = std::vector<std::string>;
  135. char charAt(size_t p) const {
  136. if (p >= template_text.size())
  137. throw std::logic_error("EOS found in key");
  138. return template_text[p];
  139. }
  140. size_t parseKey(
  141. size_t pos,
  142. std::ostream& k,
  143. bool& comma_before,
  144. bool& comma_after) const {
  145. comma_before = false;
  146. comma_after = false;
  147. pos++;
  148. if (charAt(pos) == '{') {
  149. pos++;
  150. if (charAt(pos) == ',') {
  151. comma_before = true;
  152. pos++;
  153. }
  154. pos = parseIdent(pos, k);
  155. if (charAt(pos) == ',') {
  156. comma_after = true;
  157. pos++;
  158. }
  159. if (charAt(pos) != '}')
  160. throw std::logic_error("missing terminating '}'");
  161. pos++;
  162. return pos;
  163. } else {
  164. return parseIdent(pos, k);
  165. }
  166. }
  167. size_t parseIdent(size_t pos, std::ostream& k) const {
  168. while (pos < template_text.size() &&
  169. (isalnum(template_text[pos]) || template_text[pos] == '_')) {
  170. k << template_text[pos];
  171. pos++;
  172. }
  173. return pos;
  174. }
  175. void emitCommaSeparatedList(
  176. std::ostream& out,
  177. const string_list& strings,
  178. bool comma_before,
  179. bool comma_after) const {
  180. if (comma_before && !strings.empty())
  181. out << ", ";
  182. for (const auto i : c10::irange(strings.size())) {
  183. if (i > 0)
  184. out << ", ";
  185. out << strings[i];
  186. }
  187. if (comma_after && !strings.empty())
  188. out << ", ";
  189. }
  190. // These indentation functions follow the convention that they never emit
  191. // leading or trailing newlines when the input string does not have leading
  192. // or trailing newlines. It's the responsibility of the calling function
  193. // to indent correctly in the context.
  194. void emitIndent(std::ostream& out, size_t indent) const {
  195. for ([[maybe_unused]] const auto i : c10::irange(indent)) {
  196. out << " ";
  197. }
  198. }
  199. void emitStringWithIndents(
  200. std::ostream& out,
  201. size_t indent,
  202. const std::string& str) const {
  203. for (auto c : str) {
  204. out << c;
  205. if (c == '\n') {
  206. emitIndent(out, indent);
  207. }
  208. }
  209. }
  210. void emitLinesIndented(
  211. std::stringstream& out,
  212. size_t indent,
  213. const string_list& strings) const {
  214. for (const auto i : c10::irange(strings.size())) {
  215. if (i > 0)
  216. emitIndent(out, indent);
  217. emitStringWithIndents(out, indent, strings[i]);
  218. if (i + 1 != strings.size())
  219. out << "\n";
  220. }
  221. }
  222. std::string template_text;
  223. };
  224. static inline std::string format(const std::string& fmt, TemplateEnv& env) {
  225. return CodeTemplate(fmt).format(env);
  226. }
  227. } // namespace at::jit