00001 // ---------------------------------------------------------------------- 00002 // 00003 // C-Breeze 00004 // C Compiler Framework 00005 // 00006 // Copyright (c) 2000 University of Texas at Austin 00007 // 00008 // Samuel Z. Guyer 00009 // Daniel A. Jimenez 00010 // Calvin Lin 00011 // 00012 // Permission is hereby granted, free of charge, to any person 00013 // obtaining a copy of this software and associated documentation 00014 // files (the "Software"), to deal in the Software without 00015 // restriction, including without limitation the rights to use, copy, 00016 // modify, merge, publish, distribute, sublicense, and/or sell copies 00017 // of the Software, and to permit persons to whom the Software is 00018 // furnished to do so, subject to the following conditions: 00019 // 00020 // The above copyright notice and this permission notice shall be 00021 // included in all copies or substantial portions of the Software. 00022 // 00023 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00024 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00025 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00026 // NONINFRINGEMENT. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT 00027 // AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 00028 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 00029 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00030 // THE SOFTWARE. 00031 // 00032 // We acknowledge the C-to-C Translator from MIT Laboratory for 00033 // Computer Science for inspiring parts of the C-Breeze design. 00034 // 00035 // ---------------------------------------------------------------------- 00036 00037 #ifndef CBZ_OUTPUT_CONTEXT_H 00038 #define CBZ_OUTPUT_CONTEXT_H 00039 00040 00041 00042 typedef list< int > int_list; 00043 typedef int_list::iterator int_list_p; 00044 00045 class output_context 00046 { 00047 private: 00048 00049 ostream & _out; 00050 int _pos; 00051 int _line; 00052 00053 int_list _parens; 00054 00055 // -- Maintain statement depth 00056 00057 int _depth; 00058 00059 // -- Remember the last output character 00060 00061 char _last; 00062 00063 // -- Internal new-line function 00064 00065 void _new_line(); 00066 00067 // -- Print some spaces 00068 00069 void _indent(const int num); 00070 00071 public: 00072 00073 output_context(ostream & out) 00074 : _out(out), 00075 _pos(0), 00076 _line(0), 00077 _parens(), 00078 _depth(0), 00079 _last('\n') 00080 {} 00081 00082 // -- Output operators 00083 00084 output_context & operator<<(const string & s); 00085 output_context & operator<<(const char * s); 00086 00087 // -- IMPORTANT: this operator controls indenting information so 00088 // it's important to always output delimiters using it. 00089 00090 output_context & operator<<(const char c); 00091 00092 // -- Handle statement boundaries.. 00093 00094 void indent_in() { ++_depth; } 00095 void indent_out() { --_depth; } 00096 00097 // -- Start a new line anywhere 00098 00099 void new_line(); 00100 00101 // -- Continue a line that was getting too long 00102 00103 void continue_line(); 00104 00105 // -- Add a space, if necessary (based on the last character) 00106 00107 void space(); 00108 00109 // -- Return the current line position 00110 00111 int pos() const { return _pos; } 00112 00113 // -- Width for line-wrapping... 00114 00115 static int wrap_width; 00116 00117 }; 00118 00119 00120 #endif // CBZ_OUTPUT_CONTEXT_H