2
0

ppCommandFile.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_output(ostream *out);
  22. void set_scope(PPScope *scope);
  23. PPScope *get_scope() const;
  24. bool read_file(const string &filename);
  25. bool read_stream(istream &in, const string &filename);
  26. bool read_stream(istream &in);
  27. void begin_read();
  28. bool read_line(string line);
  29. bool end_read();
  30. protected:
  31. bool handle_command(const string &line);
  32. bool handle_if_command();
  33. bool handle_elif_command();
  34. bool handle_else_command();
  35. bool handle_endif_command();
  36. bool handle_begin_command();
  37. bool handle_forscopes_command();
  38. bool handle_foreach_command();
  39. bool handle_formap_command();
  40. bool handle_defsub_command(bool is_defsub);
  41. bool handle_output_command();
  42. bool handle_end_command();
  43. bool handle_format_command();
  44. bool handle_print_command();
  45. bool handle_include_command();
  46. bool handle_sinclude_command();
  47. bool handle_call_command();
  48. bool handle_error_command();
  49. bool handle_defer_command();
  50. bool handle_define_command();
  51. bool handle_set_command();
  52. bool handle_map_command();
  53. bool handle_addmap_command();
  54. bool include_file(const string &filename);
  55. bool replay_forscopes(const string &name);
  56. bool replay_foreach(const string &varname, const vector<string> &words);
  57. bool replay_formap(const string &varname, const string &mapvar);
  58. bool compare_output(const string &new_contents, const string &filename,
  59. bool notouch);
  60. bool failed_if() const;
  61. bool is_valid_formal(const string &formal_parameter_name) const;
  62. private:
  63. class PushFilename {
  64. public:
  65. PushFilename(PPScope *scope, const string &filename);
  66. ~PushFilename();
  67. PPScope *_scope;
  68. string _old_thisdirprefix;
  69. string _old_thisfilename;
  70. };
  71. private:
  72. class BlockNesting;
  73. enum IfState {
  74. IS_on, // e.g. a passed #if
  75. IS_else, // after matching an #else
  76. IS_off, // e.g. a failed #if
  77. IS_done // e.g. after reaching an #else or #elif for a passed #if.
  78. };
  79. class IfNesting {
  80. public:
  81. IfNesting(IfState state);
  82. void push(PPCommandFile *file);
  83. void pop(PPCommandFile *file);
  84. IfState _state;
  85. BlockNesting *_block;
  86. IfNesting *_next;
  87. };
  88. enum BlockState {
  89. BS_begin,
  90. BS_forscopes,
  91. BS_nested_forscopes,
  92. BS_foreach,
  93. BS_nested_foreach,
  94. BS_formap,
  95. BS_nested_formap,
  96. BS_defsub,
  97. BS_defun,
  98. BS_output
  99. };
  100. enum WriteFormat {
  101. WF_straight, // write lines directly as they come in
  102. WF_collapse, // collapse out consecutive blank lines
  103. WF_makefile // fancy makefile formatting
  104. };
  105. class WriteState {
  106. public:
  107. WriteState();
  108. WriteState(const WriteState &copy);
  109. bool write_line(const string &line);
  110. bool write_collapse_line(const string &line);
  111. bool write_makefile_line(const string &line);
  112. ostream *_out;
  113. WriteFormat _format;
  114. bool _last_blank;
  115. };
  116. enum OutputFlags {
  117. OF_notouch = 0x001,
  118. };
  119. class BlockNesting {
  120. public:
  121. BlockNesting(BlockState state, const string &name);
  122. void push(PPCommandFile *file);
  123. void pop(PPCommandFile *file);
  124. BlockState _state;
  125. string _name;
  126. IfNesting *_if;
  127. WriteState *_write_state;
  128. PPScope *_scope;
  129. string _filename;
  130. ostrstream _output;
  131. vector<string> _words;
  132. int _flags;
  133. BlockNesting *_next;
  134. };
  135. PPScope *_native_scope;
  136. PPScope *_scope;
  137. bool _got_command;
  138. bool _in_for;
  139. IfNesting *_if_nesting;
  140. BlockNesting *_block_nesting;
  141. string _command;
  142. string _params;
  143. WriteState *_write_state;
  144. vector<string> _saved_lines;
  145. friend PPCommandFile::IfNesting;
  146. friend PPCommandFile::WriteState;
  147. friend PPCommandFile::BlockNesting;
  148. };
  149. #endif