GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
token.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_token_h)
27#define octave_token_h 1
28
29#include "octave-config.h"
30
31#include <string>
32#include <variant>
33
34#include "comment-list.h"
35#include "error.h"
36#include "filepos.h"
37#include "ov.h"
38
40
41class OCTINTERP_API token
42{
43public:
44
55
75
77 : m_type_tag (invalid_token)
78 { }
79
80 token (int id, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
81 : m_beg_pos (beg_pos), m_end_pos (end_pos), m_tok_id (id), m_leading_comments (lst)
82 { }
83
84 token (int id, bool is_kw, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
85 : m_beg_pos (beg_pos), m_end_pos (end_pos), m_tok_id (id), m_type_tag (is_kw ? keyword_token : generic_token), m_leading_comments (lst)
86 { }
87
88 token (int id, const char *s, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
89 : m_beg_pos (beg_pos), m_end_pos (end_pos), m_tok_id (id), m_type_tag (string_token), m_tok_info (s), m_leading_comments (lst)
90 { }
91
92 token (int id, const std::string& s, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
93 : m_beg_pos (beg_pos), m_end_pos (end_pos), m_tok_id (id), m_type_tag (string_token), m_tok_info (s), m_leading_comments (lst)
94 { }
95
96 token (int id, const octave_value& val, const std::string& s, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
97 : m_beg_pos (beg_pos), m_end_pos (end_pos), m_tok_id (id), m_type_tag (numeric_token), m_tok_info (val), m_orig_text (s), m_leading_comments (lst)
98 { }
99
100 token (int id, end_tok_type t, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
101 : m_beg_pos (beg_pos), m_end_pos (end_pos), m_tok_id (id), m_type_tag (ettype_token), m_tok_info (t), m_leading_comments (lst)
102 { }
103
104 token (int id, const std::string& meth, const std::string& cls, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
105 : m_beg_pos (beg_pos), m_end_pos (end_pos), m_tok_id (id), m_type_tag (scls_name_token), m_tok_info (meth, cls), m_leading_comments (lst)
106 { }
107
108 OCTAVE_DEFAULT_COPY_MOVE_DELETE (token)
109
110 void mark_may_be_command () { m_maybe_cmd = true; }
111 bool may_be_command () const { return m_maybe_cmd; }
112
113 void mark_trailing_space () { m_tspc = true; }
114 bool space_follows_token () const { return m_tspc; }
115
116 operator bool () const { return m_type_tag != invalid_token; }
117
118 int token_id () const { return m_tok_id; }
119
120 bool token_is (int id) const { return m_tok_id == id; }
121 bool token_is (const token *tok) const
122 {
123 return tok ? token_is (tok->token_id ()) : false;
124 }
125
126 filepos beg_pos () const { return m_beg_pos; }
127 filepos end_pos () const { return m_end_pos; }
128
129 void beg_pos (const filepos& pos) { m_beg_pos = pos; }
130 void end_pos (const filepos& pos) { m_end_pos = pos; }
131
132 comment_list leading_comments () const { return m_leading_comments; }
133 comment_list trailing_comments () const { return m_trailing_comments; }
134
135 void leading_comments (const comment_list& lst) { m_leading_comments = lst; }
136 void trailing_comments (const comment_list& lst) { m_trailing_comments = lst; }
137
138 // These will probably be removed.
139 int line () const { return m_beg_pos.line (); }
140 int column () const { return m_beg_pos.column (); }
141
142 bool iskeyword () const
143 {
144 return m_type_tag == keyword_token || m_type_tag == ettype_token;
145 }
146
147 bool isstring () const { return m_type_tag == string_token; }
148
149 std::string text () const { return m_tok_info.text (); }
150 octave_value number () const { return m_tok_info.number (); }
151 token_type ttype () const { return m_type_tag; }
152 end_tok_type ettype () const { return m_tok_info.ettype (); }
153
154 std::string superclass_method_name () const { return m_tok_info.superclass_method_name (); }
155 std::string superclass_class_name () const { return m_tok_info.superclass_class_name (); }
156
157 std::string text_rep () const { return m_orig_text; }
158
159private:
160
161 class superclass_info
162 {
163 public:
164
165 superclass_info (const std::string& meth, const std::string& cls)
166 : m_method_name (meth), m_class_name (cls)
167 { }
168
169 OCTAVE_DEFAULT_COPY_MOVE_DELETE (superclass_info)
170
171 std::string method_name () const { return m_method_name; }
172
173 std::string class_name () const { return m_class_name; }
174
175 private:
176
177 //--------
178
179 // The name of the method to call. This is the text before the
180 // "@" and may be of the form "object.method".
181 std::string m_method_name;
182
183 // The name of the superclass. This is the text after the "@"
184 // and may be of the form "object.method".
185 std::string m_class_name;
186 };
187
188 class tok_info
189 {
190 public:
191
192 tok_info (const char *s) : m_value (std::string (s)) { }
193
194 tok_info (const std::string& str) : m_value (str) { }
195
196 tok_info (const octave_value& num) : m_value (octave_value (num)) { }
197
198 tok_info (end_tok_type et) : m_value (et) { }
199
200 tok_info (const std::string& meth, const std::string& cls) : m_value (superclass_info (meth, cls)) { }
201
202 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE_DELETE (tok_info)
203
204 std::string text () const
205 {
206 panic_unless (std::holds_alternative<std::string> (m_value));
207 return std::get<std::string> (m_value);
208 }
209
210 octave_value number () const
211 {
212 panic_unless (std::holds_alternative<octave_value> (m_value));
213 return std::get<octave_value> (m_value);
214 }
215
216 token::end_tok_type ettype () const
217 {
218 panic_unless (std::holds_alternative<end_tok_type> (m_value));
219 return std::get<end_tok_type> (m_value);
220 }
221
222 std::string
223 superclass_method_name () const
224 {
225 panic_unless (std::holds_alternative<superclass_info> (m_value));
226 return std::get<superclass_info> (m_value).method_name ();
227 }
228
229 std::string
230 superclass_class_name () const
231 {
232 panic_unless (std::holds_alternative<superclass_info> (m_value));
233 return std::get<superclass_info> (m_value).class_name ();
234 }
235
236 private:
237
238 std::variant<std::string, octave_value, end_tok_type, superclass_info> m_value;
239 };
240
241 //--------
242
243 bool m_maybe_cmd {false};
244
245 bool m_tspc {false};
246
247 filepos m_beg_pos;
248 filepos m_end_pos;
249
250 int m_tok_id {-1};
251
252 token_type m_type_tag {generic_token};
253
254 tok_info m_tok_info;
255
256 std::string m_orig_text;
257
258 // Comments that appear prior to the token.
259 comment_list m_leading_comments;
260
261 // Comments that appear after the token. This list is only used to
262 // hold comments that appear after the last token in a file.
263 comment_list m_trailing_comments;
264};
265
266OCTAVE_END_NAMESPACE(octave)
267
268#endif
Definition token.h:42
void trailing_comments(const comment_list &lst)
Definition token.h:136
std::string text_rep() const
Definition token.h:157
bool isstring() const
Definition token.h:147
std::string superclass_class_name() const
Definition token.h:155
filepos end_pos() const
Definition token.h:127
int token_id() const
Definition token.h:118
token(int id, bool is_kw, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:84
comment_list leading_comments() const
Definition token.h:132
void mark_trailing_space()
Definition token.h:113
void leading_comments(const comment_list &lst)
Definition token.h:135
token(int id, const octave_value &val, const std::string &s, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:96
bool token_is(const token *tok) const
Definition token.h:121
token(int id, const std::string &s, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:92
void end_pos(const filepos &pos)
Definition token.h:130
std::string superclass_method_name() const
Definition token.h:154
end_tok_type ettype() const
Definition token.h:152
token()
Definition token.h:76
comment_list trailing_comments() const
Definition token.h:133
token_type
Definition token.h:46
@ keyword_token
Definition token.h:49
@ numeric_token
Definition token.h:51
@ ettype_token
Definition token.h:52
@ invalid_token
Definition token.h:47
@ string_token
Definition token.h:50
@ generic_token
Definition token.h:48
@ scls_name_token
Definition token.h:53
token(int id, end_tok_type t, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:100
filepos beg_pos() const
Definition token.h:126
void beg_pos(const filepos &pos)
Definition token.h:129
token(int id, const std::string &meth, const std::string &cls, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:104
int column() const
Definition token.h:140
end_tok_type
Definition token.h:57
@ events_end
Definition token.h:62
@ methods_end
Definition token.h:66
@ enumeration_end
Definition token.h:61
@ unwind_protect_end
Definition token.h:71
@ try_catch_end
Definition token.h:70
@ properties_end
Definition token.h:68
@ switch_end
Definition token.h:69
@ simple_end
Definition token.h:58
@ classdef_end
Definition token.h:60
@ arguments_end
Definition token.h:59
@ if_end
Definition token.h:65
@ parfor_end
Definition token.h:67
@ for_end
Definition token.h:63
@ function_end
Definition token.h:64
@ while_end
Definition token.h:73
@ spmd_end
Definition token.h:72
token(int id, const char *s, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:88
std::string text() const
Definition token.h:149
token(int id, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:80
bool token_is(int id) const
Definition token.h:120
bool may_be_command() const
Definition token.h:111
octave_value number() const
Definition token.h:150
int line() const
Definition token.h:139
token_type ttype() const
Definition token.h:151
bool space_follows_token() const
Definition token.h:114
bool iskeyword() const
Definition token.h:142
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define panic_unless(cond)
Definition panic.h:59