GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
token.cc
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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <cassert>
31 
32 #include "error.h"
33 #include "token.h"
34 
36 
37 token::token (int tv, const filepos& beg_pos, const filepos& end_pos)
38  : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
39  m_end_pos (end_pos), m_tok_val (tv), m_type_tag (generic_token),
40  m_tok_info (), m_orig_text ()
41 { }
42 
43 token::token (int tv, bool is_kw, const filepos& beg_pos,
44  const filepos& end_pos)
45  : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
46  m_end_pos (end_pos), m_tok_val (tv),
47  m_type_tag (is_kw ? keyword_token : generic_token), m_tok_info (),
48  m_orig_text ()
49 { }
50 
51 token::token (int tv, const char *s, const filepos& beg_pos,
52  const filepos& end_pos)
53  : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
54  m_end_pos (end_pos), m_tok_val (tv), m_type_tag (string_token),
55  m_tok_info (s), m_orig_text ()
56 { }
57 
58 token::token (int tv, const std::string& s, const filepos& beg_pos,
59  const filepos& end_pos)
60  : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
61  m_end_pos (end_pos), m_tok_val (tv), m_type_tag (string_token),
62  m_tok_info (s), m_orig_text ()
63 { }
64 
65 token::token (int tv, const octave_value& val, const std::string& s,
66  const filepos& beg_pos, const filepos& end_pos)
67  : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
68  m_end_pos (end_pos), m_tok_val (tv), m_type_tag (numeric_token),
69  m_tok_info (val), m_orig_text (s)
70 { }
71 
72 token::token (int tv, end_tok_type t, const filepos& beg_pos,
73  const filepos& end_pos)
74  : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
75  m_end_pos (end_pos), m_tok_val (tv), m_type_tag (ettype_token),
76  m_tok_info (t), m_orig_text ()
77 { }
78 
79 token::token (int tv, const std::string& meth, const std::string& cls,
80  const filepos& beg_pos, const filepos& end_pos)
81  : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
82  m_end_pos (end_pos), m_tok_val (tv), m_type_tag (scls_name_token),
83  m_tok_info (meth, cls), m_orig_text ()
84 { }
85 
87 {
88  if (m_type_tag == string_token)
89  delete m_tok_info.m_str;
90  else if (m_type_tag == numeric_token)
91  delete m_tok_info.m_num;
92  else if (m_type_tag == scls_name_token)
93  delete m_tok_info.m_superclass_info;
94 }
95 
96 std::string
97 token::text () const
98 {
99  panic_if (m_type_tag != string_token);
100  return *m_tok_info.m_str;
101 }
102 
105 {
106  panic_if (m_type_tag != numeric_token);
107  return *m_tok_info.m_num;
108 }
109 
111 token::ttype () const
112 {
113  return m_type_tag;
114 }
115 
118 {
119  panic_if (m_type_tag != ettype_token);
120  return m_tok_info.m_et;
121 }
122 
123 std::string
125 {
126  panic_if (m_type_tag != scls_name_token);
127  return m_tok_info.m_superclass_info->m_method_name;
128 }
129 
130 std::string
132 {
133  panic_if (m_type_tag != scls_name_token);
134  return m_tok_info.m_superclass_info->m_class_name;
135 }
136 
137 std::string
139 {
140  return m_orig_text;
141 }
142 
143 OCTAVE_END_NAMESPACE(octave)
Definition: token.h:39
token_type ttype() const
Definition: token.cc:111
std::string text_rep() const
Definition: token.cc:138
std::string superclass_class_name() const
Definition: token.cc:131
~token()
Definition: token.cc:86
end_tok_type ettype() const
Definition: token.cc:117
token(int tv, const filepos &beg_pos, const filepos &end_pos)
Definition: token.cc:37
std::string superclass_method_name() const
Definition: token.cc:124
token_type
Definition: token.h:43
@ numeric_token
Definition: token.h:47
@ ettype_token
Definition: token.h:48
@ string_token
Definition: token.h:46
@ scls_name_token
Definition: token.h:49
end_tok_type
Definition: token.h:53
std::string text() const
Definition: token.cc:97
octave_value number() const
Definition: token.cc:104
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define panic_if(cond)
Definition: error.h:509