enforce the parser protocol – throw exception if violated by the parser v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 24 Nov 2020 12:21:33 +0100
branchv_0
changeset 15 0cf8bcb9c8fc
parent 14 ea431c469403
child 16 db994a2ddffa
enforce the parser protocol – throw exception if violated by the parser
src/INICommand.cpp
--- a/src/INICommand.cpp	Mon Nov 23 21:09:33 2020 +0100
+++ b/src/INICommand.cpp	Tue Nov 24 12:21:33 2020 +0100
@@ -42,6 +42,7 @@
 	std::shared_ptr<writer::RelationalWriter> writer;
 	Configuration& configuration;
 	std::vector<std::string> currentSection;
+	bool inDocument = false;
 
 	std::string getCurrentSectionFullName() {
 		std::stringstream result;
@@ -96,6 +97,8 @@
 	}
 
 	void startDocument() override {
+		if (inDocument) throw std::out_of_range("Lunatic INI parser tried to start a document without ending the previous one.");
+		inDocument = true;
 		vector<AttributeMetadata> metadata;
 		if (configuration.enableLineNumbers) metadata.push_back({L"line", TypeId::INTEGER});
 		if (configuration.enableEventNumbers) metadata.push_back({L"event", TypeId::INTEGER});
@@ -109,10 +112,13 @@
 	};
 
 	void endDocument() override {
+		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to end a document without starting it before.");
+		inDocument = false;
 		currentSection.clear();
 	};
 
 	void startSection(const SectionStartEvent& event) override {
+		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to start a section without starting a document.");
 		currentSection.push_back(event.name);
 
 		if (configuration.enableComments && event.comment.size()) {
@@ -123,10 +129,12 @@
 	};
 
 	void endSection() override {
+		if (currentSection.empty()) throw std::out_of_range("Lunatic INI parser tried to end a section without starting it before.");
 		currentSection.pop_back();
 	};
 
 	void entry(const EntryEvent& event) override {
+		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit an entry without starting a document.");
 		Record record(&event);
 		record.comment = event.comment;
 		record.key = configuration.enableSubKeys ? event.key : event.fullKey;
@@ -136,6 +144,7 @@
 	};
 
 	void comment(const CommentEvent& event) override {
+		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit a comment without starting a document.");
 		if (configuration.enableComments) {
 			Record record(&event);
 			record.comment = event.comment;
@@ -144,6 +153,7 @@
 	}
 
 	void whitespace(const WhitespaceEvent& event) override {
+		if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit a whitespace without starting a document.");
 		if (configuration.enableWhitespace) {
 			Record record(&event);
 			record.whitespace = event.whitespace;