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