Parser.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. ErrorOr<ParseStatus> try_parse(ByteBuffer&& buffer, bool expecting_tag);
  22. ErrorOr<void> consume(StringView);
  23. bool consume_if(StringView);
  24. StringView consume_while(Function<bool(u8)> should_consume);
  25. StringView consume_until_end_of_line();
  26. bool at_end() { return m_position >= m_buffer.size(); }
  27. ErrorOr<unsigned> parse_number();
  28. Optional<unsigned> try_parse_number();
  29. ErrorOr<void> parse_response_done();
  30. ErrorOr<void> parse_untagged();
  31. ErrorOr<void> parse_capability_response();
  32. ErrorOr<StringView> parse_atom();
  33. ErrorOr<StringView> parse_quoted_string();
  34. ErrorOr<StringView> parse_literal_string();
  35. ErrorOr<StringView> parse_string();
  36. ErrorOr<StringView> parse_astring();
  37. ErrorOr<StringView> parse_nstring();
  38. ErrorOr<ResponseStatus> parse_status();
  39. ErrorOr<ListItem> parse_list_item();
  40. ErrorOr<FetchCommand::DataItem> parse_fetch_data_item();
  41. ErrorOr<FetchResponseData> parse_fetch_response();
  42. ErrorOr<Vector<Address>> parse_address_list();
  43. ErrorOr<Address> parse_address();
  44. ErrorOr<HashMap<ByteString, ByteString>> parse_body_fields_params();
  45. ErrorOr<BodyStructure> parse_body_structure();
  46. ErrorOr<BodyStructure> parse_one_part_body();
  47. ErrorOr<BodyExtension> parse_body_extension();
  48. ErrorOr<Tuple<ByteString, HashMap<ByteString, ByteString>>> parse_disposition();
  49. ErrorOr<Vector<ByteString>> parse_langs();
  50. ErrorOr<Envelope> parse_envelope();
  51. template<typename T>
  52. ErrorOr<Vector<T>> parse_list(T (*converter)(StringView));
  53. // To retain state if parsing is not finished
  54. ByteBuffer m_buffer;
  55. SolidResponse m_response;
  56. unsigned m_position { 0 };
  57. bool m_incomplete { false };
  58. };
  59. }