lexer.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SExp - A S-Expression Parser for C++
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // 2015 Ingo Ruhnke <grumbel@gmail.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef HEADER_SEXP_LEXER_HPP
  18. #define HEADER_SEXP_LEXER_HPP
  19. #include <istream>
  20. namespace sexp {
  21. class Lexer
  22. {
  23. public:
  24. enum TokenType {
  25. TOKEN_EOF,
  26. TOKEN_OPEN_PAREN,
  27. TOKEN_CLOSE_PAREN,
  28. TOKEN_DOT,
  29. TOKEN_SYMBOL,
  30. TOKEN_STRING,
  31. TOKEN_INTEGER,
  32. TOKEN_REAL,
  33. TOKEN_TRUE,
  34. TOKEN_FALSE,
  35. TOKEN_ARRAY_START
  36. };
  37. public:
  38. Lexer(std::istream& stream, bool use_arrays = false);
  39. ~Lexer();
  40. TokenType get_next_token();
  41. std::string const& get_string() const { return m_token_string; }
  42. int get_line_number() const { return m_linenumber; }
  43. private:
  44. static const int MAX_TOKEN_LENGTH = 16384;
  45. static const int BUFFER_SIZE = 16384;
  46. private:
  47. inline void next_char();
  48. inline void add_char();
  49. private:
  50. std::istream& m_stream;
  51. bool m_use_arrays;
  52. bool m_eof;
  53. int m_linenumber;
  54. char m_buffer[BUFFER_SIZE+1];
  55. char* m_bufend;
  56. char* m_bufpos;
  57. int m_c;
  58. std::string m_token_string;
  59. private:
  60. Lexer(const Lexer&);
  61. Lexer & operator=(const Lexer&);
  62. };
  63. } // namespace sexp
  64. #endif
  65. /* EOF */