diff options
Diffstat (limited to 'TAO/CIAO/CCF/CCF/CodeGenerationKit/IndentationCxx.hpp')
-rw-r--r-- | TAO/CIAO/CCF/CCF/CodeGenerationKit/IndentationCxx.hpp | 188 |
1 files changed, 148 insertions, 40 deletions
diff --git a/TAO/CIAO/CCF/CCF/CodeGenerationKit/IndentationCxx.hpp b/TAO/CIAO/CCF/CCF/CodeGenerationKit/IndentationCxx.hpp index daede420625..b7451969555 100644 --- a/TAO/CIAO/CCF/CCF/CodeGenerationKit/IndentationCxx.hpp +++ b/TAO/CIAO/CCF/CCF/CodeGenerationKit/IndentationCxx.hpp @@ -6,6 +6,7 @@ #define CCF_CODE_GENERATION_KIT_INDENTATION_CXX_HPP #include <deque> +#include <stack> #include "CCF/CodeGenerationKit/IndentationBuffer.hpp" @@ -34,10 +35,12 @@ namespace Indentation public: Cxx (Buffer<C>& out) : out_ (out), - indentation_ (0), + position_ (0), + paren_balance_ (0), spaces_ (2), construct_ (OTHER) { + indentation_.push (0); } virtual @@ -52,11 +55,30 @@ namespace Indentation try { bool defaulting = false; + + if (!hold_.empty () && hold_.back () == '(') + { + unbuffer (); // We don't need to hold it any more. + + if (c == '\n') + indentation_.push (indentation_.top () + spaces_); + else + indentation_.push (position_); + } + switch (c) { case '\n': { hold_.push_back (c); + position_ = 0; // Starting a new line. + + if (construct_ == CXX_COMMENT) + { + //std::cerr << "end comment" << endl; + construct_ = OTHER; + } + break; } case '{': @@ -65,12 +87,15 @@ namespace Indentation output_indentation (); result = write (c); ensure_new_line (); - indentation_++; + + indentation_.push (indentation_.top () + spaces_); + break; } case '}': { - if (indentation_ > 0) indentation_--; + if (indentation_.size () > 1) + indentation_.pop (); // Reduce multiple newlines to one. while (hold_.size () > 1) @@ -85,65 +110,82 @@ namespace Indentation hold_.push_back (c); - // result = write (c); - - //ensure_new_line (); // Add double newline after '}'. // hold_.push_back ('\n'); hold_.push_back ('\n'); - + position_ = 0; break; } case ';': { - // Handling '};' case. - // - - bool brace (false); - - if (hold_.size () > 1 && hold_.back () == '\n') + if (paren_balance_ != 0) + { + // We are inside for (;;) statement. Nothing to do here. + // + defaulting = true; + } + else { - bool pop_nl (false); + // Handling '};' case. + // + + bool brace (false); - for (typename Hold::reverse_iterator - i (hold_.rbegin ()), e (hold_.rend ()); i != e; ++i) + if (hold_.size () > 1 && hold_.back () == '\n') { - if (*i != '\n') + bool pop_nl (false); + + for (typename Hold::reverse_iterator + i (hold_.rbegin ()), e (hold_.rend ()); i != e; ++i) { - if (*i == '}') brace = pop_nl = true; - break; + if (*i != '\n') + { + if (*i == '}') brace = pop_nl = true; + break; + } } + + if (pop_nl) while (hold_.back () == '\n') hold_.pop_back (); } - if (pop_nl) while (hold_.back () == '\n') hold_.pop_back (); - } + output_indentation (); + result = write (c); + position_++; - output_indentation (); - result = write (c); + if (brace) + { + hold_.push_back ('\n'); + hold_.push_back ('\n'); + } - if (brace) - { - hold_.push_back ('\n'); - hold_.push_back ('\n'); + if (construct_ != STRING_LITERAL && construct_ != CHAR_LITERAL) + { + ensure_new_line (); + } } - if (construct_ != STRING_LITERAL && construct_ != CHAR_LITERAL) - { - ensure_new_line (); - } break; } case '\\': { - hold_.push_back (c); + if (construct_ != CXX_COMMENT) + { + output_indentation (); + hold_.push_back (c); + position_++; + } + else + defaulting = true; + break; } case '\"': { - if (hold_.empty () || hold_.back () != '\\') + if (construct_ != CXX_COMMENT && + (hold_.empty () || hold_.back () != '\\')) { // not escape sequence if (construct_ == STRING_LITERAL) construct_ = OTHER; @@ -155,16 +197,76 @@ namespace Indentation } case '\'': { - if (hold_.empty () || hold_.back () != '\\') + if (construct_ != CXX_COMMENT && + (hold_.empty () || hold_.back () != '\\')) { // not escape sequence if (construct_ == CHAR_LITERAL) construct_ = OTHER; - else construct_ = CHAR_LITERAL; + else + { + //std::cerr << "char literal" << endl; + construct_ = CHAR_LITERAL; + } + + } + + defaulting = true; + break; + } + case '(': + { + if (construct_ == OTHER) + { + // Hold it so that we can see what's coming next. + // + output_indentation (); + hold_.push_back (c); + position_++; + paren_balance_++; + } + else + defaulting = true; + + break; + } + case ')': + { + if (construct_ == OTHER) + { + if (indentation_.size () > 1) + indentation_.pop (); + + if (paren_balance_ > 0) + paren_balance_--; } defaulting = true; break; } + case '/': + { + if (construct_ == OTHER) + { + if (!hold_.empty () && hold_.back () == '/') + { + construct_ = CXX_COMMENT; + //std::cerr << "start comment" << endl; + defaulting = true; + } + else + { + output_indentation (); + hold_.push_back (c); + position_++; + } + } + else + { + defaulting = true; + } + + break; + } default: { defaulting = true; @@ -176,6 +278,7 @@ namespace Indentation { output_indentation (); result = write (c); + position_++; } } catch (Full const&) @@ -214,6 +317,7 @@ namespace Indentation if (hold_.empty () || hold_.back () != '\n') { hold_.push_back ('\n'); + position_ = 0; // Starting a new line. } } @@ -223,10 +327,10 @@ namespace Indentation { if (!hold_.empty () && hold_.back () == '\n') { - for (unsigned long i = 0; i < indentation_ * spaces_; i++) - { + for (unsigned long i = 0; i < indentation_.top (); i++) write (' '); - } + + position_ += indentation_.top (); } } @@ -241,7 +345,8 @@ namespace Indentation { result = out_.put (hold_.front ()); - if (result == traits_type::eof ()) throw Full (); + if (result == traits_type::eof ()) + throw Full (); hold_.pop_front (); } @@ -252,7 +357,9 @@ namespace Indentation private: Buffer<C>& out_; - unsigned long indentation_; + unsigned long position_; // Current position on the line. + unsigned long paren_balance_; // ( ) balance. + std::stack<unsigned long> indentation_; unsigned long spaces_; bool suppress_nl_; @@ -260,6 +367,7 @@ namespace Indentation enum Construct { OTHER, + CXX_COMMENT, STRING_LITERAL, CHAR_LITERAL }; |