| # kjs Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# nodes2string.cpp

    00001 // -*- c-basic-offset: 2 -*-
    00002 /*
    00003  *  This file is part of the KDE libraries
    00004  *  Copyright (C) 2002 Harri Porten (porten@kde.org)
    00005  *
    00006  *  This library is free software; you can redistribute it and/or
    00007  *  modify it under the terms of the GNU Lesser General Public
    00008  *  License as published by the Free Software Foundation; either
    00009  *  version 2 of the License, or (at your option) any later version.
    00010  *
    00011  *  This library is distributed in the hope that it will be useful,
    00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014  *  Lesser General Public License for more details.
    00015  *
    00016  *  You should have received a copy of the GNU Lesser General Public License
    00017  *  along with this library; see the file COPYING.LIB.  If not, write to
    00018  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019  *  Boston, MA 02111-1307, USA.
    00020  *
    00021  */
    00022
    00023 #include "nodes.h"
    00024
    00025 namespace KJS {
    00029   class SourceStream {
    00030   public:
    00031     enum Format {
    00032       Endl, Indent, Unindent
    00033     };
    00034
    00035     UString toString() const { return str; }
    00036     SourceStream& operator<<(const KJS::UString &);
    00037     SourceStream& operator<<(Format f);
    00038     SourceStream& operator<<(const Node *);
    00039   private:
    00040     UString str; /* TODO: buffer */
    00041     UString ind;
    00042   };
    00043 }
    00044
    00045 using namespace KJS;
    00046
    00047 SourceStream& SourceStream::operator<<(const KJS::UString &s)
    00048 {
    00049   str += s;
    00050   return *this;
    00051 }
    00052
    00053 SourceStream& SourceStream::operator<<(const Node *n)
    00054 {
    00055   if (n)
    00056     n->streamTo(*this);
    00057   return *this;
    00058 }
    00059
    00060 SourceStream& SourceStream::operator<<(Format f)
    00061 {
    00062   if (f == Endl)
    00063     str += "\n" + ind;
    00064   else if (f == Indent)
    00065     ind += "  ";
    00066   else
    00067     ind = ind.substr(0, ind.size() - 2);
    00068
    00069   return *this;
    00070 }
    00071
    00072 UString Node::toString() const
    00073 {
    00074   SourceStream str;
    00075   streamTo(str);
    00076
    00077   return str.toString();
    00078 }
    00079
    00080 void NullNode::streamTo(SourceStream &s) const { s << "null"; }
    00081
    00082 void BooleanNode::streamTo(SourceStream &s) const
    00083 {
    00084   s << (val ? "true" : "false");
    00085 }
    00086
    00087 void NumberNode::streamTo(SourceStream &s) const { s << UString::from(val); }
    00088
    00089 void StringNode::streamTo(SourceStream &s) const
    00090 {
    00091   s << '"' << val << '"';
    00092 }
    00093
    00094 void RegExpNode::streamTo(SourceStream &s) const { s <<  pattern; }
    00095
    00096 void ThisNode::streamTo(SourceStream &s) const { s << "this"; }
    00097
    00098 void ResolveNode::streamTo(SourceStream &s) const { s << ident; }
    00099
    00100 void GroupNode::streamTo(SourceStream &s) const
    00101 {
    00102   s << "(" << group << ")";
    00103 }
    00104
    00105 void ElisionNode::streamTo(SourceStream &s) const
    00106 {
    00107   if (elision)
    00108     s << elision << ",";
    00109   else
    00110     s << ",";
    00111 }
    00112
    00113 void ElementNode::streamTo(SourceStream &s) const
    00114 {
    00115   if (list)
    00116     s << list << ",";
    00117   s << elision << node;
    00118 }
    00119
    00120 void ArrayNode::streamTo(SourceStream &s) const
    00121 {
    00122   s << "[" << element << elision << "]";
    00123 }
    00124
    00125 void ObjectLiteralNode::streamTo(SourceStream &s) const
    00126 {
    00127   if (list)
    00128     s << "{ " << list << " }";
    00129   else
    00130     s << "{ }";
    00131 }
    00132
    00133 void PropertyValueNode::streamTo(SourceStream &s) const
    00134 {
    00135   if (list)
    00136     s << list << ", ";
    00137   s << name << ": " << assign;
    00138 }
    00139
    00140 void PropertyNode::streamTo(SourceStream &s) const
    00141 {
    00142   if (str.isNull())
    00143     s << UString::from(numeric);
    00144   else
    00145     s << str;
    00146 }
    00147
    00148 void AccessorNode1::streamTo(SourceStream &s) const
    00149 {
    00150   s << expr1 << "[" << expr2 << "]";
    00151 }
    00152
    00153 void AccessorNode2::streamTo(SourceStream &s) const
    00154 {
    00155   s << expr << "." << ident;
    00156 }
    00157
    00158 void ArgumentListNode::streamTo(SourceStream &s) const
    00159 {
    00160   if (list)
    00161     s << list << ", ";
    00162   s << expr;
    00163 }
    00164
    00165 void ArgumentsNode::streamTo(SourceStream &s) const
    00166 {
    00167   s << "(" << list << ")";
    00168 }
    00169
    00170 void NewExprNode::streamTo(SourceStream &s) const
    00171 {
    00172   s << "new " << expr << args;
    00173 }
    00174
    00175 void FunctionCallNode::streamTo(SourceStream &s) const
    00176 {
    00177   s << expr << args;
    00178 }
    00179
    00180 void PostfixNode::streamTo(SourceStream &s) const
    00181 {
    00182   s << expr;
    00183   if (oper == OpPlusPlus)
    00184     s << "++";
    00185   else
    00186     s << "--";
    00187 }
    00188
    00189 void DeleteNode::streamTo(SourceStream &s) const
    00190 {
    00191   s << "delete " << expr;
    00192 }
    00193
    00194 void VoidNode::streamTo(SourceStream &s) const
    00195 {
    00196   s << "void " << expr;
    00197 }
    00198
    00199 void TypeOfNode::streamTo(SourceStream &s) const
    00200 {
    00201   s << "typeof " << expr;
    00202 }
    00203
    00204 void PrefixNode::streamTo(SourceStream &s) const
    00205 {
    00206   s << expr << (oper == OpPlusPlus ? "++" : "--");
    00207 }
    00208
    00209 void UnaryPlusNode::streamTo(SourceStream &s) const
    00210 {
    00211   s << "+" << expr;
    00212 }
    00213
    00214 void NegateNode::streamTo(SourceStream &s) const
    00215 {
    00216   s << "-" << expr;
    00217 }
    00218
    00219 void BitwiseNotNode::streamTo(SourceStream &s) const
    00220 {
    00221   s << "~" << expr;
    00222 }
    00223
    00224 void LogicalNotNode::streamTo(SourceStream &s) const
    00225 {
    00226   s << "!" << expr;
    00227 }
    00228
    00229 void MultNode::streamTo(SourceStream &s) const
    00230 {
    00231   s << term1 << oper << term2;
    00232 }
    00233
    00234 void AddNode::streamTo(SourceStream &s) const
    00235 {
    00236   s << term1 << oper << term2;
    00237 }
    00238
    00239 void ShiftNode::streamTo(SourceStream &s) const
    00240 {
    00241   s << term1;
    00242   if (oper == OpLShift)
    00243     s << "<<";
    00244   else if (oper == OpRShift)
    00245     s << ">>";
    00246   else
    00247     s << ">>>";
    00248   s << term2;
    00249 }
    00250
    00251 void RelationalNode::streamTo(SourceStream &s) const
    00252 {
    00253   s << expr1;
    00254   switch (oper) {
    00255   case OpLess:
    00256     s << " < ";
    00257     break;
    00258   case OpGreater:
    00259     s << " > ";
    00260     break;
    00261   case OpLessEq:
    00262     s << " <= ";
    00263     break;
    00264   case OpGreaterEq:
    00265     s << " >= ";
    00266     break;
    00267   case OpInstanceOf:
    00268     s << " instanceof ";
    00269     break;
    00270   case OpIn:
    00271     s << " in ";
    00272     break;
    00273   default:
    00274     ;
    00275   }
    00276   s << expr2;
    00277 }
    00278
    00279 void EqualNode::streamTo(SourceStream &s) const
    00280 {
    00281   s << expr1;
    00282  switch (oper) {
    00283  case OpEqEq:
    00284    s << " == ";
    00285    break;
    00286  case OpNotEq:
    00287    s << " != ";
    00288    break;
    00289  case OpStrEq:
    00290    s << " === ";
    00291    break;
    00292  case OpStrNEq:
    00293    s << " !== ";
    00294    break;
    00295  default:
    00296    ;
    00297  }
    00298   s << expr2;
    00299 }
    00300
    00301 void BitOperNode::streamTo(SourceStream &s) const
    00302 {
    00303   s << expr1;
    00304   if (oper == OpBitAnd)
    00305     s << " & ";
    00306   else if (oper == OpBitXOr)
    00307     s << " ^ ";
    00308   else
    00309     s << " | ";
    00310   s << expr2;
    00311 }
    00312
    00313 void BinaryLogicalNode::streamTo(SourceStream &s) const
    00314 {
    00315   s << expr1 << (oper == OpAnd ? " && " : " || ") << expr2;
    00316 }
    00317
    00318 void ConditionalNode::streamTo(SourceStream &s) const
    00319 {
    00320   s << logical << " ? " << expr1 << " : " << expr2;
    00321 }
    00322
    00323 void AssignNode::streamTo(SourceStream &s) const
    00324 {
    00325   s << left;
    00326   const char *opStr;
    00327   switch (oper) {
    00328   case OpEqual:
    00329     opStr = " = ";
    00330     break;
    00331   case OpMultEq:
    00332     opStr = " *= ";
    00333     break;
    00334   case OpDivEq:
    00335     opStr = " /= ";
    00336     break;
    00337   case OpPlusEq:
    00338     opStr = " += ";
    00339     break;
    00340   case OpMinusEq:
    00341     opStr = " -= ";
    00342     break;
    00343   case OpLShift:
    00344     opStr = " <<= ";
    00345     break;
    00346   case OpRShift:
    00347     opStr = " >>= ";
    00348     break;
    00349   case OpURShift:
    00350     opStr = " >>= ";
    00351     break;
    00352   case OpAndEq:
    00353     opStr = " &= ";
    00354     break;
    00355   case OpXOrEq:
    00356     opStr = " ^= ";
    00357     break;
    00358   case OpOrEq:
    00359     opStr = " |= ";
    00360     break;
    00361   case OpModEq:
    00362     opStr = " %= ";
    00363     break;
    00364   default:
    00365     opStr = " ?= ";
    00366   }
    00367   s << opStr << expr;
    00368 }
    00369
    00370 void CommaNode::streamTo(SourceStream &s) const
    00371 {
    00372   s << expr1 << ", " << expr2;
    00373 }
    00374
    00375 void StatListNode::streamTo(SourceStream &s) const
    00376 {
    00377   s << list << statement;
    00378 }
    00379
    00380 void AssignExprNode::streamTo(SourceStream &s) const
    00381 {
    00382   s << " = " << expr;
    00383 }
    00384
    00385 void VarDeclNode::streamTo(SourceStream &s) const
    00386 {
    00387   s << ident << init;
    00388 }
    00389
    00390 void VarDeclListNode::streamTo(SourceStream &s) const
    00391 {
    00392   if (list)
    00393     s << list << ", ";
    00394   s << var;
    00395 }
    00396
    00397 void VarStatementNode::streamTo(SourceStream &s) const
    00398 {
    00399   s << SourceStream::Endl << "var " << list << ";";
    00400 }
    00401
    00402 void BlockNode::streamTo(SourceStream &s) const
    00403 {
    00404   s << SourceStream::Endl << "{" << SourceStream::Indent
    00405     << source << SourceStream::Unindent << SourceStream::Endl << "}";
    00406 }
    00407
    00408 void EmptyStatementNode::streamTo(SourceStream &s) const
    00409 {
    00410   s << SourceStream::Endl << ";";
    00411 }
    00412
    00413 void ExprStatementNode::streamTo(SourceStream &s) const
    00414 {
    00415   s << SourceStream::Endl << expr << ";";
    00416 }
    00417
    00418 void IfNode::streamTo(SourceStream &s) const
    00419 {
    00420   s << SourceStream::Endl << "if (" << expr << ")" << SourceStream::Indent
    00421     << statement1 << SourceStream::Unindent;
    00422   if (statement2)
    00423     s << SourceStream::Endl << "else" << SourceStream::Indent
    00424       << statement2 << SourceStream::Unindent;
    00425 }
    00426
    00427 void DoWhileNode::streamTo(SourceStream &s) const
    00428 {
    00429   s << SourceStream::Endl << "do " << SourceStream::Indent
    00430     << statement << SourceStream::Unindent << SourceStream::Endl
    00431     << "while (" << expr << ");";
    00432 }
    00433
    00434 void WhileNode::streamTo(SourceStream &s) const
    00435 {
    00436   s << SourceStream::Endl << "while (" << expr << ")" << SourceStream::Indent
    00437     << statement << SourceStream::Unindent;
    00438 }
    00439
    00440 void ForNode::streamTo(SourceStream &s) const
    00441 {
    00442   s << SourceStream::Endl << "for ("
    00443     << expr1  // TODO: doesn't properly do "var i = 0"
    00444     << "; " << expr2
    00445     << "; " << expr3
    00446     << ")" << SourceStream::Indent << statement << SourceStream::Unindent;
    00447 }
    00448
    00449 void ForInNode::streamTo(SourceStream &s) const
    00450 {
    00451   s << SourceStream::Endl << "for (";
    00452   if (varDecl)
    00453     s << "var " << varDecl;
    00454   if (init)
    00455     s << " = " << init;
    00456   s << " in " << expr << ")" << SourceStream::Indent
    00457     << statement << SourceStream::Unindent;
    00458 }
    00459
    00460 void ContinueNode::streamTo(SourceStream &s) const
    00461 {
    00462   s << SourceStream::Endl << "continue";
    00463   if (!ident.isNull())
    00464     s << " " << ident;
    00465   s << ";";
    00466 }
    00467
    00468 void BreakNode::streamTo(SourceStream &s) const
    00469 {
    00470   s << SourceStream::Endl << "break";
    00471   if (!ident.isNull())
    00472     s << " " << ident;
    00473   s << ";";
    00474 }
    00475
    00476 void ReturnNode::streamTo(SourceStream &s) const
    00477 {
    00478   s << SourceStream::Endl << "return";
    00479   if (value)
    00480     s << " " << value;
    00481   s << ";";
    00482 }
    00483
    00484 void WithNode::streamTo(SourceStream &s) const
    00485 {
    00486   s << SourceStream::Endl << "with (" << expr << ") "
    00487     << statement;
    00488 }
    00489
    00490 void CaseClauseNode::streamTo(SourceStream &s) const
    00491 {
    00492   s << SourceStream::Endl;
    00493   if (expr)
    00494     s << "case " << expr;
    00495   else
    00496     s << "default";
    00497   s << ":" << SourceStream::Indent;
    00498   if (list)
    00499     s << list;
    00500   s << SourceStream::Unindent;
    00501 }
    00502
    00503 void ClauseListNode::streamTo(SourceStream &s) const
    00504 {
    00505   const ClauseListNode *l = this;
    00506   do {
    00507     s << l;
    00508     l = l->nx;
    00509   } while (l);
    00510 }
    00511
    00512 void CaseBlockNode::streamTo(SourceStream &s) const
    00513 {
    00514   const ClauseListNode *cl = list1;
    00515   while (cl) {
    00516     s << cl->clause();
    00517     cl = cl->next();
    00518   }
    00519   if (def)
    00520     s << def;
    00521   cl = list2;
    00522   while (cl) {
    00523     s << cl->clause();
    00524     cl = cl->next();
    00525   }
    00526 }
    00527
    00528 void SwitchNode::streamTo(SourceStream &s) const
    00529 {
    00530   s << SourceStream::Endl << "switch (" << expr << ") {"
    00531     << SourceStream::Indent << block << SourceStream::Unindent
    00532     << SourceStream::Endl << "}";
    00533 }
    00534
    00535 void LabelNode::streamTo(SourceStream &s) const
    00536 {
    00537   s << SourceStream::Endl << label << ":" << SourceStream::Indent
    00538     << statement << SourceStream::Unindent;
    00539 }
    00540
    00541 void ThrowNode::streamTo(SourceStream &s) const
    00542 {
    00543   s << SourceStream::Endl << "throw " << expr << ";";
    00544 }
    00545
    00546 void CatchNode::streamTo(SourceStream &s) const
    00547 {
    00548   s << SourceStream::Endl << "catch (" << ident << ")" << block;
    00549 }
    00550
    00551 void FinallyNode::streamTo(SourceStream &s) const
    00552 {
    00553   s << SourceStream::Endl << "finally " << block;
    00554 }
    00555
    00556 void TryNode::streamTo(SourceStream &s) const
    00557 {
    00558   s << "try " << block
    00559     << _catch
    00560     << _final;
    00561 }
    00562
    00563 void ParameterNode::streamTo(SourceStream &s) const
    00564 {
    00565   s << id;
    00566   if (next)
    00567     s << ", " << next;
    00568 }
    00569
    00570 void FunctionBodyNode::streamTo(SourceStream &s) const {
    00571   s << SourceStream::Endl << "{" << SourceStream::Indent
    00572     << source << SourceStream::Unindent << SourceStream::Endl << "}";
    00573 }
    00574
    00575 void FuncDeclNode::streamTo(SourceStream &s) const {
    00576   s << "function " << ident << "(";
    00577   if (param)
    00578     s << param;
    00579   s << ")" << body;
    00580 }
    00581
    00582 void FuncExprNode::streamTo(SourceStream &s) const
    00583 {
    00584   s << "function " << "("
    00585     << param
    00586     << ")" << body;
    00587 }
    00588
    00589 void SourceElementNode::streamTo(SourceStream &s) const
    00590 {
    00591   if (statement)
    00592     s << statement;
    00593   else
    00594     s << function;
    00595 }
    00596
    00597 void SourceElementsNode::streamTo(SourceStream &s) const
    00598 {
    00599   s << elements << element;
    00600 }
    00601

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:32 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001