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

kjs

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