Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief This file specifies auxiliary functions and data used by a Bison
5 : * parser to convert YAML data to a key set.
6 : *
7 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
8 : */
9 :
10 : #ifndef ELEKTRA_PLUGIN_YAMBI_DRIVER_HPP
11 : #define ELEKTRA_PLUGIN_YAMBI_DRIVER_HPP
12 :
13 : // -- Imports ------------------------------------------------------------------
14 :
15 : #include <stack>
16 : #include <string>
17 :
18 : #include <kdb.hpp>
19 :
20 : #include "lexer.hpp"
21 : #include "parser.hpp"
22 :
23 : typedef yambi::Parser::location_type location_type;
24 :
25 : // -- Class --------------------------------------------------------------------
26 :
27 : /**
28 : * @brief This class provides a communication interface between the
29 : * parser (`parser`) and the parser user (`convert`).
30 : */
31 378 : class Driver
32 : {
33 :
34 : /** This variable stores the key set the drive creates from the given YAML
35 : file */
36 : kdb::KeySet keys;
37 :
38 : /**
39 : * This stack stores a key for each level of the current key name below
40 : * parent.
41 : */
42 : std::stack<kdb::Key> parents;
43 :
44 : /**
45 : * This stack stores indices for the next array elements.
46 : */
47 : std::stack<uintmax_t> indices;
48 :
49 : /**
50 : * This variable stores all error message produced by the parser.
51 : */
52 : std::string errorMessage;
53 :
54 : /**
55 : * This variable stores the number of errors caught by the parser.
56 : */
57 : size_t numberOfErrors;
58 :
59 : public:
60 : /** This variable stores the path of the YAML file the driver is parsing. */
61 : std::string filename;
62 :
63 : /**
64 : * This constructor creates a new driver for the given parent key.
65 : *
66 : * @param parent This key specifies the parent of the key set the parser
67 : * creates.
68 : */
69 : Driver (kdb::Key const & parent);
70 :
71 : /**
72 : * @brief This function parses the current YAML file.
73 : *
74 : * @param filename This parameter stores the path of the file the driver
75 : * should parse.
76 : *
77 : * @retval -3 if the given file could not be opened
78 : * @retval -2 if parsing was unsuccessful due to memory exhaustion
79 : * @retval -1 if the given file contains a syntax error
80 : * @retval 0 if parsing was successful
81 : */
82 : int parse (const std::string & filepath);
83 :
84 : /**
85 : * @brief This method retrieves the current key set produced by the driver.
86 : *
87 : * @return A key set representing the YAML data produced by the last call of
88 : * the method `parse`
89 : */
90 : kdb::KeySet getKeySet () const;
91 :
92 : /**
93 : * @brief This function will be called by the Bison parser to indicate an
94 : * error.
95 : *
96 : * @param location This value specifies the location of the erroneous input.
97 : * @param message This value stores the error message emitted by the Bison
98 : * parser.
99 : * @param input This value stores the current input of the lexer/parser as text
100 : */
101 : void error (const location_type & location, const std::string & message, std::string const & input);
102 :
103 : /**
104 : * @brief This function returns the last error message produced by the parser.
105 : *
106 : * @return A string containing an error message describing a syntax error
107 : */
108 : std::string getErrorMessage ();
109 :
110 : // ===========
111 : // = Actions =
112 : // ===========
113 :
114 : /**
115 : * @brief This function will be called before the parser enters an empty file (that might contain comments).
116 : */
117 : void enterEmpty ();
118 :
119 : /**
120 : * @brief This function will be called after the parser exits a value.
121 : *
122 : * @param text This variable contains the text stored in the value.
123 : */
124 : void exitValue (std::string const & text);
125 :
126 : /**
127 : * @brief This function will be called after the parser found a key.
128 : *
129 : * @param text This variable contains the text of the key.
130 : */
131 : void exitKey (std::string const & text);
132 :
133 : /**
134 : * @brief This function will be called after the parser exits a key-value
135 : * pair.
136 : *
137 : * @param matchedValue This variable specifies if the pair contains a value
138 : * or not.
139 : */
140 : void exitPair (bool const matchedValue);
141 :
142 : /**
143 : * @brief This function will be called after the parser enters a sequence.
144 : */
145 : void enterSequence ();
146 :
147 : /**
148 : * @brief This function will be called after the parser exits a sequence.
149 : */
150 : void exitSequence ();
151 :
152 : /**
153 : * @brief This function will be called after the parser recognizes an element
154 : * of a sequence.
155 : */
156 : void enterElement ();
157 :
158 : /**
159 : * @brief This function will be called after the parser read an element of a
160 : * sequence.
161 : */
162 : void exitElement ();
163 : };
164 :
165 : #endif // ELEKTRA_PLUGIN_YAMBI_DRIVER_HPP
|