Preprocessor.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, const 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_keep_include_statements(bool keep) { m_options.keep_include_statements = keep; }
  39. Function<Definitions(StringView)> definitions_in_header_callback { nullptr };
  40. private:
  41. void handle_preprocessor_statement(StringView const&);
  42. void handle_include_statement(StringView const&);
  43. void handle_preprocessor_keyword(StringView const& keyword, GenericLexer& line_lexer);
  44. String remove_escaped_newlines(StringView const& value);
  45. size_t do_substitution(Vector<Token> const& tokens, size_t token_index, Definition const&);
  46. Optional<Definition> create_definition(StringView line);
  47. struct MacroCall {
  48. Token name;
  49. struct Argument {
  50. Vector<Token> tokens;
  51. };
  52. Vector<Argument> arguments;
  53. size_t end_token_index { 0 };
  54. };
  55. Optional<MacroCall> parse_macro_call(Vector<Token> const& tokens, size_t token_index);
  56. String evaluate_macro_call(MacroCall const&, Definition const&);
  57. String m_filename;
  58. String m_program;
  59. Vector<Token> m_processed_tokens;
  60. Definitions m_definitions;
  61. Vector<Substitution> m_substitutions;
  62. size_t m_current_line { 0 };
  63. size_t m_current_depth { 0 };
  64. Vector<size_t> m_depths_of_taken_branches;
  65. Vector<size_t> m_depths_of_not_taken_branches;
  66. enum class State {
  67. Normal,
  68. SkipIfBranch,
  69. SkipElseBranch
  70. };
  71. State m_state { State::Normal };
  72. Vector<StringView> m_included_paths;
  73. struct Options {
  74. bool ignore_unsupported_keywords { false };
  75. bool keep_include_statements { false };
  76. } m_options;
  77. };
  78. }