ppCommandFile.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Filename: ppCommandFile.h
  2. // Created by: drose (25Sep00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #ifndef PPCOMMANDFILE_H
  6. #define PPCOMMANDFILE_H
  7. #include "ppremake.h"
  8. #include <map>
  9. #include <vector>
  10. class PPScope;
  11. ///////////////////////////////////////////////////////////////////
  12. // Class : PPCommandFile
  13. // Description : This encapsulates a file that contains #commands to
  14. // execute (like #define, #if, #begin .. #end),
  15. // $[variables] to expand, and plain text to output.
  16. ////////////////////////////////////////////////////////////////////
  17. class PPCommandFile {
  18. public:
  19. PPCommandFile(PPScope *scope);
  20. ~PPCommandFile();
  21. void set_scope(PPScope *scope);
  22. PPScope *get_scope() const;
  23. bool read_file(const string &filename);
  24. bool read_stream(istream &in);
  25. void begin_read();
  26. bool read_line(string line);
  27. bool end_read();
  28. protected:
  29. bool handle_command(const string &line);
  30. bool handle_if_command();
  31. bool handle_elif_command();
  32. bool handle_else_command();
  33. bool handle_endif_command();
  34. bool handle_begin_command();
  35. bool handle_forscopes_command();
  36. bool handle_foreach_command();
  37. bool handle_format_command();
  38. bool handle_output_command();
  39. bool handle_defsub_command();
  40. bool handle_end_command();
  41. bool handle_include_command();
  42. bool handle_sinclude_command();
  43. bool handle_call_command();
  44. bool handle_error_command();
  45. bool handle_defer_command();
  46. bool handle_define_command();
  47. bool handle_set_command();
  48. bool handle_map_command();
  49. bool include_file(const string &filename);
  50. bool replay_forscopes(const string &name);
  51. bool replay_foreach(const string &varname, const vector<string> &words);
  52. bool compare_output(const string &temp_name, const string &true_name);
  53. bool failed_if() const;
  54. private:
  55. class PushFilename {
  56. public:
  57. PushFilename(PPScope *scope, const string &filename);
  58. ~PushFilename();
  59. PPScope *_scope;
  60. string _old_thisdirprefix;
  61. string _old_thisfilename;
  62. };
  63. private:
  64. PPScope *_native_scope;
  65. PPScope *_scope;
  66. enum IfState {
  67. IS_on, // e.g. a passed #if
  68. IS_else, // after matching an #else
  69. IS_off, // e.g. a failed #if
  70. IS_done // e.g. after reaching an #else or #elif for a passed #if.
  71. };
  72. class IfNesting {
  73. public:
  74. IfState _state;
  75. IfNesting *_next;
  76. };
  77. enum BlockState {
  78. BS_begin,
  79. BS_forscopes,
  80. BS_nested_forscopes,
  81. BS_foreach,
  82. BS_nested_foreach,
  83. BS_defsub,
  84. BS_output
  85. };
  86. enum WriteFormat {
  87. WF_straight, // write lines directly as they come in
  88. WF_collapse, // collapse out consecutive blank lines
  89. WF_makefile // fancy makefile formatting
  90. };
  91. class WriteState {
  92. public:
  93. WriteState();
  94. WriteState(const WriteState &copy);
  95. bool write_line(const string &line);
  96. bool write_collapse_line(const string &line);
  97. bool write_makefile_line(const string &line);
  98. ostream *_out;
  99. WriteFormat _format;
  100. bool _last_blank;
  101. };
  102. class BlockNesting {
  103. public:
  104. BlockState _state;
  105. string _name;
  106. WriteState *_write_state;
  107. PPScope *_scope;
  108. string _true_name;
  109. char *_tempnam;
  110. ofstream _output;
  111. vector<string> _words;
  112. BlockNesting *_next;
  113. };
  114. bool _got_command;
  115. bool _in_for;
  116. IfNesting *_if_nesting;
  117. BlockNesting *_block_nesting;
  118. string _command;
  119. string _params;
  120. WriteState *_write_state;
  121. vector<string> _saved_lines;
  122. friend PPCommandFile::IfNesting;
  123. friend PPCommandFile::WriteState;
  124. friend PPCommandFile::BlockNesting;
  125. };
  126. #endif