GNU Octave  4.0.0
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-2015 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 <cstdio>
27 
28 #include <string>
29 
30 #include <stack>
31 #include <vector>
32 #include <map>
33 
34 #include "lex.h"
35 #include "symtab.h"
36 #include "token.h"
37 
39 class octave_function;
41 class tree;
43 class tree_argument_list;
44 class tree_array_list;
45 class tree_cell;
46 class tree_classdef;
48 class tree_classdef_body;
59 class tree_command;
60 class tree_constant;
61 class tree_decl_command;
63 class tree_expression;
64 class tree_fcn_handle;
65 class tree_funcall;
66 class tree_function_def;
67 class tree_identifier;
68 class tree_if_clause;
69 class tree_if_command;
72 class tree_matrix;
73 class tree_matrix;
75 class tree_statement;
77 class tree_statement_listtree_statement;
78 class tree_switch_case;
81 
82 #include "oct-obj.h"
83 
84 // Nonzero means print parser debugging info (-d).
85 extern int octave_debug;
86 
87 // TRUE means we printed messages about reading startup files.
89 
90 extern OCTINTERP_API std::string
91 get_help_from_file (const std::string& nm, bool& symbol_found,
92  std::string& file);
93 
94 extern OCTINTERP_API std::string
95 get_help_from_file (const std::string& nm, bool& symbol_found);
96 
97 extern OCTINTERP_API std::string lookup_autoload (const std::string& nm);
98 
100 
102 reverse_lookup_autoload (const std::string& nm);
103 
105 load_fcn_from_file (const std::string& file_name,
106  const std::string& dir_name = std::string (),
107  const std::string& dispatch_type = std::string (),
108  const std::string& package_name = std::string (),
109  const std::string& fcn_name = std::string (),
110  bool autoload = false);
111 
112 extern OCTINTERP_API void
113 source_file (const std::string& file_name,
114  const std::string& context = std::string (),
115  bool verbose = false, bool require_file = true,
116  const std::string& warn_for = std::string ());
117 
119 feval (const std::string& name,
120  const octave_value_list& args = octave_value_list (),
121  int nargout = 0);
122 
124 feval (octave_function *fcn,
125  const octave_value_list& args = octave_value_list (),
126  int nargout = 0);
127 
129 feval (const octave_value_list& args, int nargout = 0);
130 
132 eval_string (const std::string&, bool silent, int& parse_status, int hargout);
133 
135 eval_string (const std::string&, bool silent, int& parse_status);
136 
138 
139 // Global access to currently active lexer.
140 // FIXME: to be removed after more parser+lexer refactoring.
141 extern octave_base_lexer *LEXER;
142 
143 class
145 {
146 public:
147 
149  : endfunction_found (false),
150  autoloading (false), fcn_file_from_relative_lookup (false),
151  parsing_subfunctions (false), max_fcn_depth (0),
152  curr_fcn_depth (0), primary_fcn_scope (-1),
153  curr_class_name (), curr_package_name (), function_scopes (),
154  primary_fcn_ptr (0), subfunction_names (), classdef_object (0),
155  stmt_list (0), lexer (lxr)
156  { }
157 
158  ~octave_base_parser (void);
159 
160  void reset (void);
161 
162  // Error mesages for mismatched end tokens.
163  void end_error (const char *type, token::end_tok_type ettype, int l, int c);
164 
165  // Check to see that end tokens are properly matched.
166  bool end_token_ok (token *tok, token::end_tok_type expected);
167 
168  // Maybe print a warning if an assignment expression is used as the
169  // test in a logical expression.
170  void maybe_warn_assign_as_truth_value (tree_expression *expr);
171 
172  // Maybe print a warning about switch labels that aren't constants.
173  void maybe_warn_variable_switch_label (tree_expression *expr);
174 
175  // Finish building a range.
176  tree_expression *finish_colon_expression (tree_colon_expression *e);
177 
178  // Build a constant.
179  tree_constant *make_constant (int op, token *tok_val);
180 
181  // Build a function handle.
183 
184  // Build an anonymous function handle.
186  make_anon_fcn_handle (tree_parameter_list *param_list, tree_statement *stmt);
187 
188  // Build a binary expression.
190  make_binary_op (int op, tree_expression *op1, token *tok_val,
191  tree_expression *op2);
192 
193  // Build a boolean expression.
195  make_boolean_op (int op, tree_expression *op1, token *tok_val,
196  tree_expression *op2);
197 
198  // Build a prefix expression.
200  make_prefix_op (int op, tree_expression *op1, token *tok_val);
201 
202  // Build a postfix expression.
204  make_postfix_op (int op, tree_expression *op1, token *tok_val);
205 
206  // Build an unwind-protect command.
207  tree_command *
208  make_unwind_command (token *unwind_tok, tree_statement_list *body,
209  tree_statement_list *cleanup, token *end_tok,
211 
212  // Build a try-catch command.
213  tree_command *
214  make_try_command (token *try_tok, tree_statement_list *body,
215  char catch_sep, tree_statement_list *cleanup,
216  token *end_tok, octave_comment_list *lc,
217  octave_comment_list *mc);
218 
219  // Build a while command.
220  tree_command *
221  make_while_command (token *while_tok, tree_expression *expr,
222  tree_statement_list *body, token *end_tok,
223  octave_comment_list *lc);
224 
225  // Build a do-until command.
226  tree_command *
227  make_do_until_command (token *until_tok, tree_statement_list *body,
229 
230  // Build a for command.
231  tree_command *
232  make_for_command (int tok_id, token *for_tok, tree_argument_list *lhs,
234  tree_statement_list *body, token *end_tok,
235  octave_comment_list *lc);
236 
237  // Build a break command.
238  tree_command *make_break_command (token *break_tok);
239 
240  // Build a continue command.
241  tree_command *make_continue_command (token *continue_tok);
242 
243  // Build a return command.
244  tree_command *make_return_command (token *return_tok);
245 
246  // Start an if command.
248  start_if_command (tree_expression *expr, tree_statement_list *list);
249 
250  // Finish an if command.
252  finish_if_command (token *if_tok, tree_if_command_list *list,
253  token *end_tok, octave_comment_list *lc);
254 
255  // Build an elseif clause.
257  make_elseif_clause (token *elseif_tok, tree_expression *expr,
259 
260  // Finish a switch command.
262  finish_switch_command (token *switch_tok, tree_expression *expr,
263  tree_switch_case_list *list, token *end_tok,
264  octave_comment_list *lc);
265 
266  // Build a switch case.
268  make_switch_case (token *case_tok, tree_expression *expr,
270 
271  // Build an assignment to a variable.
273  make_assign_op (int op, tree_argument_list *lhs, token *eq_tok,
274  tree_expression *rhs);
275 
276  // Define a script.
277  void make_script (tree_statement_list *cmds, tree_statement *end_script);
278 
279  // Begin defining a function.
281  start_function (tree_parameter_list *param_list, tree_statement_list *body,
282  tree_statement *end_function);
283 
284  // Create a no-op statement for end_function.
285  tree_statement *make_end (const std::string& type, bool eof, int l, int c);
286 
287  // Do most of the work for defining a function.
289  frob_function (const std::string& fname, octave_user_function *fcn);
290 
291  // Finish defining a function.
293  finish_function (tree_parameter_list *ret_list,
295  int l, int c);
296 
297  // Reset state after parsing function.
298  void
299  recover_from_parsing_function (void);
300 
301  tree_funcall *
302  make_superclass_ref (const std::string& method_nm,
303  const std::string& class_nm);
304 
305  tree_funcall *
306  make_meta_class_query (const std::string& class_nm);
307 
308  tree_classdef *
309  make_classdef (token *tok_val, tree_classdef_attribute_list *a,
311  tree_classdef_body *body, token *end_tok,
312  octave_comment_list *lc);
313 
315  make_classdef_properties_block (token *tok_val,
318  token *end_tok, octave_comment_list *lc);
319 
321  make_classdef_methods_block (token *tok_val,
324  token *end_tok, octave_comment_list *lc);
325 
327  make_classdef_events_block (token *tok_val,
330  token *end_tok, octave_comment_list *lc);
331 
333  make_classdef_enum_block (token *tok_val,
336  token *end_tok, octave_comment_list *lc);
337 
339  start_classdef_external_method (tree_identifier *id,
340  tree_parameter_list *pl);
341 
343  finish_classdef_external_method (octave_user_function *fcn,
344  tree_parameter_list *ret_list,
345  octave_comment_list *cl);
346 
347  // Make an index expression.
349  make_index_expression (tree_expression *expr,
350  tree_argument_list *args, char type);
351 
352  // Make an indirect reference expression.
354  make_indirect_ref (tree_expression *expr, const std::string&);
355 
356  // Make an indirect reference expression with dynamic field name.
358  make_indirect_ref (tree_expression *expr, tree_expression *field);
359 
360  // Make a declaration command.
362  make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst);
363 
364  // Validate matrix or cell
365  bool validate_array_list (tree_expression *e);
366 
367  // Validate matrix object used in "[lhs] = ..." assignments.
368  tree_argument_list *validate_matrix_for_assignment (tree_expression *e);
369 
370  // Finish building an array_list (common action for finish_matrix
371  // and finish_cell).
372  tree_expression *finish_array_list (tree_array_list *a);
373 
374  // Finish building a matrix list.
375  tree_expression *finish_matrix (tree_matrix *m);
376 
377  // Finish building a cell list.
378  tree_expression *finish_cell (tree_cell *c);
379 
380  // Maybe print a warning. Duh.
381  void maybe_warn_missing_semi (tree_statement_list *);
382 
383  // Set the print flag for a statement based on the separator type.
385  set_stmt_print_flag (tree_statement_list *, char, bool);
386 
387  // Finish building a statement.
388  template <class T>
389  tree_statement *make_statement (T *arg);
390 
391  // Create a statement list.
392  tree_statement_list *make_statement_list (tree_statement *stmt);
393 
394  // Append a statement to an existing statement list.
396  append_statement_list (tree_statement_list *list, char sep,
397  tree_statement *stmt, bool warn_missing_semi);
398 
399  // Generic error messages.
400  void bison_error (const char *s);
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.
433  std::string curr_class_name;
434 
435  // Name of the current package when we are parsing an element contained
436  // in a package directory (+-directory).
437  std::string curr_package_name;
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 private:
462 
463  // No copying!
464 
466 
468 };
469 
470 class
472 {
473 public:
474 
476  : octave_base_parser (*(new octave_lexer ()))
477  { }
478 
479  octave_parser (FILE *file)
480  : octave_base_parser (*(new octave_lexer (file)))
481  { }
482 
483  octave_parser (const std::string& eval_string)
484  : octave_base_parser (*(new octave_lexer (eval_string)))
485  { }
486 
488  : octave_base_parser (lxr)
489  { }
490 
491  ~octave_parser (void) { }
492 
493  int run (void);
494 
495 private:
496 
497  // No copying!
498 
499  octave_parser (const octave_parser&);
500 
502 };
503 
504 class
506 {
507 public:
508 
510  : octave_base_parser (*(new octave_push_lexer ())), parser_state (0)
511  {
512  init ();
513  }
514 
515  ~octave_push_parser (void);
516 
517  void init (void);
518 
519  int run (const std::string& input, bool eof);
520 
521 private:
522 
523  // Internal state of the Bison parser.
525 
526  // No copying!
527 
529 
531 };
532 
533 #endif
std::list< std::string > subfunction_names
Definition: parse.h:450
octave_parser(const std::string &eval_string)
Definition: parse.h:483
std::string curr_package_name
Definition: parse.h:437
bool reading_startup_message_printed
Definition: oct-parse.cc:149
tree_switch_command & operator=(const tree_switch_command &)
std::string curr_class_name
Definition: parse.h:433
octave_parser(void)
Definition: parse.h:475
octave_base_lexer * LEXER
Definition: oct-parse.cc:137
OCTINTERP_API octave_value_list feval(const std::string &name, const octave_value_list &args=octave_value_list(), int nargout=0)
Definition: oct-parse.cc:8625
octave_parser(FILE *file)
Definition: parse.h:479
OCTINTERP_API octave_function * load_fcn_from_file(const std::string &file_name, const std::string &dir_name=std::string(), const std::string &dispatch_type=std::string(), const std::string &package_name=std::string(), const std::string &fcn_name=std::string(), bool autoload=false)
Definition: oct-parse.cc:8198
OCTINTERP_API std::string get_help_from_file(const std::string &nm, bool &symbol_found, std::string &file)
Definition: oct-parse.cc:8107
tree_classdef * classdef_object
Definition: parse.h:453
void * parser_state
Definition: parse.h:524
OCTINTERP_API string_vector reverse_lookup_autoload(const std::string &nm)
Definition: oct-parse.cc:8185
tree_switch_case_list * list
Definition: pt-select.h:287
octave_parser(octave_lexer &lxr)
Definition: parse.h:487
#define OCTINTERP_API
Definition: mexproto.h:66
#define lexer
Definition: oct-parse.cc:170
static llvm::LLVMContext & context
Definition: jit-typeinfo.cc:76
OCTINTERP_API std::string lookup_autoload(const std::string &nm)
Definition: oct-parse.cc:8157
end_tok_type
Definition: token.h:46
OCTINTERP_API string_vector autoloaded_functions(void)
Definition: oct-parse.cc:8172
octave_function * primary_fcn_ptr
Definition: parse.h:445
OCTINTERP_API octave_value_list eval_string(const std::string &, bool silent, int &parse_status, int hargout)
Definition: oct-parse.cc:8810
bool parsing_subfunctions
Definition: parse.h:414
octave_base_parser(octave_base_lexer &lxr)
Definition: parse.h:148
~octave_parser(void)
Definition: parse.h:491
double arg(double x)
Definition: lo-mappers.h:37
octave_base_lexer & lexer
Definition: parse.h:459
symbol_table::scope_id primary_fcn_scope
Definition: parse.h:429
bool autoloading
Definition: parse.h:406
std::vector< symbol_table::scope_id > function_scopes
Definition: parse.h:442
tree_expression * expr
Definition: pt-select.h:284
Definition: pt.h:35
bool endfunction_found
Definition: parse.h:403
static octave_value make_fcn_handle(octave_builtin::fcn ff, const std::string &nm)
Definition: ov-classdef.cc:129
OCTINTERP_API void source_file(const std::string &file_name, const std::string &context=std::string(), bool verbose=false, bool require_file=true, const std::string &warn_for=std::string())
Definition: oct-parse.cc:8405
tree_statement_list * stmt_list
Definition: parse.h:456
bool fcn_file_from_relative_lookup
Definition: parse.h:410
int curr_fcn_depth
Definition: parse.h:425
static int input(yyscan_t yyscanner)
int octave_debug
OCTINTERP_API void cleanup_statement_list(tree_statement_list **lst)
Definition: oct-parse.cc:8907
octave_push_parser(void)
Definition: parse.h:509
Definition: token.h:30