FreeCAD

Форк
0
/
ExpressionTokenizer.cpp 
150 строк · 5.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2015 Eivind Kvedalen <eivind@kvedalen.name>             *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
#include <string>
26
#include <tuple>
27
#endif
28

29
#include "ExpressionParser.h"
30
#include "ExpressionTokenizer.h"
31

32
using namespace App;
33

34

35
// Code below inspired by blog entry:
36
// https://john.nachtimwald.com/2009/07/04/qcompleter-and-comma-separated-tags/
37

38
QString ExpressionTokenizer::perform(const QString& prefix, int pos)
39
{
40
    // ExpressionParser::tokenize() only supports std::string but we need a tuple QString
41
    // because due to UTF-8 encoding a std::string may be longer than a QString
42
    // See https://forum.freecad.org/viewtopic.php?f=3&t=69931
43
    auto tokenizeExpression = [](const QString& expr) {
44
        std::vector<std::tuple<int, int, std::string>> result =
45
            ExpressionParser::tokenize(expr.toStdString());
46
        std::vector<std::tuple<int, int, QString>> tokens;
47
        std::transform(result.cbegin(),
48
                       result.cend(),
49
                       std::back_inserter(tokens),
50
                       [&](const std::tuple<int, int, std::string>& item) {
51
                           return std::make_tuple(
52
                               std::get<0>(item),
53
                               QString::fromStdString(expr.toStdString().substr(0, std::get<1>(item))).size(),
54
                               QString::fromStdString(std::get<2>(item))
55
                           );
56
                       });
57
        return tokens;
58
    };
59

60
    QString completionPrefix;
61

62
    // Compute start; if prefix starts with =, start parsing from offset 1.
63
    int start = (prefix.size() > 0 && prefix.at(0) == QChar::fromLatin1('=')) ? 1 : 0;
64

65
    // Tokenize prefix
66
    std::vector<std::tuple<int, int, QString> > tokens = tokenizeExpression(prefix.mid(start));
67

68
    // No tokens
69
    if (tokens.empty()) {
70
        return {};
71
    }
72

73
    prefixEnd = prefix.size();
74

75
    // Pop those trailing tokens depending on the given position, which may be
76
    // in the middle of a token, and we shall include that token.
77
    for (auto it = tokens.begin(); it != tokens.end(); ++it) {
78
        if (std::get<1>(*it) >= pos) {
79
            // Include the immediately followed '.' or '#', because we'll be
80
            // inserting these separators too, in ExpressionCompleteModel::pathFromIndex()
81
            if (it != tokens.begin() && std::get<0>(*it) != '.' && std::get<0>(*it) != '#')
82
                it = it - 1;
83
            tokens.resize(it - tokens.begin() + 1);
84
            prefixEnd = start + std::get<1>(*it) + (int)std::get<2>(*it).size();
85
            break;
86
        }
87
    }
88

89
    int trim = 0;
90
    if (prefixEnd > pos)
91
        trim = prefixEnd - pos;
92

93
    // Extract last tokens that can be rebuilt to a variable
94
    long i = static_cast<long>(tokens.size()) - 1;
95

96
    // First, check if we have unclosing string starting from the end
97
    bool stringing = false;
98
    for (; i >= 0; --i) {
99
        int token = std::get<0>(tokens[i]);
100
        if (token == ExpressionParser::STRING) {
101
            stringing = false;
102
            break;
103
        }
104

105
        if (token == ExpressionParser::LT && i > 0
106
            && std::get<0>(tokens[i - 1]) == ExpressionParser::LT) {
107
            --i;
108
            stringing = true;
109
            break;
110
        }
111
    }
112

113
    // Not an unclosed string and the last character is a space
114
    if (!stringing && !prefix.isEmpty() &&
115
            prefixEnd > 0 && prefixEnd <= prefix.size() &&
116
            prefix[prefixEnd-1] == QChar(32)) {
117
        return {};
118
    }
119

120
    if (!stringing) {
121
        i = static_cast<long>(tokens.size()) - 1;
122
        for (; i >= 0; --i) {
123
            int token = std::get<0>(tokens[i]);
124
            if (token != '.' &&
125
                token != '#' &&
126
                token != ExpressionParser::IDENTIFIER &&
127
                token != ExpressionParser::STRING &&
128
                token != ExpressionParser::UNIT)
129
                break;
130
        }
131
        ++i;
132
    }
133

134
    // Set prefix start for use when replacing later
135
    if (i == static_cast<long>(tokens.size()))
136
        prefixStart = prefixEnd;
137
    else
138
        prefixStart = start + std::get<1>(tokens[i]);
139

140
    // Build prefix from tokens
141
    while (i < static_cast<long>(tokens.size())) {
142
        completionPrefix += std::get<2>(tokens[i]);
143
        ++i;
144
    }
145

146
    if (trim && trim < int(completionPrefix.size()))
147
        completionPrefix.resize(completionPrefix.size() - trim);
148

149
    return completionPrefix;
150
}
151

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.