GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
token.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2023 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 
33 #include "filepos.h"
34 #include "ov.h"
35 
37 
38 class token
39 {
40 public:
41 
43  {
50  };
51 
53  {
70  };
71 
72  token (int tv, const filepos& beg_pos, const filepos& end_pos);
73 
74  token (int tv, bool is_keyword, const filepos& beg_pos
75  , const filepos& end_pos);
76 
77  token (int tv, const char *s, const filepos& beg_pos,
78  const filepos& end_pos);
79 
80  token (int tv, const std::string& s, const filepos& beg_pos,
81  const filepos& end_pos);
82 
83  token (int tv, const octave_value& val, const std::string& s,
84  const filepos& beg_pos, const filepos& end_pos);
85 
86  token (int tv, end_tok_type t, const filepos& beg_pos,
87  const filepos& end_pos);
88 
89  token (int tv, const std::string& mth, const std::string& cls,
90  const filepos& beg_pos, const filepos& end_pos);
91 
92  // No copying!
93 
94  token (const token&) = delete;
95 
96  token& operator = (const token&) = delete;
97 
98  ~token (void);
99 
100  void mark_may_be_command (void) { m_maybe_cmd = true; }
101  bool may_be_command (void) const { return m_maybe_cmd; }
102 
103  void mark_trailing_space (void) { m_tspc = true; }
104  bool space_follows_token (void) const { return m_tspc; }
105 
106  int token_value (void) const { return m_tok_val; }
107  bool token_value_is (int tv) const { return tv == m_tok_val; }
108 
109  filepos beg_pos (void) const { return m_beg_pos; }
110  filepos end_pos (void) const { return m_end_pos; }
111 
112  void beg_pos (const filepos& pos) { m_beg_pos = pos; }
113  void end_pos (const filepos& pos) { m_end_pos = pos; }
114 
115  // These will probably be removed.
116  int line (void) const { return m_beg_pos.line (); }
117  int column (void) const { return m_beg_pos.column (); }
118 
119  bool iskeyword (void) const
120  {
121  return m_type_tag == keyword_token || m_type_tag == ettype_token;
122  }
123 
124  bool isstring (void) const { return m_type_tag == string_token; }
125 
126  std::string text (void) const;
127  octave_value number (void) const;
128  token_type ttype (void) const;
129  end_tok_type ettype (void) const;
130 
131  std::string superclass_method_name (void) const;
132  std::string superclass_class_name (void) const;
133 
134  std::string text_rep (void) const;
135 
136 private:
137 
139 
140  bool m_tspc;
141 
144 
146 
148 
149  union tok_info
150  {
151  tok_info (void) { }
152 
153  tok_info (const char *s) : m_str (new std::string (s)) { }
154 
155  tok_info (const std::string& str) : m_str (new std::string (str)) { }
156 
157  tok_info (const octave_value& num) : m_num (new octave_value (num)) { }
158 
159  tok_info (end_tok_type et) : m_et (et) { }
160 
161  tok_info (const std::string& meth, const std::string& cls)
162  : m_superclass_info (new superclass_info (meth, cls))
163  { }
164 
165  tok_info (const tok_info&) = delete;
166 
167  tok_info& operator = (const tok_info&) = delete;
168 
169  ~tok_info (void) { }
170 
171  std::string *m_str;
172 
174 
176 
178  {
179  public:
180  superclass_info (const std::string& meth, const std::string& cls)
181  : m_method_name (meth), m_class_name (cls)
182  { }
183 
184  superclass_info (void) = delete;
185 
186  superclass_info (const superclass_info&) = delete;
187 
188  superclass_info& operator = (const superclass_info&) = delete;
189 
190  ~superclass_info (void) = default;
191 
192  //--------
193 
194  // The name of the method to call. This is the text before the
195  // "@" and may be of the form "object.method".
196  std::string m_method_name;
197 
198  // The name of the superclass. This is the text after the "@"
199  // and may be of the form "object.method".
200  std::string m_class_name;
201  };
202 
204  };
205 
207 
208  std::string m_orig_text;
209 };
210 
212 
213 #endif
OCTAVE_END_NAMESPACE(octave)
Definition: token.h:39
bool may_be_command(void) const
Definition: token.h:101
bool iskeyword(void) const
Definition: token.h:119
filepos m_beg_pos
Definition: token.h:142
int line(void) const
Definition: token.h:116
void mark_trailing_space(void)
Definition: token.h:103
void mark_may_be_command(void)
Definition: token.h:100
bool m_tspc
Definition: token.h:140
filepos beg_pos(void) const
Definition: token.h:109
bool token_value_is(int tv) const
Definition: token.h:107
int m_tok_val
Definition: token.h:145
bool isstring(void) const
Definition: token.h:124
tok_info m_tok_info
Definition: token.h:206
filepos end_pos(void) const
Definition: token.h:110
void end_pos(const filepos &pos)
Definition: token.h:113
int token_value(void) const
Definition: token.h:106
filepos m_end_pos
Definition: token.h:143
token_type
Definition: token.h:43
@ keyword_token
Definition: token.h:45
@ numeric_token
Definition: token.h:47
@ ettype_token
Definition: token.h:48
@ string_token
Definition: token.h:46
@ generic_token
Definition: token.h:44
@ scls_name_token
Definition: token.h:49
void beg_pos(const filepos &pos)
Definition: token.h:112
std::string m_orig_text
Definition: token.h:208
end_tok_type
Definition: token.h:53
@ events_end
Definition: token.h:58
@ methods_end
Definition: token.h:62
@ enumeration_end
Definition: token.h:57
@ unwind_protect_end
Definition: token.h:67
@ try_catch_end
Definition: token.h:66
@ properties_end
Definition: token.h:64
@ switch_end
Definition: token.h:65
@ simple_end
Definition: token.h:54
@ classdef_end
Definition: token.h:56
@ arguments_end
Definition: token.h:55
@ if_end
Definition: token.h:61
@ parfor_end
Definition: token.h:63
@ for_end
Definition: token.h:59
@ function_end
Definition: token.h:60
@ while_end
Definition: token.h:69
@ spmd_end
Definition: token.h:68
token_type m_type_tag
Definition: token.h:147
token(const token &)=delete
bool m_maybe_cmd
Definition: token.h:138
bool space_follows_token(void) const
Definition: token.h:104
int column(void) const
Definition: token.h:117
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
superclass_info(const std::string &meth, const std::string &cls)
Definition: token.h:180
superclass_info(const superclass_info &)=delete
std::string * m_str
Definition: token.h:171
tok_info(const tok_info &)=delete
end_tok_type m_et
Definition: token.h:175
superclass_info * m_superclass_info
Definition: token.h:203
tok_info(const char *s)
Definition: token.h:153
tok_info(const std::string &meth, const std::string &cls)
Definition: token.h:161
tok_info(void)
Definition: token.h:151
~tok_info(void)
Definition: token.h:169
tok_info(end_tok_type et)
Definition: token.h:159
octave_value * m_num
Definition: token.h:173
tok_info(const octave_value &num)
Definition: token.h:157
tok_info(const std::string &str)
Definition: token.h:155