lib Library API Documentation

koscript_parsenode.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "koscript_parsenode.h"
00021 #include "koscript_eval.h"
00022 #include "koscript_context.h"
00023 
00024 #include <iostream>
00025 using namespace std;
00026 
00027 extern QString idl_lexFile;
00028 extern QString toplevelFile;
00029 extern int idl_line_no;
00030 
00031 typedef struct
00032 {
00033   const char *str;
00034   int   data;     // 0 = no further information
00035                   // 1 = ident
00036 } ParseNodeDoc;
00037 
00038 
00039 static ParseNodeDoc parseNodeDoc[] = {
00040   { "definitions", 0 },
00041   { "exports", 0 },
00042   { "t_vertical_line", 0 },
00043   { "t_circumflex", 0 },
00044   { "t_ampersand", 0 },
00045   { "t_shiftright", 0 },
00046   { "t_shiftleft", 0 },
00047   { "t_plus_sign", 0 },
00048   { "t_minus_sign", 0 },
00049   { "t_asterik", 0 },
00050   { "t_solidus", 0 },
00051   { "t_percent_sign", 0 },
00052   { "t_tilde", 0 },
00053   { "t_integer_literal", 2 },
00054   { "t_string_literal", 3 },
00055   { "t_character_literal", 4 },
00056   { "t_floating_pt_literal", 5 },
00057   { "t_boolean_literal", 6 },
00058   { "scoped_name", 1 },
00059   { "const_dcl", 1 },
00060   { "func_dcl", 1 },
00061   { "func_lines", 0 },
00062   { "assign_expr", 0 },
00063   { "t_equal", 0 },
00064   { "t_notequal", 0 },
00065   { "t_less_or_equal", 0 },
00066   { "t_greater_or_equal", 0 },
00067   { "t_array", 0 },
00068   { "t_dict", 0 },
00069   { "func_params", 0 },
00070   { "func_param_in", 1 },
00071   { "func_param_out", 1 },
00072   { "func_param_inout", 1 },
00073   { "t_func_call", 0 },
00074   { "member_expr", 1 },
00075   { "t_array_const", 0 },
00076   { "t_array_element", 0 },
00077   { "t_dict_const", 0 },
00078   { "t_dict_element", 0 },
00079   { "t_while", 0 },
00080   { "t_do", 0 },
00081   { "t_for", 0 },
00082   { "t_if", 0 },
00083   { "t_incr", 0 },
00084   { "t_decr", 0 },
00085   { "t_less", 0 },
00086   { "t_greater", 0 },
00087   { "t_foreach", 0 },
00088   { "t_match", 1 },
00089   { "t_subst", 1 },
00090   { "t_not", 0 },
00091   { "func_call_params", 0 },
00092   { "t_return", 0 },
00093   { "destructor_dcl", 0 },
00094   { "import", 1 },
00095   { "t_struct", 1 },
00096   { "t_strtuct_members", 1 },
00097   { "t_qualified_names", 0 },
00098   { "t_scope", 0 },
00099   { "t_try", 0 },
00100   { "t_catch", 0 },
00101   { "t_catch_default", 0 },
00102   { "t_raise", 0 },
00103   { "t_cell", 0 },
00104   { "t_range", 0 },
00105   { "from", 0 },
00106   { "plus_assign", 0 },
00107   { "minus_assign", 0 },
00108   { "bool_or", 0 },
00109   { "bool_and", 0 },
00110   { "t_regexp_group", 0 },
00111   { "t_input", 0 },
00112   { "t_line", 0 },
00113   { "t_match_line", 0 }
00114 };
00115 
00116 typedef bool (*KSEval)( KSParseNode*, KSContext& );
00117 
00118 static KSEval KSEvalJump[] = {
00119   KSEval_definitions,
00120   KSEval_exports,
00121   KSEval_t_vertical_line,
00122   KSEval_t_circumflex,
00123   KSEval_t_ampersand,
00124   KSEval_t_shiftright,
00125   KSEval_t_shiftleft,
00126   KSEval_t_plus_sign,
00127   KSEval_t_minus_sign,
00128   KSEval_t_asterik,
00129   KSEval_t_solidus,
00130   KSEval_t_percent_sign,
00131   KSEval_t_tilde,
00132   KSEval_t_integer_literal,
00133   KSEval_t_string_literal,
00134   KSEval_t_character_literal,
00135   KSEval_t_floating_pt_literal,
00136   KSEval_t_boolean_literal,
00137   KSEval_scoped_name,
00138   KSEval_const_dcl,
00139   KSEval_func_dcl,
00140   KSEval_func_lines,
00141   KSEval_assign_expr,
00142   KSEval_t_equal,
00143   KSEval_t_notequal,
00144   KSEval_t_less_or_equal,
00145   KSEval_t_greater_or_equal,
00146   KSEval_t_array,
00147   KSEval_t_dict,
00148   KSEval_func_params,
00149   KSEval_func_param_in,
00150   KSEval_func_param_out,
00151   KSEval_func_param_inout,
00152   KSEval_t_func_call,
00153   KSEval_member_expr,
00154   KSEval_t_array_const,
00155   KSEval_t_array_element,
00156   KSEval_t_dict_const,
00157   KSEval_t_dict_element,
00158   KSEval_t_while,
00159   KSEval_t_do,
00160   KSEval_t_for,
00161   KSEval_t_if,
00162   KSEval_t_incr,
00163   KSEval_t_decr,
00164   KSEval_t_less,
00165   KSEval_t_greater,
00166   KSEval_t_foreach,
00167   KSEval_t_match,
00168   KSEval_t_subst,
00169   KSEval_t_not,
00170   KSEval_func_call_params,
00171   KSEval_t_return,
00172   KSEval_destructor_dcl,
00173   KSEval_import,
00174   KSEval_t_struct,
00175   KSEval_t_struct_members,
00176   KSEval_t_qualified_names,
00177   KSEval_t_scope,
00178   KSEval_t_try,
00179   KSEval_t_catch,
00180   KSEval_t_catch_default,
00181   KSEval_t_raise,
00182   KSEval_t_cell,
00183   KSEval_t_range,
00184   KSEval_from,
00185   KSEval_plus_assign,
00186   KSEval_minus_assign,
00187   KSEval_bool_or,
00188   KSEval_bool_and,
00189   KSEval_t_regexp_group,
00190   KSEval_t_input,
00191   KSEval_t_line,
00192   KSEval_t_match_line,
00193 };
00194 
00195 KSParseNode::KSParseNode( KSParseNodeType aType, KSParseNode *one,
00196                       KSParseNode *two, KSParseNode *three,
00197                       KSParseNode *four, KSParseNode *five )
00198 {
00199   m_extra = 0;
00200   str = 0;
00201   type = aType;
00202   ident = QString::null;
00203   b1 = one;
00204   b2 = two;
00205   b3 = three;
00206   b4 = four;
00207   b5 = five;
00208   fname = idl_lexFile;
00209   line_no = idl_line_no;
00210   bIsToplevel = ( idl_lexFile == toplevelFile );
00211 }
00212 
00213 KSParseNode::~KSParseNode()
00214 {
00215   clear();
00216 }
00217 
00218 void KSParseNode::clear()
00219 {
00220   if ( m_extra ) delete m_extra;
00221   m_extra = 0;
00222   if ( str ) delete str;
00223   str = 0;
00224   if( b1 ) delete b1;
00225   b1 = 0;
00226   // t_incr / t_decr checks as b1==b2 is used to detect postfix notation
00227   // This fixes the *ugly* memory corruption (double deletion)
00228   if( b2 && type!=t_incr && type!=t_decr ) delete b2;
00229   b2 = 0;
00230   if( b3 ) delete b3;
00231   b3 = 0;
00232   if( b4 ) delete b4;
00233   b4 = 0;
00234   if( b5 ) delete b5;
00235   b5 = 0;
00236 }
00237 
00238 void KSParseNode::setIdent( const QString& anIdent )
00239 {
00240   ident = anIdent;
00241 }
00242 
00243 void KSParseNode::setIdent( QString* anIdent )
00244 {
00245   ident = *anIdent;
00246   delete anIdent;
00247 }
00248 
00249 void KSParseNode::setIdent( const char *anIdent )
00250 {
00251   ident = anIdent;
00252 }
00253 
00254 QString KSParseNode::getIdent()
00255 {
00256   return ident;
00257 }
00258 
00259 QString KSParseNode::getFname()
00260 {
00261   return fname;
00262 }
00263 
00264 long KSParseNode::getLineNo()
00265 {
00266   return line_no;
00267 }
00268 
00269 bool KSParseNode::isToplevel()
00270 {
00271   return bIsToplevel;
00272 }
00273 
00274 void KSParseNode::setIntegerLiteral( KScript::Long l )
00275 {
00276   _u._int = l;
00277 }
00278 
00279 KScript::Long KSParseNode::getIntegerLiteral()
00280 {
00281   return _u._int;
00282 }
00283 
00284 void KSParseNode::setStringLiteral( const char* s )
00285 {
00286   str = new QString( s );
00287 }
00288 
00289 void KSParseNode::setStringLiteral( const QString& s )
00290 {
00291   str = new QString( s );
00292 }
00293 
00294 void KSParseNode::setStringLiteral( QString* s )
00295 {
00296   // We take ownership
00297   str = s;
00298 }
00299 
00300 QString KSParseNode::getStringLiteral()
00301 {
00302   return *str;
00303 }
00304 
00305 void KSParseNode::setCharacterLiteral( const KScript::Char& c )
00306 {
00307   _u._char = c.unicode();
00308 }
00309 
00310 KScript::Char KSParseNode::getCharacterLiteral()
00311 {
00312   return QChar( _u._char );
00313 }
00314 
00315 void KSParseNode::setFloatingPtLiteral( KScript::Double f )
00316 {
00317   _u._float = f;
00318 }
00319 
00320 KScript::Double KSParseNode::getFloatingPtLiteral()
00321 {
00322   return _u._float;
00323 }
00324 
00325 void KSParseNode::setBooleanLiteral( KScript::Boolean b )
00326 {
00327   _u._bool = b;
00328 }
00329 
00330 KScript::Boolean KSParseNode::getBooleanLiteral()
00331 {
00332   return _u._bool;
00333 }
00334 
00335 KSParseNodeType KSParseNode::getType() const
00336 {
00337   return type;
00338 }
00339 
00340 
00341 KSParseNode *KSParseNode::getBranch( int i )
00342 {
00343   switch( i ) {
00344   case 1:
00345     return b1;
00346   case 2:
00347     return b2;
00348   case 3:
00349     return b3;
00350   case 4:
00351     return b4;
00352   case 5:
00353     return b5;
00354   default:
00355     Q_ASSERT( 0 );
00356   }
00357   return 0;
00358 }
00359 
00360 
00361 void KSParseNode::setBranch( int i, KSParseNode *node )
00362 {
00363   switch( i ) {
00364   case 1:
00365     if( b1 ) delete b1;
00366     b1 = node;
00367     break;
00368   case 2:
00369     if( b2 ) delete b2;
00370     b2 = node;
00371     break;
00372   case 3:
00373     if( b3 ) delete b3;
00374     b3 = node;
00375     break;
00376   case 4:
00377     if( b4 ) delete b4;
00378     b4 = node;
00379     break;
00380   case 5:
00381     if( b5 ) delete b5;
00382     b5 = node;
00383     break;
00384   default:
00385     Q_ASSERT( 0 );
00386   }
00387 }
00388 
00389 
00390 void KSParseNode::printBranch( int indent, const char *tag, bool detailed )
00391 {
00392   for( int i = 0; i < indent; i++ )
00393     cout << " ";
00394   cout << tag;
00395   cout << parseNodeDoc[ (int) type ].str;
00396   switch( parseNodeDoc[ (int) type ].data ) {
00397   case 0:
00398     break;
00399   case 1:
00400     cout << " (" << ident.local8Bit() << ")";
00401     break;
00402   case 2:
00403     cout << " (" << getIntegerLiteral() << ")";
00404     break;
00405   case 3:
00406     cout << " (" << getStringLiteral().local8Bit() << ")";
00407     break;
00408   case 4:
00409     cout << " (" << getCharacterLiteral() << ")";
00410     break;
00411   case 5:
00412     cout << " (" << getFloatingPtLiteral() << ")";
00413     break;
00414   case 6:
00415     cout << " (" << getBooleanLiteral() << ")";
00416     break;
00417   default:
00418     Q_ASSERT( 0 );
00419   }
00420   cout << endl;
00421   if( b1 )
00422     b1->printBranch( indent + 2, "1: ", detailed );
00423   if( b2 )
00424     b2->printBranch( indent + 2, "2: ", detailed );
00425   if( b3 )
00426     b3->printBranch( indent + 2, "3: ", detailed );
00427   if( b4 )
00428     b4->printBranch( indent + 2, "4: ", detailed );
00429   if( b5 )
00430     b5->printBranch( indent + 2, "5: ", detailed );
00431 }
00432 
00433 KSParseNodeExtra* KSParseNode::extra()
00434 {
00435   return m_extra;
00436 }
00437 
00438 void KSParseNode::setExtra( KSParseNodeExtra* e )
00439 {
00440   m_extra = e;
00441 }
00442 
00443 void KSParseNode::print( bool detailed )
00444 {
00445   printBranch( 0, "", detailed );
00446 }
00447 
00448 bool KSParseNode::eval( KSContext& _context )
00449 {
00450   return (KSEvalJump[ (int)type ])( this, _context );
00451 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 13:19:25 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2003