GNU Octave  6.2.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-2021 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 "symrec.h"
35 
36 namespace octave
37 {
38  class token
39  {
40  public:
41 
43  {
51  };
52 
54  {
69  };
70 
71  token (int tv, const filepos& beg_pos, const filepos& end_pos);
72 
73  token (int tv, bool is_keyword, const filepos& beg_pos
74  , const filepos& end_pos);
75 
76  token (int tv, const char *s, const filepos& beg_pos,
77  const filepos& end_pos);
78 
79  token (int tv, const std::string& s, const filepos& beg_pos,
80  const filepos& end_pos);
81 
82  token (int tv, double d, const std::string& s, const filepos& beg_pos,
83  const filepos& end_pos);
84 
85  token (int tv, end_tok_type t, const filepos& beg_pos,
86  const filepos& end_pos);
87 
88  token (int tv, const symbol_record& s, const filepos& beg_pos,
89  const filepos& end_pos);
90 
91  token (int tv, const std::string& mth, const std::string& cls,
92  const filepos& beg_pos, const filepos& end_pos);
93 
94  // No copying!
95 
96  token (const token&) = delete;
97 
98  token& operator = (const token&) = delete;
99 
100  ~token (void);
101 
102  void mark_may_be_command (void) { m_maybe_cmd = true; }
103  bool may_be_command (void) const { return m_maybe_cmd; }
104 
105  void mark_trailing_space (void) { m_tspc = true; }
106  bool space_follows_token (void) const { return m_tspc; }
107 
108  int token_value (void) const { return m_tok_val; }
109  bool token_value_is (int tv) const { return tv == m_tok_val; }
110 
111  filepos beg_pos (void) const { return m_beg_pos; }
112  filepos end_pos (void) const { return m_end_pos; }
113 
114  void beg_pos (const filepos& pos) { m_beg_pos = pos; }
115  void end_pos (const filepos& pos) { m_end_pos = pos; }
116 
117  // These will probably be removed.
118  int line (void) const { return m_beg_pos.line (); }
119  int column (void) const { return m_beg_pos.column (); }
120 
121  bool iskeyword (void) const
122  {
124  }
125 
126  OCTAVE_DEPRECATED (5, "use 'octave::iskeyword' instead")
127  bool is_keyword (void) const
128  {
129  return iskeyword ();
130  }
131 
132  bool is_symbol (void) const
133  {
134  return m_type_tag == sym_rec_token;
135  }
136 
137  std::string text (void) const;
138  std::string symbol_name (void) const;
139  double number (void) const;
140  token_type ttype (void) const;
141  end_tok_type ettype (void) const;
142  symbol_record sym_rec (void) const;
143 
144  std::string superclass_method_name (void) const;
145  std::string superclass_class_name (void) const;
146 
147  std::string text_rep (void) const;
148 
149  private:
150 
152 
153  bool m_tspc;
154 
157 
159 
161 
162  union tok_info
163  {
164  tok_info (void) { }
165 
166  tok_info (const char *s) : m_str (new std::string (s)) { }
167 
168  tok_info (const std::string& str) : m_str (new std::string (str)) { }
169 
170  tok_info (double num) : m_num (num) { }
171 
172  tok_info (end_tok_type et) : m_et (et) { }
173 
175  : m_sr (new symbol_record (sr))
176  { }
177 
178  tok_info (const std::string& meth, const std::string& cls)
179  : m_superclass_info (new superclass_info (meth, cls))
180  { }
181 
182  tok_info (const tok_info&) = delete;
183 
184  tok_info& operator = (const tok_info&) = delete;
185 
186  ~tok_info (void) { }
187 
188  std::string *m_str;
189 
190  double m_num;
191 
193 
195 
197  {
198  superclass_info (void) = delete;
199 
200  superclass_info (const std::string& meth,
201  const std::string& cls)
202  : m_method_name (meth), m_class_name (cls)
203  { }
204 
205  superclass_info (const superclass_info&) = delete;
206 
208 
209  ~superclass_info (void) = default;
210 
211  // The name of the method to call. This is the text before the
212  // "@" and may be of the form "object.method".
213  std::string m_method_name;
214 
215  // The name of the superclass. This is the text after the "@"
216  // and may be of the form "object.method".
217  std::string m_class_name;
218  };
219 
221  };
222 
224 
225  std::string m_orig_text;
226  };
227 }
228 
229 #endif
void line(int l)
Definition: filepos.h:51
void column(int c)
Definition: filepos.h:52
std::string superclass_class_name(void) const
Definition: token.cc:152
end_tok_type ettype(void) const
Definition: token.cc:131
token_type ttype(void) const
Definition: token.cc:125
bool token_value_is(int tv) const
Definition: token.h:109
void beg_pos(const filepos &pos)
Definition: token.h:114
filepos m_beg_pos
Definition: token.h:155
tok_info m_tok_info
Definition: token.h:223
int line(void) const
Definition: token.h:118
token(int tv, const filepos &beg_pos, const filepos &end_pos)
Definition: token.cc:37
void mark_trailing_space(void)
Definition: token.h:105
std::string superclass_method_name(void) const
Definition: token.cc:145
token(const token &)=delete
token & operator=(const token &)=delete
bool space_follows_token(void) const
Definition: token.h:106
token_type m_type_tag
Definition: token.h:160
@ try_catch_end
Definition: token.h:66
@ unwind_protect_end
Definition: token.h:67
@ while_end
Definition: token.h:68
@ parfor_end
Definition: token.h:63
@ switch_end
Definition: token.h:65
@ classdef_end
Definition: token.h:56
@ events_end
Definition: token.h:58
@ methods_end
Definition: token.h:62
@ function_end
Definition: token.h:60
@ simple_end
Definition: token.h:55
@ properties_end
Definition: token.h:64
@ enumeration_end
Definition: token.h:57
std::string text(void) const
Definition: token.cc:104
bool iskeyword(void) const
Definition: token.h:121
bool is_symbol(void) const
Definition: token.h:132
double number(void) const
Definition: token.cc:118
void mark_may_be_command(void)
Definition: token.h:102
bool may_be_command(void) const
Definition: token.h:103
bool m_maybe_cmd
Definition: token.h:151
int m_tok_val
Definition: token.h:158
~token(void)
Definition: token.cc:93
@ ettype_token
Definition: token.h:48
@ string_token
Definition: token.h:46
@ sym_rec_token
Definition: token.h:49
@ keyword_token
Definition: token.h:45
@ generic_token
Definition: token.h:44
@ double_token
Definition: token.h:47
@ scls_name_token
Definition: token.h:50
int column(void) const
Definition: token.h:119
void end_pos(const filepos &pos)
Definition: token.h:115
std::string m_orig_text
Definition: token.h:225
int token_value(void) const
Definition: token.h:108
filepos beg_pos(void) const
Definition: token.h:111
std::string symbol_name(void) const
Definition: token.cc:111
bool m_tspc
Definition: token.h:153
symbol_record sym_rec(void) const
Definition: token.cc:138
bool is_keyword(void) const
Definition: token.h:127
filepos m_end_pos
Definition: token.h:156
filepos end_pos(void) const
Definition: token.h:112
std::string text_rep(void) const
Definition: token.cc:159
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
superclass_info(const superclass_info &)=delete
superclass_info & operator=(const superclass_info &)=delete
superclass_info(const std::string &meth, const std::string &cls)
Definition: token.h:200
std::string * m_str
Definition: token.h:188
tok_info(const std::string &meth, const std::string &cls)
Definition: token.h:178
tok_info(end_tok_type et)
Definition: token.h:172
symbol_record * m_sr
Definition: token.h:194
end_tok_type m_et
Definition: token.h:192
tok_info(const std::string &str)
Definition: token.h:168
tok_info & operator=(const tok_info &)=delete
tok_info(const char *s)
Definition: token.h:166
tok_info(double num)
Definition: token.h:170
tok_info(const tok_info &)=delete
superclass_info * m_superclass_info
Definition: token.h:220
tok_info(const symbol_record &sr)
Definition: token.h:174