cppPreprocessor.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Filename: cppPreprocessor.h
  2. // Created by: drose (22Oct99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef CPPPREPROCESSOR_H
  19. #define CPPPREPROCESSOR_H
  20. #include <dtoolbase.h>
  21. #include "cppManifest.h"
  22. #include "cppToken.h"
  23. #include "cppFile.h"
  24. #include "cppCommentBlock.h"
  25. #include <dSearchPath.h>
  26. #include <vector_string.h>
  27. #include <map>
  28. #include <list>
  29. #include <vector>
  30. class CPPScope;
  31. class CPPTemplateParameterList;
  32. class CPPExpression;
  33. //#define CPP_VERBOSE_LEX
  34. ///////////////////////////////////////////////////////////////////
  35. // Class : CPPPreprocessor
  36. // Description :
  37. ////////////////////////////////////////////////////////////////////
  38. class CPPPreprocessor {
  39. public:
  40. CPPPreprocessor();
  41. void set_verbose(int verbose);
  42. int get_verbose() const;
  43. void copy_filepos(const CPPPreprocessor &other);
  44. CPPFile get_file() const;
  45. int get_line_number() const;
  46. int get_col_number() const;
  47. CPPToken get_next_token();
  48. #ifdef CPP_VERBOSE_LEX
  49. CPPToken get_next_token0();
  50. int _token_index;
  51. #endif
  52. void warning(const string &message, int line = 0, int col = 0,
  53. CPPFile file = CPPFile());
  54. void error(const string &message, int line = 0, int col = 0,
  55. CPPFile file = CPPFile());
  56. CPPCommentBlock *get_comment_before(int line, CPPFile file);
  57. int get_warning_count() const;
  58. int get_error_count() const;
  59. typedef map<string, CPPManifest *> Manifests;
  60. Manifests _manifests;
  61. DSearchPath _include_path;
  62. DSearchPath _system_include_path;
  63. CPPComments _comments;
  64. typedef set<CPPFile> ParsedFiles;
  65. ParsedFiles _parsed_files;
  66. typedef set<string> Includes;
  67. Includes _quote_includes;
  68. Includes _angle_includes;
  69. // This is normally true, to indicate that the preprocessor should
  70. // decode identifiers like foo::bar<snarf> into a single IDENTIFIER,
  71. // TYPENAME_IDENTIFIER, or SCOPING token for yacc's convenience.
  72. // When false, it leaves them alone and returns a sequence of
  73. // SIMPLE_IDENTIFIER and SCOPE tokens instead.
  74. bool _resolve_identifiers;
  75. // The default _verbose level is 1, which will output normal error
  76. // and warning messages but nothing else. Set this to 0 to make the
  77. // warning messages go away (although the counts will still be
  78. // incremented), or set it higher to get more debugging information.
  79. int _verbose;
  80. protected:
  81. bool init_cpp(const CPPFile &file);
  82. bool init_const_expr(const string &expr);
  83. bool init_type(const string &type);
  84. bool push_file(const CPPFile &file);
  85. bool push_string(const string &input, bool lock_position);
  86. string expand_manifests(const string &input_expr);
  87. CPPExpression *parse_expr(const string &expr, CPPScope *current_scope,
  88. CPPScope *global_scope);
  89. private:
  90. CPPToken internal_get_next_token();
  91. int skip_whitespace(int c);
  92. int skip_comment(int c);
  93. int skip_c_comment(int c);
  94. int skip_cpp_comment(int c);
  95. int process_directive(int c);
  96. int get_preprocessor_command(int c, string &command);
  97. int get_preprocessor_args(int c, string &args);
  98. void handle_define_directive(const string &args, int first_line,
  99. int first_col, const CPPFile &first_file);
  100. void handle_undef_directive(const string &args, int first_line,
  101. int first_col, const CPPFile &first_file);
  102. void handle_ifdef_directive(const string &args, int first_line,
  103. int first_col, const CPPFile &first_file);
  104. void handle_ifndef_directive(const string &args, int first_line,
  105. int first_col, const CPPFile &first_file);
  106. void handle_if_directive(const string &args, int first_line,
  107. int first_col, const CPPFile &first_file);
  108. void handle_include_directive(const string &args, int first_line,
  109. int first_col, const CPPFile &first_file);
  110. void handle_error_directive(const string &args, int first_line,
  111. int first_col, const CPPFile &first_file);
  112. void skip_false_if_block(bool consider_elifs);
  113. CPPToken get_quoted_char(int c);
  114. CPPToken get_quoted_string(int c);
  115. CPPToken get_identifier(int c);
  116. CPPToken expand_manifest(const CPPManifest *manifest);
  117. void extract_manifest_args(const string &name, int num_args,
  118. vector_string &args);
  119. void expand_defined_function(string &expr, size_t q, size_t &p);
  120. void expand_manifest_inline(string &expr, size_t q, size_t &p,
  121. const CPPManifest *manifest);
  122. void extract_manifest_args_inline(const string &name, int num_args,
  123. vector_string &args,
  124. const string &expr, size_t &p);
  125. CPPToken get_number(int c, int c2 = 0);
  126. static int check_keyword(const string &name);
  127. string scan_quoted(int c);
  128. bool should_ignore_manifest(const CPPManifest *manifest) const;
  129. bool should_ignore_preprocessor() const;
  130. int get();
  131. void unget(int c);
  132. CPPTemplateParameterList *
  133. nested_parse_template_instantiation(CPPTemplateScope *scope);
  134. void skip_to_end_nested();
  135. void skip_to_angle_bracket();
  136. class InputFile {
  137. public:
  138. InputFile();
  139. ~InputFile();
  140. bool open(const CPPFile &file);
  141. bool connect_input(const string &input);
  142. int get();
  143. const CPPManifest *_ignore_manifest;
  144. CPPFile _file;
  145. string _input;
  146. istream *_in;
  147. int _line_number;
  148. int _col_number;
  149. bool _lock_position;
  150. int _prev_last_c;
  151. };
  152. // This must be a list and not a vector because we don't have a good
  153. // copy constructor defined for InputFile.
  154. typedef list<InputFile> Files;
  155. Files _files;
  156. enum State {
  157. S_normal, S_eof, S_nested, S_end_nested
  158. };
  159. State _state;
  160. int _paren_nesting;
  161. bool _angle_bracket_found;
  162. bool _start_of_line;
  163. int _unget;
  164. int _last_c;
  165. bool _last_cpp_comment;
  166. bool _save_comments;
  167. vector<CPPToken> _saved_tokens;
  168. int _warning_count;
  169. int _error_count;
  170. };
  171. #endif