Preprocessor.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/Optional.h>
  30. #include <AK/String.h>
  31. #include <AK/StringView.h>
  32. #include <AK/Vector.h>
  33. namespace Cpp {
  34. class Preprocessor {
  35. public:
  36. explicit Preprocessor(const String& filename, const StringView& program);
  37. const String& process();
  38. const String& processed_text();
  39. Vector<StringView> included_paths() const { return m_included_paths; }
  40. struct DefinedValue {
  41. Optional<StringView> value;
  42. FlyString filename;
  43. size_t line { 0 };
  44. size_t column { 0 };
  45. };
  46. using Definitions = HashMap<StringView, DefinedValue>;
  47. const Definitions& definitions() const { return m_definitions; }
  48. void set_ignore_unsupported_keywords(bool ignore) { m_options.ignore_unsupported_keywords = ignore; }
  49. private:
  50. void handle_preprocessor_line(const StringView&);
  51. Definitions m_definitions;
  52. const String m_filename;
  53. const StringView m_program;
  54. StringBuilder m_builder;
  55. Vector<StringView> m_lines;
  56. size_t m_line_index { 0 };
  57. size_t m_current_depth { 0 };
  58. Vector<size_t> m_depths_of_taken_branches;
  59. Vector<size_t> m_depths_of_not_taken_branches;
  60. enum class State {
  61. Normal,
  62. SkipIfBranch,
  63. SkipElseBranch
  64. };
  65. State m_state { State::Normal };
  66. Vector<StringView> m_included_paths;
  67. String m_processed_text;
  68. struct Options {
  69. bool ignore_unsupported_keywords { false };
  70. } m_options;
  71. };
  72. }