Build tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BBBParser.h 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #pragma once
  2. #include <unordered_map>
  3. #include <vector>
  4. #include <string>
  5. #include <iostream>
  6. #include <exception>
  7. struct BBBParseError: std::exception {
  8. BBBParseError(std::string msg): message(msg) {}
  9. std::string message;
  10. const char *what() const noexcept override {
  11. return message.c_str();
  12. }
  13. };
  14. class BBBParser {
  15. public:
  16. using Variables = std::unordered_map<std::string, std::vector<std::string>>;
  17. static const int FLAG_NONE = 0;
  18. static const int FLAG_ONE_LINE = 1 << 0;
  19. BBBParser(std::istream &stream, int flags, int line = 1, int ch = 1):
  20. flags_(flags), line_(line), ch_(ch), stream_(stream) {}
  21. void parse(Variables &vars);
  22. int peek() { return stream_.peek(); }
  23. int peek2() { stream_.get(); int ch = peek(); stream_.unget(); return ch; }
  24. int get();
  25. void skip(char);
  26. void skip() { get(); }
  27. int line() const { return line_; }
  28. int ch() const { return ch_; }
  29. private:
  30. enum class Operator {
  31. COLON_EQUALS,
  32. PLUS_EQUALS,
  33. EQUALS_PLUS,
  34. NONE,
  35. };
  36. [[noreturn]] void error(std::string);
  37. Operator readOperator();
  38. void skipWhitespaceLine();
  39. void skipWhitespace();
  40. char parseEscape();
  41. void parseExpansion(const Variables &vars, std::vector<std::string> &values);
  42. void parseQuotedExpansion(const Variables &vars, std::string &content);
  43. void parseQuotedString(const Variables &vars, std::string &content);
  44. bool parseString(const Variables &vars, std::string &content, int sep = -1);
  45. bool parseIdentifier(std::string &content);
  46. int flags_;
  47. int line_;
  48. int ch_;
  49. std::istream &stream_;
  50. };