• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
nodes2string.cpp
1 // -*- c-basic-offset: 2 -*-
2 /*
3  * This file is part of the KDE libraries
4  * Copyright (C) 2002 Harri Porten (porten@kde.org)
5  * Copyright (C) 2003 Apple Computer, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23 
24 #include "nodes.h"
25 
26 namespace KJS {
30  class SourceStream {
31  public:
32  enum Format {
33  Endl, Indent, Unindent
34  };
35 
36  UString toString() const { return str; }
37  SourceStream& operator<<(const Identifier &);
38  SourceStream& operator<<(const KJS::UString &);
39  SourceStream& operator<<(const char *);
40  SourceStream& operator<<(char);
41  SourceStream& operator<<(Format f);
42  SourceStream& operator<<(const Node *);
43  private:
44  UString str; /* TODO: buffer */
45  UString ind;
46  };
47 }
48 
49 using namespace KJS;
50 
51 SourceStream& SourceStream::operator<<(char c)
52 {
53  str += UString(c);
54  return *this;
55 }
56 
57 SourceStream& SourceStream::operator<<(const char *s)
58 {
59  str += UString(s);
60  return *this;
61 }
62 
63 SourceStream& SourceStream::operator<<(const UString &s)
64 {
65  str += s;
66  return *this;
67 }
68 
69 SourceStream& SourceStream::operator<<(const Identifier &s)
70 {
71  str += s.ustring();
72  return *this;
73 }
74 
75 SourceStream& SourceStream::operator<<(const Node *n)
76 {
77  if (n)
78  n->streamTo(*this);
79  return *this;
80 }
81 
82 SourceStream& SourceStream::operator<<(Format f)
83 {
84  switch (f) {
85  case Endl:
86  str += "\n" + ind;
87  break;
88  case Indent:
89  ind += " ";
90  break;
91  case Unindent:
92  ind = ind.substr(0, ind.size() - 2);
93  break;
94  }
95 
96  return *this;
97 }
98 
99 UString unescapeStr(UString str)
100 {
101  UString unescaped = "";
102  int i = 0;
103  int copied = 0;
104  for (i = 0; i <= str.size(); i++) {
105  if (str[i] == '"') {
106  if (copied < i)
107  unescaped += str.substr(copied,i-copied);
108  copied = i+1;
109  unescaped += "\\\"";
110  }
111  }
112  if (copied < i)
113  unescaped += str.substr(copied,i-copied);
114  return unescaped;
115 }
116 
117 UString Node::toCode() const
118 {
119  SourceStream str;
120  streamTo(str);
121 
122  return str.toString();
123 }
124 
125 void NullNode::streamTo(SourceStream &s) const { s << "null"; }
126 
127 void BooleanNode::streamTo(SourceStream &s) const
128 {
129  s << (val ? "true" : "false");
130 }
131 
132 void NumberNode::streamTo(SourceStream &s) const { s << UString::from(val); }
133 
134 void StringNode::streamTo(SourceStream &s) const
135 {
136  s << '"' << unescapeStr(val) << '"';
137 }
138 
139 void RegExpNode::streamTo(SourceStream &s) const { s << "/" << pattern << "/" << flags; }
140 
141 void ThisNode::streamTo(SourceStream &s) const { s << "this"; }
142 
143 void ResolveNode::streamTo(SourceStream &s) const { s << ident; }
144 
145 void GroupNode::streamTo(SourceStream &s) const
146 {
147  s << "(" << group << ")";
148 }
149 
150 void ElementNode::streamTo(SourceStream &s) const
151 {
152  for (const ElementNode *n = this; n; n = n->list) {
153  for (int i = 0; i < n->elision; i++)
154  s << ",";
155  s << n->node;
156  if ( n->list )
157  s << ",";
158  }
159 }
160 
161 void ArrayNode::streamTo(SourceStream &s) const
162 {
163  s << "[" << element;
164  for (int i = 0; i < elision; i++)
165  s << ",";
166  s << "]";
167 }
168 
169 void ObjectLiteralNode::streamTo(SourceStream &s) const
170 {
171  if (list)
172  s << "{ " << list << " }";
173  else
174  s << "{ }";
175 }
176 
177 void PropertyValueNode::streamTo(SourceStream &s) const
178 {
179  for (const PropertyValueNode *n = this; n; n = n->list)
180  s << n->name << ": " << n->assign;
181 }
182 
183 void PropertyNode::streamTo(SourceStream &s) const
184 {
185  if (str.isNull())
186  s << UString::from(numeric);
187  else
188  s << str;
189 }
190 
191 void AccessorNode1::streamTo(SourceStream &s) const
192 {
193  s << expr1 << "[" << expr2 << "]";
194 }
195 
196 void AccessorNode2::streamTo(SourceStream &s) const
197 {
198  s << expr << "." << ident;
199 }
200 
201 void ArgumentListNode::streamTo(SourceStream &s) const
202 {
203  s << expr;
204  for (ArgumentListNode *n = list; n; n = n->list)
205  s << ", " << n->expr;
206 }
207 
208 void ArgumentsNode::streamTo(SourceStream &s) const
209 {
210  s << "(" << list << ")";
211 }
212 
213 void NewExprNode::streamTo(SourceStream &s) const
214 {
215  s << "new " << expr << args;
216 }
217 
218 void FunctionCallNode::streamTo(SourceStream &s) const
219 {
220  s << expr << args;
221 }
222 
223 void PostfixNode::streamTo(SourceStream &s) const
224 {
225  s << expr;
226  if (oper == OpPlusPlus)
227  s << "++";
228  else
229  s << "--";
230 }
231 
232 void DeleteNode::streamTo(SourceStream &s) const
233 {
234  s << "delete " << expr;
235 }
236 
237 void VoidNode::streamTo(SourceStream &s) const
238 {
239  s << "void " << expr;
240 }
241 
242 void TypeOfNode::streamTo(SourceStream &s) const
243 {
244  s << "typeof " << expr;
245 }
246 
247 void PrefixNode::streamTo(SourceStream &s) const
248 {
249  s << (oper == OpPlusPlus ? "++" : "--") << expr;
250 }
251 
252 void UnaryPlusNode::streamTo(SourceStream &s) const
253 {
254  s << "+" << expr;
255 }
256 
257 void NegateNode::streamTo(SourceStream &s) const
258 {
259  s << "-" << expr;
260 }
261 
262 void BitwiseNotNode::streamTo(SourceStream &s) const
263 {
264  s << "~" << expr;
265 }
266 
267 void LogicalNotNode::streamTo(SourceStream &s) const
268 {
269  s << "!" << expr;
270 }
271 
272 void MultNode::streamTo(SourceStream &s) const
273 {
274  s << term1 << oper << term2;
275 }
276 
277 void AddNode::streamTo(SourceStream &s) const
278 {
279  s << term1 << oper << term2;
280 }
281 
282 void AppendStringNode::streamTo(SourceStream &s) const
283 {
284  s << term << "+" << '"' << unescapeStr(str) << '"';
285 }
286 
287 void ShiftNode::streamTo(SourceStream &s) const
288 {
289  s << term1;
290  if (oper == OpLShift)
291  s << "<<";
292  else if (oper == OpRShift)
293  s << ">>";
294  else
295  s << ">>>";
296  s << term2;
297 }
298 
299 void RelationalNode::streamTo(SourceStream &s) const
300 {
301  s << expr1;
302  switch (oper) {
303  case OpLess:
304  s << " < ";
305  break;
306  case OpGreater:
307  s << " > ";
308  break;
309  case OpLessEq:
310  s << " <= ";
311  break;
312  case OpGreaterEq:
313  s << " >= ";
314  break;
315  case OpInstanceOf:
316  s << " instanceof ";
317  break;
318  case OpIn:
319  s << " in ";
320  break;
321  default:
322  ;
323  }
324  s << expr2;
325 }
326 
327 void EqualNode::streamTo(SourceStream &s) const
328 {
329  s << expr1;
330  switch (oper) {
331  case OpEqEq:
332  s << " == ";
333  break;
334  case OpNotEq:
335  s << " != ";
336  break;
337  case OpStrEq:
338  s << " === ";
339  break;
340  case OpStrNEq:
341  s << " !== ";
342  break;
343  default:
344  ;
345  }
346  s << expr2;
347 }
348 
349 void BitOperNode::streamTo(SourceStream &s) const
350 {
351  s << expr1;
352  if (oper == OpBitAnd)
353  s << " & ";
354  else if (oper == OpBitXOr)
355  s << " ^ ";
356  else
357  s << " | ";
358  s << expr2;
359 }
360 
361 void BinaryLogicalNode::streamTo(SourceStream &s) const
362 {
363  s << expr1 << (oper == OpAnd ? " && " : " || ") << expr2;
364 }
365 
366 void ConditionalNode::streamTo(SourceStream &s) const
367 {
368  s << logical << " ? " << expr1 << " : " << expr2;
369 }
370 
371 void AssignNode::streamTo(SourceStream &s) const
372 {
373  s << left;
374  const char *opStr;
375  switch (oper) {
376  case OpEqual:
377  opStr = " = ";
378  break;
379  case OpMultEq:
380  opStr = " *= ";
381  break;
382  case OpDivEq:
383  opStr = " /= ";
384  break;
385  case OpPlusEq:
386  opStr = " += ";
387  break;
388  case OpMinusEq:
389  opStr = " -= ";
390  break;
391  case OpLShift:
392  opStr = " <<= ";
393  break;
394  case OpRShift:
395  opStr = " >>= ";
396  break;
397  case OpURShift:
398  opStr = " >>= ";
399  break;
400  case OpAndEq:
401  opStr = " &= ";
402  break;
403  case OpXOrEq:
404  opStr = " ^= ";
405  break;
406  case OpOrEq:
407  opStr = " |= ";
408  break;
409  case OpModEq:
410  opStr = " %= ";
411  break;
412  default:
413  opStr = " ?= ";
414  }
415  s << opStr << expr;
416 }
417 
418 void CommaNode::streamTo(SourceStream &s) const
419 {
420  s << expr1 << ", " << expr2;
421 }
422 
423 void StatListNode::streamTo(SourceStream &s) const
424 {
425  for (const StatListNode *n = this; n; n = n->list)
426  s << n->statement;
427 }
428 
429 void AssignExprNode::streamTo(SourceStream &s) const
430 {
431  s << " = " << expr;
432 }
433 
434 void VarDeclNode::streamTo(SourceStream &s) const
435 {
436  s << ident << init;
437 }
438 
439 void VarDeclListNode::streamTo(SourceStream &s) const
440 {
441  s << var;
442  for (VarDeclListNode *n = list; n; n = n->list)
443  s << ", " << n->var;
444 }
445 
446 void VarStatementNode::streamTo(SourceStream &s) const
447 {
448  s << SourceStream::Endl << "var " << list << ";";
449 }
450 
451 void BlockNode::streamTo(SourceStream &s) const
452 {
453  s << SourceStream::Endl << "{" << SourceStream::Indent
454  << source << SourceStream::Unindent << SourceStream::Endl << "}";
455 }
456 
457 void EmptyStatementNode::streamTo(SourceStream &s) const
458 {
459  s << SourceStream::Endl << ";";
460 }
461 
462 void ExprStatementNode::streamTo(SourceStream &s) const
463 {
464  s << SourceStream::Endl << expr << ";";
465 }
466 
467 void IfNode::streamTo(SourceStream &s) const
468 {
469  s << SourceStream::Endl << "if (" << expr << ")" << SourceStream::Indent
470  << statement1 << SourceStream::Unindent;
471  if (statement2)
472  s << SourceStream::Endl << "else" << SourceStream::Indent
473  << statement2 << SourceStream::Unindent;
474 }
475 
476 void DoWhileNode::streamTo(SourceStream &s) const
477 {
478  s << SourceStream::Endl << "do " << SourceStream::Indent
479  << statement << SourceStream::Unindent << SourceStream::Endl
480  << "while (" << expr << ");";
481 }
482 
483 void WhileNode::streamTo(SourceStream &s) const
484 {
485  s << SourceStream::Endl << "while (" << expr << ")" << SourceStream::Indent
486  << statement << SourceStream::Unindent;
487 }
488 
489 void ForNode::streamTo(SourceStream &s) const
490 {
491  s << SourceStream::Endl << "for ("
492  << expr1 // TODO: doesn't properly do "var i = 0"
493  << "; " << expr2
494  << "; " << expr3
495  << ")" << SourceStream::Indent << statement << SourceStream::Unindent;
496 }
497 
498 void ForInNode::streamTo(SourceStream &s) const
499 {
500  s << SourceStream::Endl << "for (";
501  if (varDecl)
502  s << "var " << varDecl;
503  if (init)
504  s << " = " << init;
505  s << " in " << expr << ")" << SourceStream::Indent
506  << statement << SourceStream::Unindent;
507 }
508 
509 void ContinueNode::streamTo(SourceStream &s) const
510 {
511  s << SourceStream::Endl << "continue";
512  if (!ident.isNull())
513  s << " " << ident;
514  s << ";";
515 }
516 
517 void BreakNode::streamTo(SourceStream &s) const
518 {
519  s << SourceStream::Endl << "break";
520  if (!ident.isNull())
521  s << " " << ident;
522  s << ";";
523 }
524 
525 void ReturnNode::streamTo(SourceStream &s) const
526 {
527  s << SourceStream::Endl << "return";
528  if (value)
529  s << " " << value;
530  s << ";";
531 }
532 
533 void WithNode::streamTo(SourceStream &s) const
534 {
535  s << SourceStream::Endl << "with (" << expr << ") "
536  << statement;
537 }
538 
539 void CaseClauseNode::streamTo(SourceStream &s) const
540 {
541  s << SourceStream::Endl;
542  if (expr)
543  s << "case " << expr;
544  else
545  s << "default";
546  s << ":" << SourceStream::Indent;
547  if (list)
548  s << list;
549  s << SourceStream::Unindent;
550 }
551 
552 void ClauseListNode::streamTo(SourceStream &s) const
553 {
554  for (const ClauseListNode *n = this; n; n = n->next())
555  s << n->clause();
556 }
557 
558 void CaseBlockNode::streamTo(SourceStream &s) const
559 {
560  for (const ClauseListNode *n = list1; n; n = n->next())
561  s << n->clause();
562  if (def)
563  s << def;
564  for (const ClauseListNode *n = list2; n; n = n->next())
565  s << n->clause();
566 }
567 
568 void SwitchNode::streamTo(SourceStream &s) const
569 {
570  s << SourceStream::Endl << "switch (" << expr << ") {"
571  << SourceStream::Indent << block << SourceStream::Unindent
572  << SourceStream::Endl << "}";
573 }
574 
575 void LabelNode::streamTo(SourceStream &s) const
576 {
577  s << SourceStream::Endl << label << ":" << SourceStream::Indent
578  << statement << SourceStream::Unindent;
579 }
580 
581 void ThrowNode::streamTo(SourceStream &s) const
582 {
583  s << SourceStream::Endl << "throw " << expr << ";";
584 }
585 
586 void CatchNode::streamTo(SourceStream &s) const
587 {
588  s << SourceStream::Endl << "catch (" << ident << ")" << block;
589 }
590 
591 void FinallyNode::streamTo(SourceStream &s) const
592 {
593  s << SourceStream::Endl << "finally " << block;
594 }
595 
596 void TryNode::streamTo(SourceStream &s) const
597 {
598  s << SourceStream::Endl << "try " << block
599  << _catch
600  << _final;
601 }
602 
603 void ParameterNode::streamTo(SourceStream &s) const
604 {
605  s << id;
606  for (ParameterNode *n = next; n; n = n->next)
607  s << ", " << n->id;
608 }
609 
610 void FuncDeclNode::streamTo(SourceStream &s) const {
611  s << SourceStream::Endl << "function " << ident << "(";
612  if (param)
613  s << param;
614  s << ")" << body;
615 }
616 
617 void FuncExprNode::streamTo(SourceStream &s) const
618 {
619  s << "function " << "("
620  << param
621  << ")" << body;
622 }
623 
624 void SourceElementsNode::streamTo(SourceStream &s) const
625 {
626  for (const SourceElementsNode *n = this; n; n = n->elements)
627  s << n->element;
628 }
629 
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32
KJS::Identifier::ustring
const UString & ustring() const
returns a UString of the identifier
Definition: identifier.h:52
KJS::UString
Unicode string class.
Definition: ustring.h:190
KJS::UString::size
int size() const
Definition: ustring.h:360
KJS::UString::substr
UString substr(int pos=0, int len=-1) const
Definition: ustring.cpp:869
KJS::UString::from
static UString from(int i)
Constructs a string from an int.
Definition: ustring.cpp:341
operator<<
kdbgstream & operator<<(const TQValueList< T > &list)
KStdAccel::label
TQString label(StdAccel id)

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kjs by doxygen 1.9.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |