libvata2  [unstable] git snapshot
parser.hh
Go to the documentation of this file.
1 /* parser.hh -- VTF format parser
2  *
3  * Copyright (c) 2018 Ondrej Lengal <ondra.lengal@gmail.com>
4  *
5  * This file is a part of libvata2.
6  *
7  * this program is free software; you can redistribute it and/or modify
8  * it under the terms of the gnu general public license as published by
9  * the free software foundation; either version 3 of the license, or
10  * (at your option) any later version.
11  *
12  * this program is distributed in the hope that it will be useful,
13  * but without any warranty; without even the implied warranty of
14  * merchantability or fitness for a particular purpose. see the
15  * gnu general public license for more details.
16  */
17 
18 
19 #ifndef _VATA2_PARSER_HH_
20 #define _VATA2_PARSER_HH_
21 
22 #include <cassert>
23 #include <list>
24 #include <map>
25 #include <ostream>
26 #include <string>
27 #include <vector>
28 
29 // VATA headers
30 #include <vata2/util.hh>
31 
32 namespace Vata2
33 {
34 namespace Parser
35 {
36 
37 using KeyListStore = std::map<std::string, std::vector<std::string>>;
38 using BodyLine = std::vector<std::string>;
39 
40 /** Parsed data (single section) */
42 {
43  std::string type;
45  std::list<BodyLine> body;
46 
47  ParsedSection() : type(), dict(), body() { }
48 
49  /// Is the section empty?
50  bool empty() const { return type.empty() && dict.empty() && body.empty(); }
51 
52  /// Output stream operator
53  friend std::ostream& operator<<(std::ostream& os, const ParsedSection& parsec)
54  { // {{{
55  os << "@" << parsec.type << "\n";
56  for (const auto& string_list_pair : parsec.dict)
57  {
58  os << "%" << string_list_pair.first;
59  for (const std::string& str : string_list_pair.second)
60  {
61  os << " " << str;
62  }
63 
64  os << "\n";
65  }
66 
67  os << "# Body:\n";
68  for (const auto& body_line : parsec.body)
69  {
70  bool first = true;
71  for (const std::string& str : body_line)
72  {
73  if (!first) { os << " ";}
74  first = false;
75  os << str;
76  }
77  os << "\n";
78  }
79 
80  return os;
81  } // operator<< }}}
82 
83  /// Equality operator
84  bool operator==(const ParsedSection& rhs) const
85  { // {{{
86  return
87  this->type == rhs.type &&
88  this->dict == rhs.dict &&
89  this->body == rhs.body;
90  } // }}}
91  bool operator!=(const ParsedSection& rhs) const { return !(*this == rhs); }
92 };
93 
94 
95 
96 /** Parsed data */
97 using Parsed = std::vector<ParsedSection>;
98 
99 /** Parses a string into an intermediary structure */
100 Parsed parse_vtf(const std::string& input);
101 
102 /** Parses a stream into an intermediary structure */
103 Parsed parse_vtf(std::istream& input);
104 
105 /** Parses one section from a stream into an intermediary structure */
106 ParsedSection parse_vtf_section(std::istream& input);
107 
108 /** Parses one section from a string into an intermediary structure */
109 ParsedSection parse_vtf_section(const std::string& input);
110 
111 /// registers dispatcher
112 void init();
113 
114 // CLOSING NAMESPACES AND GUARDS
115 } /* Parser */
116 } /* Vata2 */
117 
118 #endif /* _VATA2_PARSER_HH_ */
119 
120 
Parsed parse_vtf(const std::string &input)
Parses a string into an intermediary structure.
Parsed data (single section)
Definition: parser.hh:41
bool operator==(const ParsedSection &rhs) const
Equality operator.
Definition: parser.hh:84
std::vector< std::string > BodyLine
Definition: parser.hh:38
std::vector< ParsedSection > Parsed
Parsed data.
Definition: parser.hh:97
std::list< BodyLine > body
Definition: parser.hh:45
std::map< std::string, std::vector< std::string >> KeyListStore
Definition: parser.hh:37
void init()
registers dispatcher
friend std::ostream & operator<<(std::ostream &os, const ParsedSection &parsec)
Output stream operator.
Definition: parser.hh:53
bool operator!=(const ParsedSection &rhs) const
Definition: parser.hh:91
bool empty() const
Is the section empty?
Definition: parser.hh:50
ParsedSection parse_vtf_section(std::istream &input)
Parses one section from a stream into an intermediary structure.