source: trunk/src/script/qscriptprettypretty.cpp@ 397

Last change on this file since 397 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 27.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtScript module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qscriptprettypretty_p.h"
43
44#ifndef QT_NO_SCRIPT
45
46#include "qscriptengine_p.h"
47#include "qscriptvalueimpl_p.h"
48#include "qscriptcontext_p.h"
49#include "qscriptmember_p.h"
50#include "qscriptobject_p.h"
51#include "qscriptast_p.h"
52
53#include <QtCore/QString>
54#include <QtCore/QTextStream>
55#include <QtCore/QtDebug>
56
57QT_BEGIN_NAMESPACE
58
59namespace QScript {
60QString numberToString(qsreal value);
61}
62
63using namespace QScript;
64
65PrettyPretty::PrettyPretty(QTextStream &o):
66 out(o), m_indentLevel(0)
67{
68}
69
70PrettyPretty::~PrettyPretty()
71{
72}
73
74void PrettyPretty::acceptAsBlock(AST::Node *node)
75{
76 out << "{";
77 pushIndentLevel();
78 newlineAndIndent();
79 accept(node);
80 popIndentLevel();
81 newlineAndIndent();
82 out << "}";
83}
84
85int PrettyPretty::operatorPrecedenceLevel(int op)
86{
87 switch (op) {
88 case QSOperator::Div:
89 case QSOperator::Mod:
90 case QSOperator::Mul:
91 return 5;
92 case QSOperator::Add:
93 case QSOperator::Sub:
94 return 6;
95 case QSOperator::LShift:
96 case QSOperator::RShift:
97 case QSOperator::URShift:
98 return 7;
99 case QSOperator::Ge:
100 case QSOperator::Gt:
101 case QSOperator::In:
102 case QSOperator::InstanceOf:
103 case QSOperator::Le:
104 case QSOperator::Lt:
105 return 8;
106 case QSOperator::Equal:
107 case QSOperator::NotEqual:
108 case QSOperator::StrictEqual:
109 case QSOperator::StrictNotEqual:
110 return 9;
111 case QSOperator::BitAnd:
112 return 10;
113 case QSOperator::BitXor:
114 return 11;
115 case QSOperator::BitOr:
116 return 12;
117 case QSOperator::And:
118 return 13;
119 case QSOperator::Or:
120 return 14;
121 case QSOperator::InplaceAnd:
122 case QSOperator::InplaceSub:
123 case QSOperator::InplaceDiv:
124 case QSOperator::InplaceAdd:
125 case QSOperator::InplaceLeftShift:
126 case QSOperator::InplaceMod:
127 case QSOperator::InplaceMul:
128 case QSOperator::InplaceOr:
129 case QSOperator::InplaceRightShift:
130 case QSOperator::InplaceURightShift:
131 case QSOperator::InplaceXor:
132 case QSOperator::Assign:
133 return 16;
134 default:
135 Q_ASSERT_X(false, "PrettyPretty::operatorPrecedenceLevel()", "bad operator");
136 }
137 return 0;
138}
139
140int PrettyPretty::compareOperatorPrecedence(int op1, int op2)
141{
142 int prec1 = operatorPrecedenceLevel(op1);
143 int prec2 = operatorPrecedenceLevel(op2);
144 if (prec1 == prec2)
145 return 0;
146 if (prec1 > prec2)
147 return -1;
148 return 1;
149}
150
151QTextStream &PrettyPretty::operator () (AST::Node *node, int level)
152{
153 int was = indentLevel(level);
154 accept(node);
155 indentLevel(was);
156 return out;
157}
158
159QTextStream &PrettyPretty::newlineAndIndent()
160{
161 enum { IND = 4 };
162 out << endl << QString().fill(QLatin1Char(' '), m_indentLevel * IND);
163 return out;
164}
165
166void PrettyPretty::accept(AST::Node *node)
167{
168 AST::Node::acceptChild(node, this);
169}
170
171bool PrettyPretty::visit(AST::ThisExpression *node)
172{
173 Q_UNUSED(node);
174 out << "this";
175 return true;
176}
177
178void PrettyPretty::endVisit(AST::ThisExpression *node)
179{
180 Q_UNUSED(node);
181}
182
183bool PrettyPretty::visit(AST::IdentifierExpression *node)
184{
185 out << QScriptEnginePrivate::toString(node->name);
186 return true;
187}
188
189void PrettyPretty::endVisit(AST::IdentifierExpression *node)
190{
191 Q_UNUSED(node);
192}
193
194bool PrettyPretty::visit(AST::NullExpression *node)
195{
196 Q_UNUSED(node);
197 out << "null";
198 return false;
199}
200
201void PrettyPretty::endVisit(AST::NullExpression *node)
202{
203 Q_UNUSED(node);
204}
205
206bool PrettyPretty::visit(AST::TrueLiteral *node)
207{
208 Q_UNUSED(node);
209 out << "true";
210 return false;
211}
212
213void PrettyPretty::endVisit(AST::TrueLiteral *node)
214{
215 Q_UNUSED(node);
216}
217
218bool PrettyPretty::visit(AST::FalseLiteral *node)
219{
220 Q_UNUSED(node);
221 out << "false";
222 return false;
223}
224
225void PrettyPretty::endVisit(AST::FalseLiteral *node)
226{
227 Q_UNUSED(node);
228}
229
230bool PrettyPretty::visit(AST::StringLiteral *node)
231{
232 QString lit = QScriptEnginePrivate::toString(node->value);
233 lit.replace(QLatin1String("\\"), QLatin1String("\\\\"));
234 out << "\"" << lit << "\"";
235 return false;
236}
237
238void PrettyPretty::endVisit(AST::StringLiteral *node)
239{
240 Q_UNUSED(node);
241}
242
243bool PrettyPretty::visit(AST::NumericLiteral *node)
244{
245 out << QScript::numberToString(node->value);
246 return true;
247}
248
249void PrettyPretty::endVisit(AST::NumericLiteral *node)
250{
251 Q_UNUSED(node);
252}
253
254bool PrettyPretty::visit(AST::RegExpLiteral *node)
255{
256 out << "/" << QScriptEnginePrivate::toString(node->pattern) << "/";
257 if (node->flags)
258 out << QScript::Ecma::RegExp::flagsToString(node->flags);
259
260 return true;
261}
262
263void PrettyPretty::endVisit(AST::RegExpLiteral *node)
264{
265 Q_UNUSED(node);
266}
267
268bool PrettyPretty::visit(AST::ArrayLiteral *node)
269{
270 out << "[";
271 accept(node->elements);
272 accept(node->elision);
273 out << "]";
274 return false;
275}
276
277void PrettyPretty::endVisit(AST::ArrayLiteral *node)
278{
279 Q_UNUSED(node);
280}
281
282bool PrettyPretty::visit(AST::ObjectLiteral *node)
283{
284 out << "{";
285 if (node->properties) {
286 pushIndentLevel();
287 AST::PropertyNameAndValueList *prop;
288 for (prop = node->properties; prop != 0; prop = prop->next) {
289 newlineAndIndent();
290 accept(prop);
291 if (prop->next)
292 out << ",";
293 }
294 popIndentLevel();
295 newlineAndIndent();
296 }
297 out << "}";
298 return false;
299}
300
301void PrettyPretty::endVisit(AST::ObjectLiteral *node)
302{
303 Q_UNUSED(node);
304}
305
306bool PrettyPretty::visit(AST::ElementList *node)
307{
308 accept(node->elision);
309 accept(node->expression);
310 for (node = node->next; node != 0; node = node->next) {
311 out << ", ";
312 accept(node->elision);
313 accept(node->expression);
314 }
315 return false;
316}
317
318void PrettyPretty::endVisit(AST::ElementList *node)
319{
320 Q_UNUSED(node);
321}
322
323bool PrettyPretty::visit(AST::Elision *node)
324{
325 out << ", ";
326 for (AST::Elision *eit = node->next; eit != 0; eit = eit->next)
327 out << ", ";
328 return false;
329}
330
331void PrettyPretty::endVisit(AST::Elision *node)
332{
333 Q_UNUSED(node);
334}
335
336bool PrettyPretty::visit(AST::PropertyNameAndValueList *node)
337{
338 accept(node->name);
339 out << ": ";
340 accept(node->value);
341 return false;
342}
343
344void PrettyPretty::endVisit(AST::PropertyNameAndValueList *node)
345{
346 Q_UNUSED(node);
347}
348
349bool PrettyPretty::visit(AST::IdentifierPropertyName *node)
350{
351 out << QScriptEnginePrivate::toString(node->id);
352 return false;
353}
354
355void PrettyPretty::endVisit(AST::IdentifierPropertyName *node)
356{
357 Q_UNUSED(node);
358}
359
360bool PrettyPretty::visit(AST::StringLiteralPropertyName *node)
361{
362 QString lit = QScriptEnginePrivate::toString(node->id);
363 lit.replace(QLatin1String("\\"), QLatin1String("\\\\"));
364 out << lit;
365 return false;
366}
367
368void PrettyPretty::endVisit(AST::StringLiteralPropertyName *node)
369{
370 Q_UNUSED(node);
371}
372
373bool PrettyPretty::visit(AST::NumericLiteralPropertyName *node)
374{
375 out << node->id;
376 return false;
377}
378
379void PrettyPretty::endVisit(AST::NumericLiteralPropertyName *node)
380{
381 Q_UNUSED(node);
382}
383
384bool PrettyPretty::visit(AST::ArrayMemberExpression *node)
385{
386 accept(node->base);
387 out << "[";
388 accept(node->expression);
389 out << "]";
390 return false;
391}
392
393void PrettyPretty::endVisit(AST::ArrayMemberExpression *node)
394{
395 Q_UNUSED(node);
396}
397
398bool PrettyPretty::visit(AST::FieldMemberExpression *node)
399{
400 accept(node->base);
401 out << "." << QScriptEnginePrivate::toString(node->name);
402 return false;
403}
404
405void PrettyPretty::endVisit(AST::FieldMemberExpression *node)
406{
407 Q_UNUSED(node);
408}
409
410bool PrettyPretty::visit(AST::NewMemberExpression *node)
411{
412 out << "new ";
413 accept(node->base);
414 out << "(";
415 accept(node->arguments);
416 out << ")";
417 return false;
418}
419
420void PrettyPretty::endVisit(AST::NewMemberExpression *node)
421{
422 Q_UNUSED(node);
423}
424
425bool PrettyPretty::visit(AST::NewExpression *node)
426{
427 Q_UNUSED(node);
428 out << "new ";
429 return true;
430}
431
432void PrettyPretty::endVisit(AST::NewExpression *node)
433{
434 Q_UNUSED(node);
435}
436
437bool PrettyPretty::visit(AST::CallExpression *node)
438{
439 accept(node->base);
440 out << "(";
441 accept(node->arguments);
442 out << ")";
443 return false;
444}
445
446void PrettyPretty::endVisit(AST::CallExpression *node)
447{
448 Q_UNUSED(node);
449}
450
451bool PrettyPretty::visit(AST::ArgumentList *node)
452{
453 accept(node->expression);
454 for (node = node->next; node != 0; node = node->next) {
455 out << ", ";
456 accept(node->expression);
457 }
458 return false;
459}
460
461void PrettyPretty::endVisit(AST::ArgumentList *node)
462{
463 Q_UNUSED(node);
464}
465
466bool PrettyPretty::visit(AST::PostIncrementExpression *node)
467{
468 Q_UNUSED(node);
469 return true;
470}
471
472void PrettyPretty::endVisit(AST::PostIncrementExpression *node)
473{
474 Q_UNUSED(node);
475 out << "++";
476}
477
478bool PrettyPretty::visit(AST::PostDecrementExpression *node)
479{
480 Q_UNUSED(node);
481 return true;
482}
483
484void PrettyPretty::endVisit(AST::PostDecrementExpression *node)
485{
486 Q_UNUSED(node);
487 out << "--";
488}
489
490bool PrettyPretty::visit(AST::DeleteExpression *node)
491{
492 Q_UNUSED(node);
493 out << "delete ";
494 return true;
495}
496
497void PrettyPretty::endVisit(AST::DeleteExpression *node)
498{
499 Q_UNUSED(node);
500}
501
502bool PrettyPretty::visit(AST::VoidExpression *node)
503{
504 Q_UNUSED(node);
505 out << "void ";
506 return true;
507}
508
509void PrettyPretty::endVisit(AST::VoidExpression *node)
510{
511 Q_UNUSED(node);
512}
513
514bool PrettyPretty::visit(AST::TypeOfExpression *node)
515{
516 Q_UNUSED(node);
517 out << "typeof ";
518 return true;
519}
520
521void PrettyPretty::endVisit(AST::TypeOfExpression *node)
522{
523 Q_UNUSED(node);
524}
525
526bool PrettyPretty::visit(AST::PreIncrementExpression *node)
527{
528 Q_UNUSED(node);
529 out << "++";
530 return true;
531}
532
533void PrettyPretty::endVisit(AST::PreIncrementExpression *node)
534{
535 Q_UNUSED(node);
536}
537
538bool PrettyPretty::visit(AST::PreDecrementExpression *node)
539{
540 Q_UNUSED(node);
541 out << "--";
542 return true;
543}
544
545void PrettyPretty::endVisit(AST::PreDecrementExpression *node)
546{
547 Q_UNUSED(node);
548}
549
550bool PrettyPretty::visit(AST::UnaryPlusExpression *node)
551{
552 out << "+";
553 bool needParens = (node->expression->binaryExpressionCast() != 0);
554 if (needParens)
555 out << "(";
556 accept(node->expression);
557 if (needParens)
558 out << ")";
559 return false;
560}
561
562void PrettyPretty::endVisit(AST::UnaryPlusExpression *node)
563{
564 Q_UNUSED(node);
565}
566
567bool PrettyPretty::visit(AST::UnaryMinusExpression *node)
568{
569 out << "-";
570 bool needParens = (node->expression->binaryExpressionCast() != 0);
571 if (needParens)
572 out << "(";
573 accept(node->expression);
574 if (needParens)
575 out << ")";
576 return false;
577}
578
579void PrettyPretty::endVisit(AST::UnaryMinusExpression *node)
580{
581 Q_UNUSED(node);
582}
583
584bool PrettyPretty::visit(AST::TildeExpression *node)
585{
586 out << "~";
587 bool needParens = (node->expression->binaryExpressionCast() != 0);
588 if (needParens)
589 out << "(";
590 accept(node->expression);
591 if (needParens)
592 out << ")";
593 return false;
594}
595
596void PrettyPretty::endVisit(AST::TildeExpression *node)
597{
598 Q_UNUSED(node);
599}
600
601bool PrettyPretty::visit(AST::NotExpression *node)
602{
603 out << "!";
604 bool needParens = (node->expression->binaryExpressionCast() != 0);
605 if (needParens)
606 out << "(";
607 accept(node->expression);
608 if (needParens)
609 out << ")";
610 return false;
611}
612
613void PrettyPretty::endVisit(AST::NotExpression *node)
614{
615 Q_UNUSED(node);
616}
617
618bool PrettyPretty::visit(AST::BinaryExpression *node)
619{
620 bool needParens = node->left->binaryExpressionCast()
621 && (compareOperatorPrecedence(node->left->binaryExpressionCast()->op, node->op) < 0);
622 if (needParens)
623 out << "(";
624 accept(node->left);
625 if (needParens)
626 out << ")";
627 QString s;
628 switch (node->op) {
629 case QSOperator::Add:
630 s = QLatin1String("+"); break;
631 case QSOperator::And:
632 s = QLatin1String("&&"); break;
633 case QSOperator::InplaceAnd:
634 s = QLatin1String("&="); break;
635 case QSOperator::Assign:
636 s = QLatin1String("="); break;
637 case QSOperator::BitAnd:
638 s = QLatin1String("&"); break;
639 case QSOperator::BitOr:
640 s = QLatin1String("|"); break;
641 case QSOperator::BitXor:
642 s = QLatin1String("^"); break;
643 case QSOperator::InplaceSub:
644 s = QLatin1String("-="); break;
645 case QSOperator::Div:
646 s = QLatin1String("/"); break;
647 case QSOperator::InplaceDiv:
648 s = QLatin1String("/="); break;
649 case QSOperator::Equal:
650 s = QLatin1String("=="); break;
651 case QSOperator::Ge:
652 s = QLatin1String(">="); break;
653 case QSOperator::Gt:
654 s = QLatin1String(">"); break;
655 case QSOperator::In:
656 s = QLatin1String("in"); break;
657 case QSOperator::InplaceAdd:
658 s = QLatin1String("+="); break;
659 case QSOperator::InstanceOf:
660 s = QLatin1String("instanceof"); break;
661 case QSOperator::Le:
662 s = QLatin1String("<="); break;
663 case QSOperator::LShift:
664 s = QLatin1String("<<"); break;
665 case QSOperator::InplaceLeftShift:
666 s = QLatin1String("<<="); break;
667 case QSOperator::Lt:
668 s = QLatin1String("<"); break;
669 case QSOperator::Mod:
670 s = QLatin1String("%"); break;
671 case QSOperator::InplaceMod:
672 s = QLatin1String("%="); break;
673 case QSOperator::Mul:
674 s = QLatin1String("*"); break;
675 case QSOperator::InplaceMul:
676 s = QLatin1String("*="); break;
677 case QSOperator::NotEqual:
678 s = QLatin1String("!="); break;
679 case QSOperator::Or:
680 s = QLatin1String("||"); break;
681 case QSOperator::InplaceOr:
682 s = QLatin1String("|="); break;
683 case QSOperator::RShift:
684 s = QLatin1String(">>"); break;
685 case QSOperator::InplaceRightShift:
686 s = QLatin1String(">>="); break;
687 case QSOperator::StrictEqual:
688 s = QLatin1String("==="); break;
689 case QSOperator::StrictNotEqual:
690 s = QLatin1String("!=="); break;
691 case QSOperator::Sub:
692 s = QLatin1String("-"); break;
693 case QSOperator::URShift:
694 s = QLatin1String(">>>"); break;
695 case QSOperator::InplaceURightShift:
696 s = QLatin1String(">>>="); break;
697 case QSOperator::InplaceXor:
698 s = QLatin1String("^="); break;
699 default:
700 Q_ASSERT (0);
701 }
702 out << " " << s << " ";
703 needParens = node->right->binaryExpressionCast()
704 && (compareOperatorPrecedence(node->right->binaryExpressionCast()->op, node->op) <= 0);
705 if (needParens)
706 out << "(";
707 accept(node->right);
708 if (needParens)
709 out << ")";
710 return false;
711}
712
713void PrettyPretty::endVisit(AST::BinaryExpression *node)
714{
715 Q_UNUSED(node);
716}
717
718bool PrettyPretty::visit(AST::ConditionalExpression *node)
719{
720 accept(node->expression);
721 out << " ? ";
722 accept(node->ok);
723 out << " : ";
724 accept(node->ko);
725 return false;
726}
727
728void PrettyPretty::endVisit(AST::ConditionalExpression *node)
729{
730 Q_UNUSED(node);
731}
732
733bool PrettyPretty::visit(AST::Expression *node)
734{
735 accept(node->left);
736 out << ", ";
737 accept(node->right);
738 return false;
739}
740
741void PrettyPretty::endVisit(AST::Expression *node)
742{
743 Q_UNUSED(node);
744}
745
746bool PrettyPretty::visit(AST::Block *node)
747{
748 Q_UNUSED(node);
749 return true;
750}
751
752void PrettyPretty::endVisit(AST::Block *node)
753{
754 Q_UNUSED(node);
755}
756
757bool PrettyPretty::visit(AST::StatementList *node)
758{
759 accept(node->statement);
760 for (node = node->next; node != 0; node = node->next) {
761 newlineAndIndent();
762 accept(node->statement);
763 }
764 return false;
765}
766
767void PrettyPretty::endVisit(AST::StatementList *node)
768{
769 Q_UNUSED(node);
770}
771
772bool PrettyPretty::visit(AST::VariableDeclarationList *node)
773{
774 AST::VariableDeclarationList *it = node;
775
776 do {
777 it->declaration->accept(this);
778 it = it->next;
779 if (it)
780 out << ", ";
781 } while (it);
782
783 return false;
784}
785
786void PrettyPretty::endVisit(AST::VariableDeclarationList *node)
787{
788 Q_UNUSED(node);
789}
790
791bool PrettyPretty::visit(AST::VariableStatement *node)
792{
793 out << "var ";
794 Q_UNUSED(node);
795 return true;
796}
797
798void PrettyPretty::endVisit(AST::VariableStatement *node)
799{
800 Q_UNUSED(node);
801 out << ";";
802}
803
804bool PrettyPretty::visit(AST::VariableDeclaration *node)
805{
806 out << QScriptEnginePrivate::toString(node->name);
807 if (node->expression) {
808 out << " = ";
809 accept(node->expression);
810 }
811 return false;
812}
813
814void PrettyPretty::endVisit(AST::VariableDeclaration *node)
815{
816 Q_UNUSED(node);
817}
818
819bool PrettyPretty::visit(AST::EmptyStatement *node)
820{
821 Q_UNUSED(node);
822 out << ";";
823 return true;
824}
825
826void PrettyPretty::endVisit(AST::EmptyStatement *node)
827{
828 Q_UNUSED(node);
829}
830
831bool PrettyPretty::visit(AST::ExpressionStatement *node)
832{
833 accept(node->expression);
834 out << ";";
835 return false;
836}
837
838void PrettyPretty::endVisit(AST::ExpressionStatement *node)
839{
840 Q_UNUSED(node);
841}
842
843bool PrettyPretty::visit(AST::IfStatement *node)
844{
845 out << "if (";
846 accept(node->expression);
847 out << ") ";
848 acceptAsBlock(node->ok);
849 if (node->ko) {
850 out << " else ";
851 acceptAsBlock(node->ko);
852 }
853 return false;
854}
855
856void PrettyPretty::endVisit(AST::IfStatement *node)
857{
858 Q_UNUSED(node);
859}
860
861bool PrettyPretty::visit(AST::DoWhileStatement *node)
862{
863 out << "do ";
864 acceptAsBlock(node->statement);
865 out << " while (";
866 accept(node->expression);
867 out << ");";
868 return false;
869}
870
871void PrettyPretty::endVisit(AST::DoWhileStatement *node)
872{
873 Q_UNUSED(node);
874}
875
876bool PrettyPretty::visit(AST::WhileStatement *node)
877{
878 out << "while (";
879 accept(node->expression);
880 out << ") ";
881 acceptAsBlock(node->statement);
882 return false;
883}
884
885void PrettyPretty::endVisit(AST::WhileStatement *node)
886{
887 Q_UNUSED(node);
888}
889
890bool PrettyPretty::visit(AST::ForStatement *node)
891{
892 out << "for (";
893 accept(node->initialiser);
894 out << "; ";
895 accept(node->condition);
896 out << "; ";
897 accept(node->expression);
898 out << ") ";
899 acceptAsBlock(node->statement);
900 return false;
901}
902
903void PrettyPretty::endVisit(AST::ForStatement *node)
904{
905 Q_UNUSED(node);
906}
907
908bool PrettyPretty::visit(AST::LocalForStatement *node)
909{
910 out << "for (var ";
911 accept(node->declarations);
912 out << "; ";
913 accept(node->condition);
914 out << "; ";
915 accept(node->expression);
916 out << ") ";
917 acceptAsBlock(node->statement);
918 return false;
919}
920
921void PrettyPretty::endVisit(AST::LocalForStatement *node)
922{
923 Q_UNUSED(node);
924}
925
926bool PrettyPretty::visit(AST::ForEachStatement *node)
927{
928 out << "for (";
929 accept(node->initialiser);
930 out << " in ";
931 accept(node->expression);
932 out << ") ";
933 acceptAsBlock(node->statement);
934 return false;
935}
936
937void PrettyPretty::endVisit(AST::ForEachStatement *node)
938{
939 Q_UNUSED(node);
940}
941
942bool PrettyPretty::visit(AST::LocalForEachStatement *node)
943{
944 out << "for (var ";
945 accept(node->declaration);
946 out << " in ";
947 accept(node->expression);
948 out << ") ";
949 acceptAsBlock(node->statement);
950 return false;
951}
952
953void PrettyPretty::endVisit(AST::LocalForEachStatement *node)
954{
955 Q_UNUSED(node);
956}
957
958bool PrettyPretty::visit(AST::ContinueStatement *node)
959{
960 out << "continue";
961 if (node->label) {
962 out << " " << QScriptEnginePrivate::toString(node->label);
963 }
964 out << ";";
965 return false;
966}
967
968void PrettyPretty::endVisit(AST::ContinueStatement *node)
969{
970 Q_UNUSED(node);
971}
972
973bool PrettyPretty::visit(AST::BreakStatement *node)
974{
975 out << "break";
976 if (node->label) {
977 out << " " << QScriptEnginePrivate::toString(node->label);
978 }
979 out << ";";
980 return false;
981}
982
983void PrettyPretty::endVisit(AST::BreakStatement *node)
984{
985 Q_UNUSED(node);
986}
987
988bool PrettyPretty::visit(AST::ReturnStatement *node)
989{
990 out << "return";
991 if (node->expression) {
992 out << " ";
993 accept(node->expression);
994 }
995 out << ";";
996 return false;
997}
998
999void PrettyPretty::endVisit(AST::ReturnStatement *node)
1000{
1001 Q_UNUSED(node);
1002}
1003
1004bool PrettyPretty::visit(AST::WithStatement *node)
1005{
1006 out << "with (";
1007 accept(node->expression);
1008 out << ") ";
1009 acceptAsBlock(node->statement);
1010 return false;
1011}
1012
1013void PrettyPretty::endVisit(AST::WithStatement *node)
1014{
1015 Q_UNUSED(node);
1016}
1017
1018bool PrettyPretty::visit(AST::SwitchStatement *node)
1019{
1020 out << "switch (";
1021 accept(node->expression);
1022 out << ") ";
1023 acceptAsBlock(node->block);
1024 return false;
1025}
1026
1027void PrettyPretty::endVisit(AST::SwitchStatement *node)
1028{
1029 Q_UNUSED(node);
1030}
1031
1032bool PrettyPretty::visit(AST::CaseBlock *node)
1033{
1034 accept(node->clauses);
1035 if (node->defaultClause) {
1036 newlineAndIndent();
1037 accept(node->defaultClause);
1038 }
1039 if (node->moreClauses) {
1040 newlineAndIndent();
1041 accept(node->moreClauses);
1042 }
1043 return false;
1044}
1045
1046void PrettyPretty::endVisit(AST::CaseBlock *node)
1047{
1048 Q_UNUSED(node);
1049}
1050
1051bool PrettyPretty::visit(AST::CaseClauses *node)
1052{
1053 accept(node->clause);
1054 for (node = node->next; node != 0; node = node->next) {
1055 newlineAndIndent();
1056 accept(node->clause);
1057 }
1058 return false;
1059}
1060
1061void PrettyPretty::endVisit(AST::CaseClauses *node)
1062{
1063 Q_UNUSED(node);
1064}
1065
1066bool PrettyPretty::visit(AST::CaseClause *node)
1067{
1068 out << "case ";
1069 accept(node->expression);
1070 out << ":";
1071 if (node->statements) {
1072 newlineAndIndent();
1073 accept(node->statements);
1074 }
1075 return false;
1076}
1077
1078void PrettyPretty::endVisit(AST::CaseClause *node)
1079{
1080 Q_UNUSED(node);
1081}
1082
1083bool PrettyPretty::visit(AST::DefaultClause *node)
1084{
1085 Q_UNUSED(node);
1086 out << "default:";
1087 newlineAndIndent();
1088 return true;
1089}
1090
1091void PrettyPretty::endVisit(AST::DefaultClause *node)
1092{
1093 Q_UNUSED(node);
1094}
1095
1096bool PrettyPretty::visit(AST::LabelledStatement *node)
1097{
1098 out << QScriptEnginePrivate::toString(node->label) << ": ";
1099 return true;
1100}
1101
1102void PrettyPretty::endVisit(AST::LabelledStatement *node)
1103{
1104 Q_UNUSED(node);
1105}
1106
1107bool PrettyPretty::visit(AST::ThrowStatement *node)
1108{
1109 Q_UNUSED(node);
1110 out << "throw ";
1111 accept(node->expression);
1112 out << ";";
1113 return false;
1114}
1115
1116void PrettyPretty::endVisit(AST::ThrowStatement *node)
1117{
1118 Q_UNUSED(node);
1119}
1120
1121bool PrettyPretty::visit(AST::TryStatement *node)
1122{
1123 out << "try ";
1124 acceptAsBlock(node->statement);
1125 if (node->catchExpression) {
1126 out << " catch (" << QScriptEnginePrivate::toString(node->catchExpression->name) << ") ";
1127 acceptAsBlock(node->catchExpression->statement);
1128 }
1129 if (node->finallyExpression) {
1130 out << " finally ";
1131 acceptAsBlock(node->finallyExpression->statement);
1132 }
1133 return false;
1134}
1135
1136void PrettyPretty::endVisit(AST::TryStatement *node)
1137{
1138 Q_UNUSED(node);
1139}
1140
1141bool PrettyPretty::visit(AST::Catch *node)
1142{
1143 Q_UNUSED(node);
1144 return true;
1145}
1146
1147void PrettyPretty::endVisit(AST::Catch *node)
1148{
1149 Q_UNUSED(node);
1150}
1151
1152bool PrettyPretty::visit(AST::Finally *node)
1153{
1154 Q_UNUSED(node);
1155 out << "finally ";
1156 return true;
1157}
1158
1159void PrettyPretty::endVisit(AST::Finally *node)
1160{
1161 Q_UNUSED(node);
1162}
1163
1164bool PrettyPretty::visit(AST::FunctionDeclaration *node)
1165{
1166 out << "function";
1167
1168 if (node->name)
1169 out << " " << QScriptEnginePrivate::toString(node->name);
1170
1171 // the arguments
1172 out << "(";
1173 for (AST::FormalParameterList *it = node->formals; it; it = it->next) {
1174 if (it->name)
1175 out << QScriptEnginePrivate::toString(it->name);
1176
1177 if (it->next)
1178 out << ", ";
1179 }
1180 out << ")";
1181
1182 // the function body
1183 out << " {";
1184
1185 if (node->body) {
1186 pushIndentLevel();
1187 newlineAndIndent();
1188 accept(node->body);
1189 popIndentLevel();
1190 newlineAndIndent();
1191 }
1192
1193 out << "}";
1194
1195 return false;
1196}
1197
1198void PrettyPretty::endVisit(AST::FunctionDeclaration *node)
1199{
1200 Q_UNUSED(node);
1201}
1202
1203bool PrettyPretty::visit(AST::FunctionExpression *node)
1204{
1205 out << "function";
1206
1207 if (node->name)
1208 out << " " << QScriptEnginePrivate::toString(node->name);
1209
1210 // the arguments
1211 out << "(";
1212 for (AST::FormalParameterList *it = node->formals; it; it = it->next) {
1213 if (it->name)
1214 out << QScriptEnginePrivate::toString(it->name);
1215
1216 if (it->next)
1217 out << ", ";
1218 }
1219 out << ")";
1220
1221 // the function body
1222 out << " {";
1223
1224 if (node->body) {
1225 pushIndentLevel();
1226 newlineAndIndent();
1227 accept(node->body);
1228 popIndentLevel();
1229 newlineAndIndent();
1230 }
1231
1232 out << "}";
1233
1234 return false;
1235}
1236
1237void PrettyPretty::endVisit(AST::FunctionExpression *node)
1238{
1239 Q_UNUSED(node);
1240}
1241
1242bool PrettyPretty::visit(AST::FormalParameterList *node)
1243{
1244 Q_UNUSED(node);
1245 return true;
1246}
1247
1248void PrettyPretty::endVisit(AST::FormalParameterList *node)
1249{
1250 Q_UNUSED(node);
1251}
1252
1253bool PrettyPretty::visit(AST::FunctionBody *node)
1254{
1255 Q_UNUSED(node);
1256 return true;
1257}
1258
1259void PrettyPretty::endVisit(AST::FunctionBody *node)
1260{
1261 Q_UNUSED(node);
1262}
1263
1264bool PrettyPretty::visit(AST::Program *node)
1265{
1266 Q_UNUSED(node);
1267 return true;
1268}
1269
1270void PrettyPretty::endVisit(AST::Program *node)
1271{
1272 Q_UNUSED(node);
1273}
1274
1275bool PrettyPretty::visit(AST::SourceElements *node)
1276{
1277 Q_UNUSED(node);
1278 accept(node->element);
1279 for (node = node->next; node != 0; node = node->next) {
1280 newlineAndIndent();
1281 accept(node->element);
1282 }
1283 return false;
1284}
1285
1286void PrettyPretty::endVisit(AST::SourceElements *node)
1287{
1288 Q_UNUSED(node);
1289}
1290
1291bool PrettyPretty::visit(AST::FunctionSourceElement *node)
1292{
1293 Q_UNUSED(node);
1294 return true;
1295}
1296
1297void PrettyPretty::endVisit(AST::FunctionSourceElement *node)
1298{
1299 Q_UNUSED(node);
1300}
1301
1302bool PrettyPretty::visit(AST::StatementSourceElement *node)
1303{
1304 Q_UNUSED(node);
1305 return true;
1306}
1307
1308void PrettyPretty::endVisit(AST::StatementSourceElement *node)
1309{
1310 Q_UNUSED(node);
1311}
1312
1313bool PrettyPretty::visit(AST::DebuggerStatement *node)
1314{
1315 Q_UNUSED(node);
1316 out << "debugger";
1317 return true;
1318}
1319
1320void PrettyPretty::endVisit(AST::DebuggerStatement *node)
1321{
1322 Q_UNUSED(node);
1323 out << ";";
1324}
1325
1326bool PrettyPretty::preVisit(AST::Node *node)
1327{
1328 Q_UNUSED(node);
1329 return true;
1330}
1331
1332QT_END_NAMESPACE
1333
1334#endif // QT_NO_SCRIPT
Note: See TracBrowser for help on using the repository browser.