GNU Octave 10.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-2025 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 token
42{
43public:
44
55
75
76public:
77
79 : m_type_tag (invalid_token)
80 { }
81
82 token (int id, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
83 : m_beg_pos (beg_pos), m_end_pos (end_pos), m_tok_id (id), m_leading_comments (lst)
84 { }
85
86 token (int id, bool is_kw, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
87 : 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)
88 { }
89
90 token (int id, const char *s, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
91 : 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)
92 { }
93
94 token (int id, const std::string& s, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
95 : 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)
96 { }
97
98 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 ())
99 : 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)
100 { }
101
102 token (int id, end_tok_type t, const filepos& beg_pos, const filepos& end_pos, const comment_list& lst = comment_list ())
103 : 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)
104 { }
105
106 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 ())
107 : 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)
108 { }
109
110 OCTAVE_DEFAULT_COPY_MOVE_DELETE (token)
111
112 void mark_may_be_command () { m_maybe_cmd = true; }
113 bool may_be_command () const { return m_maybe_cmd; }
114
115 void mark_trailing_space () { m_tspc = true; }
116 bool space_follows_token () const { return m_tspc; }
117
118 operator bool () const { return m_type_tag != invalid_token; }
119
120 int token_id () const { return m_tok_id; }
121
122 bool token_is (int id) const { return m_tok_id == id; }
123 bool token_is (const token *tok) const
124 {
125 return tok ? token_is (tok->token_id ()) : false;
126 }
127
128 filepos beg_pos () const { return m_beg_pos; }
129 filepos end_pos () const { return m_end_pos; }
130
131 void beg_pos (const filepos& pos) { m_beg_pos = pos; }
132 void end_pos (const filepos& pos) { m_end_pos = pos; }
133
134 comment_list leading_comments () const { return m_leading_comments; }
135 comment_list trailing_comments () const { return m_trailing_comments; }
136
137 void leading_comments (const comment_list& lst) { m_leading_comments = lst; }
138 void trailing_comments (const comment_list& lst) { m_trailing_comments = lst; }
139
140 // These will probably be removed.
141 int line () const { return m_beg_pos.line (); }
142 int column () const { return m_beg_pos.column (); }
143
144 bool iskeyword () const
145 {
146 return m_type_tag == keyword_token || m_type_tag == ettype_token;
147 }
148
149 bool isstring () const { return m_type_tag == string_token; }
150
151 std::string text () const { return m_tok_info.text (); }
152 octave_value number () const { return m_tok_info.number (); }
153 token_type ttype () const { return m_type_tag; }
154 end_tok_type ettype () const { return m_tok_info.ettype (); }
155
156 std::string superclass_method_name () const { return m_tok_info.superclass_method_name (); }
157 std::string superclass_class_name () const { return m_tok_info.superclass_class_name (); }
158
159 std::string text_rep () const { return m_orig_text; }
160
161private:
162
163 bool m_maybe_cmd {false};
164
165 bool m_tspc {false};
166
167 filepos m_beg_pos;
168 filepos m_end_pos;
169
170 int m_tok_id {-1};
171
172 token_type m_type_tag {generic_token};
173
174 class superclass_info
175 {
176 public:
177
178 superclass_info (const std::string& meth, const std::string& cls)
179 : m_method_name (meth), m_class_name (cls)
180 { }
181
182 OCTAVE_DEFAULT_COPY_MOVE_DELETE (superclass_info)
183
184 std::string method_name () const { return m_method_name; }
185
186 std::string class_name () const { return m_class_name; }
187
188 private:
189
190 //--------
191
192 // The name of the method to call. This is the text before the
193 // "@" and may be of the form "object.method".
194 std::string m_method_name;
195
196 // The name of the superclass. This is the text after the "@"
197 // and may be of the form "object.method".
198 std::string m_class_name;
199 };
200
201 class tok_info
202 {
203 public:
204
205 tok_info (const char *s) : m_value (std::string (s)) { }
206
207 tok_info (const std::string& str) : m_value (str) { }
208
209 tok_info (const octave_value& num) : m_value (octave_value (num)) { }
210
211 tok_info (end_tok_type et) : m_value (et) { }
212
213 tok_info (const std::string& meth, const std::string& cls) : m_value (superclass_info (meth, cls)) { }
214
215 OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE_DELETE (tok_info)
216
217 std::string text () const
218 {
219 panic_unless (std::holds_alternative<std::string> (m_value));
220 return std::get<std::string> (m_value);
221 }
222
223 octave_value number () const
224 {
225 panic_unless (std::holds_alternative<octave_value> (m_value));
226 return std::get<octave_value> (m_value);
227 }
228
229 token::end_tok_type ettype () const
230 {
231 panic_unless (std::holds_alternative<end_tok_type> (m_value));
232 return std::get<end_tok_type> (m_value);
233 }
234
235 std::string
236 superclass_method_name () const
237 {
238 panic_unless (std::holds_alternative<superclass_info> (m_value));
239 return std::get<superclass_info> (m_value).method_name ();
240 }
241
242 std::string
243 superclass_class_name () const
244 {
245 panic_unless (std::holds_alternative<superclass_info> (m_value));
246 return std::get<superclass_info> (m_value).class_name ();
247 }
248
249 private:
250
251 std::variant<std::string, octave_value, end_tok_type, superclass_info> m_value;
252 };
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:138
std::string text_rep() const
Definition token.h:159
bool isstring() const
Definition token.h:149
std::string superclass_class_name() const
Definition token.h:157
filepos end_pos() const
Definition token.h:129
int token_id() const
Definition token.h:120
token(int id, bool is_kw, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:86
comment_list leading_comments() const
Definition token.h:134
void mark_trailing_space()
Definition token.h:115
void leading_comments(const comment_list &lst)
Definition token.h:137
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:98
bool token_is(const token *tok) const
Definition token.h:123
token(int id, const std::string &s, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:94
void end_pos(const filepos &pos)
Definition token.h:132
std::string superclass_method_name() const
Definition token.h:156
end_tok_type ettype() const
Definition token.h:154
token()
Definition token.h:78
comment_list trailing_comments() const
Definition token.h:135
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:102
filepos beg_pos() const
Definition token.h:128
void beg_pos(const filepos &pos)
Definition token.h:131
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:106
int column() const
Definition token.h:142
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:90
std::string text() const
Definition token.h:151
token(int id, const filepos &beg_pos, const filepos &end_pos, const comment_list &lst=comment_list())
Definition token.h:82
bool token_is(int id) const
Definition token.h:122
bool may_be_command() const
Definition token.h:113
octave_value number() const
Definition token.h:152
int line() const
Definition token.h:141
token_type ttype() const
Definition token.h:153
bool space_follows_token() const
Definition token.h:116
bool iskeyword() const
Definition token.h:144
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define panic_unless(cond)
Definition panic.h:59