File indexing completed on 2024-05-05 08:46:34
0001 /* This file is part of kdev-pg-qt 0002 Copyright (C) 2005 Roberto Raggi <roberto@kdevelop.org> 0003 Copyright (C) 2006 Jakob Petsovits <jpetso@gmx.at> 0004 0005 This library is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU Library General Public 0007 License as published by the Free Software Foundation; either 0008 version 2 of the License, or (at your option) any later version. 0009 0010 This library is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 Library General Public License for more details. 0014 0015 You should have received a copy of the GNU Library General Public License 0016 along with this library; see the file COPYING.LIB. If not, write to 0017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #include "kdev-pg-pretty-printer.h" 0022 0023 #include <QList> 0024 #include <QStack> 0025 #include <iostream> 0026 #include <cassert> 0027 0028 0029 namespace KDevPG 0030 { 0031 0032 void PrettyPrinter::visitZero(Model::ZeroItem *node) 0033 { 0034 Q_UNUSED(node); 0035 out << "0"; 0036 } 0037 0038 void PrettyPrinter::visitPlus(Model::PlusItem *node) 0039 { 0040 out << "("; 0041 visitNode(node->mItem); 0042 out << ")"; 0043 out << "+"; 0044 } 0045 0046 void PrettyPrinter::visitStar(Model::StarItem *node) 0047 { 0048 out << "("; 0049 visitNode(node->mItem); 0050 out << ")"; 0051 out << "*"; 0052 } 0053 0054 void PrettyPrinter::visitSymbol(Model::SymbolItem *node) 0055 { 0056 out << node->mName; 0057 } 0058 0059 void PrettyPrinter::visitAction(Model::ActionItem *node) 0060 { 0061 visitNode(node->mItem); 0062 } 0063 0064 void PrettyPrinter::visitAlternative(Model::AlternativeItem *node) 0065 { 0066 QList<Model::Node*> top_level_nodes; 0067 0068 QStack<Model::Node*> working_list; 0069 working_list.push(node->mRight); 0070 working_list.push(node->mLeft); 0071 0072 while (!working_list.empty()) 0073 { 0074 Model::Node *n = working_list.top(); 0075 working_list.pop(); 0076 0077 if (Model::AlternativeItem *a = nodeCast<Model::AlternativeItem*>(n)) 0078 { 0079 working_list.push(a->mRight); 0080 working_list.push(a->mLeft); 0081 } 0082 else 0083 { 0084 top_level_nodes.push_back(n); 0085 } 0086 } 0087 0088 bool initial = true; 0089 0090 out << "("; 0091 QList<Model::Node*>::iterator it = top_level_nodes.begin(); 0092 while (it != top_level_nodes.end()) 0093 { 0094 if (!initial) 0095 out << " | "; 0096 0097 Model::Node *n = *it++; 0098 visitNode(n); 0099 initial = false; 0100 } 0101 out << ")"; 0102 } 0103 0104 void PrettyPrinter::visitCons(Model::ConsItem *node) 0105 { 0106 visitNode(node->mLeft); 0107 out << " "; 0108 visitNode(node->mRight); 0109 } 0110 0111 void PrettyPrinter::visitEvolve(Model::EvolveItem *node) 0112 { 0113 visitNode(node->mItem); 0114 out << " -> "; 0115 visitNode(node->mSymbol); 0116 } 0117 0118 void PrettyPrinter::visitTryCatch(Model::TryCatchItem *node) 0119 { 0120 out << "try/" << (node->mCatchItem ? "rollback" : "recover") << "("; 0121 visitNode(node->mTryItem); 0122 out << ")"; 0123 0124 if (node->mCatchItem) 0125 { 0126 out << " catch("; 0127 visitNode(node->mCatchItem); 0128 out << ")"; 0129 } 0130 } 0131 0132 void PrettyPrinter::visitAlias(Model::AliasItem *node) 0133 { 0134 Q_UNUSED(node); 0135 assert(0); // ### not implemented yet 0136 } 0137 0138 void PrettyPrinter::visitTerminal(Model::TerminalItem *node) 0139 { 0140 out << node->mName; 0141 } 0142 0143 void PrettyPrinter::visitInlinedNonTerminal(Model::InlinedNonTerminalItem *node) 0144 { 0145 visitNode(node->mSymbol); 0146 } 0147 0148 void PrettyPrinter::visitNonTerminal(Model::NonTerminalItem *node) 0149 { 0150 visitNode(node->mSymbol); 0151 } 0152 0153 void PrettyPrinter::visitAnnotation(Model::AnnotationItem *node) 0154 { 0155 out << ((node->mDeclaration->mIsSequence) ? "#" : "") 0156 << node->mDeclaration->mName 0157 << ((node->mDeclaration->mStorageType 0158 == Model::VariableDeclarationItem::StorageTemporary) ? ":" : "="); 0159 visitNode(node->mItem); 0160 } 0161 0162 }