GNU Octave  9.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-2024 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  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (token)
93 
94  ~token ();
95 
96  void mark_may_be_command () { m_maybe_cmd = true; }
97  bool may_be_command () const { return m_maybe_cmd; }
98 
99  void mark_trailing_space () { m_tspc = true; }
100  bool space_follows_token () const { return m_tspc; }
101 
102  int token_value () const { return m_tok_val; }
103  bool token_value_is (int tv) const { return tv == m_tok_val; }
104 
105  filepos beg_pos () const { return m_beg_pos; }
106  filepos end_pos () const { return m_end_pos; }
107 
108  void beg_pos (const filepos& pos) { m_beg_pos = pos; }
109  void end_pos (const filepos& pos) { m_end_pos = pos; }
110 
111  // These will probably be removed.
112  int line () const { return m_beg_pos.line (); }
113  int column () const { return m_beg_pos.column (); }
114 
115  bool iskeyword () const
116  {
117  return m_type_tag == keyword_token || m_type_tag == ettype_token;
118  }
119 
120  bool isstring () const { return m_type_tag == string_token; }
121 
122  std::string text () const;
123  octave_value number () const;
124  token_type ttype () const;
125  end_tok_type ettype () const;
126 
127  std::string superclass_method_name () const;
128  std::string superclass_class_name () const;
129 
130  std::string text_rep () const;
131 
132 private:
133 
134  bool m_maybe_cmd;
135 
136  bool m_tspc;
137 
138  filepos m_beg_pos;
139  filepos m_end_pos;
140 
141  int m_tok_val;
142 
143  token_type m_type_tag;
144 
145  union tok_info
146  {
147  tok_info () { }
148 
149  tok_info (const char *s) : m_str (new std::string (s)) { }
150 
151  tok_info (const std::string& str) : m_str (new std::string (str)) { }
152 
153  tok_info (const octave_value& num) : m_num (new octave_value (num)) { }
154 
155  tok_info (end_tok_type et) : m_et (et) { }
156 
157  tok_info (const std::string& meth, const std::string& cls)
158  : m_superclass_info (new superclass_info (meth, cls))
159  { }
160 
161  OCTAVE_DISABLE_COPY_MOVE (tok_info)
162 
163  ~tok_info () { }
164 
165  std::string *m_str;
166 
167  octave_value *m_num;
168 
169  end_tok_type m_et;
170 
172  {
173  public:
174  superclass_info (const std::string& meth, const std::string& cls)
175  : m_method_name (meth), m_class_name (cls)
176  { }
177 
178  superclass_info () = delete;
179 
180  OCTAVE_DISABLE_COPY_MOVE (superclass_info)
181 
182  ~superclass_info () = default;
183 
184  //--------
185 
186  // The name of the method to call. This is the text before the
187  // "@" and may be of the form "object.method".
188  std::string m_method_name;
189 
190  // The name of the superclass. This is the text after the "@"
191  // and may be of the form "object.method".
192  std::string m_class_name;
193  };
194 
195  superclass_info *m_superclass_info;
196  };
197 
198  tok_info m_tok_info;
199 
200  std::string m_orig_text;
201 };
202 
203 OCTAVE_END_NAMESPACE(octave)
204 
205 #endif
Definition: token.h:39
bool isstring() const
Definition: token.h:120
filepos end_pos() const
Definition: token.h:106
int token_value() const
Definition: token.h:102
void mark_trailing_space()
Definition: token.h:99
bool token_value_is(int tv) const
Definition: token.h:103
void end_pos(const filepos &pos)
Definition: token.h:109
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
filepos beg_pos() const
Definition: token.h:105
void beg_pos(const filepos &pos)
Definition: token.h:108
int column() const
Definition: token.h:113
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
void mark_may_be_command()
Definition: token.h:96
bool may_be_command() const
Definition: token.h:97
int line() const
Definition: token.h:112
bool space_follows_token() const
Definition: token.h:100
bool iskeyword() const
Definition: token.h:115
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
superclass_info(const std::string &meth, const std::string &cls)
Definition: token.h:174