author | František Kučera <franta-hg@frantovo.cz> |
Tue, 21 Sep 2021 21:02:43 +0200 | |
branch | v_0 |
changeset 34 | 7eb3dcacba7b |
parent 28 | 596a724fbb83 |
child 35 | 930f17f16fd7 |
permissions | -rw-r--r-- |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#include <vector> |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
19 |
#include <regex> |
16 | 20 |
#include <sstream> |
21 |
#include <stdexcept> |
|
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include "INIReader.h" |
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
24 |
#include "uri.h" |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
|
16 | 26 |
namespace relpipe { |
27 |
namespace in { |
|
28 |
namespace ini { |
|
29 |
namespace lib { |
|
30 |
||
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
class INIReaderImpl : public INIReader { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
private: |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
std::istream& input; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
std::vector<INIContentHandler*> handlers; |
16 | 35 |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
36 |
class ConfiguredUnescapingProcessor { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
37 |
public: |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
38 |
std::shared_ptr<UnescapingProcessor> processor; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
39 |
const std::string uri; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
40 |
bool enbaled; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
41 |
|
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
42 |
ConfiguredUnescapingProcessor(std::shared_ptr<UnescapingProcessor> processor, const std::string uri, bool enbaled) : processor(processor), uri(uri), enbaled(enbaled) { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
43 |
} |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
44 |
|
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
45 |
}; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
46 |
|
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
47 |
std::vector<ConfiguredUnescapingProcessor> unescapingProcessors; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
48 |
|
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
49 |
class ConfiguredDialect { |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
50 |
public: |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
51 |
std::shared_ptr<Dialect> dialect; |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
52 |
const std::string uri; |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
53 |
|
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
54 |
ConfiguredDialect(std::shared_ptr<Dialect> dialect, const std::string uri) : dialect(dialect), uri(uri) { |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
55 |
} |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
56 |
|
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
57 |
}; |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
58 |
|
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
59 |
std::vector<ConfiguredDialect> dialects; |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
60 |
|
34
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
61 |
/** |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
62 |
* If there is a „\“ backspace at the end of a physical line, the logical line continues on the next physical line. |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
63 |
* |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
64 |
* Disabling this option makes sense only if we also disable the unescaping processors (unescape-basic, unescape-backspace). |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
65 |
* Otherwise they will complain about „Missing escape sequence“ because they got „\“ at the end of the value. |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
66 |
*/ |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
67 |
bool allowLineContinuationsWithEscaping = true; |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
68 |
|
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
69 |
/** |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
70 |
* If a line starts with a space, it is continuation of the previous line. |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
71 |
* This rule conflicts with default ignorance of such insignificant whitespace and is quite specific to the Java MANIFEST.MF dialect. |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
72 |
*/ |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
73 |
bool allowLineContinuationsWithSpace = false; |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
74 |
|
16 | 75 |
/** |
76 |
* By default, we ignore all leading whitespace on continuing lines. |
|
77 |
* If there should be some spaces or tabs, they should be placed on the previous line before the „\“. |
|
78 |
* If a line break is desired, it should be written as \n (escaped) or the value should be quoted in " or '. |
|
79 |
* |
|
26 | 80 |
* TODO: several options: |
81 |
* - enabled, disabled |
|
82 |
* - if disabled, then: keep backslash, trim backslash, escape backslash |
|
83 |
* (keep requires support in some further unescaping phase, or it will cause an error) |
|
84 |
* - keep or trim the line end |
|
85 |
* - keep or trim the leading spaces |
|
86 |
* - allow comments interleaved with continuing lines (the freaky systemd syntax) |
|
87 |
* |
|
16 | 88 |
* Related specifications: |
89 |
* - https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Properties.html |
|
26 | 90 |
* - https://www.freedesktop.org/software/systemd/man/systemd.syntax.html |
16 | 91 |
*/ |
23
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
92 |
bool trimLeadingSpacesOnContinuingLines = true; |
16 | 93 |
|
24
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
94 |
|
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
95 |
/** |
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
96 |
* Some dialects or configuration files in general does not support sections. |
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
97 |
* Then a line, that looks like an INI section, should be interpreted as a key |
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
98 |
* (or error, if does not have a proper key-value separator). |
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
99 |
*/ |
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
100 |
bool allowSections = true; |
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
101 |
|
16 | 102 |
/** |
103 |
* KDE uses some weird INI dialect that allows [section][x] syntax where „x“ is kind of „tag“ that signalizes some properties of given section. |
|
104 |
* Line „[section_1][$i]“ means that the „section_1“ is „locked“. |
|
105 |
* We may emit this information somehow later, but for now, it is just ignored. |
|
106 |
* |
|
107 |
* TODO: Is „section tag“ right name? |
|
108 |
* |
|
109 |
* Related specifications: |
|
110 |
* - https://userbase.kde.org/KDE_System_Administration/Configuration_Files#Lock_Down |
|
111 |
*/ |
|
112 |
bool allowSectionTags = true; |
|
113 |
||
114 |
/** |
|
115 |
* If whole key is „aaa[bbb]“ then „aaa“ is considered to be the key and „bbb“ the sub-key. |
|
116 |
* No \[ escaping is currently supported, so the key might not contain the bracket character. |
|
117 |
* |
|
118 |
* Related specifications: |
|
119 |
* - https://userbase.kde.org/KDE_System_Administration/Configuration_Files#Shell_Expansion |
|
120 |
* - https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s05.html |
|
121 |
*/ |
|
122 |
bool allowSubKeys = true; |
|
123 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
124 |
/** |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
125 |
* Classic INI uses „key=value“ syntax. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
126 |
* But some other formats/dialects might use key:value. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
127 |
* |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
128 |
* Only single character separators are supported. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
129 |
* If multiple separators should be recognized (e.g. both „=“ and „:“), this string will contain all of them, |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
130 |
* i.e. „:=“ does not mean that the „key:=value“ syntax, but „key=value“ or „key:value“. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
131 |
*/ |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
132 |
std::string keyValueSeparators = "="; |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
133 |
|
21
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
134 |
/** |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
135 |
* Classic INI uses „; comment“ syntax. |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
136 |
* But many existing files contain „# comment“ lines. |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
137 |
* |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
138 |
* Only single character separators are supported (works same as keyValueSeparators). |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
139 |
*/ |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
140 |
std::string commentSeparators = ";#"; |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
141 |
|
22
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
142 |
/** |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
143 |
* INI often support both "quotes" and 'apostrophes' styles. |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
144 |
* But some dialects may support only one of them or not support quoting at all. |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
145 |
* |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
146 |
* In such case e.g. „key="some value"“ would mean that the value is „"value"“ (including the quotes). |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
147 |
* Thus it is important to allow disabling quote recognizing (which is done by setting this parameter to empty string). |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
148 |
* |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
149 |
* Only single character quotes are supported (works same as keyValueSeparators). |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
150 |
*/ |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
151 |
std::string quotes = "\"'"; |
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
152 |
|
16 | 153 |
int lineNumber = 1; |
154 |
int eventNumber = 0; |
|
155 |
||
156 |
/** |
|
157 |
* Should be always used instead of input.peek(). |
|
158 |
* Skips \r. |
|
159 |
*/ |
|
160 |
char peek() { |
|
161 |
// In 2020 there is no need to manually return the carriage. However some legacy systems still do it. |
|
162 |
char ch = input.peek(); |
|
163 |
if (ch == '\r') { |
|
164 |
input.get(); |
|
165 |
ch = input.peek(); |
|
166 |
} |
|
167 |
return ch; |
|
168 |
} |
|
169 |
||
170 |
/** |
|
171 |
* Should be always used instead of input.get(). |
|
172 |
* Counts the lines and skips \r. |
|
173 |
*/ |
|
174 |
char get() { |
|
175 |
char ch = input.get(); |
|
176 |
if (ch == '\n') lineNumber++; |
|
177 |
else if (ch == '\r') ch = get(); |
|
178 |
return ch; |
|
179 |
} |
|
180 |
||
181 |
std::string readSpacesAndTabs() { |
|
182 |
std::stringstream result; |
|
183 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t'); ch = peek()) result.put(get()); |
|
184 |
return result.str(); |
|
185 |
} |
|
186 |
||
187 |
std::string readAllWhitespace() { |
|
188 |
std::stringstream result; |
|
189 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); ch = peek()) result.put(get()); |
|
190 |
return result.str(); |
|
191 |
} |
|
192 |
||
193 |
void processContinuingLine(std::stringstream& result) { |
|
23
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
194 |
if (trimLeadingSpacesOnContinuingLines) readSpacesAndTabs(); |
16 | 195 |
else result.put('\n'); |
196 |
} |
|
197 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
198 |
std::string readUntil(const char until, bool* found = nullptr) { |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
199 |
return readUntil(std::string(1, until), found); |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
200 |
} |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
201 |
|
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
202 |
std::string readUntil(const std::string& until, bool* found = nullptr) { |
16 | 203 |
std::stringstream result; |
204 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
205 |
for (char ch = peek(); input.good() && !oneOf(ch, until); ch = peek()) { |
34
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
206 |
if (allowLineContinuationsWithEscaping && ch == '\\') { |
16 | 207 |
get(); |
208 |
ch = get(); |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
209 |
if (oneOf(ch, until) && ch == '\n') processContinuingLine(result); |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
210 |
else if (oneOf(ch, until)) result.put(ch); |
16 | 211 |
else if (ch == std::istream::traits_type::eof()) break; |
212 |
else result.put('\\').put(ch); |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
213 |
// unescaping is done in two phases: |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
214 |
// here we unescape just the \n (LF) |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
215 |
// other escape sequences are leaved untouched and will be processed in later phases, see see UnescapingINIHandler |
16 | 216 |
} else { |
217 |
ch = get(); |
|
218 |
result.put(ch); |
|
219 |
} |
|
220 |
} |
|
221 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
222 |
if (oneOf(peek(), until)) { |
16 | 223 |
get(); |
224 |
if (found) *found = true; |
|
225 |
} else { |
|
226 |
if (found) *found = false; |
|
227 |
} |
|
228 |
||
229 |
return result.str(); |
|
230 |
} |
|
231 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
232 |
std::string readToken(const char until, char* quote = nullptr, bool* found = nullptr) { |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
233 |
return readToken(std::string(1, until), quote, found); |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
234 |
} |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
235 |
|
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
236 |
std::string readToken(const std::string& until, char* quote = nullptr, bool* found = nullptr) { |
16 | 237 |
std::string result; |
238 |
||
239 |
char ch = peek(); |
|
240 |
if (isQuote(ch)) { |
|
241 |
if (quote) *quote = ch; |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
242 |
result = readUntil(std::string(1, get()), found); |
16 | 243 |
} else { |
244 |
if (quote) *quote = 0; |
|
245 |
result = readUntil(until, found); |
|
246 |
} |
|
247 |
||
248 |
return result; |
|
249 |
} |
|
250 |
||
251 |
std::string readTokenAndEatTerminator(char until, char* quote = nullptr, bool* found = nullptr) { |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
252 |
return readTokenAndEatTerminator(std::string(1, until), quote, found); |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
253 |
} |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
254 |
|
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
255 |
std::string readTokenAndEatTerminator(const std::string& until, char* quote = nullptr, bool* found = nullptr) { |
16 | 256 |
std::string result = readToken(until, quote, found); |
257 |
if (*quote) { |
|
258 |
readAllWhitespace(); |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
259 |
if (!oneOf(get(), until)) throw std::logic_error(std::string("missing „") + until + "“ after quoted section name"); |
16 | 260 |
} |
261 |
return result; |
|
262 |
} |
|
263 |
||
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
264 |
std::string unescape(const std::string& value, UnescapingProcessor::TextType type) { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
265 |
std::string result = value; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
266 |
for (ConfiguredUnescapingProcessor p : unescapingProcessors) if (p.enbaled) result = p.processor->unescape(result, type); |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
267 |
return result; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
268 |
} |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
269 |
|
16 | 270 |
bool isComment(char ch) { |
21
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
271 |
return oneOf(ch, commentSeparators); |
16 | 272 |
} |
273 |
||
274 |
bool isQuote(char ch) { |
|
22
29d673a54ecf
prepare for multiple and configurable quotes (e.g. „key="value"“ and „key='value'“ or disabling quote support at all)
František Kučera <franta-hg@frantovo.cz>
parents:
21
diff
changeset
|
275 |
return oneOf(ch, quotes); |
16 | 276 |
} |
277 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
278 |
/** |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
279 |
* @param ch character to be evaluated |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
280 |
* @param options list of options (characters) |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
281 |
* @return whether ch is one of options |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
282 |
*/ |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
283 |
bool oneOf(char ch, const std::string& options) { |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
284 |
return options.find(ch) != std::string::npos; |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
285 |
} |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
286 |
|
16 | 287 |
std::string trim(std::string s) { |
288 |
return std::regex_replace(s, std::regex("^\\s+|\\s+$"), ""); |
|
289 |
} |
|
290 |
||
23
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
291 |
/** |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
292 |
* TODO: use a common method |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
293 |
*/ |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
294 |
bool parseBoolean(const std::string& value) { |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
295 |
if (value == "true") return true; |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
296 |
else if (value == "false") return false; |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
297 |
else throw std::invalid_argument(std::string("Unable to parse boolean value: ") + value + " (expecting true or false)"); |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
298 |
} |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
299 |
|
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
300 |
void setDialect(const std::string& uri) { |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
301 |
for (ConfiguredDialect& d : dialects) { |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
302 |
if (d.uri == uri) { |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
303 |
d.dialect->apply(*this); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
304 |
return; |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
305 |
} |
23
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
306 |
} |
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
307 |
throw std::invalid_argument(std::string("Unsupported INI dialect: ") + uri); |
23
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
308 |
} |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
309 |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
310 |
bool setUnescaping(const std::string& uri, const std::string& value) { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
311 |
for (ConfiguredUnescapingProcessor& p : unescapingProcessors) { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
312 |
if (p.uri == uri) { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
313 |
p.enbaled = parseBoolean(value); |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
314 |
return true; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
315 |
} |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
316 |
} |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
317 |
return false; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
318 |
} |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
319 |
|
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
320 |
public: |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
321 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
322 |
INIReaderImpl(std::istream& input) : input(input) { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
323 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
324 |
|
23
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
325 |
void setOption(const std::string& uri, const std::string& value) override { |
34
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
326 |
if (uri == option::AllowLineContinuationWithEscaping) allowLineContinuationsWithEscaping = parseBoolean(value); |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
327 |
else if (uri == option::AllowLineContinuationWithSpace) allowLineContinuationsWithSpace = parseBoolean(value); |
7eb3dcacba7b
implement --parser-option allow-line-continuation-with-escaping + add skeleton for allow-line-continuation-with-space
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
328 |
else if (uri == option::TrimContinuingLines) trimLeadingSpacesOnContinuingLines = parseBoolean(value); |
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
329 |
else if (uri == option::AllowSections) allowSections = parseBoolean(value); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
330 |
else if (uri == option::AllowSectionTags) allowSectionTags = parseBoolean(value); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
331 |
else if (uri == option::AllowSubKeys) allowSubKeys = parseBoolean(value); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
332 |
else if (uri == option::CommentSeparators) commentSeparators = value; |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
333 |
else if (uri == option::KeyValueSeparators) keyValueSeparators = value; |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
334 |
else if (uri == option::Quotes) quotes = value; |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
335 |
else if (uri == option::Dialect) setDialect(value); |
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
336 |
else if (setUnescaping(uri, value)); |
23
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
337 |
else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“"); |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
338 |
} |
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
339 |
|
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
340 |
void addHandler(INIContentHandler* handler) override { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
341 |
handlers.push_back(handler); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
342 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
343 |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
344 |
void addUnescapingProcessor(std::shared_ptr<UnescapingProcessor> processor, const std::string uri, bool enabledByDefault) override { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
345 |
unescapingProcessors.push_back({processor, uri, enabledByDefault}); |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
346 |
} |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
347 |
|
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
348 |
void addDialect(std::shared_ptr<Dialect> dialect, const std::string uri, bool enabledByDefault) override { |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
349 |
dialects.push_back({dialect, uri}); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
350 |
if (enabledByDefault) dialect->apply(*this); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
351 |
} |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
352 |
|
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
353 |
void process() override { |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
354 |
for (INIContentHandler* handler : handlers) handler->startDocument(); |
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
355 |
|
16 | 356 |
bool inSection = false; |
357 |
||
358 |
while (input.good()) { // TODO: condition |
|
359 |
{ |
|
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
360 |
INIContentHandler::WhitespaceEvent event; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
361 |
event.lineNumber = lineNumber; |
16 | 362 |
std::string whitespace = readAllWhitespace(); |
363 |
if (whitespace.size()) { |
|
364 |
event.eventNumber = ++eventNumber; |
|
365 |
event.whitespace = whitespace; |
|
366 |
for (INIContentHandler* handler : handlers) handler->whitespace(event); |
|
367 |
} |
|
368 |
} |
|
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
369 |
|
16 | 370 |
bool found; |
371 |
char quote; |
|
372 |
||
373 |
char ch = peek(); |
|
2
f031a4dc7c52
add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
374 |
|
16 | 375 |
if (ch == std::istream::traits_type::eof()) { |
376 |
break; |
|
24
07e0a2edf3bc
add --parser-option allow-sections (Java .properties have no sections)
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
377 |
} else if (ch == '[' && allowSections) { |
16 | 378 |
if (inSection) for (INIContentHandler* handler : handlers) handler->endSection(); |
379 |
inSection = true; |
|
380 |
INIContentHandler::SectionStartEvent event; |
|
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
381 |
event.lineNumber = lineNumber; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
382 |
event.eventNumber = ++eventNumber; |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
383 |
get(); |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
384 |
readAllWhitespace(); |
16 | 385 |
event.name = readTokenAndEatTerminator(']', "e, &found); |
25
b9067d2812e5
trim section names if not in quotes
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
386 |
if (!quote) event.name = trim(event.name); |
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
387 |
event.name = unescape(event.name, UnescapingProcessor::TextType::SectionName); |
16 | 388 |
|
389 |
readSpacesAndTabs(); |
|
390 |
if (allowSectionTags && peek() == '[') { |
|
391 |
get(); |
|
392 |
event.tag = readTokenAndEatTerminator(']', "e, &found); |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
393 |
event.tag = unescape(event.tag, UnescapingProcessor::TextType::SectionTag); |
16 | 394 |
} |
395 |
||
396 |
readSpacesAndTabs(); |
|
397 |
ch = peek(); |
|
398 |
if (isComment(ch)) { |
|
399 |
get(); |
|
400 |
readSpacesAndTabs(); |
|
401 |
event.comment = readUntil('\n', &found); |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
402 |
event.comment = unescape(event.comment, UnescapingProcessor::TextType::SectionComment); |
16 | 403 |
} else if (ch == '\n') { |
404 |
get(); |
|
405 |
} else { |
|
406 |
throw std::logic_error(std::string("unexpected content after the section: '") + event.name + "'"); |
|
407 |
} |
|
408 |
||
409 |
for (INIContentHandler* handler : handlers) handler->startSection(event); |
|
410 |
} else if (isComment(ch)) { |
|
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
411 |
INIContentHandler::CommentEvent event; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
412 |
event.lineNumber = lineNumber; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
413 |
event.eventNumber = ++eventNumber; |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
414 |
get(); |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
415 |
readSpacesAndTabs(); |
16 | 416 |
event.comment = readUntil('\n', &found); |
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
417 |
event.comment = unescape(event.comment, UnescapingProcessor::TextType::Comment); |
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
418 |
for (INIContentHandler* handler : handlers) handler->comment(event); |
16 | 419 |
} else { |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
420 |
INIContentHandler::EntryEvent event; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
421 |
event.lineNumber = lineNumber; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
422 |
event.eventNumber = ++eventNumber; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
423 |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
424 |
std::string fullKey = readToken(keyValueSeparators, "e, &found); |
16 | 425 |
if (!found) throw std::logic_error(std::string("missing = after key: '") + fullKey + "'"); |
426 |
if (!quote) fullKey = trim(fullKey); |
|
427 |
readSpacesAndTabs(); |
|
428 |
||
429 |
if (quote) { |
|
430 |
ch = get(); |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
431 |
if (oneOf(ch, keyValueSeparators)) readSpacesAndTabs(); |
16 | 432 |
else throw std::logic_error(std::string("missing = after quoted key: '") + fullKey + "'"); |
433 |
} |
|
434 |
||
435 |
std::string value = readToken('\n', "e, &found); |
|
436 |
if (!quote) value = trim(value); |
|
437 |
||
438 |
event.key = fullKey; |
|
439 |
event.fullKey = fullKey; |
|
440 |
event.value = value; |
|
14
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
441 |
|
16 | 442 |
if (allowSubKeys) { |
443 |
std::smatch match; |
|
444 |
if (std::regex_match(fullKey, match, std::regex("([^\\[]+)\\[([^\\[]+)\\]"))) { |
|
445 |
event.key = match[1]; |
|
446 |
event.subKey = match[2]; |
|
447 |
event.fullKey = fullKey; |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
448 |
event.subKey = unescape(event.subKey, UnescapingProcessor::TextType::EntryKey); |
16 | 449 |
} |
450 |
} |
|
451 |
||
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
452 |
event.key = unescape(event.key, UnescapingProcessor::TextType::EntryKey); |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
453 |
event.fullKey = unescape(event.fullKey, UnescapingProcessor::TextType::EntryKey); |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
454 |
event.value = unescape(event.value, UnescapingProcessor::TextType::EntryValue); |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
455 |
|
16 | 456 |
if (quote) { |
457 |
readSpacesAndTabs(); |
|
458 |
ch = peek(); |
|
459 |
if (isComment(ch)) { |
|
460 |
get(); |
|
461 |
readSpacesAndTabs(); |
|
462 |
event.comment = readUntil('\n', &found); |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
463 |
event.comment = unescape(event.comment, UnescapingProcessor::TextType::EntryComment); |
16 | 464 |
} else if (ch == '\n') { |
465 |
get(); |
|
466 |
} else { |
|
26 | 467 |
// TODO: optional support for multiple tokens in a single entry? |
468 |
// modes: array, concatenate |
|
469 |
// some-array-1 = "item 1" "item 2" 'item 3' item 4 |
|
470 |
// some-array-2 = "item 1" "item 2" 'item 3' item_4 item_5 |
|
471 |
// some-bash-style-string-value = "this "will' be' concatenated → this will be concatenated |
|
16 | 472 |
throw std::logic_error(std::string("unexpected content after the quoted value: key='") + fullKey + "' value='" + event.value + "'"); |
14
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
473 |
} |
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
474 |
} |
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
475 |
|
2
f031a4dc7c52
add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
476 |
for (INIContentHandler* handler : handlers) handler->entry(event); |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
477 |
} |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
478 |
} |
16 | 479 |
// TODO: error at the end, catch premature/unexpected EOF |
480 |
// TODO: unescape + trim values + ignore \r |
|
481 |
// TODO: count lines |
|
2
f031a4dc7c52
add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
482 |
if (inSection) for (INIContentHandler* handler : handlers) handler->endSection(); |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
483 |
for (INIContentHandler* handler : handlers) handler->endDocument(); |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
484 |
} |
16 | 485 |
|
486 |
// General feautres: |
|
487 |
// TODO: warning/error handler |
|
488 |
// TODO: support also escaped characters |
|
489 |
// TODO: support also Java .properties and manifest.mf formats? |
|
490 |
// TODO: support also nested sections – hierarchy |
|
491 |
// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections |
|
492 |
// TODO: support also option for alternative key-value separator (: instead of =) |
|
493 |
// TODO: support also other encodings (currently only UTF-8 is supported) |
|
494 |
// TODO: better exceptions |
|
495 |
||
496 |
// Lossless conversions: |
|
497 |
// TODO: emit also the quote style ('/"/) |
|
498 |
// TODO: emit also the comment style (;/#) ? |
|
499 |
// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ? |
|
500 |
// TODO: emit also the line-end type (LF/CRLF) ? |
|
501 |
||
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
502 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
503 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
504 |
INIReader* INIReader::create(std::istream& input) { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
505 |
return new INIReaderImpl(input); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
506 |
} |
16 | 507 |
|
508 |
} |
|
509 |
} |
|
510 |
} |
|
511 |
} |