File: parser.h

package info (click to toggle)
umbrello 4%3A25.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,212 kB
  • sloc: cpp: 144,235; php: 2,405; sh: 855; xml: 354; cs: 309; java: 91; python: 68; makefile: 11; sql: 7
file content (518 lines) | stat: -rw-r--r-- 16,644 bytes parent folder | download | duplicates (2)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
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
/* This file is part of KDevelop
    SPDX-FileCopyrightText: 2002, 2003 Roberto Raggi <roberto@kdevelop.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#ifndef PARSER_H
#define PARSER_H

#include "ast.h"

// qt includes
#include <QObject>
#include <QString>
#include <QStringList>
#include <QList>
#include <set>
#include <vector>
#include <iostream>

struct ParserPrivateData;

class Driver;
class Lexer;
class Token;
struct Error;


class CommentFormatter
{
    static inline bool isWhite(QChar c)
    {
        return c.isSpace();
    }

    static void rStrip(QString str, QString& from)
    {
        if (str.isEmpty()) return;

        int i = 0;
        int ip = from.length();
        int s = from.length();

        for (int a = s-1; a >= 0; a--) {
            if (isWhite(from[a])) {
                continue;
            } else {
                if (from[a] == str[i]) {
                    i++;
                    ip = a;
                    if (i == (int)str.length()) break;
                } else {
                    break;
                }
            }
        }

        if (ip != (int)from.length()) from = from.left(ip);
    }

    static void strip(QString str, QString& from)
    {
        if (str.isEmpty()) return;

        int i = 0;
        int ip = 0;
        int s = from.length();

        for (int a = 0; a < s; a++) {
            if (isWhite(from[a])) {
                continue;
            } else {
                if (from[a] == str[i]) {
                    i++;
                    ip = a+1;
                    if (i == (int)str.length()) break;
                } else {
                    break;
                }
            }
        }

        if (ip) from = from.mid(ip);
    }

public:

    static QString formatComment(QString comment)
    {
        QString ret;
        int i = 0;
        int s = comment.length();
        while (i < s && comment[i] == QLatin1Char('/')) {
            i++;
        }

        if (i > 1) {
            ret = comment.mid(i);
        } else {
            ///remove the star in each line
            QStringList lines = comment.split(QLatin1String("\n"));

            if (lines.isEmpty()) return ret;

            strip(QLatin1String("/**"), lines.front());
            rStrip(QLatin1String("/**"), lines.back());

            QStringList::iterator it = lines.begin();
            ++it;
            QStringList::iterator eit = lines.end();

            if (it != lines.end()) {
                --eit;

                for (; it != eit; ++it) {
                    strip(QLatin1String("*"), *it);
                }

                if (lines.front().trimmed().isEmpty())
                    lines.pop_front();

                if (lines.back().trimmed().isEmpty())
                    lines.pop_back();
            }

            ret = lines.join(QLatin1String("\n"));
        }

        return ret;
    }
};

class Comment
{
    QString m_text;
    int m_line;
    bool m_formatted;


    void format()
    {
        if (m_formatted) return;
        m_formatted = true;
        m_text = CommentFormatter::formatComment(m_text);
    }

public:
    Comment(QString text = QString(), int line = -1) : m_text(text), m_line(line), m_formatted(false)
    {
    }

    Comment(int line) : m_line(line), m_formatted(false)
    {
    }

    void operator += (Comment rhs)
    {
        format();
        rhs.format();
        m_text += QLatin1String("\n") + rhs.m_text;
    }

    operator bool() const
    {
        return !m_text.isEmpty();
    }

    operator QString()
    {
        format();
        return m_text;
    }

    inline int line() const
    {
        return m_line;
    }

    bool operator < (Comment& rhs) const
    {
        return m_line < rhs.m_line;
    }

    bool isSame (const Comment& rhs)
    {
        if (rhs.m_formatted) format();
        return m_text == rhs.m_text;
    }

    struct cmp {
        bool operator () (const Comment& c1, const Comment& c2) const
        {
            return c1.line() < c2.line();
        }
    };
};


class CommentStore
{
private:
    typedef std::set< Comment, Comment::cmp > CommentSet;
    CommentSet m_comments;

public:

    ///Returns the comment nearest to "end"(inclusive), and returns & removes it
    Comment getCommentInRange(int end, int start = 0)
    {
        CommentSet::iterator it = m_comments.lower_bound(end);


        while (it != m_comments.begin() && (*it).line() > end) {
            --it;
        }

        if (it != m_comments.end() && (*it).line() >= start && (*it).line() <= end) {
            Comment ret = *it;
            m_comments.erase(it);
            return ret;
        } else {
            return Comment();
        }
    }

    /**
     * Returns the comments between "end" (inclusive) and "start" and removes it
     * @param end last line to return
     * @param classInit protects against independent comments which are further away of the class
     * @param start start line to return
     * @return instance of class Comment with combined comment
     */
    Comment getCommentsInRange(int end, bool classInit = false, int start = 0)
    {
        std::vector<Comment> tempStorage; // The vector is required in order to output the comments in the correct order since the iterator goes reverse
        Comment ret;

        if (m_comments.empty())
            return ret;

        // The following lines will look ahead if there is a independent comment.
        CommentSet::iterator it = m_comments.end();
        --it; // .end() points to an element behind the container, so it's safe to move it to the actual last element
        int tempLine = (*it).line();
        if(tempLine < (end-2) && classInit) { // protects against independent comments which are further away of the class
            m_comments.clear();
            return ret;
        }
        tempStorage.push_back(*it);

        while (it != m_comments.begin() && (*it).line() >= start && (*it).line() <= end) { // goes reverse through the comments because the required comments are at the end of m_comments
            --it;
            if(tempLine == ((*it).line() + 1)) {
                tempLine--;
                tempStorage.push_back(*it);
            } else { // as soons as first independent comment is found the rest isn't interesting anymore (protects against multiline independent comments)
               m_comments.clear();
               break;
            }
        }

        for (auto it2 = tempStorage.rbegin(); it2 != tempStorage.rend(); ++it2) { // outputs the comments in the correct order
            if(it2 == tempStorage.rbegin()) {
                ret = (*it2);
            }
            else {
                ret += (*it2);
            }
        }
        return ret;
    }

    ///Returns and removes the comment in the line
    Comment getComment(int line)
    {
        CommentSet::iterator it = m_comments.find(line);
        if (it != m_comments.end()) {
            Comment ret = *it;
            m_comments.erase(it);
            return ret;
        } else {
            return Comment();
        }
    }

    void addComment(Comment comment)
    {

        CommentSet::iterator it = m_comments.find(comment);
        if (it != m_comments.end()) {
            if (comment.isSame(*it)) return;
            Comment c = *it;
            c += comment;
            comment = c;
            m_comments.erase(it);
        }

        m_comments.insert(comment);
    }

    ///Does not delete the comment
    Comment latestComment()
    {
        CommentSet::iterator it = m_comments.end();
        if (it == m_comments.begin()) return Comment();
        --it;
        return *it;
    }

    void clear()
    {
        m_comments.clear();
    }
};


#define PARSER_DEBUG_METHOD DEBUG() << __func__ << " token=" << m_lexer->lookAhead(0).text() << "\n"

class Parser : public QObject
{
    Q_OBJECT
public:
    Parser(Driver* driver, Lexer* lexer);
    virtual ~Parser();

private:
    virtual bool reportError(const Error& err);
    /** @todo remove*/ virtual bool reportError(const QString& msg);
    /** @todo remove*/ virtual void syntaxError();

public /*rules*/ :

    bool parseTranslationUnit(TranslationUnitAST::Node& node);

    bool parseDeclaration(DeclarationAST::Node& node);
    bool parseBlockDeclaration(DeclarationAST::Node& node);
    bool parseLinkageSpecification(DeclarationAST::Node& node);
    bool parseLinkageBody(LinkageBodyAST::Node& node);
    bool parseNamespace(DeclarationAST::Node& node);
    bool parseNamespaceAliasDefinition(DeclarationAST::Node& node);
    bool parseUsing(DeclarationAST::Node& node);
    bool parseUsingDirective(DeclarationAST::Node& node);
    bool parseTypedef(DeclarationAST::Node& node);
    bool parseAsmDefinition(DeclarationAST::Node& node);
    bool parseTemplateDeclaration(DeclarationAST::Node& node);
    bool parseDeclarationInternal(DeclarationAST::Node& node);

    bool parseUnqualifiedName(ClassOrNamespaceNameAST::Node& node);
    bool parseStringLiteral(AST::Node& node);
    bool parseName(NameAST::Node& node);
    bool parseOperatorFunctionId(AST::Node& node);
    bool parseTemplateArgumentList(TemplateArgumentListAST::Node& node, bool reportError=true);
    bool parseOperator(AST::Node& node);
    bool parseCvQualify(GroupAST::Node& node);
    bool parseSimpleTypeSpecifier(TypeSpecifierAST::Node& node);
    bool parsePtrOperator(AST::Node& node);
    bool parseTemplateArgument(AST::Node& node);
    bool parseTypeSpecifier(TypeSpecifierAST::Node& node);
    bool parseTypeSpecifierOrClassSpec(TypeSpecifierAST::Node& node);
    bool parseDeclarator(DeclaratorAST::Node& node);
    bool parseTemplateParameterList(TemplateParameterListAST::Node& node);
    bool parseTemplateParameter(TemplateParameterAST::Node& node);
    bool parseStorageClassSpecifier(GroupAST::Node& node);
    bool parseFunctionSpecifier(GroupAST::Node& node);
    bool parseInitDeclaratorList(InitDeclaratorListAST::Node& node);
    bool parseInitDeclarator(InitDeclaratorAST::Node& node);
    bool parseParameterDeclarationClause(ParameterDeclarationClauseAST::Node& node);
    bool parseCtorInitializer(AST::Node& node);
    bool parsePtrToMember(AST::Node& node);
    bool parseEnumSpecifier(TypeSpecifierAST::Node& node);
    bool parseClassSpecifier(TypeSpecifierAST::Node& node);
    bool parseWinDeclSpec(GroupAST::Node& node);
    bool parseElaboratedTypeSpecifier(TypeSpecifierAST::Node& node);
    bool parseDeclaratorId(NameAST::Node& node);
    bool parseExceptionSpecification(GroupAST::Node& node);
    bool parseEnumerator(EnumeratorAST::Node& node);
    bool parseTypeParameter(TypeParameterAST::Node& node);
    bool parseParameterDeclaration(ParameterDeclarationAST::Node& node);
    bool parseTypeId(AST::Node& node);
    bool parseAbstractDeclarator(DeclaratorAST::Node& node);
    bool parseParameterDeclarationList(ParameterDeclarationListAST::Node& node);
    bool parseMemberSpecification(DeclarationAST::Node& node);
    bool parseAccessSpecifier(AST::Node& node);
    bool parseTypeIdList(GroupAST::Node& node);
    bool parseMemInitializerList(AST::Node& node);
    bool parseMemInitializer(AST::Node& node);
    bool parseInitializer(AST::Node& node);
    bool parseBaseClause(BaseClauseAST::Node& node);
    bool parseBaseSpecifier(BaseSpecifierAST::Node& node);
    bool parseInitializerClause(AST::Node& node);
    bool parseMemInitializerId(NameAST::Node& node);
    bool parseFunctionBody(StatementListAST::Node& node);

    // expression
    bool skipExpression(AST::Node& node);
    bool skipCommaExpression(AST::Node& node);
    bool skipExpressionStatement(StatementAST::Node& node);

    bool parseExpression(AST::Node& node);
    bool parsePrimaryExpression(AST::Node& node);
    bool parsePostfixExpression(AST::Node& node);
    bool parseUnaryExpression(AST::Node& node);
    bool parseNewExpression(AST::Node& node);
    bool parseNewTypeId(AST::Node& node);
    bool parseNewDeclarator(AST::Node& node);
    bool parseNewInitializer(AST::Node& node);
    bool parseDeleteExpression(AST::Node& node);
    bool parseCastExpression(AST::Node& node);
    bool parsePmExpression(AST::Node& node);
    bool parseMultiplicativeExpression(AST::Node& node);
    bool parseAdditiveExpression(AST::Node& node);
    bool parseShiftExpression(AST::Node& node);
    bool parseRelationalExpression(AST::Node& node, bool templArgs=false);
    bool parseEqualityExpression(AST::Node& node, bool templArgs=false);
    bool parseAndExpression(AST::Node& node, bool templArgs=false);
    bool parseExclusiveOrExpression(AST::Node& node, bool templArgs=false);
    bool parseInclusiveOrExpression(AST::Node& node, bool templArgs=false);
    bool parseLogicalAndExpression(AST::Node& node, bool templArgs=false);
    bool parseLogicalOrExpression(AST::Node& node, bool templArgs=false);
    bool parseConditionalExpression(AST::Node& node);
    bool parseAssignmentExpression(AST::Node& node);
    bool parseConstantExpression(AST::Node& node);
    bool parseCommaExpression(AST::Node& node);
    bool parseThrowExpression(AST::Node& node);

    // statement
    bool parseCondition(ConditionAST::Node& node);
    bool parseStatement(StatementAST::Node& node);
    bool parseWhileStatement(StatementAST::Node& node);
    bool parseDoStatement(StatementAST::Node& node);
    bool parseForStatement(StatementAST::Node& node);
    bool parseForEachStatement(StatementAST::Node& node); // qt4 [erbsland]
    bool parseCompoundStatement(StatementAST::Node& node);
    bool parseForInitStatement(StatementAST::Node& node);
    bool parseIfStatement(StatementAST::Node& node);
    bool parseSwitchStatement(StatementAST::Node& node);
    bool parseLabeledStatement(StatementAST::Node& node);
    bool parseDeclarationStatement(StatementAST::Node& node);
    bool parseTryBlockStatement(StatementAST::Node& node);

    // objective c
    bool parseObjcDef(DeclarationAST::Node& node);
    bool parseObjcClassDef(DeclarationAST::Node& node);
    bool parseObjcClassDecl(DeclarationAST::Node& node);
    bool parseObjcProtocolDecl(DeclarationAST::Node& node);
    bool parseObjcAliasDecl(DeclarationAST::Node& node);
    bool parseObjcProtocolDef(DeclarationAST::Node& node);
    bool parseObjcMethodDef(DeclarationAST::Node& node);

    bool parseIvarDeclList(AST::Node& node);
    bool parseIvarDecls(AST::Node& node);
    bool parseIvarDecl(AST::Node& node);
    bool parseIvars(AST::Node& node);
    bool parseIvarDeclarator(AST::Node& node);
    bool parseMethodDecl(AST::Node& node);
    bool parseUnarySelector(AST::Node& node);
    bool parseKeywordSelector(AST::Node& node);
    bool parseSelector(AST::Node& node);
    bool parseKeywordDecl(AST::Node& node);
    bool parseReceiver(AST::Node& node);
    bool parseObjcMessageExpr(AST::Node& node);
    bool parseMessageArgs(AST::Node& node);
    bool parseKeywordExpr(AST::Node& node);
    bool parseKeywordArgList(AST::Node& node);
    bool parseKeywordArg(AST::Node& node);
    bool parseReservedWord(AST::Node& node);
    bool parseMyParms(AST::Node& node);
    bool parseMyParm(AST::Node& node);
    bool parseOptParmList(AST::Node& node);
    bool parseObjcSelectorExpr(AST::Node& node);
    bool parseSelectorArg(AST::Node& node);
    bool parseKeywordNameList(AST::Node& node);
    bool parseKeywordName(AST::Node& node);
    bool parseObjcEncodeExpr(AST::Node& node);
    bool parseObjcString(AST::Node& node);
    bool parseProtocolRefs(AST::Node& node);
    bool parseIdentifierList(GroupAST::Node& node);
    bool parseIdentifierColon(AST::Node& node);
    bool parseObjcProtocolExpr(AST::Node& node);
    bool parseObjcOpenBracketExpr(AST::Node& node);
    bool parseObjcCloseBracket(AST::Node& node);

    void nextToken(bool skipComments = true);

    ///parses all comments until the end of the line
    Comment comment();
    void preparseLineComments(int line);
    void processComment(int offset = 0);
    void clearComment();

    bool skipUntil(int token);
    bool skipUntilDeclaration();
    bool skipUntilStatement();
    bool skip(int l, int r);
    QString toString(int start, int end, const QString& sep=QLatin1String(" ")) const;

private:
    int currentLine();
    CommentStore m_commentStore;

    template<class Type>
    void eventuallyTakeComment(int startLn, int line, Type& ast);
    template<class Type>
    void eventuallyTakeComment(Type& ast);

    ParserPrivateData* d;
    Driver *m_driver;
    Lexer *m_lexer;
    Comment m_currentComment;
    int m_problems;
    int m_maxProblems;
    bool objcp;
    Parser(const Parser& source);
    void operator = (const Parser& source);
};


#endif