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