line_join.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 /*! \file
00029   \brief Line joining filter.
00030   
00031   Definition of line_join class performing joining of lines.
00032   \author pt
00033 */
00034 #include <lestes/common.hh> 
00035 #include <lestes/lang/cplus/lex/line_join.hh>
00036 #include <lestes/lang/cplus/lex/ucn_filter.hh>
00037 package(lestes);
00038 package(lang);
00039 package(cplus);
00040 package(lex);
00041 
00042 using namespace ::std;
00043 
00044 /*!
00045   Creates the object.
00046   \post state == START
00047 */
00048 line_join::line_join(void):
00049         state(START)
00050 {
00051 }
00052 
00053 /*!
00054   Reads next token, discards backslash newline sequences.
00055   If backslash newline is encountered before EOF,
00056   the newline is returned to avoid reporting no newline
00057   at the end of file.
00058   
00059 */
00060 ptr<ucn_token> line_join::read(void)
00061 {
00062         ptr<ucn_token> t;
00063         
00064         if (state == SAVED) {
00065                 // something left in buffer
00066                 t = saved;
00067                 // release reference
00068                 saved = NULL;
00069                 state = START;
00070                 return t;
00071         }
00072 
00073         ucn_token_type utt;
00074         ucn u;
00075 
00076         while (true) {
00077                 // read new token
00078                 t = input_read();
00079                 utt = t->type_get();
00080                 u = t->value_get();
00081                 switch (state) {
00082                         case AFTER:
00083                                 if (utt == ucn_token::TOK_EOF) {
00084                                         // get the newline from the last pair
00085                                         ptr<ucn_token> tmp = saved;
00086                                         // save the EOF
00087                                         saved = t;
00088                                         state = SAVED;
00089                                         return tmp;
00090                                 }
00091                                 saved = NULL;
00092                                 state = START;
00093                                 // fall through
00094                         case START:
00095                                 if (utt == ucn_token::TOK_BASIC && u == character::ascii_backslash) {
00096                                         // save the location
00097                                         saved = t;
00098                                         state = BACKSLASH;
00099                                 } else {
00100                                         return t;
00101                                 }
00102                                 break;
00103                         case BACKSLASH:
00104                                 // backslash newline combination
00105                                 if (utt == ucn_token::TOK_BASIC && u == character::ascii_new_line) {
00106                                         // switch to the newline in case EOF follows
00107                                         saved = t;
00108                                         // start anew
00109                                         state = AFTER;
00110                                 } else {
00111                                         if (utt != ucn_token::TOK_BASIC || u != character::ascii_backslash) {
00112                                                 // character different from backslash
00113                                                 state = SAVED;
00114                                         }
00115                                         // get the backslash
00116                                         ptr<ucn_token> tmp = saved;
00117                                         // save current
00118                                         saved = t;
00119                                         // return the backslash
00120                                         return tmp;
00121                                 }
00122                                 break;
00123                         default:
00124                                 lassert2(false,"You should never get here");
00125                 }
00126         }
00127 }
00128 
00129 /*!
00130   Marks the object.
00131 */
00132 void line_join::gc_mark(void)
00133 {
00134         saved.gc_mark();
00135         ucn_filter::gc_mark();
00136 }
00137 
00138 /*!
00139   Returns new instance.
00140   \return  New instance of the class.
00141 */
00142 ptr<line_join> line_join::create(void)
00143 {
00144         return new line_join();
00145 }
00146 
00147 end_package(lex);
00148 end_package(cplus);
00149 end_package(lang);
00150 end_package(lestes);
00151 
00152 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:22:37 2007 for lestes by doxygen 1.5.1-20070107