forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.hpp
165 lines (136 loc) · 4.27 KB
/
driver.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @file
*
* @brief This file specifies auxiliary functions and data used by a Bison
* parser to convert YAML data to a key set.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef ELEKTRA_PLUGIN_YAMBI_DRIVER_HPP
#define ELEKTRA_PLUGIN_YAMBI_DRIVER_HPP
// -- Imports ------------------------------------------------------------------
#include <stack>
#include <string>
#include <kdb.hpp>
#include "lexer.hpp"
#include "parser.hpp"
typedef yambi::Parser::location_type location_type;
// -- Class --------------------------------------------------------------------
/**
* @brief This class provides a communication interface between the
* parser (`parser`) and the parser user (`convert`).
*/
class Driver
{
/** This variable stores the key set the drive creates from the given YAML
file */
kdb::KeySet keys;
/**
* This stack stores a key for each level of the current key name below
* parent.
*/
std::stack<kdb::Key> parents;
/**
* This stack stores indices for the next array elements.
*/
std::stack<uintmax_t> indices;
/**
* This variable stores all error message produced by the parser.
*/
std::string errorMessage;
/**
* This variable stores the number of errors caught by the parser.
*/
size_t numberOfErrors;
public:
/** This variable stores the path of the YAML file the driver is parsing. */
std::string filename;
/**
* This constructor creates a new driver for the given parent key.
*
* @param parent This key specifies the parent of the key set the parser
* creates.
*/
Driver (kdb::Key const & parent);
/**
* @brief This function parses the current YAML file.
*
* @param filename This parameter stores the path of the file the driver
* should parse.
*
* @retval -3 if the given file could not be opened
* @retval -2 if parsing was unsuccessful due to memory exhaustion
* @retval -1 if the given file contains a syntax error
* @retval 0 if parsing was successful
*/
int parse (const std::string & filepath);
/**
* @brief This method retrieves the current key set produced by the driver.
*
* @return A key set representing the YAML data produced by the last call of
* the method `parse`
*/
kdb::KeySet getKeySet () const;
/**
* @brief This function will be called by the Bison parser to indicate an
* error.
*
* @param location This value specifies the location of the erroneous input.
* @param message This value stores the error message emitted by the Bison
* parser.
* @param input This value stores the current input of the lexer/parser as text
*/
void error (const location_type & location, const std::string & message, std::string const & input);
/**
* @brief This function returns the last error message produced by the parser.
*
* @return A string containing an error message describing a syntax error
*/
std::string getErrorMessage ();
// ===========
// = Actions =
// ===========
/**
* @brief This function will be called before the parser enters an empty file (that might contain comments).
*/
void enterEmpty ();
/**
* @brief This function will be called after the parser exits a value.
*
* @param text This variable contains the text stored in the value.
*/
void exitValue (std::string const & text);
/**
* @brief This function will be called after the parser found a key.
*
* @param text This variable contains the text of the key.
*/
void exitKey (std::string const & text);
/**
* @brief This function will be called after the parser exits a key-value
* pair.
*
* @param matchedValue This variable specifies if the pair contains a value
* or not.
*/
void exitPair (bool const matchedValue);
/**
* @brief This function will be called after the parser enters a sequence.
*/
void enterSequence ();
/**
* @brief This function will be called after the parser exits a sequence.
*/
void exitSequence ();
/**
* @brief This function will be called after the parser recognizes an element
* of a sequence.
*/
void enterElement ();
/**
* @brief This function will be called after the parser read an element of a
* sequence.
*/
void exitElement ();
};
#endif // ELEKTRA_PLUGIN_YAMBI_DRIVER_HPP