fork download
  1. struct Tag {
  2. std::unordered_map<std::string, boost::variant<int, double, std::string, std::complex> contents;
  3. };
  4. boost::variant<int, double, std::string, std::complex> parse_tag_end(T& begin, end) {
  5. boost::variant<int, double, std::string, std::complex> out;
  6. if (*begin == TokenType::Integer) {
  7. int i;
  8. std::stringstream sstr(begin->contents);
  9. begin++;
  10. sstr >> i;
  11. return out = i;
  12. }
  13. if (*begin == TokenType::Double) {
  14. double i;
  15. std::stringstream sstr(begin->contents);
  16. begin++;
  17. sstr >> i;
  18. return out = i;
  19. }
  20. // and complex and filename
  21. }
  22. std::unordered_map<std::string, Tag> parse(std::vector<Token> tokens) {
  23. std::unordered_map<std::string, Tag> result;
  24. auto curr = tokens.begin();
  25. auto end = tokens.end();
  26. while(curr != end) {
  27. if (*curr != TokenType::LessThan)
  28. throw std::runtime_error("File must open with '<'");
  29. curr++;
  30. if (*curr != TokenType::Identifier)
  31. throw std::runtime_error("Expected identifier after '<'");
  32. auto curr_tag = curr->contents;
  33. Tag& t = result[curr_tag];
  34. cur++;
  35. if (*curr != TokenType::GreaterThan)
  36. throw std::runtime_error("Expected '>' after identifier");
  37. curr++;
  38. // now match tag_contents
  39. while(curr != TokenType::LessThan) { // '<' introduces the close
  40. if (*curr != TokenType::Identifier)
  41. throw std::runtime_error("Expected identifier before '<'");
  42. auto& out = t.contents[curr->contents];
  43. curr++;
  44. if (*curr != TokenType::Equals)
  45. throw std::runtime_error("Expected '=' after identifier");
  46. curr++;
  47. out = parse_tag_end(curr, end);
  48. }
  49. // now match the end
  50. curr++; // already know this is '<'
  51. if (*curr != TokenType::Identifier)
  52. throw std::runtime_error("Expected identifier after '<'");
  53. if (curr->contents != curr_tag)
  54. throw std::runtime_error("Expected closing tag to be equivalent to opening.");
  55. curr++;
  56. if (*curr != TokenType::GreaterThan)
  57. throw std::runtime_error("Expected '>' after identifier");
  58. }
  59. return result;
  60. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty