Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/expr/Tokenizer.h

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 16 сент. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_EXPR_TOKENIZER_H_
23 #define LSP_PLUG_IN_EXPR_TOKENIZER_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/runtime/LSPString.h>
27 #include <lsp-plug.in/expr/token.h>
28 #include <lsp-plug.in/io/IInSequence.h>
29
30 namespace lsp
31 {
32 namespace expr
33 {
34
35 class Tokenizer
36 {
37 private:
38 io::IInSequence *pIn;
39 lsp_swchar_t cCurrent;
40 token_t enToken;
41 LSPString sValue;
42 union
43 {
44 double fValue;
45 ssize_t iValue;
46 };
47 status_t nError;
48 size_t nUnget;
49
50 protected:
51 lsp_swchar_t lookup();
52 lsp_swchar_t commit_lookup(token_t token);
53 lsp_swchar_t skip_whitespace();
54 token_t lookup_identifier(token_t type);
55 token_t lookup_string();
56 token_t lookup_number();
57 token_t lookup_color();
58 token_t commit(token_t token);
59 token_t set_error(status_t code);
60 token_t decode_bareword();
61
62 static bool parse_digit(int *digit, lsp_wchar_t ch, int radix);
63
64 public:
65 explicit Tokenizer(io::IInSequence *in);
66 virtual ~Tokenizer();
67
68 public:
69 /**
70 * Check that character matches first identifier letter
71 * @param ch character
72 * @return true if character matches
73 */
74 static bool is_identifier_first(lsp_wchar_t ch);
75
76 /**
77 * Check that character matches hexadecimal letter
78 * @param ch character
79 * @return true if character matches
80 */
81 static bool is_hex(lsp_wchar_t ch);
82
83 /**
84 * Check that character matches next identifier letter
85 * @param ch character
86 * @return true if character matchers
87 */
88 static bool is_identifier_next(lsp_wchar_t ch);
89
90 /**
91 * Get token
92 * @param flags tokenizing flags
93 * @return current token value
94 */
95 token_t get_token(size_t flags = TF_NONE);
96
97 /**
98 * Unget token
99 */
100 inline void unget() { ++nUnget; };
101
102 /**
103 * Get current token
104 * @return current token
105 */
106 inline token_t current() const { return enToken; }
107
108 /**
109 * Get current token value for tokens that consist of characters
110 * @return current token value
111 */
112 inline const LSPString *text_value() const { return &sValue; }
113
114 /**
115 * Get floating-point value of the token
116 * @return floating-point value of the token
117 */
118 inline const double float_value() const { return fValue; }
119
120 /**
121 * Get integer value of the token
122 * @return integer value of the token
123 */
124 inline ssize_t int_value() const { return iValue; }
125
126 /**
127 * Get last error code
128 * @return last error code
129 */
130 inline status_t error() const { return nError; }
131 };
132
133 } /* namespace expr */
134 } /* namespace lsp */
135
136 #endif /* LSP_PLUG_IN_EXPR_TOKENIZER_H_ */