Preprocessor.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/FlyString.h>
  8. #include <AK/Function.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/Optional.h>
  11. #include <AK/String.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(const String& filename, StringView program);
  19. Vector<Token> process_and_lex();
  20. Vector<StringView> included_paths() const { return m_included_paths; }
  21. struct Definition {
  22. String key;
  23. Vector<String> parameters;
  24. String value;
  25. FlyString filename;
  26. size_t line { 0 };
  27. size_t column { 0 };
  28. };
  29. using Definitions = HashMap<String, Definition>;
  30. struct Substitution {
  31. Vector<Token> original_tokens;
  32. Definition defined_value;
  33. String 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. private:
  42. void handle_preprocessor_statement(StringView);
  43. void handle_include_statement(StringView);
  44. void handle_preprocessor_keyword(StringView keyword, GenericLexer& line_lexer);
  45. String remove_escaped_newlines(StringView value);
  46. size_t do_substitution(Vector<Token> const& tokens, size_t token_index, Definition const&);
  47. Optional<Definition> create_definition(StringView line);
  48. struct MacroCall {
  49. Token name;
  50. struct Argument {
  51. Vector<Token> tokens;
  52. };
  53. Vector<Argument> arguments;
  54. size_t end_token_index { 0 };
  55. };
  56. Optional<MacroCall> parse_macro_call(Vector<Token> const& tokens, size_t token_index);
  57. String evaluate_macro_call(MacroCall const&, Definition const&);
  58. String m_filename;
  59. String m_program;
  60. Vector<Token> m_processed_tokens;
  61. Definitions m_definitions;
  62. Vector<Substitution> m_substitutions;
  63. size_t m_current_line { 0 };
  64. size_t m_current_depth { 0 };
  65. Vector<size_t> m_depths_of_taken_branches;
  66. Vector<size_t> m_depths_of_not_taken_branches;
  67. enum class State {
  68. Normal,
  69. SkipIfBranch,
  70. SkipElseBranch
  71. };
  72. State m_state { State::Normal };
  73. Vector<StringView> m_included_paths;
  74. struct Options {
  75. bool ignore_unsupported_keywords { false };
  76. bool ignore_invalid_statements { false };
  77. bool keep_include_statements { false };
  78. } m_options;
  79. };
  80. }