stereotypedefinitionparser.cpp 34.2 KB
Newer Older
Jochen Becher's avatar
Jochen Becher committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/***************************************************************************
**
** Copyright (C) 2015 Jochen Becher
** Contact: http://www.qt.io/licensing
**
** 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
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#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;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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
64 65 66 67 68 69 70 71 72 73 74 75
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;

76 77 78 79 80 81 82 83 84 85 86 87 88
// 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
89
// Toolbar Definition
90 91 92 93 94 95
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
96 97

// Operatoren
98 99 100 101 102 103 104
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
105 106 107 108 109 110 111

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;
}

112 113 114
StereotypeDefinitionParserError::StereotypeDefinitionParserError(const QString &errorMsg, const SourcePos &sourcePos)
    : Exception(errorMsg),
      m_sourcePos(sourcePos)
Jochen Becher's avatar
Jochen Becher committed
115 116 117 118 119 120 121
{
}

StereotypeDefinitionParserError::~StereotypeDefinitionParserError()
{
}

122
class StereotypeDefinitionParser::StereotypeDefinitionParserPrivate
Jochen Becher's avatar
Jochen Becher committed
123
{
124
public:
125
    TextScanner *m_scanner = 0;
Jochen Becher's avatar
Jochen Becher committed
126 127 128

};

129
class StereotypeDefinitionParser::IconCommandParameter
Jochen Becher's avatar
Jochen Becher committed
130
{
131
public:
132 133 134 135 136
    IconCommandParameter() = default;

    IconCommandParameter(int keyword, ShapeValueF::Unit unit, ShapeValueF::Origin origin = ShapeValueF::OriginSmart)
        : m_keyword(keyword),
          m_unit(unit),
137
          m_origin(origin)
Jochen Becher's avatar
Jochen Becher committed
138 139 140
    {
    }

141
    int m_keyword = -1;
142 143
    ShapeValueF::Unit m_unit;
    ShapeValueF::Origin m_origin;
Jochen Becher's avatar
Jochen Becher committed
144 145
};

146 147 148
StereotypeDefinitionParser::StereotypeDefinitionParser(QObject *parent)
    : QObject(parent),
      d(new StereotypeDefinitionParserPrivate)
Jochen Becher's avatar
Jochen Becher committed
149 150 151 152 153 154 155 156 157 158
{
}

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

void StereotypeDefinitionParser::parse(ITextSource *source)
{
159 160
    TextScanner textScanner;
    textScanner.setKeywords(
Jochen Becher's avatar
Jochen Becher committed
161 162
                QList<QPair<QString, int> >()
                << qMakePair(QString(QStringLiteral("icon")), KEYWORD_ICON)
163
                << qMakePair(QString(QStringLiteral("id")), KEYWORD_ID)
Jochen Becher's avatar
Jochen Becher committed
164 165 166 167 168 169 170 171 172 173 174
                << 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)
175
                << qMakePair(QString(QStringLiteral("shape")), KEYWORD_SHAPE)
Jochen Becher's avatar
Jochen Becher committed
176 177 178 179 180 181 182 183 184 185 186
                << 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)
187 188 189 190 191 192 193 194 195 196 197
                << 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
198 199
                << qMakePair(QString(QStringLiteral("toolbar")), KEYWORD_TOOLBAR)
                << qMakePair(QString(QStringLiteral("priority")), KEYWORD_PRIORITY)
200
                << qMakePair(QString(QStringLiteral("tools")), KEYWORD_TOOLS)
Jochen Becher's avatar
Jochen Becher committed
201
                << qMakePair(QString(QStringLiteral("tool")), KEYWORD_TOOL)
202
                << qMakePair(QString(QStringLiteral("element")), KEYWORD_ELEMENT)
Jochen Becher's avatar
Jochen Becher committed
203 204
                << qMakePair(QString(QStringLiteral("separator")), KEYWORD_SEPARATOR)
                );
205
    textScanner.setOperators(
Jochen Becher's avatar
Jochen Becher committed
206 207
                QList<QPair<QString, int> >()
                << qMakePair(QString(QStringLiteral(";")), OPERATOR_SEMICOLON)
208 209
                << qMakePair(QString(QStringLiteral("{")), OPERATOR_BRACE_OPEN)
                << qMakePair(QString(QStringLiteral("}")), OPERATOR_BRACE_CLOSE)
Jochen Becher's avatar
Jochen Becher committed
210 211 212 213 214
                << qMakePair(QString(QStringLiteral(":")), OPERATOR_COLON)
                << qMakePair(QString(QStringLiteral(",")), OPERATOR_COMMA)
                << qMakePair(QString(QStringLiteral(".")), OPERATOR_PERIOD)
                << qMakePair(QString(QStringLiteral("-")), OPERATOR_MINUS)
                );
215
    textScanner.setSource(source);
Jochen Becher's avatar
Jochen Becher committed
216

217
    d->m_scanner = &textScanner;
Jochen Becher's avatar
Jochen Becher committed
218 219 220
    try {
        parseFile();
    } catch (...) {
221
        d->m_scanner = 0;
Jochen Becher's avatar
Jochen Becher committed
222 223
        throw;
    }
224
    d->m_scanner = 0;
Jochen Becher's avatar
Jochen Becher committed
225 226 227 228 229 230
}

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

void StereotypeDefinitionParser::parseIcon()
{
244
    StereotypeIcon stereotypeIcon;
Jochen Becher's avatar
Jochen Becher committed
245 246
    QSet<StereotypeIcon::Element> elements;
    QSet<QString> stereotypes;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    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
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 299 300 301 302 303
            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
304
            }
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
            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
322
            }
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
            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
338
            }
339 340 341 342 343 344 345 346 347 348
            break;
        }
        case KEYWORD_BASECOLOR:
            stereotypeIcon.setBaseColor(parseColorProperty());
            break;
        case KEYWORD_SHAPE:
            parseIconShape(&stereotypeIcon);
            break;
        default:
            throwUnknownPropertyError(token);
Jochen Becher's avatar
Jochen Becher committed
349
        }
350 351
        if (!expectPropertySeparatorOrBlockEnd())
            break;
Jochen Becher's avatar
Jochen Becher committed
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 492 493 494 495 496
    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
497 498 499 500 501
}

void StereotypeDefinitionParser::parseToolbar()
{
    Toolbar toolbar;
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    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
526 527 528
    emit toolbarParsed(toolbar);
}

529
void StereotypeDefinitionParser::parseToolbarTools(Toolbar *toolbar)
Jochen Becher's avatar
Jochen Becher committed
530
{
531 532
    QList<Toolbar::Tool> tools;
    expectBlockBegin();
Jochen Becher's avatar
Jochen Becher committed
533
    Token token;
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    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
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 585 586 587 588 589
        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
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 617 618 619 620 621
    }
}

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 (;;) {
622
        Token token = d->m_scanner->read();
623
        if (token.type() != Token::TokenIdentifier && token.type() != Token::TokenKeyword) {
624
            throw StereotypeDefinitionParserError(QStringLiteral("Expected identifier."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
625
        }
626
        identifiers.append(token.text());
627
        token = d->m_scanner->read();
628
        if (token.type() != Token::TokenOperator || token.subtype() != OPERATOR_COMMA) {
629
            d->m_scanner->unread(token);
Jochen Becher's avatar
Jochen Becher committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
            break;
        }
    }
    return identifiers;
}

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

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

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

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

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

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

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

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

731
void StereotypeDefinitionParser::expectBlockBegin()
Jochen Becher's avatar
Jochen Becher committed
732
{
733 734
    skipEOLTokens();
    expectOperator(OPERATOR_BRACE_OPEN, QStringLiteral("{"));
Jochen Becher's avatar
Jochen Becher committed
735 736
}

737
bool StereotypeDefinitionParser::readProperty(Token *token)
Jochen Becher's avatar
Jochen Becher committed
738
{
739 740 741 742 743 744 745 746 747 748
    *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
749 750
}

751
void StereotypeDefinitionParser::throwUnknownPropertyError(const Token &token)
Jochen Becher's avatar
Jochen Becher committed
752
{
753 754 755 756 757 758
    throw StereotypeDefinitionParserError(QString(QStringLiteral("Unknown property '%1'.")).arg(token.text()), token.sourcePos());
}

bool StereotypeDefinitionParser::expectPropertySeparatorOrBlockEnd()
{
    bool ok = false;
759
    Token token = d->m_scanner->read();
760 761 762 763
    if (token.type() == Token::TokenEndOfLine) {
        skipEOLTokens();
        token = d->m_scanner->read();
        ok = true;
764
    }
765 766 767 768 769 770 771 772 773
    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
774 775
}

776
void StereotypeDefinitionParser::skipOptionalEmptyBlock()
Jochen Becher's avatar
Jochen Becher committed
777
{
778
    Token token = d->m_scanner->read();
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
    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 {
800
        d->m_scanner->unread(token);
Jochen Becher's avatar
Jochen Becher committed
801
    }
802 803 804 805 806 807 808 809 810 811 812 813
}

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
814 815
}

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

823
void StereotypeDefinitionParser::expectColon()
Jochen Becher's avatar
Jochen Becher committed
824
{
825
    expectOperator(OPERATOR_COLON, QStringLiteral(":"));
Jochen Becher's avatar
Jochen Becher committed
826 827
}

828
void StereotypeDefinitionParser::skipEOLTokens()
Jochen Becher's avatar
Jochen Becher committed
829
{
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    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
847 848
}

849
} // namespace qmt