Parser.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Result.h>
  9. #include <LibIMAP/Objects.h>
  10. namespace IMAP {
  11. class Client;
  12. struct ParseStatus {
  13. bool successful;
  14. Optional<Response> response;
  15. };
  16. class Parser {
  17. public:
  18. ParseStatus parse(ByteBuffer&& buffer, bool expecting_tag);
  19. private:
  20. static MailboxFlag parse_mailbox_flag(StringView);
  21. void consume(StringView);
  22. bool try_consume(StringView);
  23. StringView consume_while(Function<bool(u8)> should_consume);
  24. StringView consume_until_end_of_line();
  25. bool at_end() { return position >= m_buffer.size(); };
  26. unsigned parse_number();
  27. Optional<unsigned> try_parse_number();
  28. void parse_response_done();
  29. void parse_untagged();
  30. void parse_capability_response();
  31. StringView parse_atom();
  32. StringView parse_quoted_string();
  33. StringView parse_literal_string();
  34. StringView parse_string();
  35. StringView parse_astring();
  36. Optional<StringView> parse_nstring();
  37. ResponseStatus parse_status();
  38. ListItem parse_list_item();
  39. FetchCommand::DataItem parse_fetch_data_item();
  40. FetchResponseData parse_fetch_response();
  41. Optional<Vector<Address>> parse_address_list();
  42. Address parse_address();
  43. HashMap<DeprecatedString, DeprecatedString> parse_body_fields_params();
  44. BodyStructure parse_body_structure();
  45. BodyStructure parse_one_part_body();
  46. BodyExtension parse_body_extension();
  47. Tuple<DeprecatedString, HashMap<DeprecatedString, DeprecatedString>> parse_disposition();
  48. Vector<DeprecatedString> parse_langs();
  49. Envelope parse_envelope();
  50. template<typename T>
  51. Vector<T> parse_list(T (*converter)(StringView));
  52. // To retain state if parsing is not finished
  53. ByteBuffer m_buffer;
  54. SolidResponse m_response;
  55. unsigned position { 0 };
  56. bool m_incomplete { false };
  57. bool m_parsing_failed { false };
  58. };
  59. }