1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Dmitry Savchenko
4 ** Copyright (C) 2016 Vasiliy Sorokin
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of Qt Creator.
8 **
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ****************************************************************************/
26 
27 #pragma once
28 
29 #include "keyword.h"
30 #include "todoitem.h"
31 
32 namespace Todo {
33 namespace Internal {
34 
35 class LineParser
36 {
37 public:
38     explicit LineParser(const KeywordList &keywordList);
39 
40     void setKeywordList(const KeywordList &keywordList);
41     QList<TodoItem> parse(const QString &line);
42 
43     // This can also be used from KeywordDialog to avoid code duplication
44     static bool isKeywordSeparator(const QChar &ch);
45 
46 private:
47 
48     // map key here is keyword start position in the text line
49     // and map value is keyword index in m_keywords
50     using KeywordEntryCandidates = QMap<int, int> ;
51 
52     struct KeywordEntry {
53         int keywordIndex;
54         int keywordStart;
55         QString text;
56     };
57 
58     KeywordEntryCandidates findKeywordEntryCandidates(const QString &line);
59     bool isKeywordAt(int index, const QString &line, const QString &keyword);
60     bool isFirstCharOfTheWord(int index, const QString &line);
61     bool isLastCharOfTheWord(int index, const QString &line);
62     QList<KeywordEntry> keywordEntriesFromCandidates(const QMap<int, int> &candidates, const QString &line);
63     QString trimSeparators(const QString &string);
64     bool startsWithSeparator(const QString &string);
65     bool endsWithSeparator(const QString &string);
66     QList<TodoItem> todoItemsFromKeywordEntries(const QList<KeywordEntry> &entries);
67 
68     KeywordList m_keywords;
69 };
70 
71 } // namespace Internal
72 } // namespace Todo
73