Beatmup
Beatmup::Listing::Parser Class Reference

Public Types

typedef std::map< std::string, std::vector< Listing::Block > > Content
 

Public Member Functions

 Parser (InputStream &stream, Content &chapters)
 
 Parser (std::istream &stream, Content &chapters)
 

Private Member Functions

void extractKeyValuePair (std::string line)
 
void processLine (std::string line)
 

Private Attributes

Contentchapters
 
std::vector< Listing::Block > * currentChapter
 
std::string chapterIndent
 
int lineCounter
 

Detailed Description

Definition at line 28 of file listing.cpp.

Member Typedef Documentation

◆ Content

typedef std::map<std::string, std::vector<Listing::Block> > Beatmup::Listing::Parser::Content

Definition at line 30 of file listing.cpp.

Constructor & Destructor Documentation

◆ Parser() [1/2]

Beatmup::Listing::Parser::Parser ( InputStream stream,
Content chapters 
)
inline

Definition at line 123 of file listing.cpp.

123  : chapters(chapters), currentChapter(nullptr), lineCounter(0) {
124  static const int SIZE = 1024;
125  char buffer[SIZE];
126  std::string line;
127  while (!stream.eof()) {
128  std::memset(buffer, 0, SIZE);
129  stream(buffer, SIZE);
130  int i = 0;
131  while (i < SIZE && buffer[i] != 0) {
132  if (buffer[i] == '\n') {
133  processLine(line);
134  line = "";
135  }
136  else
137  line += buffer[i];
138  ++i;
139  }
140  }
141  processLine(line);
142  }
virtual bool eof() const =0
Returns true, if the end of the stream is reached (i.e., all the data is read or the stream is empty)...
std::vector< Listing::Block > * currentChapter
Definition: listing.cpp:33
void processLine(std::string line)
Definition: listing.cpp:49

◆ Parser() [2/2]

Beatmup::Listing::Parser::Parser ( std::istream &  stream,
Content chapters 
)
inline

Definition at line 145 of file listing.cpp.

145  : chapters(chapters), currentChapter(nullptr), lineCounter(0) {
146  for (std::string line; std::getline(stream, line); )
147  processLine(line);
148  }

Member Function Documentation

◆ extractKeyValuePair()

void Beatmup::Listing::Parser::extractKeyValuePair ( std::string  line)
inlineprivate

Definition at line 37 of file listing.cpp.

37  {
38  const auto delim = line.find(":");
39  if (delim != std::string::npos) {
40  size_t i = delim + 1;
41  while (i < line.size() && line[i] == ' ')
42  ++i;
43  currentChapter->back().mapping.emplace(line.substr(0, delim), line.substr(i));
44  return;
45  }
46  throw RuntimeError("Line " + std::to_string(lineCounter) + ": cannot extract key-value pair from " + line);
47  }
std::string to_string(Beatmup::NNets::ActivationFunction function)

◆ processLine()

void Beatmup::Listing::Parser::processLine ( std::string  line)
inlineprivate

Definition at line 49 of file listing.cpp.

49  {
50  const std::string originalLine = line;
51  lineCounter++;
52 
53  if (line.empty())
54  return;
55 
56  // CR+LF
57  if (line.back() == '\r')
58  line.pop_back();
59 
60  // process comments
61  auto numberSign = line.find('#');
62  if (numberSign != std::string::npos) {
63  bool quoted = false;
64  for (size_t i = 0; i < numberSign - 1; ++i)
65  if (line[i] == '"')
66  quoted = !quoted;
67  // the number sign '#' is not quoted
68  if (!quoted)
69  line = line.substr(0, numberSign);
70  }
71 
72  // right trim
73  auto l = line.find_last_not_of(' ');
74  if (l == std::string::npos)
75  return;
76  if (l != line.length() - 1)
77  line = line.substr(0, l + 1);
78 
79  // empty
80  if (line.empty())
81  return;
82 
83  // new chapter
84  if (line.front() != ' ' && line.front() != '-' && line.back() == ':') {
85  line.pop_back();
86  auto it = chapters.emplace(std::piecewise_construct, std::forward_as_tuple(line), std::forward_as_tuple());
87  currentChapter = &it.first->second;
88  return;
89  }
90 
91  // new entry
92  if (currentChapter) {
93  // no chapter indent yet defined, expecting the one
94  if (chapterIndent.empty()) {
95  auto i = line.find_first_not_of(' ');
96  if (i != std::string::npos && i + 2 < line.length() && line[i] == '-' && line[i + 1] == ' ') {
97  chapterIndent = line.substr(0, i + 2);
98  currentChapter->push_back(lineCounter);
99  extractKeyValuePair(line.substr(i + 2));
100  return;
101  }
102  }
103 
104  // if the line starts with the chapter indent, new entry
105  else if (line.substr(0, chapterIndent.length()) == chapterIndent) {
106  currentChapter->push_back(lineCounter);
107  extractKeyValuePair(line.substr(chapterIndent.length()));
108  return;
109  }
110 
111  // if the line starts with a whitespace ident as long as the chapter indent, new key-value pair
112  else if (line.find_first_not_of(' ') == chapterIndent.length()) {
113  extractKeyValuePair(line.substr(chapterIndent.length()));
114  return;
115  }
116  }
117 
118  throw RuntimeError("Line " + std::to_string(lineCounter) + ": unexpected indent\n" + originalLine);
119  }
void extractKeyValuePair(std::string line)
Definition: listing.cpp:37
std::string chapterIndent
Definition: listing.cpp:34

Member Data Documentation

◆ chapters

Content& Beatmup::Listing::Parser::chapters
private

Definition at line 32 of file listing.cpp.

◆ currentChapter

std::vector<Listing::Block>* Beatmup::Listing::Parser::currentChapter
private

Definition at line 33 of file listing.cpp.

◆ chapterIndent

std::string Beatmup::Listing::Parser::chapterIndent
private

Definition at line 34 of file listing.cpp.

◆ lineCounter

int Beatmup::Listing::Parser::lineCounter
private

Definition at line 35 of file listing.cpp.


The documentation for this class was generated from the following file: