1
/***************************************************************************
2
* Copyright (c) 2015 Eivind Kvedalen <eivind@kvedalen.name> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
29
#include "ExpressionParser.h"
30
#include "ExpressionTokenizer.h"
35
// Code below inspired by blog entry:
36
// https://john.nachtimwald.com/2009/07/04/qcompleter-and-comma-separated-tags/
38
QString ExpressionTokenizer::perform(const QString& prefix, int pos)
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(),
49
std::back_inserter(tokens),
50
[&](const std::tuple<int, int, std::string>& item) {
51
return std::make_tuple(
53
QString::fromStdString(expr.toStdString().substr(0, std::get<1>(item))).size(),
54
QString::fromStdString(std::get<2>(item))
60
QString completionPrefix;
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;
66
std::vector<std::tuple<int, int, QString> > tokens = tokenizeExpression(prefix.mid(start));
73
prefixEnd = prefix.size();
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) != '#')
83
tokens.resize(it - tokens.begin() + 1);
84
prefixEnd = start + std::get<1>(*it) + (int)std::get<2>(*it).size();
91
trim = prefixEnd - pos;
93
// Extract last tokens that can be rebuilt to a variable
94
long i = static_cast<long>(tokens.size()) - 1;
96
// First, check if we have unclosing string starting from the end
97
bool stringing = false;
99
int token = std::get<0>(tokens[i]);
100
if (token == ExpressionParser::STRING) {
105
if (token == ExpressionParser::LT && i > 0
106
&& std::get<0>(tokens[i - 1]) == ExpressionParser::LT) {
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)) {
121
i = static_cast<long>(tokens.size()) - 1;
122
for (; i >= 0; --i) {
123
int token = std::get<0>(tokens[i]);
126
token != ExpressionParser::IDENTIFIER &&
127
token != ExpressionParser::STRING &&
128
token != ExpressionParser::UNIT)
134
// Set prefix start for use when replacing later
135
if (i == static_cast<long>(tokens.size()))
136
prefixStart = prefixEnd;
138
prefixStart = start + std::get<1>(tokens[i]);
140
// Build prefix from tokens
141
while (i < static_cast<long>(tokens.size())) {
142
completionPrefix += std::get<2>(tokens[i]);
146
if (trim && trim < int(completionPrefix.size()))
147
completionPrefix.resize(completionPrefix.size() - trim);
149
return completionPrefix;