| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Eivind Kvedalen <[email protected]> * |
| 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 <string> |
| 24 |
#include <tuple> |
| 25 |
|
| 26 |
#include "ExpressionParser.h" |
| 27 |
#include "ExpressionTokenizer.h" |
| 28 |
|
| 29 |
using namespace App; |
| 30 |
|
| 31 |
|
| 32 |
// Code below inspired by blog entry: |
| 33 |
// https://john.nachtimwald.com/2009/07/04/qcompleter-and-comma-separated-tags/ |
| 34 |
|
| 35 |
QString ExpressionTokenizer::perform(const QString& prefix, int pos) |
| 36 |
{ |
| 37 |
// ExpressionParser::tokenize() only supports std::string but we need a tuple QString |
| 38 |
// because due to UTF-8 encoding a std::string may be longer than a QString |
| 39 |
// See https://forum.freecad.org/viewtopic.php?f=3&t=69931 |
| 40 |
auto tokenizeExpression = [](const QString& expr) { |
| 41 |
std::vector<std::tuple<int, int, std::string>> result = |
| 42 |
ExpressionParser::tokenize(expr.toStdString()); |
| 43 |
std::vector<std::tuple<int, int, QString>> tokens; |
| 44 |
std::transform( |
| 45 |
result.cbegin(), |
| 46 |
result.cend(), |
| 47 |
std::back_inserter(tokens), |
| 48 |
[&](const std::tuple<int, int, std::string>& item) { |
| 49 |
return std::make_tuple( |
| 50 |
std::get<0>(item), |
| 51 |
QString::fromStdString(expr.toStdString().substr(0, std::get<1>(item))).size(), |
| 52 |
QString::fromStdString(std::get<2>(item))); |
| 53 |
}); |
| 54 |
return tokens; |
| 55 |
}; |
| 56 |
|
| 57 |
QString completionPrefix; |
| 58 |
|
| 59 |
// Compute start; if prefix starts with =, start parsing from offset 1. |
| 60 |
int start = (prefix.size() > 0 && prefix.at(0) == QChar::fromLatin1('=')) ? 1 : 0; |
| 61 |
|
| 62 |
// Tokenize prefix |
| 63 |
std::vector<std::tuple<int, int, QString>> tokens = tokenizeExpression(prefix.mid(start)); |
| 64 |
|
| 65 |
// No tokens |
| 66 |
if (tokens.empty()) { |
| 67 |
return {}; |
| 68 |
} |
| 69 |
|
| 70 |
prefixEnd = prefix.size(); |
| 71 |
|
| 72 |
// Pop those trailing tokens depending on the given position, which may be |
| 73 |
// in the middle of a token, and we shall include that token. |
| 74 |
for (auto it = tokens.begin(); it != tokens.end(); ++it) { |
| 75 |
int tokenType = std::get<0>(*it); |
| 76 |
int location = std::get<1>(*it); |
| 77 |
int tokenLength = static_cast<int> (std::get<2>(*it).size()); |
| 78 |
if (location >= pos) { |
| 79 |
// Include the immediately followed '.' or '#', because we'll be |
| 80 |
// inserting these separators too, in ExpressionCompleteModel::pathFromIndex() |
| 81 |
if (it != tokens.begin() && tokenType != '.' && tokenType != '#') { |
| 82 |
--it; |
| 83 |
location = std::get<1>(*it); |
| 84 |
tokenLength = static_cast<int>(std::get<2>(*it).size()); |
| 85 |
} |
| 86 |
tokens.resize(it - tokens.begin() + 1); |
| 87 |
prefixEnd = start + location + tokenLength; |
| 88 |
break; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
int trim = 0; |
| 93 |
if (prefixEnd > pos) { |
| 94 |
trim = prefixEnd - pos; |
| 95 |
} |
| 96 |
|
| 97 |
// Extract last tokens that can be rebuilt to a variable |
| 98 |
long i = static_cast<long>(tokens.size()) - 1; |
| 99 |
|
| 100 |
// First, check if we have unclosing string starting from the end |
| 101 |
bool stringing = false; |
| 102 |
for (; i >= 0; --i) { |
| 103 |
int token = std::get<0>(tokens[i]); |
| 104 |
if (token == ExpressionParser::STRING) { |
| 105 |
stringing = false; |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
if (token == ExpressionParser::LT && i > 0 |
| 110 |
&& std::get<0>(tokens[i - 1]) == ExpressionParser::LT) { |
| 111 |
--i; |
| 112 |
stringing = true; |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Not an unclosed string and the last character is a space |
| 118 |
if (!stringing && !prefix.isEmpty() && prefixEnd > 0 && prefixEnd <= prefix.size() |
| 119 |
&& prefix[prefixEnd - 1] == QChar(32)) { |
| 120 |
return {}; |
| 121 |
} |
| 122 |
|
| 123 |
if (!stringing) { |
| 124 |
i = static_cast<long>(tokens.size()) - 1; |
| 125 |
for (; i >= 0; --i) { |
| 126 |
int token = std::get<0>(tokens[i]); |
| 127 |
if (token != '.' && token != '#' && token != ExpressionParser::IDENTIFIER |
| 128 |
&& token != ExpressionParser::INTEGER && token != ExpressionParser::STRING |
| 129 |
&& token != ExpressionParser::UNIT && token != ExpressionParser::ONE) { |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
++i; |
| 134 |
} |
| 135 |
|
| 136 |
// Set prefix start for use when replacing later |
| 137 |
if (i == static_cast<long>(tokens.size())) { |
| 138 |
prefixStart = prefixEnd; |
| 139 |
} |
| 140 |
else { |
| 141 |
prefixStart = start + std::get<1>(tokens[i]); |
| 142 |
} |
| 143 |
|
| 144 |
// Build prefix from tokens |
| 145 |
while (i < static_cast<long>(tokens.size())) { |
| 146 |
completionPrefix += std::get<2>(tokens[i]); |
| 147 |
++i; |
| 148 |
} |
| 149 |
|
| 150 |
if (trim && trim < int(completionPrefix.size())) { |
| 151 |
completionPrefix.resize(completionPrefix.size() - trim); |
| 152 |
} |
| 153 |
|
| 154 |
return completionPrefix; |
| 155 |
} |