GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
parse.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2017 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_parse_h)
24 #define octave_parse_h 1
25 
26 #include "octave-config.h"
27 
28 #include <cstdio>
29 
30 #include <string>
31 
32 #include <stack>
33 #include <vector>
34 #include <map>
35 
36 #include "lex.h"
37 #include "symtab.h"
38 #include "token.h"
39 
41 class octave_function;
43 class tree;
45 class tree_argument_list;
46 class tree_array_list;
47 class tree_cell;
48 class tree_classdef;
50 class tree_classdef_body;
61 class tree_command;
62 class tree_constant;
63 class tree_decl_command;
65 class tree_expression;
66 class tree_fcn_handle;
67 class tree_funcall;
68 class tree_function_def;
69 class tree_identifier;
70 class tree_if_clause;
71 class tree_if_command;
74 class tree_matrix;
75 class tree_matrix;
77 class tree_statement;
79 class tree_statement_listtree_statement;
80 class tree_switch_case;
83 
84 #include "ovl.h"
85 
86 // Nonzero means print parser debugging info (-d).
87 extern int octave_debug;
88 
89 // TRUE means we printed messages about reading startup files.
91 
93 get_help_from_file (const std::string& nm, bool& symbol_found,
94  std::string& file);
95 
97 get_help_from_file (const std::string& nm, bool& symbol_found);
98 
100 
102 
105 
107 load_fcn_from_file (const std::string& file_name,
108  const std::string& dir_name = "",
109  const std::string& dispatch_type = "",
110  const std::string& package_name = "",
111  const std::string& fcn_name = "",
112  bool autoload = false);
113 
114 extern OCTINTERP_API void
115 source_file (const std::string& file_name,
116  const std::string& context = "",
117  bool verbose = false, bool require_file = true,
118  const std::string& warn_for = "");
119 
121 feval (const std::string& name,
123  int nargout = 0);
124 
128  int nargout = 0);
129 
131 feval (const octave_value_list& args, int nargout = 0);
132 
134 eval_string (const std::string&, bool silent, int& parse_status, int nargout);
135 
137 eval_string (const std::string&, bool silent, int& parse_status);
138 
140 
141 namespace octave
142 {
143  // Global access to currently active lexer.
144  // FIXME: to be removed after more parser+lexer refactoring.
145  extern base_lexer *LEXER;
146 
147  class
149  {
150  public:
151 
152  base_parser (base_lexer& lxr);
153 
154  ~base_parser (void);
155 
156  void reset (void);
157 
158  // Error mesages for mismatched end tokens.
159  void end_token_error (token *tok, token::end_tok_type expected);
160 
161  // Check to see that end tokens are properly matched.
162  bool end_token_ok (token *tok, token::end_tok_type expected);
163 
164  // Maybe print a warning if an assignment expression is used as the
165  // test in a logical expression.
166  void maybe_warn_assign_as_truth_value (tree_expression *expr);
167 
168  // Maybe print a warning about switch labels that aren't constants.
169  void maybe_warn_variable_switch_label (tree_expression *expr);
170 
171  // Finish building a range.
172  tree_expression *finish_colon_expression (tree_colon_expression *e);
173 
174  // Build a constant.
175  tree_constant *make_constant (int op, token *tok_val);
176 
177  // Build a function handle.
179 
180  // Build an anonymous function handle.
182  make_anon_fcn_handle (tree_parameter_list *param_list, tree_statement *stmt);
183 
184  // Build a binary expression.
186  make_binary_op (int op, tree_expression *op1, token *tok_val,
187  tree_expression *op2);
188 
189  // Build a boolean expression.
191  make_boolean_op (int op, tree_expression *op1, token *tok_val,
192  tree_expression *op2);
193 
194  // Build a prefix expression.
196  make_prefix_op (int op, tree_expression *op1, token *tok_val);
197 
198  // Build a postfix expression.
200  make_postfix_op (int op, tree_expression *op1, token *tok_val);
201 
202  // Build an unwind-protect command.
203  tree_command *
204  make_unwind_command (token *unwind_tok, tree_statement_list *body,
205  tree_statement_list *cleanup, token *end_tok,
207 
208  // Build a try-catch command.
209  tree_command *
210  make_try_command (token *try_tok, tree_statement_list *body,
211  char catch_sep, tree_statement_list *cleanup,
212  token *end_tok, octave_comment_list *lc,
213  octave_comment_list *mc);
214 
215  // Build a while command.
216  tree_command *
217  make_while_command (token *while_tok, tree_expression *expr,
218  tree_statement_list *body, token *end_tok,
219  octave_comment_list *lc);
220 
221  // Build a do-until command.
222  tree_command *
223  make_do_until_command (token *until_tok, tree_statement_list *body,
225 
226  // Build a for command.
227  tree_command *
228  make_for_command (int tok_id, token *for_tok, tree_argument_list *lhs,
229  tree_expression *expr, tree_expression *maxproc,
230  tree_statement_list *body, token *end_tok,
231  octave_comment_list *lc);
232 
233  // Build a break command.
234  tree_command *make_break_command (token *break_tok);
235 
236  // Build a continue command.
237  tree_command *make_continue_command (token *continue_tok);
238 
239  // Build a return command.
240  tree_command *make_return_command (token *return_tok);
241 
242  // Start an if command.
244  start_if_command (tree_expression *expr, tree_statement_list *list);
245 
246  // Finish an if command.
248  finish_if_command (token *if_tok, tree_if_command_list *list,
249  token *end_tok, octave_comment_list *lc);
250 
251  // Build an elseif clause.
253  make_elseif_clause (token *elseif_tok, tree_expression *expr,
255 
256  // Finish a switch command.
258  finish_switch_command (token *switch_tok, tree_expression *expr,
259  tree_switch_case_list *list, token *end_tok,
260  octave_comment_list *lc);
261 
262  // Build a switch case.
264  make_switch_case (token *case_tok, tree_expression *expr,
266 
267  // Build an assignment to a variable.
269  make_assign_op (int op, tree_argument_list *lhs, token *eq_tok,
270  tree_expression *rhs);
271 
272  // Define a script.
273  void make_script (tree_statement_list *cmds, tree_statement *end_script);
274 
275  // Begin defining a function.
277  start_function (tree_parameter_list *param_list, tree_statement_list *body,
278  tree_statement *end_function);
279 
280  // Create a no-op statement for end_function.
281  tree_statement *make_end (const std::string& type, bool eof, int l, int c);
282 
283  // Do most of the work for defining a function.
285  frob_function (const std::string& fname, octave_user_function *fcn);
286 
287  // Finish defining a function.
289  finish_function (tree_parameter_list *ret_list,
291  int l, int c);
292 
293  // Reset state after parsing function.
294  void
295  recover_from_parsing_function (void);
296 
297  tree_funcall *
298  make_superclass_ref (const std::string& method_nm,
299  const std::string& class_nm);
300 
301  tree_funcall *
302  make_meta_class_query (const std::string& class_nm);
303 
304  tree_classdef *
305  make_classdef (token *tok_val, tree_classdef_attribute_list *a,
307  tree_classdef_body *body, token *end_tok,
308  octave_comment_list *lc);
309 
311  make_classdef_properties_block (token *tok_val,
314  token *end_tok, octave_comment_list *lc);
315 
317  make_classdef_methods_block (token *tok_val,
320  token *end_tok, octave_comment_list *lc);
321 
323  make_classdef_events_block (token *tok_val,
326  token *end_tok, octave_comment_list *lc);
327 
329  make_classdef_enum_block (token *tok_val,
332  token *end_tok, octave_comment_list *lc);
333 
335  start_classdef_external_method (tree_identifier *id,
336  tree_parameter_list *pl);
337 
339  finish_classdef_external_method (octave_user_function *fcn,
340  tree_parameter_list *ret_list,
341  octave_comment_list *cl);
342 
343  // Make an index expression.
345  make_index_expression (tree_expression *expr,
346  tree_argument_list *args, char type);
347 
348  // Make an indirect reference expression.
350  make_indirect_ref (tree_expression *expr, const std::string&);
351 
352  // Make an indirect reference expression with dynamic field name.
354  make_indirect_ref (tree_expression *expr, tree_expression *field);
355 
356  // Make a declaration command.
358  make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst);
359 
360  // Validate matrix or cell
361  bool validate_array_list (tree_expression *e);
362 
363  // Validate matrix object used in "[lhs] = ..." assignments.
364  tree_argument_list *validate_matrix_for_assignment (tree_expression *e);
365 
366  // Finish building an array_list (common action for finish_matrix
367  // and finish_cell).
368  tree_expression *finish_array_list (tree_array_list *a);
369 
370  // Finish building a matrix list.
371  tree_expression *finish_matrix (tree_matrix *m);
372 
373  // Finish building a cell list.
374  tree_expression *finish_cell (tree_cell *c);
375 
376  // Maybe print a warning. Duh.
377  void maybe_warn_missing_semi (tree_statement_list *);
378 
379  // Set the print flag for a statement based on the separator type.
381  set_stmt_print_flag (tree_statement_list *, char, bool);
382 
383  // Finish building a statement.
384  template <typename T>
385  tree_statement *make_statement (T *arg);
386 
387  // Create a statement list.
388  tree_statement_list *make_statement_list (tree_statement *stmt);
389 
390  // Append a statement to an existing statement list.
392  append_statement_list (tree_statement_list *list, char sep,
393  tree_statement *stmt, bool warn_missing_semi);
394 
395  // Generic error messages.
396  void bison_error (const std::string& s, int l = -1, int c = -1);
397 
398  // Contains error message if Bison-generated parser returns non-zero
399  // status.
401 
402  // Have we found an explicit end to a function?
404 
405  // TRUE means we are in the process of autoloading a function.
407 
408  // TRUE means the current function file was found in a relative path
409  // element.
411 
412  // FALSE if we are still at the primary function. Subfunctions can
413  // only be declared inside function files.
415 
416  // Maximum function depth detected. Used to determine whether
417  // we have nested functions or just implicitly ended subfunctions.
419 
420  // = 0 currently outside any function.
421  // = 1 inside the primary function or a subfunction.
422  // > 1 means we are looking at a function definition that seems to be
423  // inside a function. Note that the function still might not be a
424  // nested function.
426 
427  // Scope where we install all subfunctions and nested functions. Only
428  // used while reading function files.
430 
431  // Name of the current class when we are parsing class methods or
432  // constructors.
434 
435  // Name of the current package when we are parsing an element contained
436  // in a package directory (+-directory).
438 
439  // A stack holding the nested function scopes being parsed.
440  // We don't use std::stack, because we want the clear method. Also, we
441  // must access one from the top
442  std::vector<symbol_table::scope_id> function_scopes;
443 
444  // Pointer to the primary user function or user script function.
446 
447  // List of subfunction names, initially in the order they are
448  // installed in the symbol table, then ordered as they appear in the
449  // file. Eventually stashed in the primary function object.
450  std::list<std::string> subfunction_names;
451 
452  // Pointer to the classdef object we just parsed, if any.
454 
455  // Result of parsing input.
457 
458  // State of the lexer.
460 
461  // Internal state of the Bison parser.
463 
464  private:
465 
466  // No copying!
467 
468  base_parser (const base_parser&);
469 
470  base_parser& operator = (const base_parser&);
471  };
472 
473  class
474  parser : public base_parser
475  {
476  public:
477 
478  parser (void)
479  : base_parser (*(new octave::lexer ()))
480  { }
481 
482  parser (FILE *file)
483  : base_parser (*(new octave::lexer (file)))
484  { }
485 
487  : base_parser (*(new octave::lexer (eval_string)))
488  { }
489 
491  : base_parser (lxr)
492  { }
493 
494  ~parser (void) { }
495 
496  int run (void);
497 
498  private:
499 
500  // No copying!
501 
502  parser (const parser&);
503 
504  parser& operator = (const parser&);
505  };
506 
507  class
508  push_parser : public base_parser
509  {
510  public:
511 
512  push_parser (void)
513  : base_parser (*(new octave::push_lexer ()))
514  { }
515 
516  ~push_parser (void) { }
517 
518  int run (const std::string& input, bool eof);
519 
520  private:
521 
522  // No copying!
523 
524  push_parser (const push_parser&);
525 
526  push_parser& operator = (const push_parser&);
527  };
528 }
529 
530 #endif
For example cd octave end example noindent changes the current working directory to an error message is printed and the working directory is not changed sc
Definition: dirfns.cc:120
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:120
bool reading_startup_message_printed
Definition: oct-parse.cc:142
fname
Definition: load-save.cc:754
The value of lines which begin with a space character are not saved in the history list A value of all commands are saved on the history list
Definition: oct-hist.cc:728
std::string curr_package_name
Definition: parse.h:437
base_lexer & lexer
Definition: parse.h:459
OCTINTERP_API octave_value_list feval(const std::string &name, const octave_value_list &args=octave_value_list(), int nargout=0)
parser(octave::lexer &lxr)
Definition: parse.h:490
parser(FILE *file)
Definition: parse.h:482
OCTINTERP_API std::string get_help_from_file(const std::string &nm, bool &symbol_found, std::string &file)
Definition: oct-parse.cc:8169
OCTAVE_EXPORT octave_value_list return the value of the option it must match the dimension of the state and the relative tolerance must also be a vector of the same length tem it must match the dimension of the state and the absolute tolerance must also be a vector of the same length The local error test applied at each integration step is xample roup calculate Y_a and Y _d item Given calculate Y nd enumerate In either initial values for the given components are input
Definition: DASPK-opts.cc:739
s
Definition: file-io.cc:2682
bool fcn_file_from_relative_lookup
Definition: parse.h:410
i e
Definition: data.cc:2724
octave_value arg
Definition: pr-output.cc:3440
octave_function * fcn
Definition: ov-class.cc:1743
OCTINTERP_API string_vector reverse_lookup_autoload(const std::string &nm)
Definition: oct-parse.cc:8247
~push_parser(void)
Definition: parse.h:516
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:398
JNIEnv void * args
Definition: ov-java.cc:67
bool verbose
Definition: load-save.cc:654
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:941
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
Definition: ov-usr-fcn.cc:935
#define OCTINTERP_API
Definition: mexproto.h:69
std::list< std::string > subfunction_names
Definition: parse.h:450
static llvm::LLVMContext & context
Definition: jit-typeinfo.cc:76
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
OCTINTERP_API std::string lookup_autoload(const std::string &nm)
Definition: oct-parse.cc:8219
OCTINTERP_API octave_function * 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:8260
end_tok_type
Definition: token.h:48
OCTINTERP_API string_vector autoloaded_functions(void)
Definition: oct-parse.cc:8234
idx type
Definition: ov.cc:3129
parser(void)
Definition: parse.h:478
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
base_lexer * LEXER
Definition: oct-parse.cc:139
tree_classdef * classdef_object
Definition: parse.h:453
push_parser(void)
Definition: parse.h:512
std::string parse_error_msg
Definition: parse.h:400
parser(const std::string &eval_string)
Definition: parse.h:486
std::string curr_class_name
Definition: parse.h:433
symbol_table::scope_id primary_fcn_scope
Definition: parse.h:429
bool parsing_subfunctions
Definition: parse.h:414
bool endfunction_found
Definition: parse.h:403
Definition: pt.h:39
static octave_value make_fcn_handle(octave_builtin::fcn ff, const std::string &nm)
Definition: ov-classdef.cc:127
std::vector< symbol_table::scope_id > function_scopes
Definition: parse.h:442
tree_statement_list * stmt_list
Definition: parse.h:456
int octave_debug
void * parser_state
Definition: parse.h:462
OCTINTERP_API octave_value_list eval_string(const std::string &, bool silent, int &parse_status, int nargout)
end example noindent will load the function or usages such as example autoload("foo", file_in_loadpath("bar.oct"))@end example @noindent are strongly discouraged
octave_function * primary_fcn_ptr
Definition: parse.h:445
~parser(void)
Definition: parse.h:494
OCTINTERP_API void cleanup_statement_list(tree_statement_list **lst)
OCTINTERP_API void source_file(const std::string &file_name, const std::string &context="", bool verbose=false, bool require_file=true, const std::string &warn_for="")
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
Definition: token.h:32