Skip to content

CppParser.hpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once

#include "doxide.hpp"
#include "Entity.hpp"

/**
 * C++ source parser.
 * 
 * @ingroup developer
 */
class CppParser {
public:
  /**
   * Constructor.
   */
  CppParser();

  /**
   * Destructor.
   */
  ~CppParser();

  /**
   * Parse C++ source.
   * 
   * @param file C++ source file name.
   * @param defines Macro definitions.
   * @param[in,out] root Root entity.
   */
  void parse(const std::filesystem::path& filename,
      const std::unordered_map<std::string,std::string>& defines,
      Entity& root);

private:
  /**
   * Push onto the stack.
   * 
   * @param entity Entity to push.
   * @param start Start byte of range.
   * @param end End byte of range.
   */
  void push(Entity&& entity, const uint32_t start, const uint32_t end);

  /**
   * Pop the stack down to the parent of an entity, according to its byte
   * range.
   * 
   * @param start Start byte of range.
   * @param end End byte of range.
   * 
   * @return The parent.
   * 
   * If both @p start and @p end are zero, this is interpreting as popping the
   * stack down to the root node and returning it.
   */
  Entity& pop(const uint32_t start = 0, const uint32_t end = 0);

  /**
   * Preprocess C++ source, replacing preprocessor macros as defined in the
   * config file and attempting to recover from any parse errors. This is
   * silent and does not report uncorrectable errors, these are reported
   * later.
   *
   * @param file C++ source file name.
   * @param defines Macro definitions.
   *
   * @return Preprocessed source.
   */
  std::string preprocess(const std::filesystem::path& file,
      const std::unordered_map<std::string,std::string>& defines);

  /**
   * Report errors after preprocessing.
   * 
   * @param file C++ source file name.
   * @param in Preprocessed source.
   * @param tree Parse tree for file.
   */
  void report(const std::filesystem::path& file, const std::string& in,
      TSTree* tree);

  /**
   * Stack of entities while parsing.
   */
  std::list<Entity> entities;

  /**
   * Stack of start bytes, corresponding to `entities`, while parsing.
   */
  std::list<uint32_t> starts;

  /**
   * Stack of end bytes, corresponding to `entities`, while parsing.
   */
  std::list<uint32_t> ends;

  /**
   * C++ parser.
   */
  TSParser* parser;

  /**
   * C++ entities query.
   */
  TSQuery* query;

  /**
   * C++ exclusions query.
   */
  TSQuery* query_exclude;

  /**
   * C++ inclusions query.
   */
  TSQuery* query_include;
};