GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
parse.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2025 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_parse_h)
27#define octave_parse_h 1
28
29#include "octave-config.h"
30
31#include <cstdio>
32
33#include <deque>
34#include <map>
35#include <memory>
36#include <set>
37#include <string>
38
39#include "input.h"
40#include "lex.h"
41#include "pt-misc.h"
42#include "symscope.h"
43#include "token.h"
44
45class octave_function;
48
50
51class parse_exception;
52class separator_list;
53class tree;
62class tree_array_list;
63class tree_cell;
64class tree_classdef;
77class tree_command;
78class tree_constant;
81class tree_expression;
82class tree_fcn_handle;
84class tree_identifier;
85class tree_if_clause;
86class tree_if_command;
89class tree_matrix;
90class tree_matrix;
93class tree_statement;
95class tree_statement_listtree_statement;
99
100OCTAVE_END_NAMESPACE(octave)
101
102#include "ovl.h"
103
104// Nonzero means print parser debugging info (-d).
105extern int octave_debug;
106
108
110{
111private:
112
113 class parent_scope_info
114 {
115 public:
116
117 typedef std::pair<symbol_scope, std::string> value_type;
118
119 typedef std::deque<value_type>::iterator iterator;
120 typedef std::deque<value_type>::const_iterator const_iterator;
121
122 typedef std::deque<value_type>::reverse_iterator reverse_iterator;
123 typedef std::deque<value_type>::const_reverse_iterator const_reverse_iterator;
124
125 parent_scope_info () = delete;
126
127 parent_scope_info (base_parser& parser)
128 : m_parser (parser), m_info (), m_all_names ()
129 { }
130
131 OCTAVE_DISABLE_COPY_MOVE (parent_scope_info)
132
133 ~parent_scope_info () = default;
134
135 OCTINTERP_API std::size_t size () const;
136
137 OCTINTERP_API void push (const value_type& elt);
138
139 OCTINTERP_API void push (const symbol_scope& id);
140
141 OCTINTERP_API void pop ();
142
143 OCTINTERP_API bool name_ok (const std::string& name);
144
145 OCTINTERP_API bool name_current_scope (const std::string& name);
146
147 OCTINTERP_API symbol_scope parent_scope () const;
148
149 OCTINTERP_API std::string parent_name () const;
150
151 OCTINTERP_API void clear ();
152
153 private:
154
155 base_parser& m_parser;
156 std::deque<value_type> m_info;
157 std::set<std::string> m_all_names;
158 };
159
160public:
161
162 OCTINTERP_API base_parser (base_lexer& lxr);
163
164 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (base_parser)
165
166 virtual ~base_parser ();
167
168 base_lexer& get_lexer () const { return m_lexer; }
169
170 bool at_end_of_input () const { return m_lexer.m_end_of_input; }
171
172 OCTINTERP_API void reset ();
173
174 void classdef_object (const std::shared_ptr<tree_classdef>& obj)
175 {
176 m_classdef_object = obj;
177 }
178
179 std::shared_ptr<tree_classdef> classdef_object () const
180 {
181 return m_classdef_object;
182 }
183
184 OCTINTERP_API void statement_list (std::shared_ptr<tree_statement_list>& lst);
185
186 std::shared_ptr<tree_statement_list> statement_list () const
187 {
188 return m_stmt_list;
189 }
190
191 void parsing_subfunctions (bool flag)
192 {
193 m_parsing_subfunctions = flag;
194 }
195
197 {
198 return m_parsing_subfunctions;
199 }
200
201 void parsing_local_functions (bool flag)
202 {
203 m_parsing_local_functions = flag;
204 }
205
207 {
208 return m_parsing_local_functions;
209 }
210
211 int curr_fcn_depth () const
212 {
213 return m_curr_fcn_depth;
214 }
215
216 void endfunction_found (bool flag)
217 {
218 m_endfunction_found = flag;
219 }
220
221 bool endfunction_found () const
222 {
223 return m_endfunction_found;
224 }
225
226 // Error messages for mismatched end tokens.
227 OCTINTERP_API void
228 end_token_error (token *tok, token::end_tok_type expected);
229
230 // Check to see that end tokens are properly matched.
231 OCTINTERP_API bool end_token_ok (token *tok, token::end_tok_type expected);
232
233 // Handle pushing symbol table for new function scope.
234 OCTINTERP_API bool push_fcn_symtab ();
235
236 // Build a constant.
237 OCTINTERP_API tree_constant * make_constant (token *tok);
238
239 OCTINTERP_API tree_black_hole * make_black_hole (token *tilde);
240
241 OCTINTERP_API tree_matrix * make_matrix (tree_argument_list *row);
242
243 OCTINTERP_API tree_matrix *
244 append_matrix_row (tree_matrix *matrix, token *sep_tok, tree_argument_list *row);
245
246 OCTINTERP_API tree_cell * make_cell (tree_argument_list *row);
247
248 OCTINTERP_API tree_cell *
249 append_cell_row (tree_cell *cell, token *sep_tok, tree_argument_list *row);
250
251 // Build a function handle.
252 OCTINTERP_API tree_fcn_handle * make_fcn_handle (token *tok);
253
254 // Build an anonymous function handle.
255 OCTINTERP_API tree_anon_fcn_handle *
256 make_anon_fcn_handle (token *at_tok, tree_parameter_list *param_list, tree_expression *expr);
257
258 // Build a colon expression.
259 OCTINTERP_API tree_expression *
260 make_colon_expression (tree_expression *base, token *colon_tok, tree_expression *limit);
261
262 // Build a colon expression.
263 OCTINTERP_API tree_expression *
264 make_colon_expression (tree_expression *base, token *colon_1_tok, tree_expression *incr, token *colon_2_tok, tree_expression *limit);
265
266 // Build a binary expression.
267 OCTINTERP_API tree_expression *
268 make_binary_op (tree_expression *op1, token *op_tok, tree_expression *op2);
269
270 // Maybe convert EXPR to a braindead_shortcircuit expression.
271 OCTINTERP_API void
272 maybe_convert_to_braindead_shortcircuit (tree_expression *&expr);
273
274 // Build a boolean expression.
275 OCTINTERP_API tree_expression *
276 make_boolean_op (tree_expression *op1, token *op_tok, tree_expression *op2);
277
278 // Build a prefix expression.
279 OCTINTERP_API tree_expression *
280 make_prefix_op (token *op_tok, tree_expression *op1);
281
282 // Build a postfix expression.
283 OCTINTERP_API tree_expression *
284 make_postfix_op (tree_expression *op1, token *op_tok);
285
286 // Build an unwind-protect command.
287 OCTINTERP_API tree_command *
288 make_unwind_command (token *unwind_tok, tree_statement_list *body, token *cleanup_tok, tree_statement_list *cleanup, token *end_tok);
289
290 // Build a try-catch command.
291 OCTINTERP_API tree_command *
292 make_try_command (token *try_tok, tree_statement_list *body, token *catch_tok, separator_list *catch_sep_list, tree_statement_list *cleanup, token *end_tok);
293
294 // Build a while command.
295 OCTINTERP_API tree_command *
296 make_while_command (token *while_tok, tree_expression *expr, tree_statement_list *body, token *end_tok);
297
298 // Build a do-until command.
299 OCTINTERP_API tree_command *
300 make_do_until_command (token *do_tok, tree_statement_list *body, token *until_tok, tree_expression *expr);
301
302 // Build a for command.
303 OCTINTERP_API tree_command *
304 make_for_command (token *for_tok, token *open_paren, tree_argument_list *lhs, token *eq_tok, tree_expression *expr, token *sep_tok, tree_expression *maxproc, token *close_paren, tree_statement_list *body, token *end_tok);
305
306 // Build a break command.
307 OCTINTERP_API tree_command * make_break_command (token *break_tok);
308
309 // Build a continue command.
310 OCTINTERP_API tree_command * make_continue_command (token *continue_tok);
311
312 // Build a return command.
313 OCTINTERP_API tree_command * make_return_command (token *return_tok);
314
315 // Build an spmd command.
316
317 OCTINTERP_API tree_spmd_command *
318 make_spmd_command (token *spmd_tok, tree_statement_list *body, token *end_tok);
319
320 // Start an if command.
321 OCTINTERP_API tree_if_command_list *
322 start_if_command (tree_if_clause *clause);
323
324 // Finish an if command.
325 OCTINTERP_API tree_if_command *
326 finish_if_command (tree_if_command_list *list, tree_if_clause *else_clause, token *end_tok);
327
328 // Build an elseif clause.
329 OCTINTERP_API tree_if_clause *
330 make_if_clause (token *if_tok, separator_list *if_sep_list, tree_expression *expr, tree_statement_list *list);
331
332 OCTINTERP_API tree_if_command_list *
333 append_if_clause (tree_if_command_list *list, tree_if_clause *clause);
334
335 // Finish a switch command.
336 OCTINTERP_API tree_switch_command *
337 finish_switch_command (token *switch_tok, tree_expression *expr, tree_switch_case_list *list, token *end_tok);
338
339 OCTINTERP_API tree_switch_case_list *
340 make_switch_case_list (tree_switch_case *switch_case);
341
342 // Build a switch case.
343 OCTINTERP_API tree_switch_case *
344 make_switch_case (token *case_tok, tree_expression *expr, tree_statement_list *list);
345
346 OCTINTERP_API tree_switch_case *
347 make_default_switch_case (token *default_tok, tree_statement_list *list);
348
349 OCTINTERP_API tree_switch_case_list *
350 append_switch_case (tree_switch_case_list *list, tree_switch_case *elt);
351
352 // Build an assignment to a variable.
353 OCTINTERP_API tree_expression *
354 make_assign_op (tree_argument_list *lhs, token *eq_tok, tree_expression *rhs);
355
356 // Define a script.
357 OCTINTERP_API void
358 make_script (tree_statement_list *cmds, tree_statement *end_script);
359
360 // Handle identifier that is recognized as a function name.
361 OCTINTERP_API tree_identifier *
362 make_fcn_name (tree_identifier *id);
363
364 // Define a function.
365 OCTINTERP_API tree_function_def *
366 make_function (token *fcn_tok, tree_parameter_list *ret_list, token *eq_tok, tree_identifier *id, tree_parameter_list *param_list, tree_statement_list *body, tree_statement *end_fcn_stmt);
367
368 // Begin defining a function.
369 OCTINTERP_API octave_user_function *
370 start_function (tree_identifier *id, tree_parameter_list *param_list, tree_statement_list *body, tree_statement *end_function, const std::string& doc_string);
371
372 // Create a no-op statement for end_function.
373 OCTINTERP_API tree_statement *
374 make_end (const std::string& type, bool eof, token *tok);
375
376 // Do most of the work for defining a function.
377 OCTINTERP_API octave_user_function *
379
380 // Finish defining a function.
381 OCTINTERP_API tree_function_def *
382 finish_function (token *fcn_tok, tree_parameter_list *ret_list, token *eq_tok, octave_user_function *fcn);
383
384 OCTINTERP_API tree_statement_list *
385 append_function_body (tree_statement_list *body, tree_statement_list *list);
386
387 // Make an arguments validation block.
388 OCTINTERP_API tree_arguments_block *
389 make_arguments_block (token *arguments_tok, tree_args_block_attribute_list *attr_list, tree_args_block_validation_list *validation_list, token *end_tok);
390
391 OCTINTERP_API tree_args_block_attribute_list *
392 make_args_attribute_list (tree_identifier *attribute_name);
393
394 // Make an argument validation.
395 OCTINTERP_API tree_arg_validation *
396 make_arg_validation (tree_arg_size_spec *size_spec, tree_identifier *class_name, tree_arg_validation_fcns *validation_fcns, token *eq_tok = nullptr, tree_expression *default_value = nullptr);
397
398 // Make an argument validation list.
399 OCTINTERP_API tree_args_block_validation_list *
400 make_args_validation_list (tree_arg_validation *arg_validation);
401
402 // Append an argument validation to an existing list.
403 OCTINTERP_API tree_args_block_validation_list *
404 append_args_validation_list (tree_args_block_validation_list *list, tree_arg_validation *arg_validation);
405
406 // Make an argument size specification object.
407 OCTINTERP_API tree_arg_size_spec *
408 make_arg_size_spec (tree_argument_list *size_args);
409
410 // Make a list of argument validation functions.
411 OCTINTERP_API tree_arg_validation_fcns *
412 make_arg_validation_fcns (tree_argument_list *fcn_args);
413
414 // Reset state after parsing function.
415 OCTINTERP_API void
416 recover_from_parsing_function ();
417
418 OCTINTERP_API tree_classdef *
420
421 OCTINTERP_API tree_classdef_properties_block *
422 make_classdef_properties_block (token *tok, tree_classdef_attribute_list *a, tree_classdef_property_list *plist, token *end_tok);
423
424 OCTINTERP_API tree_classdef_property_list *
425 make_classdef_property_list (tree_classdef_property *prop);
426
427 OCTINTERP_API tree_classdef_property *
428 make_classdef_property (tree_identifier *id, tree_arg_validation *av);
429
430 OCTINTERP_API tree_classdef_property_list *
431 append_classdef_property (tree_classdef_property_list *list, tree_classdef_property *elt);
432
433 OCTINTERP_API tree_classdef_methods_block *
434 make_classdef_methods_block (token *tok, tree_classdef_attribute_list *a, tree_classdef_method_list *mlist, token *end_tok);
435
436 OCTINTERP_API tree_classdef_events_block *
437 make_classdef_events_block (token *tok, tree_classdef_attribute_list *a, tree_classdef_event_list *elist, token *end_tok);
438
439 OCTINTERP_API tree_classdef_event_list *
440 make_classdef_event_list (tree_classdef_event *e);
441
442 OCTINTERP_API tree_classdef_event *
443 make_classdef_event (tree_identifier *id);
444
445 OCTINTERP_API tree_classdef_event_list *
446 append_classdef_event (tree_classdef_event_list *list, tree_classdef_event *elt);
447
448 OCTINTERP_API tree_classdef_enum_block *
449 make_classdef_enum_block (token *tok, tree_classdef_attribute_list *a, tree_classdef_enum_list *elist, token *end_tok);
450
451 OCTINTERP_API tree_classdef_enum_list *
452 make_classdef_enum_list (tree_classdef_enum *e);
453
454 OCTINTERP_API tree_classdef_enum *
455 make_classdef_enum (tree_identifier *id, token *open_paren, tree_expression *expr, token *close_paren);
456
457 OCTINTERP_API tree_classdef_enum_list *
458 append_classdef_enum (tree_classdef_enum_list *list, tree_classdef_enum *elt);
459
460 OCTINTERP_API tree_classdef_superclass_list *
461 make_classdef_superclass_list (token *lt_tok, tree_classdef_superclass *sc);
462
463 OCTINTERP_API tree_classdef_superclass *
464 make_classdef_superclass (token *fqident);
465
466 OCTINTERP_API tree_classdef_superclass_list *
467 append_classdef_superclass (tree_classdef_superclass_list *list, token *and_tok, tree_classdef_superclass *elt);
468
469 OCTINTERP_API tree_classdef_attribute_list *
470 make_classdef_attribute_list (tree_classdef_attribute *attr);
471
472 OCTINTERP_API tree_classdef_attribute *
473 make_classdef_attribute (tree_identifier *id);
474
475 OCTINTERP_API tree_classdef_attribute *
476 make_classdef_attribute (tree_identifier *id, token *eq_tok, tree_expression *expr);
477
478 OCTINTERP_API tree_classdef_attribute *
479 make_not_classdef_attribute (token *not_tok, tree_identifier *id);
480
481 OCTINTERP_API tree_classdef_attribute_list *
482 append_classdef_attribute (tree_classdef_attribute_list *list, token *sep_tok, tree_classdef_attribute *elt);
483
484 OCTINTERP_API tree_classdef_body *
485 make_classdef_body (tree_classdef_properties_block *pb);
486
487 OCTINTERP_API tree_classdef_body *
488 make_classdef_body (tree_classdef_methods_block *mb);
489
490 OCTINTERP_API tree_classdef_body *
491 make_classdef_body (tree_classdef_events_block *evb);
492
493 OCTINTERP_API tree_classdef_body *
494 make_classdef_body (tree_classdef_enum_block *enb);
495
496 OCTINTERP_API tree_classdef_body *
497 append_classdef_properties_block (tree_classdef_body *body, tree_classdef_properties_block *block);
498
499 OCTINTERP_API tree_classdef_body *
500 append_classdef_methods_block (tree_classdef_body *body, tree_classdef_methods_block *block);
501
502 OCTINTERP_API tree_classdef_body *
503 append_classdef_events_block (tree_classdef_body *body, tree_classdef_events_block *block);
504
505 OCTINTERP_API tree_classdef_body *
506 append_classdef_enum_block (tree_classdef_body *body, tree_classdef_enum_block *block);
507
508 OCTINTERP_API octave_user_function *
509 start_classdef_external_method (tree_identifier *id, tree_parameter_list *pl = nullptr);
510
511 OCTINTERP_API tree_function_def *
512 finish_classdef_external_method (octave_user_function *fcn, tree_parameter_list *ret_list = nullptr, token *eq_tok = nullptr);
513
514 OCTINTERP_API tree_classdef_method_list *
515 make_classdef_method_list (tree_function_def *fcn_def);
516
517 OCTINTERP_API tree_classdef_method_list *
518 append_classdef_method (tree_classdef_method_list *list, tree_function_def *fcn_def);
519
520 OCTINTERP_API bool
521 finish_classdef_file (tree_classdef *cls, tree_statement_list *local_fcns, token *eof_tok);
522
523 // Make a word list command.
524 OCTINTERP_API tree_index_expression *
525 make_word_list_command (tree_expression *expr, tree_argument_list *args);
526
527 // Make an index expression.
528 OCTINTERP_API tree_index_expression *
529 make_index_expression (tree_expression *expr, token *open_paren, tree_argument_list *args, token *close_paren, char type);
530
531 // Make an indirect reference expression.
532 OCTINTERP_API tree_index_expression *
533 make_indirect_ref (tree_expression *expr, token *dot_tok, token *struct_elt_tok);
534
535 // Make an indirect reference expression with dynamic field name.
536 OCTINTERP_API tree_index_expression *
537 make_indirect_ref (tree_expression *expr, token *dot_tok, token *open_paren, tree_expression *field, token *close_paren);
538
539 // Make a declaration command.
540 OCTINTERP_API tree_decl_command *
541 make_decl_command (token *tok, tree_decl_init_list *lst);
542
543 OCTINTERP_API tree_decl_init_list *
544 make_decl_init_list (tree_decl_elt *elt);
545
546 OCTINTERP_API tree_decl_elt *
547 make_decl_elt (tree_identifier *id, token *eq_op = nullptr, tree_expression *expr = nullptr);
548
549 OCTINTERP_API tree_decl_init_list *
550 append_decl_init_list (tree_decl_init_list *list, tree_decl_elt *elt);
551
552 // Validate an function parameter list.
553 OCTINTERP_API bool
554 validate_param_list (tree_parameter_list *lst, tree_parameter_list::in_or_out type);
555 // Validate matrix or cell
556 OCTINTERP_API bool validate_array_list (tree_expression *e);
557
558 // Validate matrix object used in "[lhs] = ..." assignments.
559 OCTINTERP_API tree_argument_list *
560 validate_matrix_for_assignment (tree_expression *e);
561
562 // Finish building an array_list (common action for finish_matrix
563 // and finish_cell).
564 OCTINTERP_API tree_expression *
565 finish_array_list (token *open_delim, tree_array_list *a, token *close_delim);
566
567 // Finish building a matrix list.
568 OCTINTERP_API tree_expression *
569 finish_matrix (token *open_delim, tree_matrix *m, token *close_delim);
570
571 // Finish building a cell list.
572 OCTINTERP_API tree_expression *
573 finish_cell (token *open_delim, tree_cell *c, token *close_delim);
574
575 OCTINTERP_API tree_identifier *
576 make_identifier (token *ident);
577
578 OCTINTERP_API tree_superclass_ref *
579 make_superclass_ref (token *superclassref);
580
581 OCTINTERP_API tree_metaclass_query *
582 make_metaclass_query (token *metaquery);
583
584 // Set the print flag for a statement based on the separator type.
585 OCTINTERP_API tree_statement_list *
586 set_stmt_print_flag (tree_statement_list *list, int sep_char, bool warn_missing_semi);
587
588 // Set the print flag for a statement based on the separator type.
589 OCTINTERP_API tree_statement_list *
590 set_stmt_print_flag (tree_statement_list *list, const token& sep_tok, bool warn_missing_semi);
591
592 // Set the print flag for a statement based on the separator type.
593 OCTINTERP_API tree_statement_list *
594 set_stmt_print_flag (tree_statement_list *list, separator_list *sep_list, bool warn_missing_semi);
595
596 // Finish building a statement.
597 template <typename T>
598 OCTINTERP_API tree_statement * make_statement (T *arg);
599
600 // Create a statement list.
601 OCTINTERP_API tree_statement_list *
602 make_statement_list (tree_statement *stmt);
603
604 // Append a statement to an existing statement list.
605 OCTINTERP_API tree_statement_list *
606 append_statement_list (tree_statement_list *list, int sep_char, tree_statement *stmt, bool warn_missing_semi);
607
608 // Append a statement to an existing statement list.
609 OCTINTERP_API tree_statement_list *
610 append_statement_list (tree_statement_list *list, token *sep_tok, tree_statement *stmt, bool warn_missing_semi);
611
612 // Append a statement to an existing statement list.
613 OCTINTERP_API tree_statement_list *
614 append_statement_list (tree_statement_list *list, separator_list *sep_list, tree_statement *stmt, bool warn_missing_semi);
615
616 // Create a statement list containing only function_def commands.
617 OCTINTERP_API tree_statement_list *
618 make_function_def_list (tree_function_def *fcn_def);
619
620 // Append a function_def command to an existing statement list (that
621 // should contain only other function_def commands).
622 OCTINTERP_API tree_statement_list *
623 append_function_def_list (tree_statement_list *list, separator_list *sep_list, tree_function_def *fcn_def);
624
625 OCTINTERP_API tree_argument_list *
626 make_argument_list (tree_expression *expr);
627
628 OCTINTERP_API tree_argument_list *
629 append_argument_list (tree_argument_list *list, tree_expression *expr);
630
631 OCTINTERP_API tree_argument_list *
632 append_argument_list (tree_argument_list *list, token *sep_tok, tree_expression *expr);
633
634 OCTINTERP_API tree_parameter_list *
635 make_parameter_list (tree_parameter_list::in_or_out io);
636
637 OCTINTERP_API tree_parameter_list *
638 make_parameter_list (tree_parameter_list::in_or_out io, tree_decl_elt *t);
639
640 OCTINTERP_API tree_parameter_list *
641 make_parameter_list (tree_parameter_list::in_or_out io, tree_identifier *id);
642
643 OCTINTERP_API tree_parameter_list *
644 append_parameter_list (tree_parameter_list *list, token *sep_tok, tree_decl_elt *t);
645
646 OCTINTERP_API tree_parameter_list *
647 append_parameter_list (tree_parameter_list *list, token *sep_tok, tree_identifier *id);
648
649 // Don't allow parsing command syntax. If the parser/lexer is
650 // reset, this setting is also reset to the default (allow command
651 // syntax).
652 OCTINTERP_API void disallow_command_syntax ();
653
654 // Generic error messages.
655 OCTINTERP_API void bison_error (const std::string& s);
656 OCTINTERP_API void bison_error (const std::string& s, const filepos& pos);
657 OCTINTERP_API void bison_error (const std::list<parse_exception>& pe);
658 OCTINTERP_API void bison_error (const parse_exception& pe);
659
660 friend OCTINTERP_API octave_value
661 parse_fcn_file (interpreter& interp, const std::string& full_file, const std::string& file, const std::string& dir_name, const std::string& dispatch_type, const std::string& package_name, bool require_file, bool force_script, bool autoload, bool relative_lookup);
662
663 // Thih interface allows push or pull parsers to be used
664 // equivalently, provided that the push parser also owns its input
665 // method (see below). Alternatively, the push parser interface may
666 // use a separate run method and completely separate input from
667 // lexical analysis and parsing.
668
669 virtual int run () = 0;
670
671 // Check primary script or function generated by the parser for
672 // semantic errors.
673 OCTINTERP_API bool validate_primary_fcn ();
674
675 OCTINTERP_API bool finish_input (tree_statement_list *lst, bool at_eof = false);
676
677protected:
678
679 // Contains error message if Bison-generated parser returns nonzero
680 // status.
681 std::string m_parse_error_msg;
682
683 // Have we found an explicit end to a function?
685
686 // TRUE means we are in the process of autoloading a function.
688
689 // TRUE means the current function file was found in a relative path
690 // element.
692
693 // FALSE if we are still at the primary function. Subfunctions can
694 // only be declared inside function files.
696
697 // TRUE if we are parsing local functions defined at after a
698 // classdef block. Local functions can only be declared inside
699 // classdef files.
701
702 // Maximum function depth detected. Used to determine whether
703 // we have nested functions or just implicitly ended subfunctions.
705
706 // = 0 currently outside any function.
707 // = 1 inside the primary function or a subfunction.
708 // > 1 means we are looking at a function definition that seems to be
709 // inside a function. Note that the function still might not be a
710 // nested function.
712
713 // Scope where we install all subfunctions and nested functions. Only
714 // used while reading function files.
716
717 // Name of the current class when we are parsing class methods or
718 // constructors.
719 std::string m_curr_class_name;
720
721 // Name of the current package when we are parsing an element contained
722 // in a package directory (+-directory).
724
725 // Nested function scopes and names currently being parsed.
726 parent_scope_info m_function_scopes;
727
728 // Pointer to the primary user function or user script function.
730
731 // List of subfunction names, initially in the order they are
732 // installed in the symbol table, then ordered as they appear in the
733 // file. Eventually stashed in the primary function object.
734 std::list<std::string> m_subfunction_names;
735
736 // Pointer to the classdef object we just parsed, if any.
737 std::shared_ptr<tree_classdef> m_classdef_object;
738
739 // Result of parsing input.
740 std::shared_ptr <tree_statement_list> m_stmt_list;
741
742 // State of the lexer.
744
745 // Internal state of the Bison parser.
747
748private:
749
750 // Maybe print a warning if an assignment expression is used as the
751 // test in a logical expression.
752 OCTINTERP_API void maybe_warn_assign_as_truth_value (tree_expression *expr);
753
754 // Maybe print a warning about switch labels that aren't constants.
755 OCTINTERP_API void maybe_warn_variable_switch_label (tree_expression *expr);
756
757 // Maybe print a warning.
758 OCTINTERP_API void maybe_warn_missing_semi (tree_statement_list *);
759};
760
761// Publish externally used friend functions.
762
763extern OCTINTERP_API octave_value
764parse_fcn_file (interpreter& interp, const std::string& full_file, const std::string& file, const std::string& dir_name, const std::string& dispatch_type, const std::string& package_name, bool require_file, bool force_script, bool autoload, bool relative_lookup);
765
766class parser : public base_parser
767{
768public:
769
771 : base_parser (*(new lexer (interp)))
772 { }
773
774 parser (FILE *file, interpreter& interp)
775 : base_parser (*(new lexer (file, interp)))
776 { }
777
778 parser (FILE *file, interpreter& interp, std::string encoding)
779 : base_parser (*(new lexer (file, interp, encoding)))
780 { }
781
782 parser (const std::string& eval_string, interpreter& interp)
783 : base_parser (*(new lexer (eval_string, interp)))
784 { }
785
786 // The lexer must be allocated with new. The parser object
787 // takes ownership of and deletes the lexer object in its
788 // destructor.
789
791 : base_parser (*lxr)
792 { }
793
794 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (parser)
795
796 ~parser () = default;
797
798 OCTINTERP_API int run ();
799};
800
802{
803public:
804
806 : base_parser (*(new push_lexer (interp))), m_interpreter (interp), m_reader ()
807 { }
808
809 // The parser assumes ownership of READER, which must be created
810 // with new.
811
813 : base_parser (*(new push_lexer (interp))), m_interpreter (interp), m_reader (reader)
814 { }
815
816 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (push_parser)
817
818 ~push_parser () = default;
819
820 // Use the push parser in the same way as the pull parser. The
821 // parser arranges for input through the M_READER object. See, for
822 // example, interpreter::main_loop.
823
824 OCTINTERP_API int run ();
825
826 // Parse INPUT. M_READER is not used. The user is responsible for
827 // collecting input.
828
829 OCTINTERP_API int run (const std::string& input, bool eof);
830
831private:
832
833 interpreter& m_interpreter;
834
835 std::shared_ptr<input_reader> m_reader;
836};
837
838extern OCTINTERP_API std::string
839get_help_from_file (const std::string& nm, bool& symbol_found, std::string& file);
840
841extern OCTINTERP_API std::string
842get_help_from_file (const std::string& nm, bool& symbol_found);
843
844extern OCTINTERP_API octave_value
845load_fcn_from_file (const std::string& file_name, const std::string& dir_name = "", const std::string& dispatch_type = "", const std::string& package_name = "", const std::string& fcn_name = "", bool autoload = false);
846
847extern OCTINTERP_API void
848source_file (const std::string& file_name, const std::string& context = "", bool verbose = false, bool require_file = true);
849
850extern OCTINTERP_API octave_value_list
851feval (const char *name, const octave_value_list& args = octave_value_list (), int nargout = 0);
852
853extern OCTINTERP_API octave_value_list
854feval (const std::string& name, const octave_value_list& args = octave_value_list (), int nargout = 0);
855
856extern OCTINTERP_API octave_value_list
857feval (octave_function *fcn, const octave_value_list& args = octave_value_list (), int nargout = 0);
858
859extern OCTINTERP_API octave_value_list
860feval (const octave_value& val, const octave_value_list& args = octave_value_list (), int nargout = 0);
861
862extern OCTINTERP_API octave_value_list
863feval (const octave_value_list& args, int nargout = 0);
864
865OCTAVE_END_NAMESPACE(octave)
866
867#endif
std::shared_ptr< tree_statement_list > m_stmt_list
Definition parse.h:740
int m_max_fcn_depth
Definition parse.h:704
bool endfunction_found() const
Definition parse.h:221
bool parsing_local_functions() const
Definition parse.h:206
octave_value m_primary_fcn
Definition parse.h:729
parent_scope_info m_function_scopes
Definition parse.h:726
void parsing_subfunctions(bool flag)
Definition parse.h:191
bool m_endfunction_found
Definition parse.h:684
std::shared_ptr< tree_statement_list > statement_list() const
Definition parse.h:186
base_lexer & m_lexer
Definition parse.h:743
std::string m_curr_class_name
Definition parse.h:719
bool m_parsing_local_functions
Definition parse.h:700
std::string m_curr_package_name
Definition parse.h:723
bool m_fcn_file_from_relative_lookup
Definition parse.h:691
symbol_scope m_primary_fcn_scope
Definition parse.h:715
int curr_fcn_depth() const
Definition parse.h:211
bool at_end_of_input() const
Definition parse.h:170
std::shared_ptr< tree_classdef > classdef_object() const
Definition parse.h:179
std::list< std::string > m_subfunction_names
Definition parse.h:734
bool m_autoloading
Definition parse.h:687
void parsing_local_functions(bool flag)
Definition parse.h:201
bool parsing_subfunctions() const
Definition parse.h:196
bool m_parsing_subfunctions
Definition parse.h:695
void endfunction_found(bool flag)
Definition parse.h:216
void * m_parser_state
Definition parse.h:746
virtual int run()=0
base_lexer & get_lexer() const
Definition parse.h:168
std::shared_ptr< tree_classdef > m_classdef_object
Definition parse.h:737
int m_curr_fcn_depth
Definition parse.h:711
octave_user_function * frob_function(tree_identifier *id, octave_user_function *fcn)
void classdef_object(const std::shared_ptr< tree_classdef > &obj)
Definition parse.h:174
std::string m_parse_error_msg
Definition parse.h:681
Definition lex.h:723
~parser()=default
int run()
parser(const std::string &eval_string, interpreter &interp)
Definition parse.h:782
parser(interpreter &interp)
Definition parse.h:770
parser(FILE *file, interpreter &interp, std::string encoding)
Definition parse.h:778
parser(FILE *file, interpreter &interp)
Definition parse.h:774
parser(lexer *lxr)
Definition parse.h:790
push_parser(interpreter &interp)
Definition parse.h:805
~push_parser()=default
push_parser(interpreter &interp, input_reader *reader)
Definition parse.h:812
Definition token.h:42
end_tok_type
Definition token.h:57
Definition pt.h:47
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave_value parse_fcn_file(interpreter &interp, const std::string &full_file, const std::string &file, const std::string &dir_name, const std::string &dispatch_type, const std::string &package_name, bool require_file, bool force_script, bool autoload, bool relative_lookup)
octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
std::string get_help_from_file(const std::string &nm, bool &symbol_found, std::string &file)
octave_value load_fcn_from_file(const std::string &file_name, const std::string &dir_name="", const std::string &dispatch_type="", const std::string &package_name="", const std::string &fcn_name="", bool autoload=false)
void source_file(const std::string &file_name, const std::string &context="", bool verbose=false, bool require_file=true)
int octave_debug