GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
parse.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2024 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 
45 class octave_function;
46 class octave_user_code;
48 
50 
51 class comment_list;
52 class parse_exception;
53 class tree;
55 class tree_arg_size_spec;
60 class tree_argument_list;
62 class tree_array_list;
63 class tree_cell;
64 class tree_classdef;
66 class tree_classdef_body;
77 class tree_command;
78 class tree_constant;
79 class tree_decl_command;
81 class tree_expression;
82 class tree_fcn_handle;
83 class tree_function_def;
84 class tree_identifier;
85 class tree_if_clause;
86 class tree_if_command;
89 class tree_matrix;
90 class tree_matrix;
92 class tree_spmd_command;
93 class tree_statement;
95 class tree_statement_listtree_statement;
96 class tree_switch_case;
99 
100 OCTAVE_END_NAMESPACE(octave)
101 
102 #include "ovl.h"
103 
104 // Nonzero means print parser debugging info (-d).
105 extern int octave_debug;
106 
108 
110 {
111 private:
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 
160 public:
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 
196  bool parsing_subfunctions () const
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_val);
238 
239  OCTINTERP_API tree_black_hole * make_black_hole ();
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, 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, tree_argument_list *row);
250 
251  // Build a function handle.
252  OCTINTERP_API tree_fcn_handle * make_fcn_handle (token *tok_val);
253 
254  // Build an anonymous function handle.
255  OCTINTERP_API tree_anon_fcn_handle *
256  make_anon_fcn_handle (tree_parameter_list *param_list,
257  tree_expression *expr, const filepos& at_pos);
258 
259  // Build a colon expression.
260  OCTINTERP_API tree_expression *
261  make_colon_expression (tree_expression *base, tree_expression *limit,
262  tree_expression *incr = nullptr);
263 
264  // Build a binary expression.
265  OCTINTERP_API tree_expression *
266  make_binary_op (int op, tree_expression *op1, token *tok_val,
267  tree_expression *op2);
268 
269  // Maybe convert EXPR to a braindead_shortcircuit expression.
270  OCTINTERP_API void
271  maybe_convert_to_braindead_shortcircuit (tree_expression *&expr);
272 
273  // Build a boolean expression.
274  OCTINTERP_API tree_expression *
275  make_boolean_op (int op, tree_expression *op1, token *tok_val,
276  tree_expression *op2);
277 
278  // Build a prefix expression.
279  OCTINTERP_API tree_expression *
280  make_prefix_op (int op, tree_expression *op1, token *tok_val);
281 
282  // Build a postfix expression.
283  OCTINTERP_API tree_expression *
284  make_postfix_op (int op, tree_expression *op1, token *tok_val);
285 
286  // Build an unwind-protect command.
287  OCTINTERP_API tree_command *
288  make_unwind_command (token *unwind_tok, tree_statement_list *body,
289  tree_statement_list *cleanup, token *end_tok,
290  comment_list *lc, comment_list *mc);
291 
292  // Build a try-catch command.
293  OCTINTERP_API tree_command *
294  make_try_command (token *try_tok, tree_statement_list *body,
295  char catch_sep, tree_statement_list *cleanup,
296  token *end_tok, comment_list *lc,
297  comment_list *mc);
298 
299  // Build a while command.
300  OCTINTERP_API tree_command *
301  make_while_command (token *while_tok, tree_expression *expr,
302  tree_statement_list *body, token *end_tok,
303  comment_list *lc);
304 
305  // Build a do-until command.
306  OCTINTERP_API tree_command *
307  make_do_until_command (token *until_tok, tree_statement_list *body,
308  tree_expression *expr, comment_list *lc);
309 
310  // Build a for command.
311  OCTINTERP_API tree_command *
312  make_for_command (int tok_id, token *for_tok, tree_argument_list *lhs,
313  tree_expression *expr, tree_expression *maxproc,
314  tree_statement_list *body, token *end_tok,
315  comment_list *lc);
316 
317  // Build a break command.
318  OCTINTERP_API tree_command * make_break_command (token *break_tok);
319 
320  // Build a continue command.
321  OCTINTERP_API tree_command * make_continue_command (token *continue_tok);
322 
323  // Build a return command.
324  OCTINTERP_API tree_command * make_return_command (token *return_tok);
325 
326  // Build an spmd command.
327 
328  OCTINTERP_API tree_spmd_command *
329  make_spmd_command (token *spmd_tok, tree_statement_list *body,
330  token *end_tok, comment_list *lc, comment_list *tc);
331 
332  // Start an if command.
333  OCTINTERP_API tree_if_command_list *
334  start_if_command (tree_expression *expr, tree_statement_list *list);
335 
336  // Finish an if command.
337  OCTINTERP_API tree_if_command *
338  finish_if_command (token *if_tok, tree_if_command_list *list,
339  token *end_tok, comment_list *lc);
340 
341  // Build an elseif clause.
342  OCTINTERP_API tree_if_clause *
343  make_elseif_clause (token *elseif_tok, tree_expression *expr,
344  tree_statement_list *list, comment_list *lc);
345 
346  OCTINTERP_API tree_if_clause *
347  make_else_clause (token *else_tok, comment_list *lc,
348  tree_statement_list *list);
349 
350  OCTINTERP_API tree_if_command_list *
351  append_if_clause (tree_if_command_list *list, tree_if_clause *clause);
352 
353  // Finish a switch command.
354  OCTINTERP_API tree_switch_command *
355  finish_switch_command (token *switch_tok, tree_expression *expr,
356  tree_switch_case_list *list, token *end_tok,
357  comment_list *lc);
358 
359  OCTINTERP_API tree_switch_case_list *
360  make_switch_case_list (tree_switch_case *switch_case);
361 
362  // Build a switch case.
363  OCTINTERP_API tree_switch_case *
364  make_switch_case (token *case_tok, tree_expression *expr,
365  tree_statement_list *list, comment_list *lc);
366 
367  OCTINTERP_API tree_switch_case *
368  make_default_switch_case (token *default_tok, comment_list *lc,
369  tree_statement_list *list);
370 
371  OCTINTERP_API tree_switch_case_list *
372  append_switch_case (tree_switch_case_list *list, tree_switch_case *elt);
373 
374  // Build an assignment to a variable.
375  OCTINTERP_API tree_expression *
376  make_assign_op (int op, tree_argument_list *lhs, token *eq_tok,
377  tree_expression *rhs);
378 
379  // Define a script.
380  OCTINTERP_API void
381  make_script (tree_statement_list *cmds, tree_statement *end_script);
382 
383  // Handle identifier that is recognized as a function name.
384  OCTINTERP_API tree_identifier *
385  make_fcn_name (tree_identifier *id);
386 
387  // Define a function.
388  OCTINTERP_API tree_function_def *
389  make_function (token *fcn_tok, tree_parameter_list *ret_list,
390  tree_identifier *id, tree_parameter_list *param_list,
391  tree_statement_list *body, tree_statement *end_fcn_stmt,
392  comment_list *lc, comment_list *bc);
393 
394  // Begin defining a function.
395  OCTINTERP_API octave_user_function *
396  start_function (tree_identifier *id, tree_parameter_list *param_list,
397  tree_statement_list *body, tree_statement *end_function,
398  const std::string& doc_string);
399 
400  // Create a no-op statement for end_function.
401  OCTINTERP_API tree_statement *
402  make_end (const std::string& type, bool eof,
403  const filepos& beg_pos, const filepos& end_pos);
404 
405  // Do most of the work for defining a function.
406  OCTINTERP_API octave_user_function *
408 
409  // Finish defining a function.
410  OCTINTERP_API tree_function_def *
411  finish_function (tree_parameter_list *ret_list,
413  int l, int c);
414 
415  OCTINTERP_API tree_statement_list *
416  append_function_body (tree_statement_list *body, tree_statement_list *list);
417 
418  // Make an arguments validation block.
419  OCTINTERP_API tree_arguments_block *
420  make_arguments_block (token *arguments_tok,
422  tree_args_block_validation_list *validation_list,
423  token *end_tok, comment_list *lc, comment_list *tc);
424 
425  OCTINTERP_API tree_args_block_attribute_list *
426  make_args_attribute_list (tree_identifier *attribute_name);
427 
428  // Make an argument validation.
429  OCTINTERP_API tree_arg_validation *
430  make_arg_validation (tree_arg_size_spec *size_spec,
431  tree_identifier *class_name,
432  tree_arg_validation_fcns *validation_fcns,
433  tree_expression *default_value);
434 
435  // Make an argument validation list.
436  OCTINTERP_API tree_args_block_validation_list *
437  make_args_validation_list (tree_arg_validation *arg_validation);
438 
439  // Append an argument validation to an existing list.
440  OCTINTERP_API tree_args_block_validation_list *
441  append_args_validation_list (tree_args_block_validation_list *list,
442  tree_arg_validation *arg_validation);
443 
444  // Make an argument size specification object.
445  OCTINTERP_API tree_arg_size_spec *
446  make_arg_size_spec (tree_argument_list *size_args);
447 
448  // Make a list of argument validation functions.
449  OCTINTERP_API tree_arg_validation_fcns *
450  make_arg_validation_fcns (tree_argument_list *fcn_args);
451 
452  // Reset state after parsing function.
453  OCTINTERP_API void
454  recover_from_parsing_function ();
455 
456  OCTINTERP_API tree_classdef *
457  make_classdef (token *tok_val, tree_classdef_attribute_list *a,
459  tree_classdef_body *body, token *end_tok,
460  comment_list *lc, comment_list *bc, comment_list *tc);
461 
462  OCTINTERP_API tree_classdef_properties_block *
463  make_classdef_properties_block (token *tok_val,
466  token *end_tok, comment_list *lc,
467  comment_list *tc);
468 
469  OCTINTERP_API tree_classdef_property_list *
470  make_classdef_property_list (tree_classdef_property *prop);
471 
472  OCTINTERP_API tree_classdef_property *
473  make_classdef_property (comment_list *lc, tree_identifier *id,
474  tree_arg_validation *av);
475 
476  OCTINTERP_API tree_classdef_property_list *
477  append_classdef_property (tree_classdef_property_list *list,
479 
480  OCTINTERP_API tree_classdef_methods_block *
481  make_classdef_methods_block (token *tok_val,
484  token *end_tok, comment_list *lc,
485  comment_list *tc);
486 
487  OCTINTERP_API tree_classdef_events_block *
488  make_classdef_events_block (token *tok_val,
491  token *end_tok, comment_list *lc,
492  comment_list *tc);
493 
494  OCTINTERP_API tree_classdef_events_list *
495  make_classdef_events_list (tree_classdef_event *e);
496 
497  OCTINTERP_API tree_classdef_event *
498  make_classdef_event (comment_list *lc, tree_identifier *id);
499 
500  OCTINTERP_API tree_classdef_events_list *
501  append_classdef_event (tree_classdef_events_list *list,
502  tree_classdef_event *elt);
503 
504  OCTINTERP_API tree_classdef_enum_block *
505  make_classdef_enum_block (token *tok_val,
508  token *end_tok, comment_list *lc,
509  comment_list *tc);
510 
511  OCTINTERP_API tree_classdef_enum_list *
512  make_classdef_enum_list (tree_classdef_enum *e);
513 
514  OCTINTERP_API tree_classdef_enum *
515  make_classdef_enum (tree_identifier *id, tree_expression *expr,
516  comment_list *lc);
517 
518  OCTINTERP_API tree_classdef_enum_list *
519  append_classdef_enum (tree_classdef_enum_list *list,
520  tree_classdef_enum *elt);
521 
522  OCTINTERP_API tree_classdef_superclass_list *
523  make_classdef_superclass_list (tree_classdef_superclass *sc);
524 
525  OCTINTERP_API tree_classdef_superclass *
526  make_classdef_superclass (token *fqident);
527 
528  OCTINTERP_API tree_classdef_superclass_list *
529  append_classdef_superclass (tree_classdef_superclass_list *list,
531 
532  OCTINTERP_API tree_classdef_attribute_list *
533  make_classdef_attribute_list (tree_classdef_attribute *attr);
534 
535  OCTINTERP_API tree_classdef_attribute *
536  make_classdef_attribute (tree_identifier *id,
537  tree_expression *expr = nullptr);
538 
539  OCTINTERP_API tree_classdef_attribute *
540  make_not_classdef_attribute (tree_identifier *id);
541 
542  OCTINTERP_API tree_classdef_attribute_list *
543  append_classdef_attribute (tree_classdef_attribute_list *list,
545 
546  OCTINTERP_API tree_classdef_body *
547  make_classdef_body (tree_classdef_properties_block *pb);
548 
549  OCTINTERP_API tree_classdef_body *
550  make_classdef_body (tree_classdef_methods_block *mb);
551 
552  OCTINTERP_API tree_classdef_body *
553  make_classdef_body (tree_classdef_events_block *evb);
554 
555  OCTINTERP_API tree_classdef_body *
556  make_classdef_body (tree_classdef_enum_block *enb);
557 
558  OCTINTERP_API tree_classdef_body *
559  append_classdef_properties_block (tree_classdef_body *body,
561 
562  OCTINTERP_API tree_classdef_body *
563  append_classdef_methods_block (tree_classdef_body *body,
565 
566  OCTINTERP_API tree_classdef_body *
567  append_classdef_events_block (tree_classdef_body *body,
569 
570  OCTINTERP_API tree_classdef_body *
571  append_classdef_enum_block (tree_classdef_body *body,
572  tree_classdef_enum_block *block);
573 
574  OCTINTERP_API octave_user_function *
575  start_classdef_external_method (tree_identifier *id,
576  tree_parameter_list *pl);
577 
578  OCTINTERP_API tree_function_def *
579  finish_classdef_external_method (octave_user_function *fcn,
580  tree_parameter_list *ret_list,
581  comment_list *cl);
582 
583  OCTINTERP_API tree_classdef_methods_list *
584  make_classdef_methods_list (tree_function_def *fcn_def);
585 
586  OCTINTERP_API tree_classdef_methods_list *
587  append_classdef_method (tree_classdef_methods_list *list,
588  tree_function_def *fcn_def);
589 
590  OCTINTERP_API bool
591  finish_classdef_file (tree_classdef *cls,
592  tree_statement_list *local_fcns);
593 
594  // Make an index expression.
595  OCTINTERP_API tree_index_expression *
596  make_index_expression (tree_expression *expr,
597  tree_argument_list *args, char type);
598 
599  // Make an indirect reference expression.
600  OCTINTERP_API tree_index_expression *
601  make_indirect_ref (tree_expression *expr, const std::string&);
602 
603  // Make an indirect reference expression with dynamic field name.
604  OCTINTERP_API tree_index_expression *
605  make_indirect_ref (tree_expression *expr, tree_expression *field);
606 
607  // Make a declaration command.
608  OCTINTERP_API tree_decl_command *
609  make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst);
610 
611  OCTINTERP_API tree_decl_init_list *
612  make_decl_init_list (tree_decl_elt *elt);
613 
614  OCTINTERP_API tree_decl_elt *
615  make_decl_elt (tree_identifier *id, token *eq_op = nullptr,
616  tree_expression *expr = nullptr);
617 
618  OCTINTERP_API tree_decl_init_list *
619  append_decl_init_list (tree_decl_init_list *list, tree_decl_elt *elt);
620 
621  // Validate an function parameter list.
622  OCTINTERP_API bool
623  validate_param_list (tree_parameter_list *lst,
625  // Validate matrix or cell
626  OCTINTERP_API bool validate_array_list (tree_expression *e);
627 
628  // Validate matrix object used in "[lhs] = ..." assignments.
629  OCTINTERP_API tree_argument_list *
630  validate_matrix_for_assignment (tree_expression *e);
631 
632  // Finish building an array_list (common action for finish_matrix
633  // and finish_cell).
634  OCTINTERP_API tree_expression *
635  finish_array_list (tree_array_list *a, token *open_delim,
636  token *close_delim);
637 
638  // Finish building a matrix list.
639  OCTINTERP_API tree_expression *
640  finish_matrix (tree_matrix *m, token *open_delim, token *close_delim);
641 
642  // Finish building a cell list.
643  OCTINTERP_API tree_expression *
644  finish_cell (tree_cell *c, token *open_delim, token *close_delim);
645 
646  OCTINTERP_API tree_identifier *
647  make_identifier (token *ident);
648 
649  OCTINTERP_API tree_superclass_ref *
650  make_superclass_ref (token *superclassref);
651 
652  OCTINTERP_API tree_metaclass_query *
653  make_metaclass_query (token *metaquery);
654 
655  // Set the print flag for a statement based on the separator type.
656  OCTINTERP_API tree_statement_list *
657  set_stmt_print_flag (tree_statement_list *, char, bool);
658 
659  // Finish building a statement.
660  template <typename T>
661  OCTINTERP_API tree_statement * make_statement (T *arg);
662 
663  // Create a statement list.
664  OCTINTERP_API tree_statement_list *
665  make_statement_list (tree_statement *stmt);
666 
667  // Append a statement to an existing statement list.
668  OCTINTERP_API tree_statement_list *
669  append_statement_list (tree_statement_list *list, char sep,
670  tree_statement *stmt, bool warn_missing_semi);
671 
672  OCTINTERP_API tree_argument_list *
673  make_argument_list (tree_expression *expr);
674 
675  OCTINTERP_API tree_argument_list *
676  append_argument_list (tree_argument_list *list, tree_expression *expr);
677 
678  OCTINTERP_API tree_parameter_list *
679  make_parameter_list (tree_parameter_list::in_or_out io);
680 
681  OCTINTERP_API tree_parameter_list *
682  make_parameter_list (tree_parameter_list::in_or_out io, tree_decl_elt *t);
683 
684  OCTINTERP_API tree_parameter_list *
685  make_parameter_list (tree_parameter_list::in_or_out io,
686  tree_identifier *id);
687 
688  OCTINTERP_API tree_parameter_list *
689  append_parameter_list (tree_parameter_list *list, tree_decl_elt *t);
690 
691  OCTINTERP_API tree_parameter_list *
692  append_parameter_list (tree_parameter_list *list, tree_identifier *id);
693 
694  // Don't allow parsing command syntax. If the parser/lexer is
695  // reset, this setting is also reset to the default (allow command
696  // syntax).
697  OCTINTERP_API void disallow_command_syntax ();
698 
699  // Generic error messages.
700  OCTINTERP_API void bison_error (const std::string& s);
701  OCTINTERP_API void bison_error (const std::string& s, const filepos& pos);
702  OCTINTERP_API void bison_error (const std::string& s, int line, int column);
703  OCTINTERP_API void bison_error (const std::list<parse_exception>& pe);
704  OCTINTERP_API void bison_error (const parse_exception& pe);
705 
706  friend OCTINTERP_API octave_value
707  parse_fcn_file (interpreter& interp, const std::string& full_file,
708  const std::string& file, const std::string& dir_name,
709  const std::string& dispatch_type,
710  const std::string& package_name, bool require_file,
711  bool force_script, bool autoload, bool relative_lookup);
712 
713  // Thih interface allows push or pull parsers to be used
714  // equivalently, provided that the push parser also owns its input
715  // method (see below). Alternatively, the push parser interface may
716  // use a separate run method and completely separate input from
717  // lexical analysis and parsing.
718 
719  virtual int run () = 0;
720 
721  // Check primary script or function generated by the parser for
722  // semantic errors.
723  OCTINTERP_API bool validate_primary_fcn ();
724 
725  OCTINTERP_API bool finish_input (tree_statement_list *lst,
726  bool at_eof = false);
727 
728 protected:
729 
730  // Contains error message if Bison-generated parser returns nonzero
731  // status.
732  std::string m_parse_error_msg;
733 
734  // Have we found an explicit end to a function?
736 
737  // TRUE means we are in the process of autoloading a function.
739 
740  // TRUE means the current function file was found in a relative path
741  // element.
743 
744  // FALSE if we are still at the primary function. Subfunctions can
745  // only be declared inside function files.
747 
748  // TRUE if we are parsing local functions defined at after a
749  // classdef block. Local functions can only be declared inside
750  // classdef files.
752 
753  // Maximum function depth detected. Used to determine whether
754  // we have nested functions or just implicitly ended subfunctions.
756 
757  // = 0 currently outside any function.
758  // = 1 inside the primary function or a subfunction.
759  // > 1 means we are looking at a function definition that seems to be
760  // inside a function. Note that the function still might not be a
761  // nested function.
763 
764  // Scope where we install all subfunctions and nested functions. Only
765  // used while reading function files.
767 
768  // Name of the current class when we are parsing class methods or
769  // constructors.
770  std::string m_curr_class_name;
771 
772  // Name of the current package when we are parsing an element contained
773  // in a package directory (+-directory).
774  std::string m_curr_package_name;
775 
776  // Nested function scopes and names currently being parsed.
777  parent_scope_info m_function_scopes;
778 
779  // Pointer to the primary user function or user script function.
781 
782  // List of subfunction names, initially in the order they are
783  // installed in the symbol table, then ordered as they appear in the
784  // file. Eventually stashed in the primary function object.
785  std::list<std::string> m_subfunction_names;
786 
787  // Pointer to the classdef object we just parsed, if any.
788  std::shared_ptr<tree_classdef> m_classdef_object;
789 
790  // Result of parsing input.
791  std::shared_ptr <tree_statement_list> m_stmt_list;
792 
793  // State of the lexer.
795 
796  // Internal state of the Bison parser.
798 
799 private:
800 
801  // Maybe print a warning if an assignment expression is used as the
802  // test in a logical expression.
803  OCTINTERP_API void maybe_warn_assign_as_truth_value (tree_expression *expr);
804 
805  // Maybe print a warning about switch labels that aren't constants.
806  OCTINTERP_API void maybe_warn_variable_switch_label (tree_expression *expr);
807 
808  // Maybe print a warning.
809  OCTINTERP_API void maybe_warn_missing_semi (tree_statement_list *);
810 };
811 
812 // Publish externally used friend functions.
813 
814 extern OCTINTERP_API octave_value
815 parse_fcn_file (interpreter& interp, const std::string& full_file,
816  const std::string& file, const std::string& dir_name,
817  const std::string& dispatch_type,
818  const std::string& package_name, bool require_file,
819  bool force_script, bool autoload, bool relative_lookup);
820 
821 class parser : public base_parser
822 {
823 public:
824 
825  parser (interpreter& interp)
826  : base_parser (*(new lexer (interp)))
827  { }
828 
829  parser (FILE *file, interpreter& interp)
830  : base_parser (*(new lexer (file, interp)))
831  { }
832 
833  parser (FILE *file, interpreter& interp, std::string encoding)
834  : base_parser (*(new lexer (file, interp, encoding)))
835  { }
836 
837  parser (const std::string& eval_string, interpreter& interp)
838  : base_parser (*(new lexer (eval_string, interp)))
839  { }
840 
841  // The lexer must be allocated with new. The parser object
842  // takes ownership of and deletes the lexer object in its
843  // destructor.
844 
845  parser (lexer *lxr)
846  : base_parser (*lxr)
847  { }
848 
849  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (parser)
850 
851  ~parser () = default;
852 
853  OCTINTERP_API int run ();
854 };
855 
856 class push_parser : public base_parser
857 {
858 public:
859 
861  : base_parser (*(new push_lexer (interp))),
862  m_interpreter (interp), m_reader ()
863  { }
864 
865  // The parser assumes ownership of READER, which must be created
866  // with new.
867 
869  : base_parser (*(new push_lexer (interp))),
870  m_interpreter (interp), m_reader (reader)
871  { }
872 
873  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (push_parser)
874 
875  ~push_parser () = default;
876 
877  // Use the push parser in the same way as the pull parser. The
878  // parser arranges for input through the M_READER object. See, for
879  // example, interpreter::main_loop.
880 
881  OCTINTERP_API int run ();
882 
883  // Parse INPUT. M_READER is not used. The user is responsible for
884  // collecting input.
885 
886  OCTINTERP_API int run (const std::string& input, bool eof);
887 
888 private:
889 
890  interpreter& m_interpreter;
891 
892  std::shared_ptr<input_reader> m_reader;
893 };
894 
895 extern OCTINTERP_API std::string
896 get_help_from_file (const std::string& nm, bool& symbol_found,
897  std::string& file);
898 
899 extern OCTINTERP_API std::string
900 get_help_from_file (const std::string& nm, bool& symbol_found);
901 
902 extern OCTINTERP_API octave_value
903 load_fcn_from_file (const std::string& file_name,
904  const std::string& dir_name = "",
905  const std::string& dispatch_type = "",
906  const std::string& package_name = "",
907  const std::string& fcn_name = "",
908  bool autoload = false);
909 
910 extern OCTINTERP_API void
911 source_file (const std::string& file_name,
912  const std::string& context = "",
913  bool verbose = false, bool require_file = true);
914 
915 extern OCTINTERP_API octave_value_list
916 feval (const char *name,
917  const octave_value_list& args = octave_value_list (),
918  int nargout = 0);
919 
920 extern OCTINTERP_API octave_value_list
921 feval (const std::string& name,
922  const octave_value_list& args = octave_value_list (),
923  int nargout = 0);
924 
925 extern OCTINTERP_API octave_value_list
927  const octave_value_list& args = octave_value_list (),
928  int nargout = 0);
929 
930 extern OCTINTERP_API octave_value_list
931 feval (const octave_value& val,
932  const octave_value_list& args = octave_value_list (),
933  int nargout = 0);
934 
935 extern OCTINTERP_API octave_value_list
936 feval (const octave_value_list& args, int nargout = 0);
937 
938 OCTAVE_END_NAMESPACE(octave)
939 
940 #endif
std::shared_ptr< tree_statement_list > statement_list() const
Definition: parse.h:186
std::shared_ptr< tree_statement_list > m_stmt_list
Definition: parse.h:791
octave_user_function * frob_function(tree_identifier *id, octave_user_function *fcn)
int m_max_fcn_depth
Definition: parse.h:755
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:780
parent_scope_info m_function_scopes
Definition: parse.h:777
void parsing_subfunctions(bool flag)
Definition: parse.h:191
bool m_endfunction_found
Definition: parse.h:735
base_lexer & m_lexer
Definition: parse.h:794
std::string m_curr_class_name
Definition: parse.h:770
bool m_parsing_local_functions
Definition: parse.h:751
std::string m_curr_package_name
Definition: parse.h:774
bool m_fcn_file_from_relative_lookup
Definition: parse.h:742
symbol_scope m_primary_fcn_scope
Definition: parse.h:766
int curr_fcn_depth() const
Definition: parse.h:211
bool at_end_of_input() const
Definition: parse.h:170
std::list< std::string > m_subfunction_names
Definition: parse.h:785
bool m_autoloading
Definition: parse.h:738
void parsing_local_functions(bool flag)
Definition: parse.h:201
bool parsing_subfunctions() const
Definition: parse.h:196
std::shared_ptr< tree_classdef > classdef_object() const
Definition: parse.h:179
bool m_parsing_subfunctions
Definition: parse.h:746
base_lexer & get_lexer() const
Definition: parse.h:168
void endfunction_found(bool flag)
Definition: parse.h:216
void * m_parser_state
Definition: parse.h:797
virtual int run()=0
std::shared_ptr< tree_classdef > m_classdef_object
Definition: parse.h:788
int m_curr_fcn_depth
Definition: parse.h:762
void classdef_object(const std::shared_ptr< tree_classdef > &obj)
Definition: parse.h:174
std::string m_parse_error_msg
Definition: parse.h:732
Definition: lex.h:760
Definition: parse.h:822
~parser()=default
int run()
Definition: oct-parse.cc:9748
parser(const std::string &eval_string, interpreter &interp)
Definition: parse.h:837
parser(interpreter &interp)
Definition: parse.h:825
parser(FILE *file, interpreter &interp, std::string encoding)
Definition: parse.h:833
parser(FILE *file, interpreter &interp)
Definition: parse.h:829
parser(lexer *lxr)
Definition: parse.h:845
push_parser(interpreter &interp)
Definition: parse.h:860
~push_parser()=default
push_parser(interpreter &interp, input_reader *reader)
Definition: parse.h:868
Definition: token.h:39
end_tok_type
Definition: token.h:53
Definition: pt.h:45
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
T octave_idx_type m
Definition: mx-inlines.cc:781
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)
Definition: oct-parse.cc:9903
octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
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