comparison libinterp/parse-tree/pt-select.h @ 33296:70b7f1c285c7

refactor comment handling in the lexer and parser These changes simplify the grammar rules in the parser by eliminating the "stash_comment" rule that previously grabbed comments from the lexer at select locations. All .m file comments are now stored in tokens created by the lexer. Most tree_* objects processed by the parser now store tokens in order to capture comment information, though some separator tokens are not handled yet (commas, semicolons, newlines, and structure element reference operators). These tokens will be handled by a future change. The tree_print_code class will also be updated for this new way of handling comments in a later change.n These changes affect the lexer, parser, octave_user_fcn, and most tree_* classes: cdef-class.cc, ov-usr-fcn.cc, ov-usr-fcn.h, lex.h, lex.ll, oct-parse.yy, parse.h, pt-arg-list.h, pt-args-block.h, pt-assign.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-eval.cc, pt-except.cc, pt-except.h, pt-exp.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-loop.cc, pt-loop.h, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-walk.cc, pt-walk.h, pt.cc, and pt.h.
author John W. Eaton <jwe@octave.org>
date Mon, 01 Apr 2024 23:27:43 -0400
parents 0b5f3219b650
children d422992b5483
comparison
equal deleted inserted replaced
33295:979a51024c94 33296:70b7f1c285c7
31 #include <list> 31 #include <list>
32 32
33 #include "comment-list.h" 33 #include "comment-list.h"
34 #include "pt-cmd.h" 34 #include "pt-cmd.h"
35 #include "pt-walk.h" 35 #include "pt-walk.h"
36 #include "token.h"
36 37
37 OCTAVE_BEGIN_NAMESPACE(octave) 38 OCTAVE_BEGIN_NAMESPACE(octave)
38 39
40 class comment_list;
39 class tree_expression; 41 class tree_expression;
40 class tree_statement_list; 42 class tree_statement_list;
41 43
42 // If. 44 // If.
43 45
44 class tree_if_clause : public tree 46 class tree_if_clause : public tree
45 { 47 {
46 public: 48 public:
47 49
48 tree_if_clause (int l = -1, int c = -1) 50 tree_if_clause (const token& tok, tree_expression *e, tree_statement_list *sl, int l = -1, int c = -1)
49 : tree (l, c), m_expr (nullptr), m_list (nullptr), m_lead_comm (nullptr) 51 : tree (l, c), m_tok (tok), m_expr (e), m_list (sl)
50 { } 52 { }
51 53
52 tree_if_clause (tree_statement_list *sl, comment_list *lc = nullptr, 54 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_if_clause)
53 int l = -1, int c = -1)
54 : tree (l, c), m_expr (nullptr), m_list (sl), m_lead_comm (lc) { }
55
56 tree_if_clause (tree_expression *e, tree_statement_list *sl,
57 comment_list *lc = nullptr,
58 int l = -1, int c = -1)
59 : tree (l, c), m_expr (e), m_list (sl), m_lead_comm (lc) { }
60
61 OCTAVE_DISABLE_COPY_MOVE (tree_if_clause)
62 55
63 ~tree_if_clause (); 56 ~tree_if_clause ();
64 57
58 token if_token () const { return m_tok; }
59
65 bool is_else_clause () { return ! m_expr; } 60 bool is_else_clause () { return ! m_expr; }
66 61
67 tree_expression * condition () { return m_expr; } 62 tree_expression * condition () { return m_expr; }
68 63
69 tree_statement_list * commands () { return m_list; } 64 tree_statement_list * commands () { return m_list; }
70 65
71 comment_list * leading_comment () { return m_lead_comm; } 66 comment_list leading_comments () const { return m_tok.leading_comments (); }
72 67
73 void accept (tree_walker& tw) 68 void accept (tree_walker& tw)
74 { 69 {
75 tw.visit_if_clause (*this); 70 tw.visit_if_clause (*this);
76 } 71 }
77 72
78 private: 73 private:
79 74
75 token m_tok;
76
80 // The condition to test. 77 // The condition to test.
81 tree_expression *m_expr; 78 tree_expression *m_expr = nullptr;
82 79
83 // The list of statements to evaluate if expr is true. 80 // The list of statements to evaluate if expr is true.
84 tree_statement_list *m_list; 81 tree_statement_list *m_list;
85
86 // Comment preceding ELSE or ELSEIF token.
87 comment_list *m_lead_comm;
88 }; 82 };
89 83
90 class tree_if_command_list : public std::list<tree_if_clause *> 84 class tree_if_command_list : public std::list<tree_if_clause *>
91 { 85 {
92 public: 86 public:
105 delete *p; 99 delete *p;
106 erase (p); 100 erase (p);
107 } 101 }
108 } 102 }
109 103
104 token if_token () const
105 {
106 if (! empty ())
107 {
108 tree_if_clause *p = front ();
109 return p->if_token ();
110 }
111
112 return token ();
113 }
114
110 void accept (tree_walker& tw) 115 void accept (tree_walker& tw)
111 { 116 {
112 tw.visit_if_command_list (*this); 117 tw.visit_if_command_list (*this);
113 } 118 }
114 }; 119 };
115 120
116 class tree_if_command : public tree_command 121 class tree_if_command : public tree_command
117 { 122 {
118 public: 123 public:
119 124
120 tree_if_command (int l = -1, int c = -1) 125 tree_if_command (const token& if_tok, const token& end_tok, int l = -1, int c = -1)
121 : tree_command (l, c), m_list (nullptr), 126 : tree_command (l, c), m_if_tok (if_tok), m_end_tok (end_tok)
122 m_lead_comm (nullptr), m_trail_comm (nullptr) 127 { }
123 { } 128
124 129 tree_if_command (const token& if_tok, tree_if_command_list *lst, const token& end_tok, int l = -1, int c = -1)
125 tree_if_command (tree_if_command_list *lst, comment_list *lc, 130 : tree_command (l, c), m_if_tok (if_tok), m_list (lst), m_end_tok (end_tok)
126 comment_list *tc, int l = -1, int c = -1) 131 { }
127 : tree_command (l, c), m_list (lst), m_lead_comm (lc), m_trail_comm (tc) 132
128 { } 133 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_if_command)
129
130 OCTAVE_DISABLE_COPY_MOVE (tree_if_command)
131 134
132 ~tree_if_command (); 135 ~tree_if_command ();
133 136
134 tree_if_command_list * cmd_list () { return m_list; } 137 tree_if_command_list * cmd_list () { return m_list; }
135 138
136 comment_list * leading_comment () { return m_lead_comm; } 139 comment_list leading_comments () const { return m_if_tok.leading_comments (); }
137
138 comment_list * trailing_comment () { return m_trail_comm; }
139 140
140 void accept (tree_walker& tw) 141 void accept (tree_walker& tw)
141 { 142 {
142 tw.visit_if_command (*this); 143 tw.visit_if_command (*this);
143 } 144 }
144 145
145 private: 146 private:
146 147
148 token m_if_tok;
149
147 // List of if commands (if, elseif, elseif, ... else, endif) 150 // List of if commands (if, elseif, elseif, ... else, endif)
148 tree_if_command_list *m_list; 151 tree_if_command_list *m_list = nullptr;
149 152
150 // Comment preceding IF token. 153 token m_end_tok;
151 comment_list *m_lead_comm;
152
153 // Comment preceding ENDIF token.
154 comment_list *m_trail_comm;
155 }; 154 };
156 155
157 // Switch. 156 // Switch.
158 157
159 class tree_switch_case : public tree 158 class tree_switch_case : public tree
160 { 159 {
161 public: 160 public:
162 161
163 tree_switch_case (int l = -1, int c = -1) 162 tree_switch_case (const token& tok, tree_statement_list *sl, int l = -1, int c = -1)
164 : tree (l, c), m_label (nullptr), m_list (nullptr), m_lead_comm (nullptr) 163 : tree (l, c), m_tok (tok), m_list (sl)
165 { } 164 { }
166 165
167 tree_switch_case (tree_statement_list *sl, comment_list *lc = nullptr, 166 tree_switch_case (const token& tok, tree_expression *e, tree_statement_list *sl, int l = -1, int c = -1)
168 int l = -1, int c = -1) 167 : tree (l, c), m_tok (tok), m_label (e), m_list (sl)
169 : tree (l, c), m_label (nullptr), m_list (sl), m_lead_comm (lc) { } 168 { }
170 169
171 tree_switch_case (tree_expression *e, tree_statement_list *sl, 170 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_switch_case)
172 comment_list *lc = nullptr,
173 int l = -1, int c = -1)
174 : tree (l, c), m_label (e), m_list (sl), m_lead_comm (lc) { }
175
176 OCTAVE_DISABLE_COPY_MOVE (tree_switch_case)
177 171
178 ~tree_switch_case (); 172 ~tree_switch_case ();
179 173
180 bool is_default_case () { return ! m_label; } 174 bool is_default_case () { return ! m_label; }
181 175
182 tree_expression * case_label () { return m_label; } 176 tree_expression * case_label () { return m_label; }
183 177
184 tree_statement_list * commands () { return m_list; } 178 tree_statement_list * commands () { return m_list; }
185 179
186 comment_list * leading_comment () { return m_lead_comm; } 180 comment_list leading_comments () const { return m_tok.leading_comments (); }
187 181
188 void accept (tree_walker& tw) 182 void accept (tree_walker& tw)
189 { 183 {
190 tw.visit_switch_case (*this); 184 tw.visit_switch_case (*this);
191 } 185 }
192 186
193 private: 187 private:
194 188
189 token m_tok;
190
195 // The case label. 191 // The case label.
196 tree_expression *m_label; 192 tree_expression *m_label = nullptr;
197 193
198 // The list of statements to evaluate if the label matches. 194 // The list of statements to evaluate if the label matches.
199 tree_statement_list *m_list; 195 tree_statement_list *m_list;
200
201 // Comment preceding CASE or OTHERWISE token.
202 comment_list *m_lead_comm;
203 }; 196 };
204 197
205 class tree_switch_case_list : public std::list<tree_switch_case *> 198 class tree_switch_case_list : public std::list<tree_switch_case *>
206 { 199 {
207 public: 200 public:
230 223
231 class tree_switch_command : public tree_command 224 class tree_switch_command : public tree_command
232 { 225 {
233 public: 226 public:
234 227
235 tree_switch_command (int l = -1, int c = -1) 228 tree_switch_command (const token& switch_tok, tree_expression *e, tree_switch_case_list *lst, const token& end_tok, int l = -1, int c = -1)
236 : tree_command (l, c), m_expr (nullptr), m_list (nullptr), 229 : tree_command (l, c), m_switch_tok (switch_tok), m_expr (e), m_list (lst), m_end_tok (end_tok)
237 m_lead_comm (nullptr), m_trail_comm (nullptr) { } 230 { }
238 231
239 tree_switch_command (tree_expression *e, tree_switch_case_list *lst, 232 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (tree_switch_command)
240 comment_list *lc, comment_list *tc,
241 int l = -1, int c = -1)
242 : tree_command (l, c), m_expr (e), m_list (lst), m_lead_comm (lc),
243 m_trail_comm (tc) { }
244
245 OCTAVE_DISABLE_COPY_MOVE (tree_switch_command)
246 233
247 ~tree_switch_command (); 234 ~tree_switch_command ();
248 235
249 tree_expression * switch_value () { return m_expr; } 236 tree_expression * switch_value () { return m_expr; }
250 237
251 tree_switch_case_list * case_list () { return m_list; } 238 tree_switch_case_list * case_list () { return m_list; }
252 239
253 comment_list * leading_comment () { return m_lead_comm; } 240 comment_list leading_comments () const { return m_switch_tok.leading_comments (); }
254
255 comment_list * trailing_comment () { return m_trail_comm; }
256 241
257 void accept (tree_walker& tw) 242 void accept (tree_walker& tw)
258 { 243 {
259 tw.visit_switch_command (*this); 244 tw.visit_switch_command (*this);
260 } 245 }
261 246
262 private: 247 private:
248
249 token m_switch_tok;
263 250
264 // Value on which to switch. 251 // Value on which to switch.
265 tree_expression *m_expr; 252 tree_expression *m_expr;
266 253
267 // List of cases (case 1, case 2, ..., default) 254 // List of cases (case 1, case 2, ..., default)
268 tree_switch_case_list *m_list; 255 tree_switch_case_list *m_list;
269 256
270 // Comment preceding SWITCH token. 257 token m_end_tok;
271 comment_list *m_lead_comm;
272
273 // Comment preceding ENDSWITCH token.
274 comment_list *m_trail_comm;
275 }; 258 };
276 259
277 OCTAVE_END_NAMESPACE(octave) 260 OCTAVE_END_NAMESPACE(octave)
278 261
279 #endif 262 #endif