pt-pr-code.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_tree_print_code_h)
00024 #define octave_tree_print_code_h 1
00025 
00026 #include <stack>
00027 #include <string>
00028 
00029 #include "comment-list.h"
00030 #include "pt-walk.h"
00031 
00032 class tree_decl_command;
00033 class tree_expression;
00034 
00035 // How to print the code that the parse trees represent.
00036 
00037 class
00038 tree_print_code : public tree_walker
00039 {
00040 public:
00041 
00042   tree_print_code (std::ostream& os_arg,
00043                    const std::string& pfx = std::string (),
00044                    bool pr_orig_txt = true)
00045     : os (os_arg), prefix (pfx), nesting (),
00046       print_original_text (pr_orig_txt),
00047       curr_print_indent_level (0), beginning_of_line (true),
00048       suppress_newlines (0)
00049   {
00050     // For "none".
00051     nesting.push ('n');
00052   }
00053 
00054   ~tree_print_code (void) { }
00055 
00056   void visit_anon_fcn_handle (tree_anon_fcn_handle&);
00057 
00058   void visit_argument_list (tree_argument_list&);
00059 
00060   void visit_binary_expression (tree_binary_expression&);
00061 
00062   void visit_break_command (tree_break_command&);
00063 
00064   void visit_colon_expression (tree_colon_expression&);
00065 
00066   void visit_continue_command (tree_continue_command&);
00067 
00068   void visit_global_command (tree_global_command&);
00069 
00070   void visit_static_command (tree_static_command&);
00071 
00072   void visit_decl_elt (tree_decl_elt&);
00073 
00074   void visit_decl_init_list (tree_decl_init_list&);
00075 
00076   void visit_simple_for_command (tree_simple_for_command&);
00077 
00078   void visit_complex_for_command (tree_complex_for_command&);
00079 
00080   void visit_octave_user_script (octave_user_script&);
00081 
00082   void visit_octave_user_function (octave_user_function&);
00083 
00084   void visit_octave_user_function_header (octave_user_function&);
00085 
00086   void visit_octave_user_function_trailer (octave_user_function&);
00087 
00088   void visit_function_def (tree_function_def&);
00089 
00090   void visit_identifier (tree_identifier&);
00091 
00092   void visit_if_clause (tree_if_clause&);
00093 
00094   void visit_if_command (tree_if_command&);
00095 
00096   void visit_if_command_list (tree_if_command_list&);
00097 
00098   void visit_index_expression (tree_index_expression&);
00099 
00100   void visit_matrix (tree_matrix&);
00101 
00102   void visit_cell (tree_cell&);
00103 
00104   void visit_multi_assignment (tree_multi_assignment&);
00105 
00106   void visit_no_op_command (tree_no_op_command&);
00107 
00108   void visit_constant (tree_constant&);
00109 
00110   void visit_fcn_handle (tree_fcn_handle&);
00111 
00112   void visit_parameter_list (tree_parameter_list&);
00113 
00114   void visit_postfix_expression (tree_postfix_expression&);
00115 
00116   void visit_prefix_expression (tree_prefix_expression&);
00117 
00118   void visit_return_command (tree_return_command&);
00119 
00120   void visit_return_list (tree_return_list&);
00121 
00122   void visit_simple_assignment (tree_simple_assignment&);
00123 
00124   void visit_statement (tree_statement&);
00125 
00126   void visit_statement_list (tree_statement_list&);
00127 
00128   void visit_switch_case (tree_switch_case&);
00129 
00130   void visit_switch_case_list (tree_switch_case_list&);
00131 
00132   void visit_switch_command (tree_switch_command&);
00133 
00134   void visit_try_catch_command (tree_try_catch_command&);
00135 
00136   void visit_unwind_protect_command (tree_unwind_protect_command&);
00137 
00138   void visit_while_command (tree_while_command&);
00139 
00140   void visit_do_until_command (tree_do_until_command&);
00141 
00142   void print_fcn_handle_body (tree_statement_list *);
00143 
00144 private:
00145 
00146   std::ostream& os;
00147 
00148   std::string prefix;
00149 
00150   std::stack<char> nesting;
00151 
00152   bool print_original_text;
00153 
00154   // Current indentation.
00155   int curr_print_indent_level;
00156 
00157   // TRUE means we are at the beginning of a line.
00158   bool beginning_of_line;
00159 
00160   // Nonzero means we are not printing newlines and indenting.
00161   int suppress_newlines;
00162 
00163   void do_decl_command (tree_decl_command& cmd);
00164 
00165   void reset_indent_level (void) { curr_print_indent_level = 0; }
00166 
00167   void increment_indent_level (void) { curr_print_indent_level += 2; }
00168 
00169   void decrement_indent_level (void) { curr_print_indent_level -= 2; }
00170 
00171   void newline (const char *alt_txt = ", ");
00172 
00173   void indent (void);
00174 
00175   void reset (void);
00176 
00177   void print_parens (const tree_expression& expr, const char *txt);
00178 
00179   void print_comment_list (octave_comment_list *comment_list);
00180 
00181   void print_comment_elt (const octave_comment_elt& comment_elt);
00182 
00183   void print_indented_comment (octave_comment_list *comment_list);
00184 
00185   // Must create with an output stream!
00186 
00187   tree_print_code (void);
00188 
00189   // No copying!
00190 
00191   tree_print_code (const tree_print_code&);
00192 
00193   tree_print_code& operator = (const tree_print_code&);
00194 };
00195 
00196 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines