Debian Codegraph @

CodeLite/SimpleTokenizer.hpp

raw
1 #ifndef SIMPLETOKENIZER_HPP
2 #define SIMPLETOKENIZER_HPP
3
4 #include "codelite_exports.h"
5
6 #include <wx/string.h>
7
8 enum eSimpleTokenizerState {
9 TOKNZR_STATE_NORMAL = 0,
10 TOKNZR_STATE_SINGLE_STRING = 1,
11 TOKNZR_STATE_DQUOTE_STRING = 2,
12 TOKNZR_STATE_ESCAPE = 3,
13 TOKNZR_STATE_LINE_COMMENT = 4,
14 TOKNZR_STATE_MULTILINE_COMMENT = 5,
15 TOKNZR_STATE_PREPROCESSOR = 6,
16 };
17
18 enum eSimpleTokenizerMode {
19 TOKNZR_MODE_NONE = -1,
20 TOKNZR_MODE_NORMAL = 0,
21 TOKNZR_MODE_COMMENTS = 1,
22 };
23
24 class WXDLLIMPEXP_CL SimpleTokenizer
25 {
26 public:
27 struct Token {
28 private:
29 // token properties
30 long m_token_position = wxNOT_FOUND;
31 long m_token_line = wxNOT_FOUND;
32 long m_token_column = wxNOT_FOUND;
33 long m_token_length = wxNOT_FOUND;
34 char m_following_char1 = 0; // the 1st following char after this token
35 char m_following_char2 = 0; // the 2nd following char after this token
36
37 public:
38 Token(long pos, long line, long col, long len)
39 : m_token_position(pos)
40 , m_token_line(line)
41 , m_token_column(col)
42 , m_token_length(len)
43 {
44 }
45
46 Token() {}
47
48 bool ok() const
49 {
50 return m_token_column != wxNOT_FOUND && m_token_length != wxNOT_FOUND && m_token_line != wxNOT_FOUND;
51 }
52
53 bool following_char1_is(char c) const { return m_following_char1 == c; }
54 bool following_char2_is(char c) const { return m_following_char2 == c; }
55 void set_following_char1(char c) { m_following_char1 = c; }
56 void set_following_char2(char c) { m_following_char2 = c; }
57
58 bool is_valid_identifier(const wxString& input_string) const
59 {
60 return ok() && (input_string.length() > (size_t)m_token_position) &&
61 isalpha(input_string[m_token_position]);
62 }
63
64 void inc_length() { ++m_token_length; }
65
66 long line() const { return m_token_line; }
67 long column() const { return m_token_column; }
68 long length() const { return m_token_length; }
69
70 void clear() { m_token_length = m_token_column = m_token_line = wxNOT_FOUND; }
71 wxString to_string(const wxString& source_string) const
72 {
73 if(!ok() || length() == 0) {
74 return wxEmptyString;
75 }
76 return source_string.Mid(m_token_position, m_token_length);
77 }
78 void set_line(long line) { m_token_line = line; }
79 };
80
81 private:
82 const wxString& m_str;
83 size_t m_pos = 0;
84 long m_line = 0;
85 long m_line_start_pos = 0;
86 eSimpleTokenizerState m_state = TOKNZR_STATE_NORMAL;
87 eSimpleTokenizerMode m_mode = TOKNZR_MODE_NONE;
88 Token m_token;
89
90 public:
91 SimpleTokenizer(const wxString& str);
92 ~SimpleTokenizer();
93
94 /**
95 * @brief return next token details
96 */
97 bool next(Token* token);
98 /**
99 * @brief while in this mode, scan for comments only
100 */
101 bool next_comment(Token* token);
102
103 /**
104 * @brief remove `*` and other decorators
105 * from a comment
106 */
107 void strip_comment(wxString& comment);
108 };
109
110 #endif // SIMPLETOKENIZER_HPP