A programming language for manipulation of streams.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.3 KiB

5 years ago
  1. #pragma once
  2. #include <map>
  3. #include <string>
  4. #include <sstream>
  5. #include <optional>
  6. #include <memory>
  7. using tool_env = std::map<std::string, std::string>;
  8. class tool
  9. {
  10. public:
  11. virtual void init(std::optional<std::shared_ptr<tool>> next) = 0;
  12. virtual void execute() = 0;
  13. virtual void enqueue(std::string) = 0;
  14. virtual bool has_queue() = 0;
  15. };
  16. inline std::string escape(std::string v)
  17. {
  18. std::stringstream sstr;
  19. for(char c : v)
  20. {
  21. switch(c)
  22. {
  23. case '\\':
  24. sstr<<"\\\\";
  25. break;
  26. case '\n':
  27. sstr<<"\\n";
  28. break;
  29. case '\t':
  30. sstr<<"\\t";
  31. break;
  32. case ':':
  33. sstr<<"\\:";
  34. break;
  35. case 0:
  36. sstr<<"\\0";
  37. break;
  38. default:
  39. sstr<<c;
  40. break;
  41. }
  42. }
  43. return sstr.str();
  44. }
  45. inline std::string unescape(std::string v)
  46. {
  47. std::stringstream sstr;
  48. std::stringstream orig{v};
  49. char point;
  50. while(orig.good())
  51. {
  52. orig >> point;
  53. if(point!='\\')
  54. {
  55. sstr << point;
  56. }
  57. else
  58. {
  59. orig >> point;
  60. switch(point)
  61. {
  62. case '\\':
  63. sstr<<"\\";
  64. break;
  65. case 'n':
  66. sstr<<"\n";
  67. break;
  68. case 't':
  69. sstr<<"\t";
  70. break;
  71. case '0':
  72. sstr<<"\0";
  73. break;
  74. case ':':
  75. sstr<<":";
  76. break;
  77. default:
  78. sstr<<point;
  79. break;
  80. }
  81. }
  82. }
  83. return sstr.str();
  84. }
  85. std::optional<std::shared_ptr<tool> > mktool(std::string toolname, tool_env env);