Skip to content

YAMLParser.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
#pragma once

#include "doxide.hpp"
#include "YAMLNode.hpp"

/**
 * Parser for YAML config files and YAML frontmatter of Markdown files. Also
 * works for JSON, as YAML is nowadays a superset of JSON.
 * 
 * @ingroup developer
 */
class YAMLParser {
public:
  /**
   * Constructor.
   *
   * @param filename File name.
   */
  YAMLParser();

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

  /**
   * Parse the file.
   * 
   * @return The contents of the file.
   */
  YAMLNode parse(const std::filesystem::path& filename);

private:
  void parseMapping(const std::filesystem::path& filename, YAMLNode& node);
  void parseSequence(const std::filesystem::path& filename, YAMLNode& node);
  void parseValue(const std::filesystem::path& filename, YAMLNode& node);

  /**
   * LibYAML parser.
   */
  yaml_parser_t parser;

  /**
   * LibYAML event.
   */
  yaml_event_t event;
};