GNU Octave  6.2.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-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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <cassert>
31 
32 #include "symrec.h"
33 #include "token.h"
34 
35 namespace octave
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, double d, const std::string& s, const filepos& beg_pos,
66  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 (double_token),
69  m_tok_info (d), 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 symbol_record& sr, const filepos& beg_pos,
80  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 (sym_rec_token),
83  m_tok_info (sr), m_orig_text ()
84  { }
85 
86  token::token (int tv, const std::string& meth, const std::string& cls,
87  const filepos& beg_pos, const filepos& end_pos)
88  : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
89  m_end_pos (end_pos), m_tok_val (tv), m_type_tag (scls_name_token),
90  m_tok_info (meth, cls), m_orig_text ()
91  { }
92 
94  {
95  if (m_type_tag == string_token)
96  delete m_tok_info.m_str;
97  else if (m_type_tag == sym_rec_token)
98  delete m_tok_info.m_sr;
99  else if (m_type_tag == scls_name_token)
101  }
102 
103  std::string
104  token::text (void) const
105  {
106  assert (m_type_tag == string_token);
107  return *m_tok_info.m_str;
108  }
109 
110  std::string
111  token::symbol_name (void) const
112  {
113  assert (m_type_tag == sym_rec_token);
114  return m_tok_info.m_sr->name ();
115  }
116 
117  double
118  token::number (void) const
119  {
120  assert (m_type_tag == double_token);
121  return m_tok_info.m_num;
122  }
123 
125  token::ttype (void) const
126  {
127  return m_type_tag;
128  }
129 
131  token::ettype (void) const
132  {
133  assert (m_type_tag == ettype_token);
134  return m_tok_info.m_et;
135  }
136 
138  token::sym_rec (void) const
139  {
140  assert (m_type_tag == sym_rec_token);
141  return *m_tok_info.m_sr;
142  }
143 
144  std::string
146  {
147  assert (m_type_tag == scls_name_token);
149  }
150 
151  std::string
153  {
154  assert (m_type_tag == scls_name_token);
156  }
157 
158  std::string
159  token::text_rep (void) const
160  {
161  return m_orig_text;
162  }
163 }
std::string name(void) const
Definition: symrec.h:185
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
tok_info m_tok_info
Definition: token.h:223
token(int tv, const filepos &beg_pos, const filepos &end_pos)
Definition: token.cc:37
std::string superclass_method_name(void) const
Definition: token.cc:145
token_type m_type_tag
Definition: token.h:160
std::string text(void) const
Definition: token.cc:104
double number(void) const
Definition: token.cc:118
~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
@ double_token
Definition: token.h:47
@ scls_name_token
Definition: token.h:50
std::string m_orig_text
Definition: token.h:225
std::string symbol_name(void) const
Definition: token.cc:111
symbol_record sym_rec(void) const
Definition: token.cc:138
std::string text_rep(void) const
Definition: token.cc:159
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
std::string * m_str
Definition: token.h:188
symbol_record * m_sr
Definition: token.h:194
end_tok_type m_et
Definition: token.h:192
superclass_info * m_superclass_info
Definition: token.h:220