lib Library API Documentation

koscript_context.h

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 #ifndef __KSCRIPT_CONTEXT_H__
00021 #define __KSCRIPT_CONTEXT_H__
00022 
00023 #include "koscript_value.h"
00024 #include "koscript_ptr.h"
00025 
00026 #include <koffice_export.h>
00027 
00028 #include <qshared.h>
00029 #include <qstring.h>
00030 #include <qptrlist.h>
00031 
00032 class KSContext;
00033 class KSParseNode;
00034 class KSInterpreter;
00035 
00036 typedef QMap<QString,KSValue::Ptr> KSNamespace;
00037 
00043 class KOSCRIPT_EXPORT KSModule : public QShared
00044 {
00045 public:
00046     typedef KSSharedPtr<KSModule> Ptr;
00047 
00048     KSModule( KSInterpreter*, const QString& name, KSParseNode* = 0 );
00049     virtual ~KSModule();
00050 
00055     virtual bool eval( KSContext& );
00056 
00064     virtual KSValue::Ptr member( KSContext&, const QString& name );
00068     virtual bool setMember( KSContext&, const QString& name, const KSValue::Ptr& v );
00069 
00073     virtual bool isPebbles() const { return FALSE; } // BIC: remove
00074 
00078     QString name() const { return m_name; }
00079 
00086     KSNamespace* nameSpace() { return &m_space; }
00087 
00097     KSValue* object( const QString& name );
00104     void addObject( const QString& name, const KSValue::Ptr& v );
00111     void removeObject( const QString& name );
00115     KSInterpreter* interpreter() { return m_interpreter; }
00116 
00117 protected:
00118     void setCode( KSParseNode* node );
00119 
00120 private:
00121     QString m_name;
00122     KSNamespace m_space;
00123     KSParseNode* m_code;
00124     KSInterpreter* m_interpreter;
00125 };
00126 
00127 
00128 class KSSubScope
00129 {
00130 public:
00131   KSSubScope() { }
00132   KSSubScope( KSNamespace* n ) { m_spaces.append( n ); }
00133 
00139   KSValue* object( const QString& name, bool insert = FALSE );
00143   void addObject( const QString& name, const KSValue::Ptr& );
00144 
00148   void pushNamespace( KSNamespace* nspace ) { m_spaces.append( nspace ); }
00149 
00153   void popNamespace() { m_spaces.removeLast(); }
00154 
00155 private:
00156   QPtrList<KSNamespace> m_spaces;
00157 };
00158 
00159 
00160 class KOSCRIPT_EXPORT KSScope : public QShared
00161 {
00162 public:
00163   typedef KSSharedPtr<KSScope> Ptr;
00164 
00168   KSScope( const KSNamespace* globalSpace, KSModule *module );
00169   KSScope( const KSScope& s );
00170 
00171   void pushLocalScope( KSSubScope* scope ) { Q_ASSERT( m_localScope == 0 ); m_localScope = scope;  }
00172   KSSubScope* popLocalScope() { KSSubScope* s = m_localScope; m_localScope = 0; return s; }
00173   KSSubScope* localScope() { return m_localScope; }
00174 
00175   void pushModule( KSModule* m ) { Q_ASSERT( m_module == 0 ); m_module = m; m_moduleSpace = m->nameSpace(); }
00176   KSModule* popModule() { KSModule* n = m_module; m_module = 0; return n; }
00177 
00178   KSModule* module() { return m_module; }
00179 
00187   KSValue* object( const QString& name, bool insert = FALSE );
00194   void addObject( const QString& name, const KSValue::Ptr& );
00195 
00196 private:
00197   KSModule* m_module;
00198   const KSNamespace* m_globalSpace;
00202   KSNamespace* m_moduleSpace;
00203   KSSubScope* m_localScope;
00204 };
00205 
00206 class KOSCRIPT_EXPORT KSException : public QShared
00207 {
00208 public:
00209   typedef KSSharedPtr<KSException> Ptr;
00210 
00211   KSException( const QString& _type, const KSValue::Ptr& _ptr, int _line = -1 );
00212   KSException( const QString& _type, const QString& _val, int _line = -1 );
00213   KSException( const KSValue::Ptr& _type, const KSValue::Ptr& ptr, int _line = -1 );
00214   ~KSException() { }
00215 
00216   const QValueList<int>& lines() { return m_lines; }
00217   void addLine( int l ) { if ( m_lines.isEmpty() ) m_lines.append( l ); else if ( m_lines.last() != l ) m_lines.append( l ); }
00218 
00219   QString toString( KSContext& context );
00220   void print( KSContext& context );
00221 
00222   KSValue* type() { return m_type; }
00223   KSValue* value() { return m_value; }
00224 
00225 private:
00226   KSValue::Ptr m_type;
00227   KSValue::Ptr m_value;
00228   QValueList<int> m_lines;
00229 };
00230 
00231 class KOSCRIPT_EXPORT KSContext
00232 {
00233 public:
00234   KSContext();
00235   KSContext( KSContext& c, bool leftexpr = false );
00236   ~KSContext();
00237 
00238   void setValue( const KSValue::Ptr& p ) { m_value = p; }
00244   void setValue( KSValue* p ) { m_value = p; }
00250   KSValue* value() { return m_value; }
00256   KSValue* shareValue() { if ( !m_value ) return 0; m_value->ref(); return m_value; }
00257 
00261   void setExtraData( KSValue* p ) { m_extraData = p; }
00262 
00266   KSValue* extraData() { return m_extraData; }
00267 
00268 
00269   void setException( KSContext& c ) { m_exception = c.exception(); if ( c.exception() ) c.exception()->ref(); }
00270   void setException( KSException::Ptr& p ) { m_exception = p; }
00271   void setException( KSException* p ) { m_exception = p; }
00272   KSException* exception() { return m_exception; }
00273   KSException* shareException() { if ( !m_exception ) return 0; m_exception->ref(); return m_exception; }
00274 
00275   void setScope( KSContext& c ) { m_scope = c.scope(); if ( c.scope() ) c.scope()->ref(); }
00276   void setScope( KSScope::Ptr& p ) { m_scope = p; }
00277   void setScope( KSScope* p ) { m_scope = p; }
00278   KSScope* scope() { return m_scope; }
00279   KSValue* object( const QString& _name ) { if ( !!m_scope ) return m_scope->object( _name, m_bLeftExpr ); return 0; }
00280 
00281   void setLeftExpr( bool b ) { m_bLeftExpr = b; }
00282   bool leftExpr() { return m_bLeftExpr; }
00283 
00287   KSModule* module() { return m_scope->module(); }
00288 
00289   KSInterpreter* interpreter() { return m_scope->module()->interpreter(); }
00290 
00297   bool returnFlag() const { return m_bReturning; }
00303   void clearReturnFlag() { m_bReturning = false; }
00310   void setReturnFlag( bool b = true ) { m_bReturning = b; }
00311 
00312   int tmpInt;
00313 
00314 private:
00315   KSValue::Ptr m_value;
00316   KSValue::Ptr m_extraData;
00317   KSException::Ptr m_exception;
00318   KSScope::Ptr m_scope;
00319   bool m_bLeftExpr;
00320   bool m_bReturning;
00321 };
00322 
00323 #endif
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:05 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003