comparison libinterp/parse-tree/pt-idx.cc @ 33309:d422992b5483

refactor position tracking in the parser This change replaces direct source line and column tracking in the tree_* classes with the beginning and ending positions in tokens created by the lexer. All line and column arguments for tree_* constructors have been removed. More tree_* classes have been modified to store tokens that are part of the syntax but not necessary for evaluation. Affected files: cdef-class.cc, ov-fcn.cc, ov-fcn.h, ov-usr-fcn.cc, ov-usr-fcn.h, bp-table.cc, oct-parse.yy, parse.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.h, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-except.h, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.h, pt-select.h, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-unop.cc, pt-unop.h, pt.cc, and pt.h.
author John W. Eaton <jwe@octave.org>
date Thu, 04 Apr 2024 00:58:56 -0400
parents 70b7f1c285c7
children 56d234504c01
comparison
equal deleted inserted replaced
33308:9d7e418f0121 33309:d422992b5483
44 44
45 OCTAVE_BEGIN_NAMESPACE(octave) 45 OCTAVE_BEGIN_NAMESPACE(octave)
46 46
47 // Index expressions. 47 // Index expressions.
48 48
49 tree_index_expression::tree_index_expression (int l, int c) 49 tree_index_expression::tree_index_expression (tree_expression *e, const token& open_delim, tree_argument_list *lst, const token& close_delim, char t)
50 : tree_expression (l, c) 50 : m_expr (e), m_args (0), m_type (), m_arg_nm (), m_dyn_field (), m_word_list_cmd (false)
51 { }
52
53 tree_index_expression::tree_index_expression (tree_expression *e, const token& open_delim, tree_argument_list *lst, const token& close_delim, int l, int c, char t)
54 : tree_expression (l, c), m_expr (e), m_args (0), m_type (), m_arg_nm (), m_dyn_field (), m_word_list_cmd (false)
55 { 51 {
56 append (open_delim, lst, close_delim, t); 52 append (open_delim, lst, close_delim, t);
57 } 53 }
58 54
59 tree_index_expression::tree_index_expression (tree_expression *e, const token& dot_tok, const token& struct_elt_tok, int l, int c) 55 tree_index_expression::tree_index_expression (tree_expression *e, const token& dot_tok, const token& struct_elt_tok)
60 : tree_expression (l, c), m_expr (e), m_args (0), m_type (), m_arg_nm (), m_dyn_field (), m_word_list_cmd (false) 56 : m_expr (e), m_args (0), m_type (), m_arg_nm (), m_dyn_field (), m_word_list_cmd (false)
61 { 57 {
62 append (dot_tok, struct_elt_tok); 58 append (dot_tok, struct_elt_tok);
63 } 59 }
64 60
65 tree_index_expression::tree_index_expression (tree_expression *e, const token& dot_tok, const token& open_paren, tree_expression *df, const token& close_paren, int l, int c) 61 tree_index_expression::tree_index_expression (tree_expression *e, const token& dot_tok, const token& open_paren, tree_expression *df, const token& close_paren)
66 : tree_expression (l, c), m_expr (e), m_args (0), m_type (), m_arg_nm (), m_dyn_field (), m_word_list_cmd (false) 62 : m_expr (e), m_args (0), m_type (), m_arg_nm (), m_dyn_field (), m_word_list_cmd (false)
67 { 63 {
68 append (dot_tok, open_paren, df, close_paren); 64 append (dot_tok, open_paren, df, close_paren);
69 } 65 }
70 66
71 // FIXME: Need to handle open_delim and close_delim.
72
73 tree_index_expression * 67 tree_index_expression *
74 tree_index_expression::append (const token& /*open_delim*/, tree_argument_list *lst, const token& /*close_delim*/, char t) 68 tree_index_expression::append (const token& open_delim, tree_argument_list *lst, const token& close_delim, char t)
75 { 69 {
70 lst->mark_in_delims (open_delim, close_delim);
76 m_args.push_back (lst); 71 m_args.push_back (lst);
77 m_type.append (1, t); 72 m_type.append (1, t);
73 m_dot_tok.push_back (token ());
78 m_arg_nm.push_back (lst ? lst->get_arg_names () : string_vector ()); 74 m_arg_nm.push_back (lst ? lst->get_arg_names () : string_vector ());
79 m_dyn_field.push_back (static_cast<tree_expression *> (nullptr)); 75 m_dyn_field.push_back (static_cast<tree_expression *> (nullptr));
80 76
81 if (lst && lst->has_magic_tilde ()) 77 if (lst && lst->has_magic_tilde ())
82 error ("invalid use of empty argument (~) in index expression"); 78 error ("invalid use of empty argument (~) in index expression");
83 79
84 return this; 80 return this;
85 } 81 }
86 82
87 // FIXME: Need to handle dot_tok.
88
89 tree_index_expression * 83 tree_index_expression *
90 tree_index_expression::append (const token& /*dot_tok*/, const token& struct_elt_tok) 84 tree_index_expression::append (const token& dot_tok, const token& struct_elt_tok)
91 { 85 {
92 m_args.push_back (static_cast<tree_argument_list *> (nullptr)); 86 m_args.push_back (static_cast<tree_argument_list *> (nullptr));
93 m_type += '.'; 87 m_type += '.';
88 m_dot_tok.push_back (dot_tok);
94 m_arg_nm.push_back (struct_elt_tok.text ()); 89 m_arg_nm.push_back (struct_elt_tok.text ());
95 m_dyn_field.push_back (static_cast<tree_expression *> (nullptr)); 90 m_dyn_field.push_back (static_cast<tree_expression *> (nullptr));
96 91
97 return this; 92 return this;
98 } 93 }
99 94
100 // FIXME: Need to handle dot_tok, open_paren, and close_paren.
101
102 tree_index_expression * 95 tree_index_expression *
103 tree_index_expression::append (const token& /*dot_tok*/, const token& /*open_paren*/, tree_expression *df, const token& /*close_paren*/) 96 tree_index_expression::append (const token& dot_tok, const token& open_paren, tree_expression *df, const token& close_paren)
104 { 97 {
105 m_args.push_back (static_cast<tree_argument_list *> (nullptr)); 98 m_args.push_back (static_cast<tree_argument_list *> (nullptr));
106 m_type += '.'; 99 m_type += '.';
100 m_dot_tok.push_back (dot_tok);
107 m_arg_nm.push_back (""); 101 m_arg_nm.push_back ("");
102 df->mark_in_delims (open_paren, close_paren);
108 m_dyn_field.push_back (df); 103 m_dyn_field.push_back (df);
109 104
110 return this; 105 return this;
111 } 106 }
112 107
134 129
135 std::string 130 std::string
136 tree_index_expression::name () const 131 tree_index_expression::name () const
137 { 132 {
138 return m_expr->name (); 133 return m_expr->name ();
134 }
135
136 filepos
137 tree_index_expression::end_pos () const
138 {
139 int n = m_args.size ();
140
141 if (n == 0)
142 return m_expr->end_pos ();
143
144 char idx_type = m_type[n-1];
145
146 if (idx_type == '(' || idx_type == '{')
147 {
148 tree_argument_list *args = m_args.back ();
149 return args->end_pos ();
150 }
151
152 if (idx_type == '.')
153 {
154 tree_expression *dyn_field = m_dyn_field.back ();
155 return dyn_field->end_pos ();
156 }
157
158 panic_impossible ();
139 } 159 }
140 160
141 std::string 161 std::string
142 tree_index_expression::get_struct_index 162 tree_index_expression::get_struct_index
143 (tree_evaluator& tw, 163 (tree_evaluator& tw,
237 } 257 }
238 258
239 tree_index_expression * 259 tree_index_expression *
240 tree_index_expression::dup (symbol_scope& scope) const 260 tree_index_expression::dup (symbol_scope& scope) const
241 { 261 {
242 tree_index_expression *new_idx_expr 262 tree_index_expression *new_idx_expr = new tree_index_expression ();
243 = new tree_index_expression (line (), column ());
244 263
245 new_idx_expr->m_expr = (m_expr ? m_expr->dup (scope) : nullptr); 264 new_idx_expr->m_expr = (m_expr ? m_expr->dup (scope) : nullptr);
246 265
247 std::list<tree_argument_list *> new_args; 266 std::list<tree_argument_list *> new_args;
248 267