cppPreprocessor.h 6.4 KB

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