[Uml-devel] KDE/kdesdk/umbrello/umbrello/codeimport/kdevcppparser

Jean Vittor jean.vittor at free.fr
Mon Apr 7 05:40:45 UTC 2008


SVN commit 794337 by jvittor:

preprocess line by line

 M  +31 -32    preprocesslexer.cpp  
 M  +1 -11     preprocesslexer.h  


--- trunk/KDE/kdesdk/umbrello/umbrello/codeimport/kdevcppparser/preprocesslexer.cpp #794336:794337
@@ -343,25 +343,42 @@
     }
 }
 
-void PreprocessLexer::nextToken( Token& tk)
-{
+void PreprocessLexer::nextLine() {
   m_source.parse( (*gr_whiteSpace)
 		  [boost::bind( &PreprocessLexer::output, this, _1, _2)]);
   QChar ch = m_source.currentChar();
   if( ch.isNull() || ch.isSpace() ){
     /* skip */
-  } else if( m_source.get_startLine()
-	     && m_source.parse( ch_p('#') >> *gr_whiteSpace).hit) {
-    m_source.set_startLine( false);
-    
+  } else if( m_source.parse( ch_p('#') >> *gr_whiteSpace).hit) {
     QString directive;
     m_source.parse( identifier_pg[ assign(directive)]); // read the directive
-
     handleDirective( directive );
-  } else if( m_source.get_startLine() && m_preprocessor.inSkip()) {
+  } else if( m_preprocessor.inSkip()) {
     // skip line and continue
     m_source.parse( gr_skipTillEol);
     return;
+  } else {
+    while( !m_source.parse( eol_p).hit) {
+      Token tk;
+      nextToken( tk );
+      if( tk.type() != -1 )
+	m_preprocessedString += tk.text();
+      if( m_source.currentChar().isNull() )
+	break;
+    }
+  }
+  if( m_source.parse( eol_p).hit) {
+    m_preprocessedString += '\n';
+  }
+}
+
+void PreprocessLexer::nextToken( Token& tk)
+{
+  m_source.parse( (*gr_whiteSpace)
+		  [boost::bind( &PreprocessLexer::output, this, _1, _2)]);
+  QChar ch = m_source.currentChar();
+  if( ch.isNull() || ch.isSpace() ){
+    /* skip */
   } else if( m_source.parse
 	     (
 	      if_p(var( m_recordComments))
@@ -429,8 +446,6 @@
 	} else {
 	  tk = m_source.createToken( Token_identifier, start, endIde);
 
-	  m_source.set_startLine( false);
-
 	  m_data->endScope();        // OPS!!
 	  m_preprocessorEnabled = preproc;
 	  return;
@@ -448,22 +463,18 @@
 
       QString textToInsert;
 
-      while( !m_source.currentChar().isNull() ){
-
+      while( !m_source.currentChar().isNull()
+	     && m_source.currentChar() != '\n'
+	     && m_source.currentChar() != '\r'){
 	m_source.parse( *gr_whiteSpace);
-
 	Token tok;
 	nextToken( tok);
-
 	bool stringify = !m_inPreproc && tok == '#';
 	bool merge = !m_inPreproc && tok == Token_concat;
-
 	if( stringify || merge )
 	  nextToken( tok);
-
 	if( tok == Token_eof )
 	  break;
-
 	QString tokText = tok.text();
 	QString str = (tok == Token_identifier && m_data->hasBind(tokText)) ? m_data->apply( tokText ) : tokText;
 	if( str == ide ){
@@ -538,11 +549,6 @@
     m_source.nextChar();
     tk = m_source.createToken( ch.unicode(), l_ptr);
   }
-  if( m_source.parse( eol_p).hit) {
-    m_source.set_startLine( true);
-    m_preprocessedString += '\n';
-  } else
-    m_source.set_startLine( false);
 }
 
 void PreprocessLexer::output( CharIterator p_first, CharIterator p_last) {
@@ -550,16 +556,9 @@
     m_preprocessedString += *p_first;
 }
 
-void PreprocessLexer::preprocess()
-{
-  m_source.set_startLine( true);
+void PreprocessLexer::preprocess() {
   for( ;; ) {
-    Token tk;
-    nextToken( tk );
-
-    if( tk.type() != -1 )
-      m_preprocessedString += tk.text();
-
+    nextLine();
     if( m_source.currentChar().isNull() )
       break;
   }
@@ -604,7 +603,7 @@
   m_source.parse( *gr_whiteSpace);
   while( !m_source.currentChar().isNull() ){
 
-    m_source.parse( *gr_whiteSpace);
+    m_source.parse( *(gr_whiteSpace | eol_p));
     QChar ch = m_source.currentChar();
 
     if( ch.isNull() || (!count && (ch == ',' || ch == ')')) )
--- trunk/KDE/kdesdk/umbrello/umbrello/codeimport/kdevcppparser/preprocesslexer.h #794336:794337
@@ -220,6 +220,7 @@
   }
   Position currentPosition() const
   {return m_source.get_currentPosition();}
+  void nextLine();
   void nextToken( Token& token);
   void output( CharIterator p_first, CharIterator p_last);
   void skip( int l, int r );
@@ -267,12 +268,6 @@
     int length() const {return m_endPtr - m_ptr;}
     void nextChar() {
       QChar l_current = *m_ptr++;
-      switch( l_current.toAscii()) {
-      case '\n':
-      case '\r':
-	m_startLine = true;
-	break;
-      }
     }
     template <typename _RuleT>
     parse_info<CharIterator> parse( _RuleT const& p_rule) {
@@ -285,7 +280,6 @@
     void reset() {
       m_source.clear();
       m_ptr = CharIterator();
-      m_startLine = false;
     }
     void set_source( const QString& source,
 		     PositionFilename const& p_filename) {
@@ -293,7 +287,6 @@
       m_ptr = CharIterator( m_source.data(),
 			    m_source.data() + m_source.length(),
 			    Position( p_filename));
-      m_startLine = true;
     }
     QString substrFrom( CharIterator start) const
     {return QString( &*start, &*m_ptr - &*start);}
@@ -301,15 +294,12 @@
     Position get_currentPosition() const {return m_ptr.get_position();}
     CharIterator get_ptr() const {return m_ptr;}
     QString const& get_source() const {return m_source;}
-    bool get_startLine() const {return m_startLine;}
     /* setters */
-    void set_startLine( bool p) {m_startLine = p;}
     void set_currentPosition( Position const& p) {m_ptr.set_position( p);}
   private:
     QString m_source;
     CharIterator m_ptr;
     const CharIterator m_endPtr;
-    bool m_startLine;
   };
   Source m_source;
   bool m_recordComments;




More information about the umbrello-devel mailing list