Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include "parser.hpp"
10 :
11 : #include <fstream>
12 : #include <iostream>
13 : #include <regex>
14 : #include <sstream>
15 :
16 : using namespace std;
17 : extern ostream cout;
18 :
19 0 : parse_t parse (std::string const & file)
20 : {
21 0 : ifstream fin (file.c_str ());
22 :
23 0 : parse_t result;
24 :
25 0 : int linenr = 0;
26 0 : string line;
27 0 : string lastIdentifier;
28 0 : map<string, string> currentMap;
29 0 : std::regex codeRegex ("^C[0-9A-Z]{5,5}$");
30 :
31 :
32 0 : while (getline (fin, line))
33 : {
34 0 : ++linenr;
35 :
36 0 : if (line.empty ())
37 : {
38 0 : result.push_back (currentMap);
39 0 : currentMap.clear ();
40 0 : continue;
41 : }
42 :
43 0 : size_t colonPos = line.find (':');
44 0 : if (colonPos == string::npos) throw parse_error ("No : found", linenr);
45 0 : std::string identifier = line.substr (0, colonPos);
46 0 : std::string text = line.substr (colonPos + 1);
47 0 : if (identifier.empty ()) identifier = lastIdentifier;
48 0 : if (identifier.empty ()) throw parse_error ("Line started with : but there was no previous identifier", linenr);
49 :
50 0 : if (identifier == "number")
51 : {
52 0 : bool isHighlevelFile = (file.find ("highlevel") != string::npos);
53 0 : if (!std::regex_match (text, codeRegex) && !isHighlevelFile)
54 0 : throw parse_error ("Error code does not match regular expression C[0-9A-Z]{5,5}", linenr);
55 : }
56 :
57 0 : if (!currentMap[identifier].empty ()) currentMap[identifier] += "\n";
58 0 : currentMap[identifier] += text;
59 :
60 0 : lastIdentifier = identifier;
61 : }
62 :
63 : // if new newline at end of file
64 0 : if (!currentMap.empty ())
65 : {
66 0 : result.push_back (currentMap);
67 : }
68 :
69 0 : return result;
70 0 : }
|