stereotypedefinitionparser.cpp 53.4 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
**
****************************************************************************/

#include "stereotypedefinitionparser.h"

#include "textscanner.h"
#include "token.h"
#include "qmt/infrastructure/qmtassert.h"
#include "qmt/stereotype/stereotypeicon.h"
#include "qmt/stereotype/shapevalue.h"
33
#include "qmt/stereotype/customrelation.h"
Jochen Becher's avatar
Jochen Becher committed
34 35 36 37 38 39 40 41 42 43 44
#include "qmt/stereotype/toolbar.h"

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

namespace qmt {

// Icon Definition
static const int KEYWORD_ICON          =  1;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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
60 61 62 63 64 65 66 67 68 69 70 71
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;

72 73 74 75 76 77 78 79 80 81 82 83 84
// 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
85
// Toolbar Definition
86 87 88 89 90 91
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
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
// Relation Definition
static const int KEYWORD_RELATION      = 100;
static const int KEYWORD_DEPENDENCY    = 101;
static const int KEYWORD_INHERITANCE   = 102;
static const int KEYWORD_ASSOCIATION   = 103;
static const int KEYWORD_NAME          = 104;
static const int KEYWORD_DIRECTION     = 105;
static const int KEYWORD_ATOB          = 106;
static const int KEYWORD_BTOA          = 107;
static const int KEYWORD_BI            = 108;
static const int KEYWORD_END           = 109;
static const int KEYWORD_A             = 110;
static const int KEYWORD_B             = 111;
static const int KEYWORD_ROLE          = 112;
static const int KEYWORD_CARDINALITY   = 113;
static const int KEYWORD_NAVIGABLE     = 114;
static const int KEYWORD_RELATIONSHIP  = 115;
static const int KEYWORD_AGGREGATION   = 116;
static const int KEYWORD_COMPOSITION   = 117;
static const int KEYWORD_SHAFT         = 118;
static const int KEYWORD_HEAD          = 119;

// Relation Shapes
static const int KEYWORD_DIAMOND       = 130;
static const int KEYWORD_TRIANGLE      = 131;
static const int KEYWORD_FILLED        = 132;
static const int KEYWORD_PATTERN       = 133;
static const int KEYWORD_SOLID         = 134;
static const int KEYWORD_DOT           = 135;
static const int KEYWORD_DASH          = 136;
static const int KEYWORD_DASHDOT       = 137;
static const int KEYWORD_DASHDOTDOT    = 138;
static const int KEYWORD_COLOR         = 139;

Jochen Becher's avatar
Jochen Becher committed
127
// Operatoren
128 129 130 131 132 133 134
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
135 136 137 138 139 140 141

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

142 143 144
StereotypeDefinitionParserError::StereotypeDefinitionParserError(const QString &errorMsg, const SourcePos &sourcePos)
    : Exception(errorMsg),
      m_sourcePos(sourcePos)
Jochen Becher's avatar
Jochen Becher committed
145 146 147 148 149 150 151
{
}

StereotypeDefinitionParserError::~StereotypeDefinitionParserError()
{
}

152
class StereotypeDefinitionParser::StereotypeDefinitionParserPrivate
Jochen Becher's avatar
Jochen Becher committed
153
{
154
public:
155
    TextScanner *m_scanner = nullptr;
Jochen Becher's avatar
Jochen Becher committed
156 157 158

};

159
class StereotypeDefinitionParser::IconCommandParameter
Jochen Becher's avatar
Jochen Becher committed
160
{
161
public:
162 163 164 165 166
    enum Type {
        ShapeValue,
        Boolean
    };

167 168 169 170
    IconCommandParameter() = default;

    IconCommandParameter(int keyword, ShapeValueF::Unit unit, ShapeValueF::Origin origin = ShapeValueF::OriginSmart)
        : m_keyword(keyword),
171
          m_type(ShapeValue),
172
          m_unit(unit),
173
          m_origin(origin)
Jochen Becher's avatar
Jochen Becher committed
174 175 176
    {
    }

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    IconCommandParameter(int keyword, Type type)
        : m_keyword(keyword),
          m_type(type)
    {
    }

    operator ShapeValueF() const { return m_shapeValue; }

    Type type() const { return m_type; }
    ShapeValueF::Unit unit() const { return m_unit; }
    ShapeValueF::Origin origin() const { return m_origin; }
    ShapeValueF shapeValue() const { return m_shapeValue; }
    void setShapeValue(const ShapeValueF &shapeValue) { m_shapeValue = shapeValue; }
    bool boolean() const { return m_boolean; }
    void setBoolean(bool boolean) { m_boolean = boolean; }

private:
194
    int m_keyword = -1;
195
    Type m_type = ShapeValue;
196
    ShapeValueF::Unit m_unit = ShapeValueF::UnitAbsolute;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    ShapeValueF::Origin m_origin = ShapeValueF::OriginSmart;
    ShapeValueF m_shapeValue;
    bool m_boolean = false;
};

class StereotypeDefinitionParser::Value
{
public:
    Value(StereotypeDefinitionParser::Type type, QVariant value)
        : m_type(type),
          m_value(value)
    {
    }

    StereotypeDefinitionParser::Type type() const { return m_type; }
    QVariant value() const { return m_value; }

private:
    StereotypeDefinitionParser::Type m_type = StereotypeDefinitionParser::Void;
    QVariant m_value;
Jochen Becher's avatar
Jochen Becher committed
217 218
};

219 220 221
StereotypeDefinitionParser::StereotypeDefinitionParser(QObject *parent)
    : QObject(parent),
      d(new StereotypeDefinitionParserPrivate)
Jochen Becher's avatar
Jochen Becher committed
222 223 224 225 226 227 228 229 230 231
{
}

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

void StereotypeDefinitionParser::parse(ITextSource *source)
{
232 233
    TextScanner textScanner;
    textScanner.setKeywords(
Jochen Becher's avatar
Jochen Becher committed
234 235
                QList<QPair<QString, int> >()
                << qMakePair(QString(QStringLiteral("icon")), KEYWORD_ICON)
236
                << qMakePair(QString(QStringLiteral("id")), KEYWORD_ID)
Jochen Becher's avatar
Jochen Becher committed
237 238 239 240 241 242 243 244 245 246 247
                << 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)
248
                << qMakePair(QString(QStringLiteral("shape")), KEYWORD_SHAPE)
Jochen Becher's avatar
Jochen Becher committed
249 250 251 252 253 254 255 256 257 258 259
                << 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)
260 261 262 263 264 265 266 267 268 269 270
                << 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
271 272
                << qMakePair(QString(QStringLiteral("toolbar")), KEYWORD_TOOLBAR)
                << qMakePair(QString(QStringLiteral("priority")), KEYWORD_PRIORITY)
273
                << qMakePair(QString(QStringLiteral("tools")), KEYWORD_TOOLS)
Jochen Becher's avatar
Jochen Becher committed
274
                << qMakePair(QString(QStringLiteral("tool")), KEYWORD_TOOL)
275
                << qMakePair(QString(QStringLiteral("element")), KEYWORD_ELEMENT)
Jochen Becher's avatar
Jochen Becher committed
276
                << qMakePair(QString(QStringLiteral("separator")), KEYWORD_SEPARATOR)
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 304 305 306
                << qMakePair(QString(QStringLiteral("relation")), KEYWORD_RELATION)
                << qMakePair(QString(QStringLiteral("dependency")), KEYWORD_DEPENDENCY)
                << qMakePair(QString(QStringLiteral("inheritance")), KEYWORD_INHERITANCE)
                << qMakePair(QString(QStringLiteral("association")), KEYWORD_ASSOCIATION)
                << qMakePair(QString(QStringLiteral("name")), KEYWORD_NAME)
                << qMakePair(QString(QStringLiteral("direction")), KEYWORD_DIRECTION)
                << qMakePair(QString(QStringLiteral("atob")), KEYWORD_ATOB)
                << qMakePair(QString(QStringLiteral("btoa")), KEYWORD_BTOA)
                << qMakePair(QString(QStringLiteral("bi")), KEYWORD_BI)
                << qMakePair(QString(QStringLiteral("end")), KEYWORD_END)
                << qMakePair(QString(QStringLiteral("a")), KEYWORD_A)
                << qMakePair(QString(QStringLiteral("b")), KEYWORD_B)
                << qMakePair(QString(QStringLiteral("role")), KEYWORD_ROLE)
                << qMakePair(QString(QStringLiteral("cardinality")), KEYWORD_CARDINALITY)
                << qMakePair(QString(QStringLiteral("navigable")), KEYWORD_NAVIGABLE)
                << qMakePair(QString(QStringLiteral("relationship")), KEYWORD_RELATIONSHIP)
                << qMakePair(QString(QStringLiteral("aggregation")), KEYWORD_AGGREGATION)
                << qMakePair(QString(QStringLiteral("composition")), KEYWORD_COMPOSITION)
                << qMakePair(QString(QStringLiteral("shaft")), KEYWORD_SHAFT)
                << qMakePair(QString(QStringLiteral("head")), KEYWORD_HEAD)
                << qMakePair(QString(QStringLiteral("diamond")), KEYWORD_DIAMOND)
                << qMakePair(QString(QStringLiteral("triangle")), KEYWORD_TRIANGLE)
                << qMakePair(QString(QStringLiteral("filled")), KEYWORD_FILLED)
                << qMakePair(QString(QStringLiteral("pattern")), KEYWORD_PATTERN)
                << qMakePair(QString(QStringLiteral("solid")), KEYWORD_SOLID)
                << qMakePair(QString(QStringLiteral("dot")), KEYWORD_DOT)
                << qMakePair(QString(QStringLiteral("dash")), KEYWORD_DASH)
                << qMakePair(QString(QStringLiteral("dashdot")), KEYWORD_DASHDOT)
                << qMakePair(QString(QStringLiteral("dashdotdot")), KEYWORD_DASHDOTDOT)
                << qMakePair(QString(QStringLiteral("color")), KEYWORD_COLOR)
Jochen Becher's avatar
Jochen Becher committed
307
                );
308
    textScanner.setOperators(
Jochen Becher's avatar
Jochen Becher committed
309 310
                QList<QPair<QString, int> >()
                << qMakePair(QString(QStringLiteral(";")), OPERATOR_SEMICOLON)
311 312
                << qMakePair(QString(QStringLiteral("{")), OPERATOR_BRACE_OPEN)
                << qMakePair(QString(QStringLiteral("}")), OPERATOR_BRACE_CLOSE)
Jochen Becher's avatar
Jochen Becher committed
313 314 315 316 317
                << qMakePair(QString(QStringLiteral(":")), OPERATOR_COLON)
                << qMakePair(QString(QStringLiteral(",")), OPERATOR_COMMA)
                << qMakePair(QString(QStringLiteral(".")), OPERATOR_PERIOD)
                << qMakePair(QString(QStringLiteral("-")), OPERATOR_MINUS)
                );
318
    textScanner.setSource(source);
Jochen Becher's avatar
Jochen Becher committed
319

320
    d->m_scanner = &textScanner;
Jochen Becher's avatar
Jochen Becher committed
321 322 323
    try {
        parseFile();
    } catch (...) {
324
        d->m_scanner = 0;
Jochen Becher's avatar
Jochen Becher committed
325 326
        throw;
    }
327
    d->m_scanner = 0;
Jochen Becher's avatar
Jochen Becher committed
328 329 330 331 332 333
}

void StereotypeDefinitionParser::parseFile()
{
    for (;;) {
        Token token = readNextToken();
334
        if (token.type() == Token::TokenEndOfInput)
Jochen Becher's avatar
Jochen Becher committed
335
            break;
336
        else if (token.type() == Token::TokenKeyword && token.subtype() == KEYWORD_ICON)
Jochen Becher's avatar
Jochen Becher committed
337
            parseIcon();
338
        else if (token.type() == Token::TokenKeyword && token.subtype() == KEYWORD_TOOLBAR)
Jochen Becher's avatar
Jochen Becher committed
339
            parseToolbar();
340 341 342 343 344 345 346 347
        else if (token.type() == Token::TokenKeyword && token.subtype() == KEYWORD_RELATION)
            parseRelation(CustomRelation::Element::Relation);
        else if (token.type() == Token::TokenKeyword && token.subtype() == KEYWORD_DEPENDENCY)
            parseRelation(CustomRelation::Element::Dependency);
        else if (token.type() == Token::TokenKeyword && token.subtype() == KEYWORD_INHERITANCE)
            parseRelation(CustomRelation::Element::Inheritance);
        else if (token.type() == Token::TokenKeyword && token.subtype() == KEYWORD_ASSOCIATION)
            parseRelation(CustomRelation::Element::Association);
348
        else
349
            throw StereotypeDefinitionParserError(QStringLiteral("Expected 'Icon', 'Toolbar', 'Relation', 'Dependency', 'Inheritance' or 'Association'."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
350 351 352 353 354
    }
}

void StereotypeDefinitionParser::parseIcon()
{
355
    StereotypeIcon stereotypeIcon;
Jochen Becher's avatar
Jochen Becher committed
356 357
    QSet<StereotypeIcon::Element> elements;
    QSet<QString> stereotypes;
358 359 360 361 362 363 364 365 366 367 368 369
    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:
        {
370 371 372 373 374 375 376 377 378
            const 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);
            parseEnums<StereotypeIcon::Element>(
                        parseIdentifierListProperty(), elementNames, token.sourcePos(),
                        [&](StereotypeIcon::Element element) { elements.insert(element); });
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
            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:
        {
398
            const static QHash<QString, StereotypeIcon::SizeLock> lockNames = QHash<QString, StereotypeIcon::SizeLock>()
399 400 401 402 403
                    << 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);
404 405 406
            parseEnum<StereotypeIcon::SizeLock>(
                        parseIdentifierProperty(), lockNames, token.sourcePos(),
                        [&](StereotypeIcon::SizeLock lock) { stereotypeIcon.setSizeLock(lock); });
407 408 409 410
            break;
        }
        case KEYWORD_DISPLAY:
        {
411
            const static QHash<QString, StereotypeIcon::Display> displayNames = QHash<QString, StereotypeIcon::Display>()
412 413 414 415 416
                    << 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);
417 418 419
            parseEnum<StereotypeIcon::Display>(
                        parseIdentifierProperty(), displayNames, token.sourcePos(),
                        [&](StereotypeIcon::Display display) { stereotypeIcon.setDisplay(display); });
420 421 422 423
            break;
        }
        case KEYWORD_TEXTALIGN:
        {
424
            const static QHash<QString, StereotypeIcon::TextAlignment> alignNames = QHash<QString, StereotypeIcon::TextAlignment>()
425 426 427
                    << qMakePair(QString(QStringLiteral("below")), StereotypeIcon::TextalignBelow)
                    << qMakePair(QString(QStringLiteral("center")), StereotypeIcon::TextalignCenter)
                    << qMakePair(QString(QStringLiteral("none")), StereotypeIcon::TextalignNone);
428 429 430
            parseEnum<StereotypeIcon::TextAlignment>(
                        parseIdentifierProperty(), alignNames, token.sourcePos(),
                        [&](StereotypeIcon::TextAlignment align) { stereotypeIcon.setTextAlignment(align); });
431 432 433 434 435 436
            break;
        }
        case KEYWORD_BASECOLOR:
            stereotypeIcon.setBaseColor(parseColorProperty());
            break;
        case KEYWORD_SHAPE:
437
            stereotypeIcon.setIconShape(parseIconShape());
438 439 440
            break;
        default:
            throwUnknownPropertyError(token);
Jochen Becher's avatar
Jochen Becher committed
441
        }
442 443
        if (!expectPropertySeparatorOrBlockEnd())
            break;
Jochen Becher's avatar
Jochen Becher committed
444
    }
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    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));
}

467 468 469 470 471 472
QPair<int, StereotypeDefinitionParser::IconCommandParameter> StereotypeDefinitionParser::BOOLEAN(int keyword)
{
    return qMakePair(keyword, IconCommandParameter(keyword, IconCommandParameter::Boolean));
}

IconShape StereotypeDefinitionParser::parseIconShape()
473 474
{
    IconShape iconShape;
475
    QHash<int, IconCommandParameter> values;
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    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;
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
        case KEYWORD_DIAMOND:
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y)
                        << SCALED(KEYWORD_WIDTH) << SCALED(KEYWORD_HEIGHT)
                        << BOOLEAN(KEYWORD_FILLED));
            iconShape.addDiamond(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)),
                                ShapeSizeF(values.value(KEYWORD_WIDTH), values.value(KEYWORD_HEIGHT)),
                                values.value(KEYWORD_FILLED).boolean());
            break;
        case KEYWORD_TRIANGLE:
            values = parseIconShapeProperties(
                        Parameters() << SCALED(KEYWORD_X) << SCALED(KEYWORD_Y)
                        << SCALED(KEYWORD_WIDTH) << SCALED(KEYWORD_HEIGHT)
                        << BOOLEAN(KEYWORD_FILLED));
            iconShape.addTriangle(ShapePointF(values.value(KEYWORD_X), values.value(KEYWORD_Y)),
                                  ShapeSizeF(values.value(KEYWORD_WIDTH), values.value(KEYWORD_HEIGHT)),
                                  values.value(KEYWORD_FILLED).boolean());
            break;
581 582 583 584 585 586
        default:
            throwUnknownPropertyError(token);
        }
        if (!expectPropertySeparatorOrBlockEnd())
            break;
    }
587
    return iconShape;
588 589
}

590
QHash<int, StereotypeDefinitionParser::IconCommandParameter> StereotypeDefinitionParser::parseIconShapeProperties(const QHash<int, IconCommandParameter> &parameters)
591 592
{
    expectBlockBegin();
593
    QHash<int, IconCommandParameter> values;
594 595 596 597
    Token token;
    while (readProperty(&token)) {
        if (parameters.contains(token.subtype())) {
            if (values.contains(token.subtype()))
598 599 600 601 602 603 604 605 606
                throw StereotypeDefinitionParserError(QStringLiteral("Property given twice."), token.sourcePos());
            IconCommandParameter parameter = parameters.value(token.subtype());
            if (parameter.type() == IconCommandParameter::ShapeValue)
                parameter.setShapeValue(ShapeValueF(parseFloatProperty(), parameter.unit(), parameter.origin()));
            else if (parameter.type() == IconCommandParameter::Boolean)
                parameter.setBoolean(parseBoolProperty());
            else
                throw StereotypeDefinitionParserError("Unexpected type of property.", token.sourcePos());
            values.insert(token.subtype(), parameter);
607 608 609 610 611 612 613 614 615 616 617
        } 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
618 619
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
void StereotypeDefinitionParser::parseRelation(CustomRelation::Element element)
{
    CustomRelation relation;
    relation.setElement(element);
    QSet<QString> stereotypes;
    expectBlockBegin();
    Token token;
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_ID:
            relation.setId(parseIdentifierProperty());
            break;
        case KEYWORD_TITLE:
            relation.setTitle(parseStringProperty());
            break;
        case KEYWORD_ELEMENTS:
            relation.setEndItems(parseIdentifierListProperty());
            break;
        case KEYWORD_STEREOTYPE:
            stereotypes.insert(parseStringProperty());
            break;
        case KEYWORD_NAME:
            relation.setName(parseStringProperty());
            break;
        case KEYWORD_DIRECTION:
        {
            const static QHash<QString, CustomRelation::Direction> directionNames = QHash<QString, CustomRelation::Direction>()
                    << qMakePair(QString(QStringLiteral("atob")), CustomRelation::Direction::AtoB)
                    << qMakePair(QString(QStringLiteral("btoa")), CustomRelation::Direction::BToA)
                    << qMakePair(QString(QStringLiteral("bi")), CustomRelation::Direction::Bi);
            if (element != CustomRelation::Element::Dependency)
                throwUnknownPropertyError(token);
            parseEnum<CustomRelation::Direction>(
                        parseIdentifierProperty(), directionNames, token.sourcePos(),
                        [&](CustomRelation::Direction direction) { relation.setDirection(direction); });
            break;
        }
        case KEYWORD_PATTERN:
        {
            const static QHash<QString, CustomRelation::ShaftPattern> patternNames = QHash<QString, CustomRelation::ShaftPattern>()
                    << qMakePair(QString(QStringLiteral("solid")), CustomRelation::ShaftPattern::Solid)
                    << qMakePair(QString(QStringLiteral("dash")), CustomRelation::ShaftPattern::Dash)
                    << qMakePair(QString(QStringLiteral("dot")), CustomRelation::ShaftPattern::Dot)
                    << qMakePair(QString(QStringLiteral("dashdot")), CustomRelation::ShaftPattern::DashDot)
                    << qMakePair(QString(QStringLiteral("dashdotdot")), CustomRelation::ShaftPattern::DashDotDot);
            if (element != CustomRelation::Element::Relation)
                throwUnknownPropertyError(token);
            parseEnum<CustomRelation::ShaftPattern>(
                        parseIdentifierProperty(), patternNames, token.sourcePos(),
                        [&](CustomRelation::ShaftPattern pattern) { relation.setShaftPattern(pattern); });
            break;
        }
        case KEYWORD_COLOR:
        {
            if (element != CustomRelation::Element::Relation)
                throwUnknownPropertyError(token);
            Value expression = parseProperty();
            if (expression.type() == Color) {
                relation.setColorType(CustomRelation::ColorType::Custom);
                relation.setColor(expression.value().value<QColor>());
            } else if (expression.type() == Identifier) {
                QString colorValue = expression.value().toString();
                QString colorName = colorValue.toLower();
                if (colorName == "a") {
                    relation.setColorType(CustomRelation::ColorType::EndA);
                } else if (colorName == "b") {
                    relation.setColorType(CustomRelation::ColorType::EndB);
                } else if (QColor::isValidColor(colorName)) {
                    relation.setColorType(CustomRelation::ColorType::Custom);
                    relation.setColor(QColor(colorName));
                } else {
                    throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for color.")).arg(colorValue), token.sourcePos());
                }
            } else {
                throw StereotypeDefinitionParserError(QStringLiteral("Unexpected value for color."), token.sourcePos());
            }
            break;
        }
        case KEYWORD_END:
            parseRelationEnd(&relation);
            break;
        default:
            throwUnknownPropertyError(token);
        }
        if (!expectPropertySeparatorOrBlockEnd())
            break;
    }
    relation.setStereotypes(stereotypes);
    if (relation.id().isEmpty())
        throw StereotypeDefinitionParserError(QStringLiteral("Missing id in Relation definition."), d->m_scanner->sourcePos());
    emit relationParsed(relation);
}

void StereotypeDefinitionParser::parseRelationEnd(CustomRelation *relation)
{
    CustomRelation::End relationEnd;
    bool isEndB = false;
    expectBlockBegin();
    Token token;
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_END:
        {
            QString endValue = parseIdentifierProperty();
            QString endName = endValue.toLower();
            if (endName == "a")
                isEndB = false;
            else if (endName == "b")
                isEndB = true;
            else
                throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\" for end.")).arg(endValue), token.sourcePos());
            break;
        }
        case KEYWORD_ELEMENTS:
            if (relation->element() != CustomRelation::Element::Relation)
                throwUnknownPropertyError(token);
            relationEnd.setEndItems(parseIdentifierListProperty());
            break;
        case KEYWORD_ROLE:
            if (relation->element() != CustomRelation::Element::Relation && relation->element() != CustomRelation::Element::Association)
                throwUnknownPropertyError(token);
            relationEnd.setRole(parseStringProperty());
            break;
        case KEYWORD_CARDINALITY:
        {
            if (relation->element() != CustomRelation::Element::Relation && relation->element() != CustomRelation::Element::Association)
                throwUnknownPropertyError(token);
            Value expression = parseProperty();
            if (expression.type() == Int || expression.type() == String)
                relationEnd.setCardinality(expression.value().toString());
            else
                throw StereotypeDefinitionParserError("Wrong type for cardinality.", token.sourcePos());
            break;
        }
        case KEYWORD_NAVIGABLE:
            if (relation->element() != CustomRelation::Element::Relation && relation->element() != CustomRelation::Element::Association)
                throwUnknownPropertyError(token);
            relationEnd.setNavigable(parseBoolProperty());
            break;
        case KEYWORD_RELATIONSHIP:
        {
            if (relation->element() != CustomRelation::Element::Association)
                throwUnknownPropertyError(token);
            const static QHash<QString, CustomRelation::Relationship> relationshipNames = QHash<QString, CustomRelation::Relationship>()
                    << qMakePair(QString(QStringLiteral("association")), CustomRelation::Relationship::Association)
                    << qMakePair(QString(QStringLiteral("aggregation")), CustomRelation::Relationship::Aggregation)
                    << qMakePair(QString(QStringLiteral("composition")), CustomRelation::Relationship::Composition);
            parseEnum<CustomRelation::Relationship>(
                        parseIdentifierProperty(), relationshipNames, token.sourcePos(),
                        [&](CustomRelation::Relationship relationship) { relationEnd.setRelationship(relationship); });
            break;
        }
        case KEYWORD_HEAD:
        {
            if (relation->element() != CustomRelation::Element::Relation)
                throwUnknownPropertyError(token);
            const static QHash<QString, CustomRelation::Head> headNames = QHash<QString, CustomRelation::Head>()
                    << qMakePair(QString(QStringLiteral("none")), CustomRelation::Head::None)
                    << qMakePair(QString(QStringLiteral("arrow")), CustomRelation::Head::Arrow)
                    << qMakePair(QString(QStringLiteral("triangle")), CustomRelation::Head::Triangle)
                    << qMakePair(QString(QStringLiteral("filledtriangle")), CustomRelation::Head::FilledTriangle)
                    << qMakePair(QString(QStringLiteral("diamond")), CustomRelation::Head::Diamond)
                    << qMakePair(QString(QStringLiteral("filleddiamond")), CustomRelation::Head::FilledDiamond);
            parseEnum<CustomRelation::Head>(
                        parseIdentifierProperty(), headNames, token.sourcePos(),
                        [&](CustomRelation::Head head) { relationEnd.setHead(head); });
            break;
        }
        case KEYWORD_SHAPE:
            if (relation->element() != CustomRelation::Element::Relation)
                throwUnknownPropertyError(token);
            relationEnd.setHead(CustomRelation::Head::Shape);
            relationEnd.setShape(parseIconShape());
            break;
        default:
            throwUnknownPropertyError(token);
        }
        if (!expectPropertySeparatorOrBlockEnd())
            break;
    }
    if (isEndB)
        relation->setEndB(relationEnd);
    else
        relation->setEndA(relationEnd);
}

Jochen Becher's avatar
Jochen Becher committed
806 807 808
void StereotypeDefinitionParser::parseToolbar()
{
    Toolbar toolbar;
809 810 811 812 813 814 815 816 817 818 819 820 821
    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;
822 823 824 825
        case KEYWORD_ELEMENT:
            toolbar.setElementTypes(parseIdentifierListProperty());
            toolbar.setToolbarType(toolbar.elementTypes().isEmpty() ? Toolbar::ObjectToolbar : Toolbar::RelationToolbar);
            break;
826 827 828 829 830 831 832 833 834 835 836
        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
837 838 839
    emit toolbarParsed(toolbar);
}

840
void StereotypeDefinitionParser::parseToolbarTools(Toolbar *toolbar)
Jochen Becher's avatar
Jochen Becher committed
841
{
842 843
    QList<Toolbar::Tool> tools;
    expectBlockBegin();
Jochen Becher's avatar
Jochen Becher committed
844
    Token token;
845 846 847 848 849 850
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_TOOL:
        {
            Toolbar::Tool tool;
            tool.m_toolType = Toolbar::TooltypeTool;
851
            parseToolbarTool(toolbar, &tool);
852 853 854 855 856 857 858 859 860
            tools.append(tool);
            break;
        }
        case KEYWORD_SEPARATOR:
            tools.append(Toolbar::Tool());
            skipOptionalEmptyBlock();
            break;
        default:
            throwUnknownPropertyError(token);
Jochen Becher's avatar
Jochen Becher committed
861
        }
862 863 864 865 866 867
        if (!expectPropertySeparatorOrBlockEnd())
            break;
    }
    toolbar->setTools(tools);
}

868
void StereotypeDefinitionParser::parseToolbarTool(const Toolbar *toolbar, Toolbar::Tool *tool)
869 870 871 872 873 874 875 876 877 878 879
{
    expectBlockBegin();
    Token token;
    while (readProperty(&token)) {
        switch (token.subtype()) {
        case KEYWORD_TITLE:
            tool->m_name = parseStringProperty();
            break;
        case KEYWORD_ELEMENT:
        {
            QString element = parseIdentifierProperty();
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
            if (toolbar->toolbarType() == Toolbar::ObjectToolbar) {
                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;
            } else {
                static QSet<QString> relationNames = QSet<QString>()
                        << "dependency"
                        << "inheritance"
                        << "association";
                QString relationName = element.toLower();
                if (relationNames.contains(relationName))
                    tool->m_elementType = relationName;
                else
                    tool->m_elementType = element;
            }
903 904 905 906 907 908 909 910 911 912
            break;
        }
        case KEYWORD_STEREOTYPE:
            tool->m_stereotype = parseStringProperty();
            break;
        default:
           throwUnknownPropertyError(token);
        }
        if (!expectPropertySeparatorOrBlockEnd())
            break;
Jochen Becher's avatar
Jochen Becher committed
913 914 915
    }
}

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
template<typename T>
void StereotypeDefinitionParser::parseEnums(const QList<QString> &identifiers,
                                            const QHash<QString, T> &identifierNames,
                                            const SourcePos &sourcePos,
                                            std::function<void (T)> setter)
{
    for (const QString &identifier : identifiers)
        parseEnum(identifier, identifierNames, sourcePos, setter);
}

template<typename T>
void StereotypeDefinitionParser::parseEnum(const QString &identifier,
                                           const QHash<QString, T> &identifierNames,
                                           const SourcePos &sourcePos,
                                           std::function<void (T)> setter)
{
    const QString name = identifier.toLower();
    if (!identifierNames.contains(name))
        throw StereotypeDefinitionParserError(QString(QStringLiteral("Unexpected value \"%1\".")).arg(identifier), sourcePos);
    setter(identifierNames.value(name));
}

Jochen Becher's avatar
Jochen Becher committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
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 (;;) {
967
        Token token = d->m_scanner->read();
968
        if (token.type() != Token::TokenIdentifier && token.type() != Token::TokenKeyword) {
969
            throw StereotypeDefinitionParserError(QStringLiteral("Expected identifier."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
970
        }
971
        identifiers.append(token.text());
972
        token = d->m_scanner->read();
973
        if (token.type() != Token::TokenOperator || token.subtype() != OPERATOR_COMMA) {
974
            d->m_scanner->unread(token);
Jochen Becher's avatar
Jochen Becher committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
            break;
        }
    }
    return identifiers;
}

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

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

993 994 995 996 997 998
StereotypeDefinitionParser::Value StereotypeDefinitionParser::parseProperty()
{
    expectColon();
    return parseExpression();
}

Jochen Becher's avatar
Jochen Becher committed
999 1000
QString StereotypeDefinitionParser::parseStringExpression()
{
1001
    Token token = d->m_scanner->read();
1002
    if (token.type() != Token::TokenString)
1003 1004
        throw StereotypeDefinitionParserError(QStringLiteral("Expected string constant."), token.sourcePos());
    return token.text();
Jochen Becher's avatar
Jochen Becher committed
1005 1006 1007 1008 1009
}

qreal StereotypeDefinitionParser::parseFloatExpression()
{
    Token token;
1010
    token = d->m_scanner->read();
1011
    if (token.type() == Token::TokenOperator && token.subtype() == OPERATOR_MINUS) {
Jochen Becher's avatar
Jochen Becher committed
1012 1013 1014
        return -parseFloatExpression();
    } else {
        bool ok = false;
1015
        if (token.type() == Token::TokenInteger) {
1016
            int value = token.text().toInt(&ok);
Jochen Becher's avatar
Jochen Becher committed
1017 1018
            QMT_CHECK(ok);
            return value;
1019
        } else if (token.type() == Token::TokenFloat) {
1020
            qreal value = token.text().toDouble(&ok);
Jochen Becher's avatar
Jochen Becher committed
1021 1022 1023
            QMT_CHECK(ok);
            return value;
        } else {
1024
            throw StereotypeDefinitionParserError(QStringLiteral("Expected number constant."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
1025 1026 1027 1028 1029 1030 1031
        }
    }
}

int StereotypeDefinitionParser::parseIntExpression()
{
    Token token;
1032
    token = d->m_scanner->read();
1033
    if (token.type() == Token::TokenOperator && token.subtype() == OPERATOR_MINUS) {
Jochen Becher's avatar
Jochen Becher committed
1034 1035 1036
        return -parseIntExpression();
    } else {
        bool ok = false;
1037
        if (token.type() == Token::TokenInteger) {
1038
            int value = token.text().toInt(&ok);
Jochen Becher's avatar
Jochen Becher committed
1039 1040 1041
            QMT_CHECK(ok);
            return value;
        } else {
1042
            throw StereotypeDefinitionParserError(QStringLiteral("Expected integer constant."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
1043 1044 1045 1046 1047 1048
        }
    }
}

QString StereotypeDefinitionParser::parseIdentifierExpression()
{
1049
    Token token = d->m_scanner->read();
1050
    if (token.type() != Token::TokenIdentifier && token.type() != Token::TokenKeyword)
1051 1052
        throw StereotypeDefinitionParserError(QStringLiteral("Expected identifier."), token.sourcePos());
    return token.text();
Jochen Becher's avatar
Jochen Becher committed
1053 1054 1055 1056
}

bool StereotypeDefinitionParser::parseBoolExpression()
{
1057
    Token token = d->m_scanner->read();
1058
    if (token.type() == Token::TokenIdentifier) {
1059
        QString value = token.text().toLower();
1060
        if (value == QStringLiteral("yes") || value == QStringLiteral("true"))
Jochen Becher's avatar
Jochen Becher committed
1061
            return true;
1062
        else if (value == QStringLiteral("no") || value == QStringLiteral("false"))
Jochen Becher's avatar
Jochen Becher committed
1063 1064
            return false;
    }
1065
    throw StereotypeDefinitionParserError(QStringLiteral("Expected 'yes', 'no', 'true' or 'false'."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
1066 1067 1068 1069
}

QColor StereotypeDefinitionParser::parseColorExpression()
{
1070
    Token token = d->m_scanner->read();
1071
    if (token.type() == Token::TokenIdentifier || token.type() == Token::TokenColor) {
1072
        QString value = token.text().toLower();
Jochen Becher's avatar
Jochen Becher committed
1073 1074 1075 1076 1077 1078
        QColor color;
        if (QColor::isValidColor(value)) {
            color.setNamedColor(value);
            return color;
        }
    }
1079
    throw StereotypeDefinitionParserError(QStringLiteral("Expected color name."), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
1080 1081
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
StereotypeDefinitionParser::Value StereotypeDefinitionParser::parseExpression()
{
    Token token = d->m_scanner->read();
    if (token.type() == Token::TokenString) {
        return Value(String, QVariant(token.text()));
    } else if (token.type() == Token::TokenOperator && token.subtype() == OPERATOR_MINUS) {
        Value v = parseExpression();
        if (v.type() == Int)
            return Value(Int, QVariant(-v.value().toInt()));
        else if (v.type() == Float)
            return Value(Float, QVariant(-v.value().toDouble()));
        else
            throw StereotypeDefinitionParserError(QStringLiteral("Illegal number expression."), token.sourcePos());
    } else if (token.type() == Token::TokenInteger) {
        bool ok = false;
        int value = token.text().toInt(&ok);
        QMT_CHECK(ok);
        return Value(Int, QVariant(value));
    } else if (token.type() == Token::TokenFloat) {
        bool ok = false;
        qreal value = token.text().toDouble(&ok);
        QMT_CHECK(ok);
        return Value(Float, QVariant(value));
    } else if (token.type() == Token::TokenColor) {
        QString value = token.text().toLower();
        QColor color;
        if (QColor::isValidColor(value)) {
            color.setNamedColor(value);
            return Value(Color, QVariant(color));
        } else {
            throw StereotypeDefinitionParserError(QStringLiteral("Invalid color."), token.sourcePos());
        }
    } else if (token.type() == Token::TokenIdentifier || token.type() == Token::TokenKeyword) {
        QString value = token.text().toLower();
        if (value == QStringLiteral("yes") || value == QStringLiteral("true"))
            return Value(Boolean, QVariant(true));
        else if (value == QStringLiteral("no") || value == QStringLiteral("false"))
            return Value(Boolean, QVariant(false));
        else
            return Value(Identifier, QVariant(token.text()));
    }
    throw StereotypeDefinitionParserError(QStringLiteral("Syntax error in expression."), token.sourcePos());
}

1126
void StereotypeDefinitionParser::expectBlockBegin()
Jochen Becher's avatar
Jochen Becher committed
1127
{
1128 1129
    skipEOLTokens();
    expectOperator(OPERATOR_BRACE_OPEN, QStringLiteral("{"));
Jochen Becher's avatar
Jochen Becher committed
1130 1131
}

1132
bool StereotypeDefinitionParser::readProperty(Token *token)
Jochen Becher's avatar
Jochen Becher committed
1133
{
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    *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
1144 1145
}

1146
void StereotypeDefinitionParser::throwUnknownPropertyError(const Token &token)
Jochen Becher's avatar
Jochen Becher committed
1147
{
1148 1149 1150 1151 1152 1153
    throw StereotypeDefinitionParserError(QString(QStringLiteral("Unknown property '%1'.")).arg(token.text()), token.sourcePos());
}

bool StereotypeDefinitionParser::expectPropertySeparatorOrBlockEnd()
{
    bool ok = false;
1154
    Token token = d->m_scanner->read();
1155 1156 1157 1158
    if (token.type() == Token::TokenEndOfLine) {
        skipEOLTokens();
        token = d->m_scanner->read();
        ok = true;
1159
    }
1160 1161 1162 1163 1164 1165 1166 1167 1168
    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
1169 1170
}

1171
void StereotypeDefinitionParser::skipOptionalEmptyBlock()
Jochen Becher's avatar
Jochen Becher committed
1172
{
1173
    Token token = d->m_scanner->read();
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    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 {
1195
        d->m_scanner->unread(token);
Jochen Becher's avatar
Jochen Becher committed
1196
    }
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
}

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
1209 1210
}

1211
void StereotypeDefinitionParser::expectOperator(int op, const QString &opName)
Jochen Becher's avatar
Jochen Becher committed
1212
{
1213
    Token token = d->m_scanner->read();
1214
    if (!isOperator(token, op))
1215
        throw StereotypeDefinitionParserError(QString(QStringLiteral("Expected '%1'.")).arg(opName), token.sourcePos());
Jochen Becher's avatar
Jochen Becher committed
1216 1217
}

1218
void StereotypeDefinitionParser::expectColon()
Jochen Becher's avatar
Jochen Becher committed
1219
{
1220
    expectOperator(OPERATOR_COLON, QStringLiteral(":"));
Jochen Becher's avatar
Jochen Becher committed
1221 1222
}

1223
void StereotypeDefinitionParser::skipEOLTokens()
Jochen Becher's avatar
Jochen Becher committed
1224
{
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
    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
1242 1243
}

1244
} // namespace qmt