GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
pt-cmd.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1994-2026 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_pt_cmd_h)
27#define octave_pt_cmd_h 1
28
29#include "octave-config.h"
30
31#include <string>
32
33#include "comment-list.h"
34#include "ov-fcn.h"
35#include "pt.h"
36#include "pt-bp.h"
37#include "pt-walk.h"
38#include "panic.h"
39#include "token.h"
40
42
43// A base class for commands.
44
45class OCTINTERP_API tree_command : public tree
46{
47public:
48
49 tree_command () = default;
50
51 OCTAVE_DISABLE_COPY_MOVE (tree_command)
52
53 virtual ~tree_command () = default;
54
55 virtual void update_end_pos (const filepos&)
56 {
57 error ("unexpected call to tree_command::update_end_pos - please report this bug");
58 }
59};
60
61// No-op.
62
63class OCTINTERP_API tree_no_op_command : public tree_command
64{
65public:
66
67 tree_no_op_command (const std::string& cmd, bool eof, const token& tok)
68 : m_eof (eof), m_tok (tok), m_orig_cmd (cmd)
69 { }
70
71 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_no_op_command)
72
73 ~tree_no_op_command () = default;
74
75 filepos beg_pos () const { return m_tok.beg_pos (); }
76 filepos end_pos () const { return m_tok.end_pos (); }
77
78 comment_list leading_comments () const { return m_tok.leading_comments (); }
79 comment_list trailing_comments () const { return m_tok.trailing_comments (); }
80
81 void update_end_pos (const filepos& pos)
82 {
83 if (is_end_of_fcn_or_script () || is_end_of_file ())
84 m_tok.end_pos (pos);
85 else
86 error ("unexpected call to tree_no_op_command::update_end_pos - please report this bug");
87 }
88
90 {
91 m_tok.trailing_comments (lst);
92 }
93
95 {
96 tw.visit_no_op_command (*this);
97 }
98
100 {
101 return (m_orig_cmd == "endfunction" || m_orig_cmd == "endscript");
102 }
103
104 bool is_end_of_file () const { return m_eof; }
105
106 std::string original_command () { return m_orig_cmd; }
107
108private:
109
110 bool m_eof;
111
112 // If defined, may be END token or EOF.
113 token m_tok;
114
115 std::string m_orig_cmd;
116};
117
118// Function definition.
119
120class OCTINTERP_API tree_function_def : public tree_command
121{
122public:
123
125
126 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_function_def)
127
128 ~tree_function_def () = default;
129
131 {
132 octave_function *f = m_fcn.function_value ();
133 return f->beg_pos ();
134 }
135
137 {
138 octave_function *f = m_fcn.function_value ();
139 return f->end_pos ();
140 }
141
143 {
144 octave_function *f = m_fcn.function_value ();
145 return f->leading_comments ();
146 }
147
149 {
150 octave_function *f = m_fcn.function_value ();
151 return f->trailing_comments ();
152 }
153
155 {
156 tw.visit_function_def (*this);
157 }
158
159 octave_value function () { return m_fcn; }
160
161private:
162
163 tree_function_def (const octave_value& v) : m_fcn (v) { }
164
165 //--------
166
167 octave_value m_fcn;
168};
169
170OCTAVE_END_NAMESPACE(octave)
171
172#endif
virtual octave_function * function_value(bool silent=false)
Definition ov-base.cc:935
virtual octave::filepos beg_pos() const
Definition ov-fcn.cc:55
virtual octave::filepos end_pos() const
Definition ov-fcn.cc:61
virtual octave::comment_list leading_comments() const
Definition ov-fcn.cc:67
virtual octave::comment_list trailing_comments() const
Definition ov-fcn.cc:73
Definition token.h:42
tree_command()=default
tree_function_def(octave_function *f)
Definition pt-cmd.h:124
void accept(tree_walker &tw)
Definition pt-cmd.h:154
~tree_function_def()=default
filepos beg_pos() const
Definition pt-cmd.h:130
comment_list trailing_comments() const
Definition pt-cmd.h:148
octave_value function()
Definition pt-cmd.h:159
filepos end_pos() const
Definition pt-cmd.h:136
comment_list leading_comments() const
Definition pt-cmd.h:142
void update_end_pos(const filepos &pos)
Definition pt-cmd.h:81
comment_list trailing_comments() const
Definition pt-cmd.h:79
filepos end_pos() const
Definition pt-cmd.h:76
filepos beg_pos() const
Definition pt-cmd.h:75
std::string original_command()
Definition pt-cmd.h:106
comment_list leading_comments() const
Definition pt-cmd.h:78
void attach_trailing_comments(const comment_list &lst)
Definition pt-cmd.h:89
bool is_end_of_fcn_or_script() const
Definition pt-cmd.h:99
void accept(tree_walker &tw)
Definition pt-cmd.h:94
tree_no_op_command(const std::string &cmd, bool eof, const token &tok)
Definition pt-cmd.h:67
~tree_no_op_command()=default
bool is_end_of_file() const
Definition pt-cmd.h:104
virtual void visit_no_op_command(tree_no_op_command &)
Definition pt-walk.cc:497
virtual void visit_function_def(tree_function_def &)
Definition pt-walk.cc:305
Definition pt.h:47
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition error.cc:1008
F77_RET_T const F77_DBLE const F77_DBLE * f