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
18#ifndef HEADER_SEXP_LEXER_HPP
19#define HEADER_SEXP_LEXER_HPP
20
21#include <istream>
22
23namespace sexp {
24
25class Lexer
26{
27public:
28 enum TokenType {
29 TOKEN_EOF,
30 TOKEN_OPEN_PAREN,
31 TOKEN_CLOSE_PAREN,
32 TOKEN_DOT,
33 TOKEN_SYMBOL,
34 TOKEN_STRING,
35 TOKEN_INTEGER,
36 TOKEN_REAL,
37 TOKEN_TRUE,
38 TOKEN_FALSE,
39 TOKEN_ARRAY_START
40 };
41
42public:
43 Lexer(std::istream& stream, bool use_arrays = false);
44 ~Lexer();
45
46 TokenType get_next_token();
47 std::string const& get_string() const { return m_token_string; }
48 int get_line_number() const { return m_linenumber; }
49
50private:
51 static const int MAX_TOKEN_LENGTH = 16384;
52 static const int BUFFER_SIZE = 16384;
53
54private:
55 inline void next_char();
56 inline void add_char();
57
58private:
59 std::istream& m_stream;
60 bool m_use_arrays;
61 bool m_eof;
62 int m_linenumber;
63 char m_buffer[BUFFER_SIZE+1];
64 char* m_bufend;
65 char* m_bufpos;
66 int m_c;
67 std::string m_token_string;
68
69private:
70 Lexer(const Lexer&);
71 Lexer & operator=(const Lexer&);
72};
73
74} // namespace sexp
75
76#endif
77
78/* EOF */
79