special_tokens.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 type assignment.
00030   
00031   Definition of special_tokens class assigning types to tokens.
00032   \author pt
00033 */
00034 
00035 #include <lestes/common.hh>
00036 #include <lestes/std/character.hh>
00037 #include <lestes/lang/cplus/lex/special_tokens.hh>
00038 #include <lestes/lang/cplus/lex/ucn_token.hh>
00039 #include <lestes/lang/cplus/lex/ucn_filter.hh>
00040 package(lestes);
00041 package(lang);
00042 package(cplus);
00043 package(lex);
00044 
00045 using namespace ::std;
00046 
00047 /*!
00048   Creates the object.
00049   \post errors == 0
00050 */
00051 special_tokens::special_tokens(void):
00052         state(START),
00053         errors(0)
00054 {
00055 }
00056 
00057 /*!
00058   Returns next token, assigns token type.
00059   Saves the EOF token to return it again.
00060   Tries to stop the error flood from encoder by sending EOF.
00061   \return The token with assigned type.
00062 */  
00063 ptr<ucn_token> special_tokens::read(void)
00064 {
00065         if (state == END)
00066                 return saved;
00067 
00068         ptr<ucn_token> t = input_read();
00069         ucn_token_type utt = t->type_get();
00070         ucn u = t->value_get();
00071 
00072         switch (utt) {
00073                 case ucn_token::TOK_NOT_EOF:
00074                         if (character::is_basic(u)) {
00075                                 t = t->clone_type(ucn_token::TOK_BASIC);
00076                         } else if (character::is_translated(u)) {
00077                                 t = t->clone_type(ucn_token::TOK_TRANSLATED);
00078                         } else {
00079                                 // TODO pt report error: character value out of range
00080                                 t = ucn_token::create_error(::lestes::msg::message::create(0xdead,"TODO char out of range",::lestes::msg::message::FLG_ERROR));
00081                         }
00082                         break;
00083                 case ucn_token::TOK_ERROR:
00084                         ++errors;
00085                         if (errors < ERRORS_LIMIT)
00086                                 break;
00087 
00088                         // prepare fake EOF
00089                         saved = ucn_token::create(ucn_token::TOK_EOF,0,t->location_get());
00090                         // TODO pt report error: wrong encoding in source, stopping
00091                         t = ucn_token::create_error(
00092                                         ::lestes::msg::message::create(0xbeef,"TODO flood",::lestes::msg::message::FLG_ERROR));
00093                         state = END;
00094                         break;
00095                 case ucn_token::TOK_EOF:
00096                         saved = t;
00097                         state = END;
00098                         break;
00099                 default:
00100                         lassert2(false,"You should never get here");
00101         }
00102 
00103         return t;
00104 }
00105 
00106 /*!
00107   Marks the object.
00108 */
00109 void special_tokens::gc_mark(void)
00110 {
00111         ucn_filter::gc_mark();
00112 }
00113 
00114 /*!
00115   Returns new instance.
00116   \post state == BEGIN
00117   \return New instance of the class.
00118 */
00119 ptr<special_tokens> special_tokens::create(void)
00120 {
00121         return new special_tokens();
00122 }
00123 
00124 end_package(lex);
00125 end_package(cplus);
00126 end_package(lang);
00127 end_package(lestes);
00128 
00129 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:23:15 2007 for lestes by doxygen 1.5.1-20070107