btool
A parser/converter/transpiler for .bib files
ValueParserState.cpp
Go to the documentation of this file.
1 #include <ValueParserState.hpp>
2 #include <boost/algorithm/string.hpp>
3 #include <boost/algorithm/string/predicate.hpp>
4 #include <GlobalParserState.hpp>
5 #include <ParserException.hpp>
6 #include <KeyParserState.hpp>
7 #include <string>
8 #include <cctype>
9 
10 using namespace std::literals::string_literals;
11 
17 ValueParserState::ValueParserState(ParserContext &context, std::vector<BibElement> &result) noexcept
18  : AbstractParserState{context, result} {}
19 
27  if (c == ',') {
28  if (value.empty()) {
29  fail("Key must not be empty"s);
30  } else if (braces.empty()) {
31  result.back().attributes.back().value = boost::trim_copy(value);
32  const auto keyState = new KeyParserState{context, result};
33  delete this;
34  return keyState;
35  } else {
36  value += c;
37  }
38  } else if (c == '{') {
39  braces.push(std::make_pair(context.line, context.column));
40  } else if (c == '}') {
41  replaceSpecialCharacter(value);
42  if (braces.empty()) {
43  result.back().attributes.back().value = boost::trim_copy(value);
44  const auto keyState = new GlobalParserState{context, result};
45  delete this;
46  return keyState;
47  } else {
48  braces.pop();
49  }
50  } else if (std::isgraph(c) || std::isspace(c)) {
51  value += c;
52  } else {
53  fail("Invalid Character in Value, got so far: ["s + value + "]"s);
54  }
55  return this;
56 }
57 
58 auto ValueParserState::replaceSpecialCharacter(std::string &s) -> void {
59  if (boost::algorithm::ends_with(s, R"(\"a)")) {
60  boost::algorithm::replace_last(s, R"(\"a)", "ä");
61  } else if (boost::algorithm::ends_with(s, R"(\"o)")) {
62  boost::algorithm::replace_last(s, R"(\"o)", "ö");
63  } else if (boost::algorithm::ends_with(s, R"(\"u)")) {
64  boost::algorithm::replace_last(s, R"(\"u)", "ü");
65  } else if (boost::algorithm::ends_with(s, R"(\ss)")) {
66  boost::algorithm::replace_last(s, R"(\ss)", "ß");
67  } else if (boost::algorithm::ends_with(s, R"(\'a)")) {
68  boost::algorithm::replace_last(s, R"(\'a)", "á");
69  } else if (boost::algorithm::ends_with(s, R"(\'e)")) {
70  boost::algorithm::replace_last(s, R"(\'e)", "é");
71  }
72 }
ValueParserState::handleCharacter
auto handleCharacter(char c) -> ParserState *override
Definition: ValueParserState.cpp:26
GlobalParserState.hpp
ParserState
Definition: ParserState.hpp:7
KeyParserState.hpp
ValueParserState.hpp
AbstractParserState
Definition: AbstractParserState.hpp:13
ValueParserState::ValueParserState
ValueParserState(ParserContext &context, std::vector< BibElement > &result) noexcept
Definition: ValueParserState.cpp:17
ParserContext
Definition: ParserContext.hpp:10
ParserException.hpp
KeyParserState
Key Parser State.
Definition: KeyParserState.hpp:14
GlobalParserState
Global Parser State.
Definition: GlobalParserState.hpp:14
ValueParserState::replaceSpecialCharacter
static auto replaceSpecialCharacter(std::string &s) -> void
Definition: ValueParserState.cpp:58