tb_parser.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_PARSER_H
  6. #define TB_PARSER_H
  7. #include "../tb_value.h"
  8. #include "../tb_tempbuffer.h"
  9. #include "../tb_str.h"
  10. namespace tb {
  11. /** Unescape backslash codes. This is done in place using the string both as source
  12. and destination. */
  13. void UnescapeString(char *str);
  14. /** Check if buf is pointing at a end quote. It may need to iterate
  15. buf backwards toward buf_start to check if any preceding backslashes
  16. make it a escaped quote (which should not be the end quote) */
  17. bool IsEndQuote(const char *buf_start, const char *buf, const char quote_type);
  18. class TBParserTarget
  19. {
  20. public:
  21. virtual ~TBParserTarget() {}
  22. virtual void OnError(int line_nr, const char *error) = 0;
  23. virtual void OnComment(int line_nr, const char *comment) = 0;
  24. virtual void OnToken(int line_nr, const char *name, TBValue &value) = 0;
  25. virtual void Enter() = 0;
  26. virtual void Leave() = 0;
  27. };
  28. class TBParserStream
  29. {
  30. public:
  31. virtual ~TBParserStream() {}
  32. virtual int GetMoreData(char *buf, int buf_len) = 0;
  33. };
  34. class TBParser
  35. {
  36. public:
  37. enum STATUS {
  38. STATUS_OK,
  39. STATUS_OUT_OF_MEMORY,
  40. STATUS_PARSE_ERROR
  41. };
  42. TBParser() { indent_spaces = -1; indent_tabs = false; }
  43. STATUS Read(TBParserStream *stream, TBParserTarget *target);
  44. private:
  45. int current_indent;
  46. int current_line_nr;
  47. TBStr multi_line_token;
  48. TBTempBuffer multi_line_value;
  49. int multi_line_sub_level;
  50. bool pending_multiline;
  51. void OnLine(char *line, TBParserTarget *target);
  52. void OnCompactLine(char *line, TBParserTarget *target);
  53. void OnMultiline(char *line, TBParserTarget *target);
  54. void ConsumeValue(TBValue &dst_value, char *&line);
  55. int indent_spaces;
  56. bool indent_tabs;
  57. };
  58. }; // namespace tb
  59. #endif // TB_PARSER_H