• 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
25namespace 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
48using namespace KJS;
49
50SourceStream& SourceStream::operator<<(char c)
51{
52 str += UString(c);
53 return *this;
54}
55
56SourceStream& SourceStream::operator<<(const char *s)
57{
58 str += UString(s);
59 return *this;
60}
61
62SourceStream& SourceStream::operator<<(const UString &s)
63{
64 str += s;
65 return *this;
66}
67
68SourceStream& SourceStream::operator<<(const Identifier &s)
69{
70 str += s.ustring();
71 return *this;
72}
73
74SourceStream& SourceStream::operator<<(const Node *n)
75{
76 if (n)
77 n->streamTo(*this);
78 return *this;
79}
80
81SourceStream& 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
98UString 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
116UString Node::toCode() const
117{
118 SourceStream str;
119 streamTo(str);
120
121 return str.toString();
122}
123
124void NullNode::streamTo(SourceStream &s) const { s << "null"; }
125
126void BooleanNode::streamTo(SourceStream &s) const
127{
128 s << (val ? "true" : "false");
129}
130
131void NumberNode::streamTo(SourceStream &s) const { s << UString::from(val); }
132
133void StringNode::streamTo(SourceStream &s) const
134{
135 s << '"' << unescapeStr(val) << '"';
136}
137
138void RegExpNode::streamTo(SourceStream &s) const { s << "/" << pattern << "/" << flags; }
139
140void ThisNode::streamTo(SourceStream &s) const { s << "this"; }
141
142void ResolveNode::streamTo(SourceStream &s) const { s << ident; }
143
144void GroupNode::streamTo(SourceStream &s) const
145{
146 s << "(" << group << ")";
147}
148
149void 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
160void ArrayNode::streamTo(SourceStream &s) const
161{
162 s << "[" << element;
163 for (int i = 0; i < elision; i++)
164 s << ",";
165 s << "]";
166}
167
168void ObjectLiteralNode::streamTo(SourceStream &s) const
169{
170 if (list)
171 s << "{ " << list << " }";
172 else
173 s << "{ }";
174}
175
176void PropertyValueNode::streamTo(SourceStream &s) const
177{
178 for (const PropertyValueNode *n = this; n; n = n->list)
179 s << n->name << ": " << n->assign;
180}
181
182void PropertyNode::streamTo(SourceStream &s) const
183{
184 if (str.isNull())
185 s << UString::from(numeric);
186 else
187 s << str;
188}
189
190void AccessorNode1::streamTo(SourceStream &s) const
191{
192 s << expr1 << "[" << expr2 << "]";
193}
194
195void AccessorNode2::streamTo(SourceStream &s) const
196{
197 s << expr << "." << ident;
198}
199
200void ArgumentListNode::streamTo(SourceStream &s) const
201{
202 s << expr;
203 for (ArgumentListNode *n = list; n; n = n->list)
204 s << ", " << n->expr;
205}
206
207void ArgumentsNode::streamTo(SourceStream &s) const
208{
209 s << "(" << list << ")";
210}
211
212void NewExprNode::streamTo(SourceStream &s) const
213{
214 s << "new " << expr << args;
215}
216
217void FunctionCallNode::streamTo(SourceStream &s) const
218{
219 s << expr << args;
220}
221
222void PostfixNode::streamTo(SourceStream &s) const
223{
224 s << expr;
225 if (oper == OpPlusPlus)
226 s << "++";
227 else
228 s << "--";
229}
230
231void DeleteNode::streamTo(SourceStream &s) const
232{
233 s << "delete " << expr;
234}
235
236void VoidNode::streamTo(SourceStream &s) const
237{
238 s << "void " << expr;
239}
240
241void TypeOfNode::streamTo(SourceStream &s) const
242{
243 s << "typeof " << expr;
244}
245
246void PrefixNode::streamTo(SourceStream &s) const
247{
248 s << (oper == OpPlusPlus ? "++" : "--") << expr;
249}
250
251void UnaryPlusNode::streamTo(SourceStream &s) const
252{
253 s << "+" << expr;
254}
255
256void NegateNode::streamTo(SourceStream &s) const
257{
258 s << "-" << expr;
259}
260
261void BitwiseNotNode::streamTo(SourceStream &s) const
262{
263 s << "~" << expr;
264}
265
266void LogicalNotNode::streamTo(SourceStream &s) const
267{
268 s << "!" << expr;
269}
270
271void MultNode::streamTo(SourceStream &s) const
272{
273 s << term1 << oper << term2;
274}
275
276void AddNode::streamTo(SourceStream &s) const
277{
278 s << term1 << oper << term2;
279}
280
281void AppendStringNode::streamTo(SourceStream &s) const
282{
283 s << term << "+" << '"' << unescapeStr(str) << '"';
284}
285
286void 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
298void 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
326void 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
348void 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
360void BinaryLogicalNode::streamTo(SourceStream &s) const
361{
362 s << expr1 << (oper == OpAnd ? " && " : " || ") << expr2;
363}
364
365void ConditionalNode::streamTo(SourceStream &s) const
366{
367 s << logical << " ? " << expr1 << " : " << expr2;
368}
369
370void 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
417void CommaNode::streamTo(SourceStream &s) const
418{
419 s << expr1 << ", " << expr2;
420}
421
422void StatListNode::streamTo(SourceStream &s) const
423{
424 for (const StatListNode *n = this; n; n = n->list)
425 s << n->statement;
426}
427
428void AssignExprNode::streamTo(SourceStream &s) const
429{
430 s << " = " << expr;
431}
432
433void VarDeclNode::streamTo(SourceStream &s) const
434{
435 s << ident << init;
436}
437
438void VarDeclListNode::streamTo(SourceStream &s) const
439{
440 s << var;
441 for (VarDeclListNode *n = list; n; n = n->list)
442 s << ", " << n->var;
443}
444
445void VarStatementNode::streamTo(SourceStream &s) const
446{
447 s << SourceStream::Endl << "var " << list << ";";
448}
449
450void BlockNode::streamTo(SourceStream &s) const
451{
452 s << SourceStream::Endl << "{" << SourceStream::Indent
453 << source << SourceStream::Unindent << SourceStream::Endl << "}";
454}
455
456void EmptyStatementNode::streamTo(SourceStream &s) const
457{
458 s << SourceStream::Endl << ";";
459}
460
461void ExprStatementNode::streamTo(SourceStream &s) const
462{
463 s << SourceStream::Endl << expr << ";";
464}
465
466void 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
475void DoWhileNode::streamTo(SourceStream &s) const
476{
477 s << SourceStream::Endl << "do " << SourceStream::Indent
478 << statement << SourceStream::Unindent << SourceStream::Endl
479 << "while (" << expr << ");";
480}
481
482void WhileNode::streamTo(SourceStream &s) const
483{
484 s << SourceStream::Endl << "while (" << expr << ")" << SourceStream::Indent
485 << statement << SourceStream::Unindent;
486}
487
488void 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
497void 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
508void ContinueNode::streamTo(SourceStream &s) const
509{
510 s << SourceStream::Endl << "continue";
511 if (!ident.isNull())
512 s << " " << ident;
513 s << ";";
514}
515
516void BreakNode::streamTo(SourceStream &s) const
517{
518 s << SourceStream::Endl << "break";
519 if (!ident.isNull())
520 s << " " << ident;
521 s << ";";
522}
523
524void ReturnNode::streamTo(SourceStream &s) const
525{
526 s << SourceStream::Endl << "return";
527 if (value)
528 s << " " << value;
529 s << ";";
530}
531
532void WithNode::streamTo(SourceStream &s) const
533{
534 s << SourceStream::Endl << "with (" << expr << ") "
535 << statement;
536}
537
538void 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
551void ClauseListNode::streamTo(SourceStream &s) const
552{
553 for (const ClauseListNode *n = this; n; n = n->next())
554 s << n->clause();
555}
556
557void 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
567void SwitchNode::streamTo(SourceStream &s) const
568{
569 s << SourceStream::Endl << "switch (" << expr << ") {"
570 << SourceStream::Indent << block << SourceStream::Unindent
571 << SourceStream::Endl << "}";
572}
573
574void LabelNode::streamTo(SourceStream &s) const
575{
576 s << SourceStream::Endl << label << ":" << SourceStream::Indent
577 << statement << SourceStream::Unindent;
578}
579
580void ThrowNode::streamTo(SourceStream &s) const
581{
582 s << SourceStream::Endl << "throw " << expr << ";";
583}
584
585void CatchNode::streamTo(SourceStream &s) const
586{
587 s << SourceStream::Endl << "catch (" << ident << ")" << block;
588}
589
590void FinallyNode::streamTo(SourceStream &s) const
591{
592 s << SourceStream::Endl << "finally " << block;
593}
594
595void TryNode::streamTo(SourceStream &s) const
596{
597 s << SourceStream::Endl << "try " << block
598 << _catch
599 << _final;
600}
601
602void ParameterNode::streamTo(SourceStream &s) const
603{
604 s << id;
605 for (ParameterNode *n = next; n; n = n->next)
606 s << ", " << n->id;
607}
608
609void FuncDeclNode::streamTo(SourceStream &s) const {
610 s << SourceStream::Endl << "function " << ident << "(";
611 if (param)
612 s << param;
613 s << ")" << body;
614}
615
616void FuncExprNode::streamTo(SourceStream &s) const
617{
618 s << "function " << "("
619 << param
620 << ")" << body;
621}
622
623void 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
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.4
This website is maintained by Timothy Pearson.