File: ini.cpp

package info (click to toggle)
libpog 0.5.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,644 kB
  • sloc: cpp: 51,038; ansic: 239; makefile: 14; python: 11; sh: 11
file content (182 lines) | stat: -rw-r--r-- 4,462 bytes parent folder | download
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
#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>

#include <pog/parser.h>

using namespace pog;

struct NoData{};
using Value = std::variant<NoData, bool, int, double, std::string>;

struct Attribute
{
	std::string key;
	Value value;
};

struct Section
{
	std::string name;
	std::vector<Attribute> attributes;
};

struct Document
{
	Section global;
	std::vector<Section> sections;
};

void print_attributes(const Section& section)
{
	for (const auto& attr : section.attributes)
	{
		fmt::print("{}::{}", section.name, attr.key);
		std::visit(overloaded {
			[](const NoData&) { fmt::print(" (no data)\n"); },
			[](bool b) { fmt::print(" (bool) = {}\n", b); },
			[](int i) { fmt::print(" (int) = {}\n", i); },
			[](double d) { fmt::print(" (double) = {}\n", d); },
			[](const std::string& s) { fmt::print(" (string) = {}\n", s); }
		}, attr.value);
	}
}

int main(int argc, char* argv[])
{
	if (argc < 2)
	{
		fmt::print("Usage: {} INPUT_FILE\n", argv[0]);
		return 1;
	}

	using ParserType = std::variant<
		Value,
		Attribute,
		Section,
		Document,
		std::vector<Attribute>,
		std::vector<Section>
	>;

	Parser<ParserType> p;

	p.token(R"(\s+)");
	p.token("\\[").symbol("[");
	p.token("\\]").symbol("]");
	p.token("=").symbol("=");
	p.token(R"([0-9]+\.[0-9]+)").symbol("double").action([](std::string_view str) -> ParserType {
		return std::stod(std::string{str});
	});
	p.token(R"([0-9]+)").symbol("int").action([](std::string_view str) -> ParserType {
		return std::stoi(std::string{str});
	});
	p.token("(true|false)").symbol("bool").action([](std::string_view str) -> ParserType {
		return str == "true";
	});
	p.token("[a-zA-Z0-9]+").symbol("id").fullword().action([](std::string_view str) -> ParserType {
		return std::string{str};
	});

	p.set_start_symbol("root");
	p.rule("root")
		.production("attrs", "sections", [](auto&& args) -> ParserType {
			return Document{
				Section{std::string{}, std::get<std::vector<Attribute>>(args[0])},
				std::get<std::vector<Section>>(args[1])
			};
		})
		.production("attrs", [](auto&& args) -> ParserType {
			return Document{
				Section{std::string{}, std::get<std::vector<Attribute>>(args[0])},
				std::vector<Section>{}
			};
		})
		.production("sections", [](auto&& args) -> ParserType {
			return Document{
				Section{std::string{}, std::vector<Attribute>{}},
				std::get<std::vector<Section>>(args[0])
			};
		})
		.production([](auto&&) -> ParserType {
			return Document{
				Section{std::string{}, std::vector<Attribute>{}},
				std::vector<Section>{}
			};
		});

	p.rule("sections")
		.production("sections", "section", [](auto&& args) -> ParserType {
			std::get<std::vector<Section>>(args[0]).push_back(std::get<Section>(args[1]));
			return std::move(args[0]);
		})
		.production("section", [](auto&& args) -> ParserType {
			return std::vector<Section>{std::get<Section>(args[0])};
		});

	p.rule("section")
		.production("[", "id", "]", "attrs", [](auto&& args) -> ParserType {
			return Section{
				std::get<std::string>(std::get<Value>(args[1])),
				std::get<std::vector<Attribute>>(args[3])
			};
		});

	p.rule("attrs")
		.production("attrs", "attr", [](auto&& args) -> ParserType {
			std::get<std::vector<Attribute>>(args[0]).push_back(std::get<Attribute>(args[1]));
			return std::move(args[0]);
		})
		.production("attr", [](auto&& args) -> ParserType {
			return std::vector<Attribute>{std::get<Attribute>(args[0])};
		});

	p.rule("attr").production("id", "=", "value", [](auto&& args) -> ParserType {
		return Attribute{
			std::get<std::string>(std::get<Value>(args[0])),
			std::get<Value>(args[2])
		};
	});

	p.rule("value")
		.production("double", [](auto&& args) -> ParserType {
			return std::move(args[0]);
		})
		.production("int", [](auto&& args) -> ParserType {
			return std::move(args[0]);
		})
		.production("bool", [](auto&& args) -> ParserType {
			return std::move(args[0]);
		})
		.production("id", [](auto&& args) -> ParserType {
			return std::move(args[0]);
		});

	p.prepare();

	std::ifstream input_file(argv[1]);
	auto document = p.parse(input_file);

	if (document)
	{
		const auto& d = document.value();
		if (std::holds_alternative<Document>(d))
		{
			const auto& dd = std::get<Document>(d);
			print_attributes(dd.global);
			for (const auto& sect : dd.sections)
				print_attributes(sect);
		}
		else
		{
			fmt::print("Parser error\n");
			return 1;
		}
	}
	else
	{
		fmt::print("Parser error\n");
		return 1;
	}
}