This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
kconfig_parser.cpp
201 lines (173 loc) · 4.65 KB
/
kconfig_parser.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "kconfig_parser.hpp"
#include "base.hpp"
#include "kconfig_parser_exception.hpp"
namespace kconfig
{
KConfigParser::KConfigParser (FileUtility & fileUtilityParam, CppKeySet & keySetParam)
: fileUtility{ fileUtilityParam }, keySet{ keySetParam }
{
}
void KConfigParser::parse (CppKey const & parent)
{
CppKey current_group{ parent.getName (), KEY_END };
CppKey current_key{ parent.getName (), KEY_END };
while (true)
{
fileUtility.skipLineIfEmptyOrComment ();
if (fileUtility.isNextCharEOF ())
{
break;
}
if (fileUtility.peekNextChar () == character_open_bracket)
{
current_group = loadGroupNameFromFile (parent);
appendIfContainsMeta (current_group);
}
else
{
current_key = loadKeyFromFile (current_group);
appendIfNotGroup (current_key, current_group);
}
}
}
kdb::Key KConfigParser::loadGroupNameFromFile (CppKey const & parent)
{
CppKey key{ parent.getName (), KEY_END };
while (fileUtility.peekNextChar () == character_open_bracket)
{
// Skip open bracket
fileUtility.skipChar ();
// If a name starts with `$`, it should be saved as a meta value instead of a key name
if (fileUtility.peekNextChar () == character_dollar_sign)
{
// Skip the dollar sign
fileUtility.skipChar ();
std::string meta_candidate{ fileUtility.getUntilChar (character_close_bracket) };
if (fileUtility.peekNextChar () == character_close_bracket)
{
fileUtility.skipChar ();
}
else
{
throw KConfigParserException::expect (fileUtility, character_close_bracket);
}
// A meta value can only be placed at the last value. E.g.
// [keyName1][$pseudoMeta][keyName2][$realMeta]
// In the previous example `realMeta` is a meta value, `pseudoMeta` is not
if (fileUtility.isNextCharNewlineOrEOF ())
{
key.setMeta (KCONFIG_METADATA_KEY, meta_candidate);
}
else
{
// Add the dollar sign that we skipped earlier
key.addBaseName (character_dollar_sign + meta_candidate);
}
}
else
{
key.addBaseName (fileUtility.getUntilChar (character_close_bracket));
if (fileUtility.peekNextChar () == character_close_bracket)
{
fileUtility.skipChar ();
}
else
{
throw KConfigParserException::expect (fileUtility, character_close_bracket);
}
}
}
if (!fileUtility.isNextCharNewlineOrEOF ())
{
throw KConfigParserException::expect (fileUtility, "new line or end of file");
}
return key;
}
CppKey KConfigParser::loadKeyFromFile (CppKey const & parent)
{
fileUtility.skipCharsIfBlank ();
if (fileUtility.isNextCharToken ())
{
throw KConfigParserException::expect (fileUtility, "key name, not a spacial character");
}
std::string keyName{ fileUtility.getUntilChar (character_equals_sign, character_open_bracket) };
// If the following line introduces problems, use `parent.dup();`
CppKey key{ parent.getName (), KEY_END };
if (fileUtility.isNextCharNewlineOrEOF ())
{
// This key doesn't contain any value
key.addBaseName (keyName);
return key;
}
std::string meta;
bool has_locale = false;
// Load the meta values and/or locales
while (fileUtility.peekNextChar () == character_open_bracket)
{
// Skip the open bracket
fileUtility.skipChar ();
if (fileUtility.peekNextChar () == character_dollar_sign)
{
// Load meta value
// Skip dollar sign
fileUtility.skipChar ();
meta += fileUtility.getUntilChar (character_close_bracket);
}
else
{
// Load locale
if (has_locale)
{
throw KConfigParserException{ fileUtility, "Only one locale is allowed in a single key definition." };
}
else
{
has_locale = true;
}
keyName += character_open_bracket + fileUtility.getUntilChar (character_close_bracket) + character_close_bracket;
}
if (fileUtility.peekNextChar () == character_close_bracket)
{
fileUtility.skipChar ();
}
else
{
throw KConfigParserException::expect (fileUtility, character_close_bracket);
}
}
key.addBaseName (keyName);
// Skip empty spaces if any
fileUtility.skipCharsIfBlank ();
// Load the value
if (fileUtility.peekNextChar () == character_equals_sign)
{
// Skip the equals sign
fileUtility.skipChar ();
fileUtility.skipCharsIfBlank ();
key.setString (fileUtility.getUntilChar (character_newline));
}
else if (!fileUtility.isNextCharNewlineOrEOF ())
{
throw KConfigParserException::expect (fileUtility, "new line or end of file");
}
if (!meta.empty ())
{
key.setMeta (KCONFIG_METADATA_KEY, meta);
}
return key;
}
void KConfigParser::appendIfContainsMeta (CppKey const & key)
{
if (key.hasMeta (KCONFIG_METADATA_KEY))
{
keySet.append (key);
}
}
void KConfigParser::appendIfNotGroup (CppKey const & key, CppKey const & group)
{
if (key != group)
{
keySet.append (key);
}
}
}