Preprocessor.h 3.1 KB

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