stereotypedefinitionparser.cpp 33.9 KB
Newer Older
1
/****************************************************************************
Jochen Becher's avatar
Jochen Becher committed
2
**
3 4
** Copyright (C) 2016 Jochen Becher
** Contact: https://www.qt.io/licensing/
Jochen Becher's avatar
Jochen Becher committed
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Jochen Becher's avatar
Jochen Becher committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Jochen Becher's avatar
Jochen Becher committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
**
****************************************************************************/

#include "stereotypedefinitionparser.h"

#include "textscanner.h"
#include "token.h"
#include "qmt/infrastructure/qmtassert.h"
#include "qmt/stereotype/stereotypeicon.h"
#include "qmt/stereotype/shapevalue.h"
#include "qmt/stereotype/toolbar.h"

#include <QHash>
#include <QSet>
#include <QPair>
#include <QDebug>

namespace qmt {

// Icon Definition
static const int KEYWORD_ICON          =  1;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static const int KEYWORD_ID            =  2;
static const int KEYWORD_TITLE         =  3;
static const int KEYWORD_ELEMENTS      =  4;
static const int KEYWORD_STEREOTYPE    =  5;
static const int KEYWORD_WIDTH         =  6;
static const int KEYWORD_HEIGHT        =  7;
static const int KEYWORD_MINWIDTH      =  8;
static const int KEYWORD_MINHEIGHT     =  9;
static const int KEYWORD_LOCK_SIZE     = 10;
static const int KEYWORD_DISPLAY       = 11;
static const int KEYWORD_TEXTALIGN     = 12;
static const int KEYWORD_BASECOLOR     = 13;
static const int KEYWORD_SHAPE         = 14;

// Shape items
Jochen Becher's avatar
Jochen Becher committed
59 60 61 62 63 64 65 66 67 68 69 70
static const int KEYWORD_CIRCLE        = 30;
static const int KEYWORD_ELLIPSE       = 31;
static const int KEYWORD_LINE          = 32;
static const int KEYWORD_RECT          = 33;
static const int KEYWORD_ROUNDEDRECT   = 34;
static const int KEYWORD_ARC           = 35;
static const int KEYWORD_MOVETO        = 36;
static const int KEYWORD_LINETO        = 37;
static const int KEYWORD_ARCMOVETO     = 38;
static const int KEYWORD_ARCTO         = 39;
static const int KEYWORD_CLOSE         = 40;

71 72 73 74 75 76 77 78 79 80 81 82 83
// Shape item parameters
static const int KEYWORD_X             = 50;
static const int KEYWORD_Y             = 51;
static const int KEYWORD_X0            = 52;
static const int KEYWORD_Y0            = 53;
static const int KEYWORD_X1            = 54;
static const int KEYWORD_Y1            = 55;
static const int KEYWORD_RADIUS        = 56;
static const int KEYWORD_RADIUS_X      = 57;
static const int KEYWORD_RADIUS_Y      = 58;
static const int KEYWORD_START         = 59;
static const int KEYWORD_SPAN          = 60;

Jochen Becher's avatar
Jochen Becher committed
84
// Toolbar Definition
85 86 87 88 89 90
static const int KEYWORD_TOOLBAR       = 70;
static const int KEYWORD_PRIORITY      = 71;
static const int KEYWORD_TOOLS         = 72;
static const int KEYWORD_TOOL          = 73;
static const int KEYWORD_ELEMENT       = 74;
static const int KEYWORD_SEPARATOR     = 75;
Jochen Becher's avatar
Jochen Becher committed
91 92

// Operatoren
93 94 95 96 97 98 99
static const int OPERATOR_SEMICOLON    =  1;
static const int OPERATOR_BRACE_OPEN   =  2;
static const int OPERATOR_BRACE_CLOSE  =  3;
static const int OPERATOR_COLON        =  4;
static const int OPERATOR_COMMA        =  5;
static const int OPERATOR_PERIOD       =  6;
static const int OPERATOR_MINUS        =  7;
Jochen Becher's avatar
Jochen Becher committed
100 101 102 103 104 105 106

template <typename T, typename U>
QHash<T, U> operator<<(QHash<T, U> hash, QPair<T, U> pair) {
    hash.insert(pair.first, pair.second);
    return hash;
}

107 108 109
StereotypeDefinitionParserError::StereotypeDefinitionParserError(const QString &errorMsg, const SourcePos &sourcePos)
    : Exception(errorMsg),
      m_sourcePos(sourcePos)
Jochen Becher's avatar
Jochen Becher committed
110 111 112 113 114 115 116
{
}

StereotypeDefinitionParserError::~StereotypeDefinitionParserError()
{
}

117
class StereotypeDefinitionParser::StereotypeDefinitionParserPrivate
Jochen Becher's avatar
Jochen Becher committed
118
{
119
public:
120
    TextScanner *m_scanner = nullptr;
Jochen Becher's avatar
Jochen Becher committed
121 122 123

};

124
class StereotypeDefinitionParser::IconCommandParameter
Jochen Becher's avatar
Jochen Becher committed
125
{
126
public:
127 128 129 130 131
    IconCommandParameter() = default;

    IconCommandParameter(int keyword, ShapeValueF::Unit unit, ShapeValueF::Origin origin = ShapeValueF::OriginSmart)
        : m_keyword(keyword),
          m_unit(unit),
132
          m_origin(origin)
Jochen Becher's avatar
Jochen Becher committed
133 134 135
    {
    }

136
    int m_keyword = -1;
137 138
    ShapeValueF::Unit m_unit = ShapeValueF::UnitAbsolute;
    ShapeValueF::Origin m_origin = ShapeValueF::OriginCenter;
Jochen Becher's avatar
Jochen Becher committed
139 140
};

141 142 143
StereotypeDefinitionParser::StereotypeDefinitionParser(QObject *parent)
    : QObject(parent),
      d(new StereotypeDefinitionParserPrivate)
Jochen Becher's avatar
Jochen Becher committed
144 145 146 147 148 149 150 151 152 153
{
}

StereotypeDefinitionParser::~StereotypeDefinitionParser()
{
    delete d;
}

void StereotypeDefinitionParser::parse(ITextSource *source)
{
154 155
    TextScanner textScanner;
    textScanner.setKeywords(
Jochen Becher's avatar
Jochen Becher committed
156 157
                QList<QPair<QString, int> >()
                << qMakePair(QString(QStringLiteral("icon")), KEYWORD_ICON)
158
                << qMakePair(QString(QStringLiteral("id")), KEYWORD_ID)
Jochen Becher's avatar
Jochen Becher committed
159 160 161 162 163 164 165 166 167 168 169
                << qMakePair(QString(QStringLiteral("title")), KEYWORD_TITLE)
                << qMakePair(QString(QStringLiteral("elements")), KEYWORD_ELEMENTS)
                << qMakePair(QString(QStringLiteral("stereotype")), KEYWORD_STEREOTYPE)
                << qMakePair(QString(QStringLiteral("width")), KEYWORD_WIDTH)
                << qMakePair(QString(QStringLiteral("height")), KEYWORD_HEIGHT)
                << qMakePair(QString(QStringLiteral("minwidth")), KEYWORD_MINWIDTH)
                << qMakePair(QString(QStringLiteral("minheight")), KEYWORD_MINHEIGHT)
                << qMakePair(QString(QStringLiteral("locksize")), KEYWORD_LOCK_SIZE)
                << qMakePair(QString(QStringLiteral("display")), KEYWORD_DISPLAY)
                << qMakePair(QString(QStringLiteral("textalignment")), KEYWORD_TEXTALIGN)
                << qMakePair(QString(QStringLiteral("basecolor")), KEYWORD_BASECOLOR)
170
                << qMakePair(QString(QStringLiteral("shape")), KEYWORD_SHAPE)
Jochen Becher's avatar
Jochen Becher committed
171 172 173 174 175 176 177 178 179 180 181
                << qMakePair(QString(QStringLiteral("circle")), KEYWORD_CIRCLE)
                << qMakePair(QString(QStringLiteral("ellipse")), KEYWORD_ELLIPSE)
                << qMakePair(QString(QStringLiteral("line")), KEYWORD_LINE)
                << qMakePair(QString(QStringLiteral("rect")), KEYWORD_RECT)
                << qMakePair(QString(QStringLiteral("roundedrect")), KEYWORD_ROUNDEDRECT)
                << qMakePair(QString(QStringLiteral("arc")), KEYWORD_ARC)
                << qMakePair(QString(QStringLiteral("moveto")), KEYWORD_MOVETO)
                << qMakePair(QString(QStringLiteral("lineto")), KEYWORD_LINETO)
                << qMakePair(QString(QStringLiteral("arcmoveto")), KEYWORD_ARCMOVETO)
                << qMakePair(QString(QStringLiteral("arcto")), KEYWORD_ARCTO)
                << qMakePair(QString(QStringLiteral("close")), KEYWORD_CLOSE)
182 183 184 185 186 187 188 189 190 191 192
                << qMakePair(QString(QStringLiteral("x")), KEYWORD_X)
                << qMakePair(QString(QStringLiteral("y")), KEYWORD_Y)
                << qMakePair(QString(QStringLiteral("x0")), KEYWORD_X0)
                << qMakePair(QString(QStringLiteral("y0")), KEYWORD_Y0)
                << qMakePair(QString(QStringLiteral("x1")), KEYWORD_X1)
                << qMakePair(QString(QStringLiteral("y1")), KEYWORD_Y1)
                << qMakePair(QString(QStringLiteral("radius")), KEYWORD_RADIUS)
                << qMakePair(QString(QStringLiteral("radiusx")), KEYWORD_RADIUS_X)
                << qMakePair(QString(QStringLiteral("radiusy")), KEYWORD_RADIUS_Y)
                << qMakePair(QString(QStringLiteral("start")), KEYWORD_START)
                << qMakePair(QString(QStringLiteral("span")), KEYWORD_SPAN)
Jochen Becher's avatar
Jochen Becher committed
193 194
                << qMakePair(QString(QStringLiteral("toolbar")), KEYWORD_TOOLBAR)
                << qMakePair(QString(QStringLiteral("priority")), KEYWORD_PRIORITY)
195
                << qMakePair(QString(QStringLiteral("tools")), KEYWORD_TOOLS)
Jochen Becher's avatar
Jochen Becher committed
196
                << qMakePair(QString(QStringLiteral("tool")), KEYWORD_TOOL)
197
                << qMakePair(QString(QStringLiteral("element")), KEYWORD_ELEMENT)
Jochen Becher's avatar
Jochen Becher committed
198 199
                << qMakePair(QString(QStringLiteral("separator")), KEYWORD_SEPARATOR)
                );
200
    textScanner.setOperators(
Jochen Becher's avatar
Jochen Becher committed
201 202
                QList<QPair<QString, int> >()
                << qMakePair(QString(QStringLiteral(";")), OPERATOR_SEMICOLON)
203 204
                << qMakePair(QString(QStringLiteral("{")), OPERATOR_BRACE_OPEN)
                << qMakePair(QString(QStringLiteral("}")), OPERATOR_BRACE_CLOSE)
Jochen Becher's avatar
Jochen Becher committed
205 206 207 208 209
                << qMakePair(QString(QStringLiteral(":")), OPERATOR_COLON)
                << qMakePair(QString(QStringLiteral(",")), OPERATOR_COMMA)
                << qMakePair(QString(QStringLiteral(".")), OPERATOR_PERIOD)
                << qMakePair(QString(QStringLiteral("-")), OPERATOR_MINUS)
                );
210
    textScanner.setSource(source);
Jochen Becher's avatar
Jochen Becher committed
211

212
    d->m_scanner = &textScanner;
Jochen Becher's avatar
Jochen Becher committed
213 214 215
    try {
        parseFile();
    } catch (...) {
216
        d->m_scanner = 0;
Jochen Becher's avatar
Jochen Becher committed
217 218
        throw;
    }
219
    d->m_scanner = 0;
Jochen Becher's avatar
Jochen Becher committed
220 221 222 223 224 225
}

void StereotypeDefinitionParser::parseFile()
{
    for (;;) {
        Token token = readNextToken();
226
        if (token.type() == Token::TokenEndOfInput)
Jochen Becher's avatar
Jochen Becher committed
227
            break;
228
        else if (token.type() == Token::TokenKeyword && token.subtype() == KEYWORD_ICON)
Jochen Becher's avatar
Jochen Becher committed
229
            parseIcon();
230
        else if (token.type() == Token::TokenKeyword && token.subtype() == KEYWORD_TOOLBAR)
Jochen Becher's avatar
Jochen Becher committed
231
            parseToolbar();
232
        else
233
            throw StereotypeDefinitionParserError(QStringLiteral("Expected 'Icon' or 'Toolbar'."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
234 235 236 237 238
    }
}

void StereotypeDefinitionParser::parseIcon()
{
239
    StereotypeIcon stereotypeIcon;
Jochen Becher's avatar
Jochen Becher committed
240 241
    QSet<StereotypeIcon::Element> elements;
    QSet<QString> stereotypes;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    expectBlockBegin();
    Token token;
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_ID:
            stereotypeIcon.setId(parseIdentifierProperty());
            break;
        case KEYWORD_TITLE:
            stereotypeIcon.setTitle(parseStringProperty());
            break;
        case KEYWORD_ELEMENTS:
        {
            QList<QString> identifiers = parseIdentifierListProperty();
            foreach (const QString &identifier, identifiers) {
                static QHash<QString, StereotypeIcon::Element> elementNames = QHash<QString, StereotypeIcon::Element>()
                        << qMakePair(QString(QStringLiteral("package")), StereotypeIcon::ElementPackage)
                        << qMakePair(QString(QStringLiteral("component")), StereotypeIcon::ElementComponent)
                        << qMakePair(QString(QStringLiteral("class")), StereotypeIcon::ElementClass)
                        << qMakePair(QString(QStringLiteral("diagram")), StereotypeIcon::ElementDiagram)
                        << qMakePair(QString(QStringLiteral("item")), StereotypeIcon::ElementItem);
                QString elementName = identifier.toLower();
                if (!elementNames.contains(elementName))
                    throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for element.")).arg(identifier), token.sourcePos());
                elements.insert(elementNames.value(elementName));
Jochen Becher's avatar
Jochen Becher committed
266
            }
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
            break;
        }
        case KEYWORD_STEREOTYPE:
            stereotypes.insert(parseStringProperty());
            break;
        case KEYWORD_WIDTH:
            stereotypeIcon.setWidth(parseFloatProperty());
            break;
        case KEYWORD_HEIGHT:
            stereotypeIcon.setHeight(parseFloatProperty());
            break;
        case KEYWORD_MINWIDTH:
            stereotypeIcon.setMinWidth(parseFloatProperty());
            break;
        case KEYWORD_MINHEIGHT:
            stereotypeIcon.setMinHeight(parseFloatProperty());
            break;
        case KEYWORD_LOCK_SIZE:
        {
            QString lockValue = parseIdentifierProperty();
            QString lockName = lockValue.toLower();
            static QHash<QString, StereotypeIcon::SizeLock> lockNames = QHash<QString, StereotypeIcon::SizeLock>()
                    << qMakePair(QString(QStringLiteral("none")), StereotypeIcon::LockNone)
                    << qMakePair(QString(QStringLiteral("width")), StereotypeIcon::LockWidth)
                    << qMakePair(QString(QStringLiteral("height")), StereotypeIcon::LockHeight)
                    << qMakePair(QString(QStringLiteral("size")), StereotypeIcon::LockSize)
                    << qMakePair(QString(QStringLiteral("ratio")), StereotypeIcon::LockRatio);
            if (lockNames.contains(lockName)) {
                StereotypeIcon::SizeLock sizeLock = lockNames.value(lockName);
                stereotypeIcon.setSizeLock(sizeLock);
            } else {
                throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for size lock.")).arg(lockValue), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
299
            }
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
            break;
        }
        case KEYWORD_DISPLAY:
        {
            QString displayValue = parseIdentifierProperty();
            QString displayName = displayValue.toLower();
            static QHash<QString, StereotypeIcon::Display> displayNames = QHash<QString, StereotypeIcon::Display>()
                    << qMakePair(QString(QStringLiteral("none")), StereotypeIcon::DisplayNone)
                    << qMakePair(QString(QStringLiteral("label")), StereotypeIcon::DisplayLabel)
                    << qMakePair(QString(QStringLiteral("decoration")), StereotypeIcon::DisplayDecoration)
                    << qMakePair(QString(QStringLiteral("icon")), StereotypeIcon::DisplayIcon)
                    << qMakePair(QString(QStringLiteral("smart")), StereotypeIcon::DisplaySmart);
            if (displayNames.contains(displayName)) {
                StereotypeIcon::Display display = displayNames.value(displayName);
                stereotypeIcon.setDisplay(display);
            } else {
                throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for stereotype display.")).arg(displayValue), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
317
            }
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
            break;
        }
        case KEYWORD_TEXTALIGN:
        {
            QString alignValue = parseIdentifierProperty();
            QString alignName = alignValue.toLower();
            static QHash<QString, StereotypeIcon::TextAlignment> alignNames = QHash<QString, StereotypeIcon::TextAlignment>()
                    << qMakePair(QString(QStringLiteral("below")), StereotypeIcon::TextalignBelow)
                    << qMakePair(QString(QStringLiteral("center")), StereotypeIcon::TextalignCenter)
                    << qMakePair(QString(QStringLiteral("none")), StereotypeIcon::TextalignNone);
            if (alignNames.contains(alignName)) {
                StereotypeIcon::TextAlignment textAlignment = alignNames.value(alignName);
                stereotypeIcon.setTextAlignment(textAlignment);
            } else {
                throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for text alignment.")).arg(alignValue), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
333
            }
334 335 336 337 338 339 340 341 342 343
            break;
        }
        case KEYWORD_BASECOLOR:
            stereotypeIcon.setBaseColor(parseColorProperty());
            break;
        case KEYWORD_SHAPE:
            parseIconShape(&stereotypeIcon);
            break;
        default:
            throwUnknownPropertyError(token);
Jochen Becher's avatar
Jochen Becher committed
344
        }
345 346
        if (!expectPropertySeparatorOrBlockEnd())
            break;
Jochen Becher's avatar
Jochen Becher committed
347
    }
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    stereotypeIcon.setElements(elements);
    stereotypeIcon.setStereotypes(stereotypes);
    if (stereotypeIcon.id().isEmpty())
        throw StereotypeDefinitionParserError(QStringLiteral("Missing id in Icon definition."), d->m_scanner->sourcePos());
    emit iconParsed(stereotypeIcon);
}

QPair<int, StereotypeDefinitionParser::IconCommandParameter> StereotypeDefinitionParser::SCALED(int keyword)
{
    return qMakePair(keyword, IconCommandParameter(keyword, ShapeValueF::UnitScaled));
}

QPair<int, StereotypeDefinitionParser::IconCommandParameter> StereotypeDefinitionParser::FIX(int keyword)
{
    return qMakePair(keyword, IconCommandParameter(keyword, ShapeValueF::UnitRelative));
}

QPair<int, StereotypeDefinitionParser::IconCommandParameter> StereotypeDefinitionParser::ABSOLUTE(int keyword)
{
    return qMakePair(keyword, IconCommandParameter(keyword, ShapeValueF::UnitAbsolute));
}

void StereotypeDefinitionParser::parseIconShape(StereotypeIcon *stereotypeIcon)
{
    IconShape iconShape;
    QHash<int, ShapeValueF> values;
    typedef QHash<int, IconCommandParameter> Parameters;
    expectBlockBegin();
    Token token;
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_CIRCLE:
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y) << SCALED(KEYWORD_RADIUS));
            iconShape.addCircle(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)), values.value(KEYWORD_RADIUS));
            break;
        case KEYWORD_ELLIPSE:
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y)
                        << SCALED(KEYWORD_RADIUS_X) << SCALED(KEYWORD_RADIUS_Y));
            iconShape.addEllipse(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)),
                                 ShapeSizeF(values.value(KEYWORD_RADIUS_X), values.value(KEYWORD_RADIUS_Y)));
            break;
        case KEYWORD_LINE:
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X0) << SCALED(KEYWORD_Y0)
                        << SCALED(KEYWORD_X1) << SCALED(KEYWORD_Y1));
            iconShape.addLine(ShapePointF(values.value(KEYWORD_X0), values.value(KEYWORD_Y0)),
                              ShapePointF(values.value(KEYWORD_X1), values.value(KEYWORD_Y1)));
            break;
        case KEYWORD_RECT:
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y)
                        << SCALED(KEYWORD_WIDTH) << SCALED(KEYWORD_HEIGHT));
            iconShape.addRect(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)),
                              ShapeSizeF(values.value(KEYWORD_WIDTH), values.value(KEYWORD_HEIGHT)));
            break;
        case KEYWORD_ROUNDEDRECT:
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y)
                        << SCALED(KEYWORD_WIDTH) << SCALED(KEYWORD_HEIGHT) << FIX(KEYWORD_RADIUS));
            iconShape.addRoundedRect(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)),
                                     ShapeSizeF(values.value(KEYWORD_WIDTH), values.value(KEYWORD_HEIGHT)),
                                     values.value(KEYWORD_RADIUS));
            break;
        case KEYWORD_ARC:
        {
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y)
                        << SCALED(KEYWORD_RADIUS_X) << SCALED(KEYWORD_RADIUS_Y)
                        << ABSOLUTE(KEYWORD_START) << ABSOLUTE(KEYWORD_SPAN));
            qreal startAngle = expectAbsoluteValue(values.value(KEYWORD_START), d->m_scanner->sourcePos());
            qreal spanAngle = expectAbsoluteValue(values.value(KEYWORD_SPAN), d->m_scanner->sourcePos());
            iconShape.addArc(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)),
                             ShapeSizeF(values.value(KEYWORD_RADIUS_X), values.value(KEYWORD_RADIUS_Y)),
                             startAngle, spanAngle);
            break;
        }
        case KEYWORD_MOVETO:
            values = parseIconShapeProperties(Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y));
            iconShape.moveTo(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)));
            break;
        case KEYWORD_LINETO:
            values = parseIconShapeProperties(Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y));
            iconShape.lineTo(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)));
            break;
        case KEYWORD_ARCMOVETO:
        {
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y)
                        << SCALED(KEYWORD_RADIUS_X) << SCALED(KEYWORD_RADIUS_Y) << ABSOLUTE(KEYWORD_START));
            qreal angle = expectAbsoluteValue(values.value(KEYWORD_START), d->m_scanner->sourcePos());
            iconShape.arcMoveTo(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)),
                                ShapeSizeF(values.value(KEYWORD_RADIUS_X), values.value(KEYWORD_RADIUS_Y)), angle);
            break;
        }
        case KEYWORD_ARCTO:
        {
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y)
                        << SCALED(KEYWORD_RADIUS_X) << SCALED(KEYWORD_RADIUS_Y)
                        << ABSOLUTE(KEYWORD_START) << ABSOLUTE(KEYWORD_SPAN));
            qreal startAngle = expectAbsoluteValue(values.value(KEYWORD_START), d->m_scanner->sourcePos());
            qreal sweepLength = expectAbsoluteValue(values.value(KEYWORD_SPAN), d->m_scanner->sourcePos());
            iconShape.arcTo(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)),
                            ShapeSizeF(values.value(KEYWORD_RADIUS_X), values.value(KEYWORD_RADIUS_Y)),
                            startAngle, sweepLength);
            break;
        }
        case KEYWORD_CLOSE:
            iconShape.closePath();
            skipOptionalEmptyBlock();
            break;
        default:
            throwUnknownPropertyError(token);
        }
        if (!expectPropertySeparatorOrBlockEnd())
            break;
    }
    stereotypeIcon->setIconShape(iconShape);
}

QHash<int, ShapeValueF> StereotypeDefinitionParser::parseIconShapeProperties(const QHash<int, IconCommandParameter> &parameters)
{
    expectBlockBegin();
    QHash<int, ShapeValueF> values;
    Token token;
    while (readProperty(&token)) {
        if (parameters.contains(token.subtype())) {
            IconCommandParameter parameter = parameters.value(token.subtype());
            if (values.contains(token.subtype()))
                throw StereotypeDefinitionParserError(QStringLiteral("Property givent twice."), token.sourcePos());
            values.insert(token.subtype(), ShapeValueF(parseFloatProperty(), parameter.m_unit, parameter.m_origin));
        } else {
            throwUnknownPropertyError(token);
        }
        if (!expectPropertySeparatorOrBlockEnd())
            break;
    }
    if (values.count() < parameters.count())
        throw StereotypeDefinitionParserError(QStringLiteral("Missing some properties."), token.sourcePos());
    else if (values.count() > parameters.count())
        throw StereotypeDefinitionParserError(QStringLiteral("Too many properties given."), token.sourcePos());
    return values;
Jochen Becher's avatar
Jochen Becher committed
492 493 494 495 496
}

void StereotypeDefinitionParser::parseToolbar()
{
    Toolbar toolbar;
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    expectBlockBegin();
    Token token;
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_ID:
            toolbar.setId(parseIdentifierProperty());
            break;
        case KEYWORD_TITLE:
            // TODO implement
            break;
        case KEYWORD_PRIORITY:
            toolbar.setPriority(parseIntProperty());
            break;
        case KEYWORD_TOOLS:
            parseToolbarTools(&toolbar);
            break;
        default:
            throwUnknownPropertyError(token);
        }
        if (!expectPropertySeparatorOrBlockEnd())
            break;
    }
    if (toolbar.id().isEmpty())
        throw StereotypeDefinitionParserError(QStringLiteral("Missing id in Toolbar definition."), d->m_scanner->sourcePos());
Jochen Becher's avatar
Jochen Becher committed
521 522 523
    emit toolbarParsed(toolbar);
}

524
void StereotypeDefinitionParser::parseToolbarTools(Toolbar *toolbar)
Jochen Becher's avatar
Jochen Becher committed
525
{
526 527
    QList<Toolbar::Tool> tools;
    expectBlockBegin();
Jochen Becher's avatar
Jochen Becher committed
528
    Token token;
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_TOOL:
        {
            Toolbar::Tool tool;
            tool.m_toolType = Toolbar::TooltypeTool;
            parseToolbarTool(&tool);
            tools.append(tool);
            break;
        }
        case KEYWORD_SEPARATOR:
            tools.append(Toolbar::Tool());
            skipOptionalEmptyBlock();
            break;
        default:
            throwUnknownPropertyError(token);
Jochen Becher's avatar
Jochen Becher committed
545
        }
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        if (!expectPropertySeparatorOrBlockEnd())
            break;
    }
    toolbar->setTools(tools);
}

void StereotypeDefinitionParser::parseToolbarTool(Toolbar::Tool *tool)
{
    expectBlockBegin();
    Token token;
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_TITLE:
            tool->m_name = parseStringProperty();
            break;
        case KEYWORD_ELEMENT:
        {
            QString element = parseIdentifierProperty();
            static QSet<QString> elementNames = QSet<QString>()
                    << QStringLiteral("package")
                    << QStringLiteral("component")
                    << QStringLiteral("class")
                    << QStringLiteral("item")
                    << QStringLiteral("annotation")
                    << QStringLiteral("boundary");
            QString elementName = element.toLower();
            if (!elementNames.contains(elementName))
                throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for element.")).arg(element), token.sourcePos());
            tool->m_elementType = elementName;
            break;
        }
        case KEYWORD_STEREOTYPE:
            tool->m_stereotype = parseStringProperty();
            break;
        default:
           throwUnknownPropertyError(token);
        }
        if (!expectPropertySeparatorOrBlockEnd())
            break;
Jochen Becher's avatar
Jochen Becher committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
    }
}

QString StereotypeDefinitionParser::parseStringProperty()
{
    expectColon();
    return parseStringExpression();
}

int StereotypeDefinitionParser::parseIntProperty()
{
    expectColon();
    return parseIntExpression();
}

qreal StereotypeDefinitionParser::parseFloatProperty()
{
    expectColon();
    return parseFloatExpression();
}

QString StereotypeDefinitionParser::parseIdentifierProperty()
{
    expectColon();
    return parseIdentifierExpression();
}

QList<QString> StereotypeDefinitionParser::parseIdentifierListProperty()
{
    QList<QString> identifiers;
    expectColon();
    for (;;) {
617
        Token token = d->m_scanner->read();
618
        if (token.type() != Token::TokenIdentifier && token.type() != Token::TokenKeyword) {
619
            throw StereotypeDefinitionParserError(QStringLiteral("Expected identifier."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
620
        }
621
        identifiers.append(token.text());
622
        token = d->m_scanner->read();
623
        if (token.type() != Token::TokenOperator || token.subtype() != OPERATOR_COMMA) {
624
            d->m_scanner->unread(token);
Jochen Becher's avatar
Jochen Becher committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
            break;
        }
    }
    return identifiers;
}

bool StereotypeDefinitionParser::parseBoolProperty()
{
    expectColon();
    return parseBoolExpression();
}

QColor StereotypeDefinitionParser::parseColorProperty()
{
    expectColon();
    return parseColorExpression();
}

QString StereotypeDefinitionParser::parseStringExpression()
{
645
    Token token = d->m_scanner->read();
646
    if (token.type() != Token::TokenString)
647 648
        throw StereotypeDefinitionParserError(QStringLiteral("Expected string constant."), token.sourcePos());
    return token.text();
Jochen Becher's avatar
Jochen Becher committed
649 650 651 652 653
}

qreal StereotypeDefinitionParser::parseFloatExpression()
{
    Token token;
654
    token = d->m_scanner->read();
655
    if (token.type() == Token::TokenOperator && token.subtype() == OPERATOR_MINUS) {
Jochen Becher's avatar
Jochen Becher committed
656 657 658
        return -parseFloatExpression();
    } else {
        bool ok = false;
659
        if (token.type() == Token::TokenInteger) {
660
            int value = token.text().toInt(&ok);
Jochen Becher's avatar
Jochen Becher committed
661 662
            QMT_CHECK(ok);
            return value;
663
        } else if (token.type() == Token::TokenFloat) {
664
            qreal value = token.text().toDouble(&ok);
Jochen Becher's avatar
Jochen Becher committed
665 666 667
            QMT_CHECK(ok);
            return value;
        } else {
668
            throw StereotypeDefinitionParserError(QStringLiteral("Expected number constant."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
669 670 671 672 673 674 675
        }
    }
}

int StereotypeDefinitionParser::parseIntExpression()
{
    Token token;
676
    token = d->m_scanner->read();
677
    if (token.type() == Token::TokenOperator && token.subtype() == OPERATOR_MINUS) {
Jochen Becher's avatar
Jochen Becher committed
678 679 680
        return -parseIntExpression();
    } else {
        bool ok = false;
681
        if (token.type() == Token::TokenInteger) {
682
            int value = token.text().toInt(&ok);
Jochen Becher's avatar
Jochen Becher committed
683 684 685
            QMT_CHECK(ok);
            return value;
        } else {
686
            throw StereotypeDefinitionParserError(QStringLiteral("Expected integer constant."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
687 688 689 690 691 692
        }
    }
}

QString StereotypeDefinitionParser::parseIdentifierExpression()
{
693
    Token token = d->m_scanner->read();
694
    if (token.type() != Token::TokenIdentifier && token.type() != Token::TokenKeyword)
695 696
        throw StereotypeDefinitionParserError(QStringLiteral("Expected identifier."), token.sourcePos());
    return token.text();
Jochen Becher's avatar
Jochen Becher committed
697 698 699 700
}

bool StereotypeDefinitionParser::parseBoolExpression()
{
701
    Token token = d->m_scanner->read();
702
    if (token.type() == Token::TokenIdentifier) {
703
        QString value = token.text().toLower();
704
        if (value == QStringLiteral("yes") || value == QStringLiteral("true"))
Jochen Becher's avatar
Jochen Becher committed
705
            return true;
706
        else if (value == QStringLiteral("no") || value == QStringLiteral("false"))
Jochen Becher's avatar
Jochen Becher committed
707 708
            return false;
    }
709
    throw StereotypeDefinitionParserError(QStringLiteral("Expected 'yes', 'no', 'true' or 'false'."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
710 711 712 713
}

QColor StereotypeDefinitionParser::parseColorExpression()
{
714
    Token token = d->m_scanner->read();
715
    if (token.type() == Token::TokenIdentifier || token.type() == Token::TokenColor) {
716
        QString value = token.text().toLower();
Jochen Becher's avatar
Jochen Becher committed
717 718 719 720 721 722
        QColor color;
        if (QColor::isValidColor(value)) {
            color.setNamedColor(value);
            return color;
        }
    }
723
    throw StereotypeDefinitionParserError(QStringLiteral("Expected color name."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
724 725
}

726
void StereotypeDefinitionParser::expectBlockBegin()
Jochen Becher's avatar
Jochen Becher committed
727
{
728 729
    skipEOLTokens();
    expectOperator(OPERATOR_BRACE_OPEN, QStringLiteral("{"));
Jochen Becher's avatar
Jochen Becher committed
730 731
}

732
bool StereotypeDefinitionParser::readProperty(Token *token)
Jochen Becher's avatar
Jochen Becher committed
733
{
734 735 736 737 738 739 740 741 742 743
    *token = readNextToken();
    if (isOperator(*token, OPERATOR_BRACE_CLOSE))
        return false;
    else if (token->type() == Token::TokenKeyword)
        return true;
    else if (token->type() == Token::TokenIdentifier)
        throwUnknownPropertyError(*token);
    else
        throw StereotypeDefinitionParserError(QStringLiteral("Syntax error."), token->sourcePos());
    return false; // will never be reached but avoids compiler warning
Jochen Becher's avatar
Jochen Becher committed
744 745
}

746
void StereotypeDefinitionParser::throwUnknownPropertyError(const Token &token)
Jochen Becher's avatar
Jochen Becher committed
747
{
748 749 750 751 752 753
    throw StereotypeDefinitionParserError(QString(QStringLiteral("Unknown property '%1'.")).arg(token.text()), token.sourcePos());
}

bool StereotypeDefinitionParser::expectPropertySeparatorOrBlockEnd()
{
    bool ok = false;
754
    Token token = d->m_scanner->read();
755 756 757 758
    if (token.type() == Token::TokenEndOfLine) {
        skipEOLTokens();
        token = d->m_scanner->read();
        ok = true;
759
    }
760 761 762 763 764 765 766 767 768
    if (token.type() == Token::TokenOperator && token.subtype() == OPERATOR_SEMICOLON)
        ok = true;
    else if (token.type() == Token::TokenOperator && token.subtype() == OPERATOR_BRACE_CLOSE)
        return false;
    else
        d->m_scanner->unread(token);
    if (!ok)
        throw StereotypeDefinitionParserError(QStringLiteral("Expected ';', '}' or end-of-line."), token.sourcePos());
    return true;
Jochen Becher's avatar
Jochen Becher committed
769 770
}

771
void StereotypeDefinitionParser::skipOptionalEmptyBlock()
Jochen Becher's avatar
Jochen Becher committed
772
{
773
    Token token = d->m_scanner->read();
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    if (token.type() == Token::TokenEndOfLine) {
        Token eolToken = token;
        for (;;) {
            token = d->m_scanner->read();
            if (token.type() != Token::TokenEndOfLine)
                break;
            eolToken = token;
        }
        if (isOperator(token, OPERATOR_BRACE_OPEN)) {
            token = readNextToken();
            if (!isOperator(token, OPERATOR_BRACE_CLOSE))
                throw StereotypeDefinitionParserError(QStringLiteral("Expected '}' in empty block."), token.sourcePos());
        } else {
            d->m_scanner->unread(token);
            d->m_scanner->unread(eolToken);
        }
    } else if (isOperator(token, OPERATOR_BRACE_OPEN)) {
        token = readNextToken();
        if (!isOperator(token, OPERATOR_BRACE_CLOSE))
            throw StereotypeDefinitionParserError(QStringLiteral("Expected '}' in empty block."), token.sourcePos());
    } else {
795
        d->m_scanner->unread(token);
Jochen Becher's avatar
Jochen Becher committed
796
    }
797 798 799 800 801 802 803 804 805 806 807 808
}

qreal StereotypeDefinitionParser::expectAbsoluteValue(const ShapeValueF &value, const SourcePos &sourcePos)
{
    if (value.unit() != ShapeValueF::UnitAbsolute || value.origin() != ShapeValueF::OriginSmart)
        throw StereotypeDefinitionParserError(QStringLiteral("Expected absolute value"), sourcePos);
    return value.value();
}

bool StereotypeDefinitionParser::isOperator(const Token &token, int op) const
{
    return token.type() == Token::TokenOperator && token.subtype() == op;
Jochen Becher's avatar
Jochen Becher committed
809 810
}

811
void StereotypeDefinitionParser::expectOperator(int op, const QString &opName)
Jochen Becher's avatar
Jochen Becher committed
812
{
813
    Token token = d->m_scanner->read();
814
    if (!isOperator(token, op))
815
        throw StereotypeDefinitionParserError(QString(QStringLiteral("Expected '%1'.")).arg(opName), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
816 817
}

818
void StereotypeDefinitionParser::expectColon()
Jochen Becher's avatar
Jochen Becher committed
819
{
820
    expectOperator(OPERATOR_COLON, QStringLiteral(":"));
Jochen Becher's avatar
Jochen Becher committed
821 822
}

823
void StereotypeDefinitionParser::skipEOLTokens()
Jochen Becher's avatar
Jochen Becher committed
824
{
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    Token token;
    for (;;) {
        token = d->m_scanner->read();
        if (token.type() != Token::TokenEndOfLine)
            break;
    }
    d->m_scanner->unread(token);
}

Token StereotypeDefinitionParser::readNextToken()
{
    Token token;
    for (;;) {
        token = d->m_scanner->read();
        if (token.type() != Token::TokenEndOfLine)
            return token;
    }
Jochen Becher's avatar
Jochen Becher committed
842 843
}

844
} // namespace qmt