Preprocessor.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteString.h>
  8. #include <AK/DeprecatedFlyString.h>
  9. #include <AK/Function.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/Optional.h>
  12. #include <AK/StringView.h>
  13. #include <AK/Vector.h>
  14. #include <LibCpp/Token.h>
  15. namespace Cpp {
  16. class Preprocessor {
  17. public:
  18. explicit Preprocessor(ByteString const& filename, StringView program);
  19. Vector<Token> process_and_lex();
  20. Vector<StringView> included_paths() const { return m_included_paths; }
  21. struct Definition {
  22. ByteString key;
  23. Vector<ByteString> parameters;
  24. ByteString value;
  25. DeprecatedFlyString filename;
  26. size_t line { 0 };
  27. size_t column { 0 };
  28. };
  29. using Definitions = HashMap<ByteString, Definition>;
  30. struct Substitution {
  31. Vector<Token> original_tokens;
  32. Definition defined_value;
  33. ByteString processed_value;
  34. };
  35. Definitions const& definitions() const { return m_definitions; }
  36. Vector<Substitution> const& substitutions() const { return m_substitutions; }
  37. void set_ignore_unsupported_keywords(bool ignore) { m_options.ignore_unsupported_keywords = ignore; }
  38. void set_ignore_invalid_statements(bool ignore) { m_options.ignore_invalid_statements = ignore; }
  39. void set_keep_include_statements(bool keep) { m_options.keep_include_statements = keep; }
  40. Function<Definitions(StringView)> definitions_in_header_callback { nullptr };
  41. Vector<Token> const& unprocessed_tokens() const { return m_unprocessed_tokens; }
  42. private:
  43. void handle_preprocessor_statement(StringView);
  44. void handle_include_statement(StringView);
  45. void handle_preprocessor_keyword(StringView keyword, GenericLexer& line_lexer);
  46. ByteString remove_escaped_newlines(StringView value);
  47. size_t do_substitution(Vector<Token> const& tokens, size_t token_index, Definition const&);
  48. Optional<Definition> create_definition(StringView line);
  49. struct MacroCall {
  50. Token name;
  51. struct Argument {
  52. Vector<Token> tokens;
  53. };
  54. Vector<Argument> arguments;
  55. size_t end_token_index { 0 };
  56. };
  57. Optional<MacroCall> parse_macro_call(Vector<Token> const& tokens, size_t token_index);
  58. ByteString evaluate_macro_call(MacroCall const&, Definition const&);
  59. ByteString m_filename;
  60. ByteString m_program;
  61. Vector<Token> m_unprocessed_tokens;
  62. Vector<Token> m_processed_tokens;
  63. Definitions m_definitions;
  64. Vector<Substitution> m_substitutions;
  65. size_t m_current_line { 0 };
  66. size_t m_current_depth { 0 };
  67. Vector<size_t> m_depths_of_taken_branches;
  68. Vector<size_t> m_depths_of_not_taken_branches;
  69. enum class State {
  70. Normal,
  71. SkipIfBranch,
  72. SkipElseBranch
  73. };
  74. State m_state { State::Normal };
  75. Vector<StringView> m_included_paths;
  76. struct Options {
  77. bool ignore_unsupported_keywords { false };
  78. bool ignore_invalid_statements { false };
  79. bool keep_include_statements { false };
  80. } m_options;
  81. };
  82. }