Mercurial > octave-nkf
comparison libinterp/octave-value/ov-struct.cc @ 15614:f2b8f90052fd
warn instead of throwing an error for invalid structure field names
* ov-struct.cc, ov-struct.h (maybe_warn_invalid_field_name):
New function.
(octave_struct::subsasgn, octave_scalar_struct::dotref,
octave_scalar_struct::subsasgn, Fstruct): Call
maybe_warn_invalid_field_name.
* pt-idx.cc (tree_index_expression::get_struct_index): Don't check for
valid structure field names here.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 20 Nov 2012 13:24:51 -0500 |
parents | 126285fce876 |
children | 18f168880226 |
comparison
equal
deleted
inserted
replaced
15613:126285fce876 | 15614:f2b8f90052fd |
---|---|
114 gripe_failed_assignment (void) | 114 gripe_failed_assignment (void) |
115 { | 115 { |
116 error ("assignment to structure element failed"); | 116 error ("assignment to structure element failed"); |
117 } | 117 } |
118 | 118 |
119 static void | |
120 maybe_warn_invalid_field_name (const std::string& key, const char *who) | |
121 { | |
122 if (! valid_identifier (key)) | |
123 { | |
124 if (who) | |
125 warning_with_id ("Octave:matlab-incompatible", | |
126 "%s: invalid structure field name '%s'", | |
127 who, key.c_str ()); | |
128 else | |
129 warning_with_id ("Octave:matlab-incompatible", | |
130 "invalid structure field name '%s'", | |
131 key.c_str ()); | |
132 } | |
133 } | |
134 | |
119 octave_value_list | 135 octave_value_list |
120 octave_struct::subsref (const std::string& type, | 136 octave_struct::subsref (const std::string& type, |
121 const std::list<octave_value_list>& idx, | 137 const std::list<octave_value_list>& idx, |
122 int nargout) | 138 int nargout) |
123 { | 139 { |
302 octave_value_list key_idx = *++p; | 318 octave_value_list key_idx = *++p; |
303 | 319 |
304 assert (key_idx.length () == 1); | 320 assert (key_idx.length () == 1); |
305 | 321 |
306 std::string key = key_idx(0).string_value (); | 322 std::string key = key_idx(0).string_value (); |
323 | |
324 maybe_warn_invalid_field_name (key, "subsasgn"); | |
325 | |
326 if (error_state) | |
327 return retval; | |
307 | 328 |
308 std::list<octave_value_list> next_idx (idx); | 329 std::list<octave_value_list> next_idx (idx); |
309 | 330 |
310 // We handled two index elements, so subsasgn to | 331 // We handled two index elements, so subsasgn to |
311 // needs to skip both of them. | 332 // needs to skip both of them. |
361 | 382 |
362 assert (key_idx.length () == 1); | 383 assert (key_idx.length () == 1); |
363 | 384 |
364 std::string key = key_idx(0).string_value (); | 385 std::string key = key_idx(0).string_value (); |
365 | 386 |
387 maybe_warn_invalid_field_name (key, "subsasgn"); | |
388 | |
389 if (error_state) | |
390 return retval; | |
391 | |
366 std::list<octave_value_list> next_idx (idx); | 392 std::list<octave_value_list> next_idx (idx); |
367 | 393 |
368 next_idx.erase (next_idx.begin ()); | 394 next_idx.erase (next_idx.begin ()); |
369 | 395 |
370 std::string next_type = type.substr (1); | 396 std::string next_type = type.substr (1); |
428 octave_value_list idxf = idx.front (); | 454 octave_value_list idxf = idx.front (); |
429 | 455 |
430 assert (key_idx.length () == 1); | 456 assert (key_idx.length () == 1); |
431 | 457 |
432 std::string key = key_idx(0).string_value (); | 458 std::string key = key_idx(0).string_value (); |
459 | |
460 maybe_warn_invalid_field_name (key, "subsasgn"); | |
461 | |
462 if (error_state) | |
463 return retval; | |
433 | 464 |
434 if (! error_state) | 465 if (! error_state) |
435 { | 466 { |
436 if (t_rhs.is_cs_list ()) | 467 if (t_rhs.is_cs_list ()) |
437 { | 468 { |
528 | 559 |
529 assert (key_idx.length () == 1); | 560 assert (key_idx.length () == 1); |
530 | 561 |
531 std::string key = key_idx(0).string_value (); | 562 std::string key = key_idx(0).string_value (); |
532 | 563 |
564 maybe_warn_invalid_field_name (key, "subsasgn"); | |
565 | |
566 if (error_state) | |
567 return retval; | |
568 | |
533 if (t_rhs.is_cs_list ()) | 569 if (t_rhs.is_cs_list ()) |
534 { | 570 { |
535 Cell tmp_cell = Cell (t_rhs.list_value ()); | 571 Cell tmp_cell = Cell (t_rhs.list_value ()); |
536 | 572 |
537 // The shape of the RHS is irrelevant, we just want | 573 // The shape of the RHS is irrelevant, we just want |
1097 | 1133 |
1098 assert (idx.length () == 1); | 1134 assert (idx.length () == 1); |
1099 | 1135 |
1100 std::string nm = idx(0).string_value (); | 1136 std::string nm = idx(0).string_value (); |
1101 | 1137 |
1102 if (! valid_identifier (nm)) | 1138 maybe_warn_invalid_field_name (nm, "subsref"); |
1103 { | 1139 |
1104 error ("subsref: invalid structure field name '%s'", nm.c_str ()); | 1140 if (error_state) |
1105 return retval; | 1141 return retval; |
1106 } | |
1107 | 1142 |
1108 retval = map.getfield (nm); | 1143 retval = map.getfield (nm); |
1109 | 1144 |
1110 if (! auto_add && retval.is_undefined ()) | 1145 if (! auto_add && retval.is_undefined ()) |
1111 error ("structure has no member '%s'", nm.c_str ()); | 1146 error ("structure has no member '%s'", nm.c_str ()); |
1224 | 1259 |
1225 assert (key_idx.length () == 1); | 1260 assert (key_idx.length () == 1); |
1226 | 1261 |
1227 std::string key = key_idx(0).string_value (); | 1262 std::string key = key_idx(0).string_value (); |
1228 | 1263 |
1229 if (! valid_identifier (key)) | 1264 maybe_warn_invalid_field_name (key, "subsasgn"); |
1230 { | 1265 |
1231 error ("subsasgn: invalid structure field name '%s'", key.c_str ()); | 1266 if (error_state) |
1232 return retval; | 1267 return retval; |
1233 } | |
1234 | 1268 |
1235 if (n > 1) | 1269 if (n > 1) |
1236 { | 1270 { |
1237 std::list<octave_value_list> next_idx (idx); | 1271 std::list<octave_value_list> next_idx (idx); |
1238 | 1272 |
1817 std::string key (args(i).string_value ()); | 1851 std::string key (args(i).string_value ()); |
1818 | 1852 |
1819 if (error_state) | 1853 if (error_state) |
1820 return retval; | 1854 return retval; |
1821 | 1855 |
1822 if (! valid_identifier (key)) | 1856 maybe_warn_invalid_field_name (key, "struct"); |
1823 { | 1857 |
1824 error ("struct: invalid structure field name '%s'", key.c_str ()); | 1858 if (error_state) |
1825 return retval; | 1859 return retval; |
1826 } | |
1827 | 1860 |
1828 // Value may be v, { v }, or { v1, v2, ... } | 1861 // Value may be v, { v }, or { v1, v2, ... } |
1829 // In the first two cases, we need to create a cell array of | 1862 // In the first two cases, we need to create a cell array of |
1830 // the appropriate dimensions filled with v. In the last case, | 1863 // the appropriate dimensions filled with v. In the last case, |
1831 // the cell array has already been determined to be of the | 1864 // the cell array has already been determined to be of the |