1
/*************************************************************************************
2
* Copyright (C) 2008 by Aleix Pol <aleixpol@kde.org> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License *
6
* as published by the Free Software Foundation; either version 2 *
7
* of the License, or (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the Free Software *
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
17
*************************************************************************************/
19
#include "htmlexpressionwriter.h"
22
#include "container.h"
24
#include <QStringList>
28
#include "stringexpressionwriter.h"
30
#include "analitzautils.h"
32
using namespace Analitza;
34
//we use the one in string*
35
QMap<Operator::OperatorType, QString> initOperators();
38
QStringList HtmlExpressionWriter::allValues(T it, const T& itEnd, ExpressionWriter* writer)
41
for(; it!=itEnd; ++it)
42
elements += (*it)->visit(writer);
48
static const QMap<Operator::OperatorType, QString> s_operators=initOperators();
50
QString oper(const QString& op) { return i18nc("html representation of an operator", "<span class='op'>%1</span>", op); }
51
QString oper(const QChar& op) { return i18nc("html representation of an operator", "<span class='op'>%1</span>", op); }
52
QString keyword(const QString& op) { return i18nc("html representation of an operator", "<span class='keyword'>%1</span>", op); }
54
HtmlExpressionWriter::HtmlExpressionWriter(const Object* o)
56
m_result=o->visit(this);
59
QString HtmlExpressionWriter::accept(const Vector* vec)
61
return keyword("vector ")+oper("{ ")+allValues<Vector::const_iterator>(vec->constBegin(), vec->constEnd(), this).join(QString(oper(", ")))+oper(" }");
64
QString HtmlExpressionWriter::accept(const List* vec)
66
if(!vec->isEmpty() && vec->at(0)->type()==Object::value && static_cast<Cn*>(vec->at(0))->format()==Cn::Char) {
67
return "<span class='string'>""+AnalitzaUtils::listToString(vec)+ ""</span>";
69
return keyword("list ")+oper("{ ")+allValues<List::const_iterator>(vec->constBegin(), vec->constEnd(), this).join(QString(oper(", ")))+oper(" }");
72
QString HtmlExpressionWriter::accept(const Cn* var)
75
return "<span class='var'>"+QString(var->isTrue() ? "true" : "false")+"</span>";
77
return "<span class='num'>"+QString::number(var->value(), 'g', 12)+"</span>";
80
QString HtmlExpressionWriter::accept(const Analitza::Ci* var)
85
QString HtmlExpressionWriter::accept(const Analitza::Operator* o)
87
return "<span class='func'>"+o->toString()+"</span>";
90
QString HtmlExpressionWriter::accept ( const Analitza::Apply* a )
92
Operator op=a->firstOperator();
97
if(a->ulimit() || a->dlimit()) {
100
bounds += a->dlimit()->visit(this);
101
bounds += oper("..");
103
bounds += a->ulimit()->visit(this);
106
bounds += oper('@')+a->domain()->visit(this);
108
foreach(Object* o, a->m_params) {
109
Object::ObjectType type=o->type();
111
if(type == Object::oper)
114
case Object::variable:
115
ret << static_cast<const Ci*>(o)->visit(this);
117
case Object::apply: {
118
Apply *c = (Apply*) o;
119
QString s = c->visit(this);
120
if(s_operators.contains(op.operatorType())) {
121
Operator child_op = c->firstOperator();
123
if(child_op.operatorType() &&
124
StringExpressionWriter::weight(&op, c->countValues())>=StringExpressionWriter::weight(&child_op, c->countValues()))
125
s=oper('(')+s+oper(')');
130
ret << o->visit(this);
135
bool func=op.operatorType()==Operator::function;
137
QString n = ret.takeFirst();
138
if(a->m_params.first()->type()!=Object::variable)
139
n=oper('(')+n+oper(')');
141
toret += n+oper('(') + ret.join(oper(", ")) + oper(')');
142
} else if(ret.count()>1 && s_operators.contains(op.operatorType())) {
143
toret += ret.join(oper(s_operators.value(op.operatorType())));
144
} else if(ret.count()==1 && op.operatorType()==Operator::minus)
145
toret += oper('-')+ret[0];
146
else if(op.operatorType()==Operator::selector) {
147
QString value = ret.takeLast();
148
if(a->m_params.last()->isApply())
149
value = oper('(')+value+oper(')');
150
toret += value + oper('[')+ret.join(oper(", "))+oper(']');
154
foreach(const Ci* bvar, a->bvarCi())
155
bvars += bvar->visit(this);
157
if(!bounds.isEmpty() || !bvars.isEmpty()) {
158
if(bvars.count()!=1) bounding +=oper('(');
159
bounding += bvars.join(oper(", "));
160
if(bvars.count()!=1) bounding +=oper(')');
162
bounding = ':'+bounding +bounds;
165
toret += op.visit(this)+oper('(')+ret.join(oper(", "))+bounding+oper(')');
171
QString HtmlExpressionWriter::accept(const Container* var)
173
QStringList ret = allValues(var->constBegin(), var->constEnd(), this);
177
switch(var->containerType()) {
178
case Container::declare:
179
toret += ret.join(oper(":="));
181
case Container::lambda: {
182
QString last=ret.takeLast();
184
foreach(const Ci* bvar, var->bvarCi())
185
bvars += bvar->visit(this);
187
if(bvars.count()!=1) toret +=oper('(');
188
toret += bvars.join(", ");
189
if(bvars.count()!=1) toret +=oper(')');
190
toret += oper("->") + last;
192
case Container::math:
193
toret += ret.join(oper("; "));
195
case Container::uplimit: //x->(n1..n2) is put at the same time
196
case Container::downlimit:
198
case Container::bvar:
199
if(ret.count()>1) toret += oper('(');
200
toret += ret.join(", ");
201
if(ret.count()>1) toret += oper(')');
203
case Container::piece:
204
toret += ret[1]+oper(" ? ")+ret[0];
206
case Container::otherwise:
207
toret += oper("? ")+ret[0];
210
toret += var->tagName()+oper(" { ")+ret.join(oper(", "))+oper(" }");
216
QString HtmlExpressionWriter::accept(const CustomObject*)
218
return "Custom Object";