Lexer.h 921 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StringView.h>
  8. #include <AK/Vector.h>
  9. #include <LibGLSL/Token.h>
  10. namespace GLSL {
  11. class Lexer {
  12. public:
  13. explicit Lexer(StringView, size_t start_line = 0);
  14. Vector<Token> lex();
  15. template<typename Callback>
  16. void lex_iterable(Callback);
  17. void set_ignore_whitespace(bool value) { m_options.ignore_whitespace = value; }
  18. private:
  19. char peek(size_t offset = 0) const;
  20. char consume();
  21. void lex_impl(Function<void(Token)>);
  22. StringView m_input;
  23. size_t m_index { 0 };
  24. Position m_previous_position { 0, 0 };
  25. Position m_position { 0, 0 };
  26. struct Options {
  27. bool ignore_whitespace { false };
  28. } m_options;
  29. };
  30. template<typename Callback>
  31. void Lexer::lex_iterable(Callback callback)
  32. {
  33. return lex_impl(move(callback));
  34. }
  35. }