concat.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 Token concatenator.
00030   
00031   Definition of concat class wrapping concat_guts flexer.
00032   \author pt
00033 */
00034 #include <lestes/common.hh>
00035 #include <lestes/std/source_location.hh>
00036 #include <lestes/lang/cplus/lex/concat.hh>
00037 #include <lestes/lang/cplus/lex/pp_token.hh>
00038 #include <lestes/lang/cplus/lex/ucn_token.hh>
00039 #include <lestes/lang/cplus/lex/token_sequence.hh>
00040 #include <lestes/lang/cplus/lex/token_value.hh>
00041 #include <lestes/lang/cplus/lex/concat.m.hh>
00042 
00043 package(lestes);
00044 package(lang);
00045 package(cplus);
00046 package(lex);
00047 
00048 // include the flex generated parser in own separate namespace
00049 package(concat_guts);
00050 #include <lestes/lang/cplus/lex/concat_guts.yy.cc>
00051 end_package(concat_guts);
00052 
00053 using namespace ::std;
00054 
00055 /*!
00056   Creates the object, allocates flex buffer.  
00057 */
00058 concat::concat(void):
00059         yy_buffer(concat_guts::yy_new_buffer(NULL,YY_BUF_SIZE))
00060 {
00061         concat_guts::yy_switch_to_buffer(yy_buffer);
00062 }
00063 
00064 /*!
00065   Finalizes the object, releases flex buffer.
00066 */
00067 concat::~concat(void)
00068 {
00069         concat_guts::yy_delete_buffer(yy_buffer);
00070 }
00071 
00072 /*!
00073   Atttempts to concatenate two tokens.
00074   \pre left != NULL || right != NULL.
00075   \param left  The left token to concatenate.
00076   \param right  The right token to concatenate.
00077   \return  A token sequence containing the new token, or the input tokens in case of error.
00078 */
00079 ptr<token_sequence> concat::process(const ptr<pp_token> &left, const ptr<pp_token> &right)
00080 {
00081         lassert(left || right);
00082 
00083         ptr<token_sequence> result = token_sequence::create();
00084         ptr<source_location> loc;
00085 
00086         value = "";
00087 
00088         if (left) {
00089                 loc = left->location_get();
00090                 value += left->spelling_get();
00091         } else {
00092                 loc = right->location_get();
00093         }
00094 
00095         if (right) {
00096                 value += right->spelling_get();
00097         }
00098   
00099         length = value.length();
00100         index = 0;
00101 
00102         // pretend having a new file
00103         concat_guts::yyrestart(NULL);
00104         ptr<pp_token> tok = concat_guts::concat_parse(loc,token_value::create(value));
00105         
00106         if (!tok) {
00107                 // concatenation failed
00108                 report << unable_to_concatenate << loc;
00109                 if (left) result->add_back(left);
00110                 if (right) result->add_back(right);
00111         } else {
00112                 // successful attempt
00113                 result->add_back(tok);
00114         }
00115         return result;
00116 }
00117 
00118 /*!
00119   Fills buffer with characters from current input string.
00120   Called through YY_INPUT flex macro.
00121   \param cbuf  The buffer to fill.
00122   \param max  Maximum count of characters to fill. 
00123   \return  The actual count of filled characters.
00124 */
00125 concat::size_type concat::yy_input(char_type *cbuf, size_type max)
00126 {
00127         size_type cnt;
00128         
00129         if (index == length) return YY_NULL;
00130 
00131         // put all tokens up to max into buffer
00132         for (cnt = 0; cnt < max; cnt++) {
00133                 if (index == length) {
00134                         *cbuf++ = static_cast<char_type>(ucn_token::TOK_EOF);
00135                         cnt++;
00136                         break;
00137                 }
00138                 ucn u = value[index++];
00139                 if (character::is_basic(u)) {
00140                         *cbuf++ = static_cast<char_type>(character::extract_value(u));
00141                 } else {
00142                         *cbuf++ = static_cast<char_type>(ucn_token::TOK_TRANSLATED);
00143                 }
00144         }
00145 
00146         return cnt;
00147 }
00148 
00149 /*!
00150   Marks the object.
00151 */
00152 void concat::gc_mark(void)
00153 {
00154 	::lestes::std::object::gc_mark();
00155 }
00156 
00157 /*!
00158   Returns the only instance of the object.
00159   \return The new instance with own flexer buffer.
00160 */
00161 ptr<concat> concat::instance(void)
00162 {
00163         if (!singleton) {
00164                 singleton =  new concat();
00165         }
00166         return singleton;
00167 }
00168 
00169 /*!
00170   The only instance of the class, managing the associated flexer.
00171 */
00172 ptr<concat> concat::singleton;
00173 
00174 end_package(lex);
00175 end_package(cplus);
00176 end_package(lang);
00177 end_package(lestes);
00178 /* vim: set ft=lestes : */

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