GNU Octave  3.8.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-2013 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;
47 class tree_command;
48 class tree_constant;
49 class tree_decl_command;
51 class tree_expression;
52 class tree_fcn_handle;
53 class tree_function_def;
54 class tree_identifier;
55 class tree_if_clause;
56 class tree_if_command;
59 class tree_matrix;
60 class tree_matrix;
62 class tree_statement;
64 class tree_statement_listtree_statement;
65 class tree_switch_case;
68 
69 #include "oct-obj.h"
70 
71 // Nonzero means print parser debugging info (-d).
72 extern int octave_debug;
73 
74 // TRUE means we printed messages about reading startup files.
76 
77 extern OCTINTERP_API std::string
78 get_help_from_file (const std::string& nm, bool& symbol_found,
79  std::string& file);
80 
81 extern OCTINTERP_API std::string
82 get_help_from_file (const std::string& nm, bool& symbol_found);
83 
84 extern OCTINTERP_API std::string lookup_autoload (const std::string& nm);
85 
87 
89 reverse_lookup_autoload (const std::string& nm);
90 
92 load_fcn_from_file (const std::string& file_name,
93  const std::string& dir_name = std::string (),
94  const std::string& dispatch_type = std::string (),
95  const std::string& fcn_name = std::string (),
96  bool autoload = false);
97 
98 extern OCTINTERP_API void
99 source_file (const std::string& file_name,
100  const std::string& context = std::string (),
101  bool verbose = false, bool require_file = true,
102  const std::string& warn_for = std::string ());
103 
105 feval (const std::string& name,
106  const octave_value_list& args = octave_value_list (),
107  int nargout = 0);
108 
110 feval (octave_function *fcn,
111  const octave_value_list& args = octave_value_list (),
112  int nargout = 0);
113 
115 feval (const octave_value_list& args, int nargout = 0);
116 
118 eval_string (const std::string&, bool silent, int& parse_status, int hargout);
119 
121 eval_string (const std::string&, bool silent, int& parse_status);
122 
124 
125 // Global access to currently active lexer.
126 // FIXME: to be removed after more parser+lexer refactoring.
127 extern octave_base_lexer *LEXER;
128 
129 class
131 {
132 public:
133 
135  : endfunction_found (false),
136  autoloading (false), fcn_file_from_relative_lookup (false),
137  parsing_subfunctions (false), max_fcn_depth (0),
138  curr_fcn_depth (0), primary_fcn_scope (-1),
139  curr_class_name (), function_scopes (), primary_fcn_ptr (0),
140  subfunction_names (), stmt_list (0),
141  lexer (lxr)
142  { }
143 
144  ~octave_base_parser (void);
145 
146  void reset (void);
147 
148  // Error mesages for mismatched end tokens.
149  void end_error (const char *type, token::end_tok_type ettype, int l, int c);
150 
151  // Check to see that end tokens are properly matched.
152  bool end_token_ok (token *tok, token::end_tok_type expected);
153 
154  // Maybe print a warning if an assignment expression is used as the
155  // test in a logical expression.
156  void maybe_warn_assign_as_truth_value (tree_expression *expr);
157 
158  // Maybe print a warning about switch labels that aren't constants.
159  void maybe_warn_variable_switch_label (tree_expression *expr);
160 
161  // Finish building a range.
162  tree_expression *finish_colon_expression (tree_colon_expression *e);
163 
164  // Build a constant.
165  tree_constant *make_constant (int op, token *tok_val);
166 
167  // Build a function handle.
169 
170  // Build an anonymous function handle.
172  make_anon_fcn_handle (tree_parameter_list *param_list, tree_statement *stmt);
173 
174  // Build a binary expression.
176  make_binary_op (int op, tree_expression *op1, token *tok_val,
177  tree_expression *op2);
178 
179  // Build a boolean expression.
181  make_boolean_op (int op, tree_expression *op1, token *tok_val,
182  tree_expression *op2);
183 
184  // Build a prefix expression.
186  make_prefix_op (int op, tree_expression *op1, token *tok_val);
187 
188  // Build a postfix expression.
190  make_postfix_op (int op, tree_expression *op1, token *tok_val);
191 
192  // Build an unwind-protect command.
193  tree_command *
194  make_unwind_command (token *unwind_tok, tree_statement_list *body,
195  tree_statement_list *cleanup, token *end_tok,
197 
198  // Build a try-catch command.
199  tree_command *
200  make_try_command (token *try_tok, tree_statement_list *body,
201  char catch_sep, tree_statement_list *cleanup,
202  token *end_tok, octave_comment_list *lc,
203  octave_comment_list *mc);
204 
205  // Build a while command.
206  tree_command *
207  make_while_command (token *while_tok, tree_expression *expr,
208  tree_statement_list *body, token *end_tok,
209  octave_comment_list *lc);
210 
211  // Build a do-until command.
212  tree_command *
213  make_do_until_command (token *until_tok, tree_statement_list *body,
215 
216  // Build a for command.
217  tree_command *
218  make_for_command (int tok_id, token *for_tok, tree_argument_list *lhs,
220  tree_statement_list *body, token *end_tok,
221  octave_comment_list *lc);
222 
223  // Build a break command.
224  tree_command *make_break_command (token *break_tok);
225 
226  // Build a continue command.
227  tree_command *make_continue_command (token *continue_tok);
228 
229  // Build a return command.
230  tree_command *make_return_command (token *return_tok);
231 
232  // Start an if command.
234  start_if_command (tree_expression *expr, tree_statement_list *list);
235 
236  // Finish an if command.
238  finish_if_command (token *if_tok, tree_if_command_list *list,
239  token *end_tok, octave_comment_list *lc);
240 
241  // Build an elseif clause.
243  make_elseif_clause (token *elseif_tok, tree_expression *expr,
245 
246  // Finish a switch command.
248  finish_switch_command (token *switch_tok, tree_expression *expr,
249  tree_switch_case_list *list, token *end_tok,
250  octave_comment_list *lc);
251 
252  // Build a switch case.
254  make_switch_case (token *case_tok, tree_expression *expr,
256 
257  // Build an assignment to a variable.
259  make_assign_op (int op, tree_argument_list *lhs, token *eq_tok,
260  tree_expression *rhs);
261 
262  // Define a script.
263  void make_script (tree_statement_list *cmds, tree_statement *end_script);
264 
265  // Begin defining a function.
267  start_function (tree_parameter_list *param_list, tree_statement_list *body,
268  tree_statement *end_function);
269 
270  // Create a no-op statement for end_function.
271  tree_statement *make_end (const std::string& type, bool eof, int l, int c);
272 
273  // Do most of the work for defining a function.
275  frob_function (const std::string& fname, octave_user_function *fcn);
276 
277  // Finish defining a function.
279  finish_function (tree_parameter_list *ret_list,
281  int l, int c);
282 
283  // Reset state after parsing function.
284  void
285  recover_from_parsing_function (void);
286 
287  // Make an index expression.
289  make_index_expression (tree_expression *expr,
290  tree_argument_list *args, char type);
291 
292  // Make an indirect reference expression.
294  make_indirect_ref (tree_expression *expr, const std::string&);
295 
296  // Make an indirect reference expression with dynamic field name.
298  make_indirect_ref (tree_expression *expr, tree_expression *field);
299 
300  // Make a declaration command.
302  make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst);
303 
304  // Validate matrix or cell
305  bool validate_array_list (tree_expression *e);
306 
307  // Validate matrix object used in "[lhs] = ..." assignments.
308  tree_argument_list *validate_matrix_for_assignment (tree_expression *e);
309 
310  // Finish building an array_list (common action for finish_matrix
311  // and finish_cell).
312  tree_expression *finish_array_list (tree_array_list *a);
313 
314  // Finish building a matrix list.
315  tree_expression *finish_matrix (tree_matrix *m);
316 
317  // Finish building a cell list.
318  tree_expression *finish_cell (tree_cell *c);
319 
320  // Maybe print a warning. Duh.
321  void maybe_warn_missing_semi (tree_statement_list *);
322 
323  // Set the print flag for a statement based on the separator type.
325  set_stmt_print_flag (tree_statement_list *, char, bool);
326 
327  // Finish building a statement.
328  template <class T>
329  tree_statement *make_statement (T *arg);
330 
331  // Create a statement list.
332  tree_statement_list *make_statement_list (tree_statement *stmt);
333 
334  // Append a statement to an existing statement list.
336  append_statement_list (tree_statement_list *list, char sep,
337  tree_statement *stmt, bool warn_missing_semi);
338 
339  // Generic error messages.
340  void bison_error (const char *s);
341 
342  // Have we found an explicit end to a function?
344 
345  // TRUE means we are in the process of autoloading a function.
347 
348  // TRUE means the current function file was found in a relative path
349  // element.
351 
352  // FALSE if we are still at the primary function. Subfunctions can
353  // only be declared inside function files.
355 
356  // Maximum function depth detected. Used to determine whether
357  // we have nested functions or just implicitly ended subfunctions.
359 
360  // = 0 currently outside any function.
361  // = 1 inside the primary function or a subfunction.
362  // > 1 means we are looking at a function definition that seems to be
363  // inside a function. Note that the function still might not be a
364  // nested function.
366 
367  // Scope where we install all subfunctions and nested functions. Only
368  // used while reading function files.
370 
371  // Name of the current class when we are parsing class methods or
372  // constructors.
373  std::string curr_class_name;
374 
375  // A stack holding the nested function scopes being parsed.
376  // We don't use std::stack, because we want the clear method. Also, we
377  // must access one from the top
378  std::vector<symbol_table::scope_id> function_scopes;
379 
380  // Pointer to the primary user function or user script function.
382 
383  // List of subfunction names, initially in the order they are
384  // installed in the symbol table, then ordered as they appear in the
385  // file. Eventually stashed in the primary function object.
386  std::list<std::string> subfunction_names;
387 
388  // Result of parsing input.
390 
391  // State of the lexer.
393 
394 private:
395 
396  // No copying!
397 
399 
401 };
402 
403 class
405 {
406 public:
407 
409  : octave_base_parser (*(new octave_lexer ()))
410  { }
411 
412  octave_parser (FILE *file)
413  : octave_base_parser (*(new octave_lexer (file)))
414  { }
415 
416  octave_parser (const std::string& eval_string)
417  : octave_base_parser (*(new octave_lexer (eval_string)))
418  { }
419 
421  : octave_base_parser (lxr)
422  { }
423 
424  ~octave_parser (void) { }
425 
426  int run (void);
427 
428 private:
429 
430  // No copying!
431 
432  octave_parser (const octave_parser&);
433 
435 };
436 
437 class
439 {
440 public:
441 
443  : octave_base_parser (*(new octave_push_lexer ())), parser_state (0)
444  {
445  init ();
446  }
447 
448  ~octave_push_parser (void);
449 
450  void init (void);
451 
452  int run (const std::string& input, bool eof);
453 
454 private:
455 
456  // Internal state of the Bison parser.
458 
459  // No copying!
460 
462 
464 };
465 
466 #endif