NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
syntax.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2017 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19
20#include "syntax.hpp"
21#include "core/utils/tools.hpp"
22
23
28{
29 vNSCRCommands.push_back("NO_SYNTAX_ELEMENTS");
30 vNPRCCommands.push_back("NO_SYNTAX_ELEMENTS");
31 vFunctions.push_back("NO_SYNTAX_ELEMENTS");
32 vMethods.push_back("NO_SYNTAX_ELEMENTS");
33 vMethodsArgs.push_back("NO_SYNTAX_ELEMENTS");
34 vOptions.push_back("NO_SYNTAX_ELEMENTS");
35 vConstants.push_back("NO_SYNTAX_ELEMENTS");
36 vSpecialValues.push_back("NO_SYNTAX_ELEMENTS");
37 vOperators.push_back("NO_SYNTAX_ELEMENTS");
38 vDocKeyWords.push_back("NO_SYNTAX_ELEMENTS");
39 vMatlabKeyWords.push_back("NO_SYNTAX_ELEMENTS");
40 vMatlabFunctions.push_back("NO_SYNTAX_ELEMENTS");
41 vCppKeyWords.push_back("NO_SYNTAX_ELEMENTS");
42 vCppFunctions.push_back("NO_SYNTAX_ELEMENTS");
43 vTeXKeyWords.push_back("NO_SYNTAX_ELEMENTS");
44
45 // The operator characters are always the same
46 sSingleOperators = "+-*/?=()[]{},;#^!&|<>%:";
47}
48
49
58NumeReSyntax::NumeReSyntax(const std::string& _sPath) : NumeReSyntax()
59{
60 loadSyntax(_sPath);
61}
62
63
75NumeReSyntax::NumeReSyntax(const std::string& _sPath, const std::vector<std::string>& vPlugins) : NumeReSyntax(_sPath)
76{
77 vNSCRCommands.insert(vNSCRCommands.end(), vPlugins.begin(), vPlugins.end());
78}
79
80
89void NumeReSyntax::loadSyntax(const std::string& _sPath)
90{
91 if (_sPath.length() && !sPath.length())
92 sPath = _sPath;
93
94 std::ifstream file_in;
95 std::string sLine;
96
97 // Open the file and check, whether the file stream is valid
98 file_in.open(sPath + "lang\\syntaxelements.nlng");
99
100 if (!file_in.good())
101 return;
102
103 // Read the overall file
104 while (!file_in.eof())
105 {
106 // Get the current line
107 std::getline(file_in, sLine);
108
109 // Ignore it, if it's empty or if it starts with a comment sign
110 if (!sLine.length())
111 continue;
112
113 if (sLine.front() == '#')
114 continue;
115
116 // Depending on the identifier at the beginning of the line, the contents
117 // are used for different syntax elements
118 // The member function "splitString" splits the line into a vector
119 if (sLine.substr(0, 13) == "NSCR_COMMANDS")
120 vNSCRCommands = splitString(sLine.substr(sLine.find('=')+1));
121 else if (sLine.substr(0, 13) == "NPRC_COMMANDS")
122 vNPRCCommands = splitString(sLine.substr(sLine.find('=')+1));
123 else if (sLine.substr(0, 9) == "FUNCTIONS")
124 vFunctions = splitString(sLine.substr(sLine.find('=')+1));
125 else if (sLine.substr(0, 11) == "METHODSARGS")
126 vMethodsArgs = splitString(sLine.substr(sLine.find('=')+1));
127 else if (sLine.substr(0, 7) == "METHODS")
128 vMethods = splitString(sLine.substr(sLine.find('=')+1));
129 else if (sLine.substr(0, 7) == "OPTIONS")
130 vOptions = splitString(sLine.substr(sLine.find('=')+1));
131 else if (sLine.substr(0, 9) == "CONSTANTS")
132 vConstants = splitString(sLine.substr(sLine.find('=')+1));
133 else if (sLine.substr(0, 11) == "SPECIALVALS")
134 vSpecialValues = splitString(sLine.substr(sLine.find('=')+1));
135 else if (sLine.substr(0, 9) == "OPERATORS")
136 vOperators = splitString(sLine.substr(sLine.find('=')+1));
137 else if (sLine.substr(0, 11) == "DOCKEYWORDS")
138 vDocKeyWords = splitString(sLine.substr(sLine.find('=')+1));
139 else if (sLine.substr(0, 9) == "BLOCKDEFS")
140 vBlockDefs = splitDefs(sLine.substr(sLine.find('=')+1));
141 else if (sLine.substr(0, 14) == "MATLABKEYWORDS")
142 vMatlabKeyWords = splitString(sLine.substr(sLine.find('=')+1));
143 else if (sLine.substr(0, 15) == "MATLABFUNCTIONS")
144 vMatlabFunctions = splitString(sLine.substr(sLine.find('=')+1));
145 else if (sLine.substr(0, 11) == "CPPKEYWORDS")
146 vCppKeyWords = splitString(sLine.substr(sLine.find('=')+1));
147 else if (sLine.substr(0, 12) == "CPPFUNCTIONS")
148 vCppFunctions = splitString(sLine.substr(sLine.find('=')+1));
149 else if (sLine.substr(0, 11) == "TEXKEYWORDS")
150 vTeXKeyWords = splitString(sLine.substr(sLine.find('=')+1));
151 }
152}
153
154
165void NumeReSyntax::addPlugins(const std::vector<std::string>& vPlugins)
166{
167 mAutoCompList.clear();
168
169 loadSyntax();
170 vNSCRCommands.insert(vNSCRCommands.end(), vPlugins.begin(), vPlugins.end());
171}
172
173
182void NumeReSyntax::setProcedureTree(const std::vector<std::string>& vTree)
183{
184 // Copy the tree
185 vProcedureTree = vTree;
186
187 // Replace the path separators with namespace characters
188 // Also remove the file extensions
189 for (size_t i = 0; i < vProcedureTree.size(); i++)
190 {
191 while (vProcedureTree[i].find('/') != std::string::npos)
192 vProcedureTree[i][vProcedureTree[i].find('/')] = '~';
193
194 while (vProcedureTree[i].find('\\') != std::string::npos)
195 vProcedureTree[i][vProcedureTree[i].find('\\')] = '~';
196
197 if (vProcedureTree[i].find(".nprc") != std::string::npos)
198 vProcedureTree[i].erase(vProcedureTree[i].rfind(".nprc"));
199 }
200}
201
202
210std::string NumeReSyntax::getBlockDefs() const
211{
212 std::string sReturn;
213 std::string endWords;
214
215 for (const SyntaxBlockDefinition& def : vBlockDefs)
216 {
217 sReturn += def.startWord + " ";
218 endWords += def.endWord + " ";
219 }
220
221 return sReturn + ";" + endWords;
222}
223
224
234std::string NumeReSyntax::constructString(const std::vector<std::string>& vVector) const
235{
236 std::string sReturn = "";
237
238 // Concatenate all elements in vVector
239 for (size_t i = 0; i < vVector.size(); i++)
240 {
241 sReturn += vVector[i] + " ";
242 }
243
244 return sReturn;
245}
246
247
256std::vector<std::string> NumeReSyntax::splitString(std::string sString)
257{
258 std::vector<std::string> vReturn;
259
260 // As long as the string has a length
261 while (sString.length())
262 {
263 // Remove leading whitespaces
264 while (sString.front() == ' ')
265 sString.erase(0,1);
266
267 // Separate at the whitespaces
268 if (sString.find(' ') != std::string::npos)
269 {
270 // Push the current token
271 vReturn.push_back(sString.substr(0,sString.find(' ')));
272
273 // Remove the current token from the string
274 sString.erase(0,sString.find(' ')+1);
275
276 // ensure that the current token has a length
277 if (!vReturn.back().length())
278 vReturn.pop_back();
279 }
280 else
281 {
282 // If no whitespace was found, simply push the complete string
283 vReturn.push_back(sString);
284 break;
285 }
286 }
287
288 return vReturn;
289}
290
291
300std::vector<SyntaxBlockDefinition> NumeReSyntax::splitDefs(std::string sDefString)
301{
302 std::vector<SyntaxBlockDefinition> vDefs;
303
305
306 for (size_t i = 0; i < defs.size(); i++)
307 {
308 std::vector<std::string> words = splitString(defs[i]);
309
310 // Block is at least two words
311 if (words.size() < 2)
312 continue;
313
314 SyntaxBlockDefinition definition;
315 definition.startWord = words.front();
316 definition.endWord = words.back();
317
318 if (words.size() > 2)
319 definition.middleWord1 = words[1];
320
321 if (words.size() > 3)
322 definition.middleWord2 = words[2];
323
324 vDefs.push_back(definition);
325 }
326
327 return vDefs;
328}
329
330
340bool NumeReSyntax::matchItem(const std::vector<std::string>& vVector, const std::string& sString)
341{
342 for (size_t i = 0; i < vVector.size(); i++)
343 {
344 // Match found -> return true
345 if (vVector[i] == sString)
346 return true;
347 }
348
349 // Nothing found
350 return false;
351}
352
353
363std::string NumeReSyntax::highlightLine(const std::string& sCommandLine)
364{
365 // Ensure that a command line with a length is available
366 if (!sCommandLine.length())
367 return "";
368
369 // Search for the error marker.
370 // If found jump to the error highlighting function and return
371 if (sCommandLine.front() == (char)15)
372 return highlightError(sCommandLine);
373
374 // Search for the warning operator.
375 if (sCommandLine.substr(0, 3) == "|!>")
376 return highlightWarning(sCommandLine);
377
378 // Create a color string, which will only contain default colors
379 std::string colors;
380
381 // Fill the color string with the default colors
382 colors.assign(sCommandLine.length(),'0'+SYNTAX_STD);
383 char c;
384
385 // Avoid some special cases
386 // These shall not be highlighted, because they are messages from the kernel
387 if (sCommandLine.substr(0,3) != "|<-"
388 && sCommandLine.substr(0,5) != "|-\?\?>"
389 && (sCommandLine.substr(0,2) != "||" || sCommandLine.substr(0,4) == "||->")
390 && sCommandLine.substr(0,4) != "|FOR"
391 && sCommandLine.substr(0,5) != "|ELSE"
392 && sCommandLine.substr(0,5) != "|ELIF"
393 && sCommandLine.substr(0,5) != "|PROC"
394 && sCommandLine.substr(0,5) != "|COMP"
395 && sCommandLine.substr(0,3) != "|IF"
396 && sCommandLine.substr(0,4) != "|TRY"
397 && sCommandLine.substr(0,5) != "|CTCH"
398 && sCommandLine.substr(0,4) != "|WHL")
399 return colors;
400
401 // Search for strings
402 if (sCommandLine.find('"') != std::string::npos)
403 {
404 char c_string = '0'+SYNTAX_STRING;
405 char c_normal = '0'+SYNTAX_STD;
406 c = c_string;
407
408 // Simply alternate the colors
409 for (size_t k = sCommandLine.find('"'); k < sCommandLine.length(); k++)
410 {
411 if (c == c_normal && sCommandLine[k] == '"' && (!k || sCommandLine[k-1] != '\\'))
412 {
413 c = c_string;
414 colors[k] = c;
415 continue;
416 }
417
418 colors[k] = c;
419
420 if (c == c_string && sCommandLine[k] == '"' && k > sCommandLine.find('"') && sCommandLine[k-1] != '\\')
421 {
422 c = c_normal;
423 }
424 }
425 }
426
427 // Search for procedures
428 if (sCommandLine.find('$') != std::string::npos)
429 {
430 int c_proc = '0'+SYNTAX_PROCEDURE;
431 int c_normal = '0'+SYNTAX_STD;
432 c = c_proc;
433
434 // Apply the color from the dollar sign just up to the next whitespace or an opening parenthesis
435 for (size_t k = sCommandLine.find('$'); k < sCommandLine.length(); k++)
436 {
437 if (sCommandLine[k] == '(' || sCommandLine[k] == ' ')
438 c = c_normal;
439 else if (sCommandLine[k] == '$')
440 c = c_proc;
441
442 if (colors[k] == '0'+SYNTAX_STD)
443 colors[k] = c;
444 else
445 c = c_normal;
446 }
447 }
448
449 // Apply the syntax elements
450 for (unsigned int i = 0; i < sCommandLine.length(); i++)
451 {
452 // find the first relevant character
453 if (!i && sCommandLine.substr(0,3) == "|<-")
454 i += 3;
455 else if (!i && (sCommandLine.substr(0,5) == "|-\?\?>"
456 || sCommandLine.substr(0,4) == "|FOR"
457 || sCommandLine.substr(0,5) == "|ELSE"
458 || sCommandLine.substr(0,5) == "|ELIF"
459 || sCommandLine.substr(0,4) == "|WHL"
460 || sCommandLine.substr(0,3) == "|IF"
461 || sCommandLine.substr(0,4) == "|TRY"
462 || sCommandLine.substr(0,5) == "|CTCH"
463 || sCommandLine.substr(0,5) == "|PROC"
464 || sCommandLine.substr(0,5) == "|COMP"))
465 i += sCommandLine.find('>')+1;
466 else if (!i && sCommandLine.substr(0,4) == "||<-")
467 i += 4;
468 else if (!i && sCommandLine.substr(0,3) == "|| ")
469 i += 3;
470
471 // Ignore whitespaces
472 if (sCommandLine[i] == ' ')
473 continue;
474
475 // Highlight numbers
476 if (sCommandLine[i] >= '0' && sCommandLine[i] <= '9')
477 {
478 unsigned int nLen = 0;
479
480 // Until the curent number ends
481 while (i+nLen < sCommandLine.length()
482 && colors[i+nLen] == '0'+SYNTAX_STD
483 && ((sCommandLine[i+nLen] >= '0' && sCommandLine[i+nLen] <= '9')
484 || sCommandLine[i+nLen] == '.'
485 || sCommandLine[i+nLen] == 'e'
486 || sCommandLine[i+nLen] == 'E'
487 || sCommandLine[i+nLen] == 'i'
488 || ((sCommandLine[i+nLen] == '-' || sCommandLine[i+nLen] == '+')
489 && (sCommandLine[i+nLen-1] == 'e' || sCommandLine[i+nLen-1] == 'E'))
490 )
491 )
492 nLen++;
493
494 // replace the characters
495 colors.replace(i, nLen, nLen, '0'+SYNTAX_NUMBER);
496 i += nLen;
497 }
498
499 // Only enter this block, if it is only default-highlighted
500 if (colors[i] == '0'+SYNTAX_STD)
501 {
502 // Highlight line comments
503 if (sCommandLine.substr(i, 2) == "##")
504 {
505 size_t len = sCommandLine.find("\\n", i+2);
506
507 if (len == std::string::npos)
508 {
509 len = colors.length() - i;
510 colors.replace(i, len, len, '0'+SYNTAX_COMMENT);
511 break;
512 }
513 else
514 {
515 len -= i;
516 colors.replace(i, len, len, '0'+SYNTAX_COMMENT);
517 i = len;
518 }
519
520 }
521
522 // Highlight block comments
523 if (sCommandLine.substr(i, 2) == "#*")
524 {
525 size_t len = sCommandLine.find("*#", i+2);
526
527 if (len == std::string::npos)
528 {
529 len = colors.length() - i;
530 colors.replace(i, len, len, '0'+SYNTAX_COMMENT);
531 break;
532 }
533 else
534 {
535 len -= i;
536 colors.replace(i, len+2, len+2, '0'+SYNTAX_COMMENT);
537 i = len+2;
538 }
539
540 }
541
542 unsigned int nLen = 0;
543
544 // Find the end of the current syntax element
545 while (i+nLen < sCommandLine.length()
546 && colors[i+nLen] == '0'+SYNTAX_STD
547 && sCommandLine[i+nLen] != ' '
548 && sCommandLine[i+nLen] != '\''
549 && sSingleOperators.find(sCommandLine[i+nLen]) == std::string::npos)
550 nLen++;
551
552 // color operators
553 if (sSingleOperators.find(sCommandLine[i+nLen]) != std::string::npos)
554 {
555 colors[i+nLen] = '0'+SYNTAX_OPERATOR;
556 }
557
558 // Color the actual syntax elements
559 if (matchItem(vNSCRCommands, sCommandLine.substr(i,nLen)))
560 {
561 // Commands
562 colors.replace(i, nLen, nLen, '0'+SYNTAX_COMMAND);
563 }
564
565 if (matchItem(vNPRCCommands, sCommandLine.substr(i,nLen)))
566 {
567 // Commands for NPRC in NSCR (highlighted differently)
568 colors.replace(i, nLen, nLen, '0'+SYNTAX_COMMAND); // Changed for debug viewer
569 }
570 else if (i+nLen <= sCommandLine.length()
571 && sCommandLine.find('.', i) < i+nLen)
572 {
573 // Methods on string variables or on data sets
574 size_t nPos = sCommandLine.find('.', i)+1;
575
576 for (size_t n = nPos; n < i+nLen; n++)
577 {
578 if (sCommandLine[n] == '.')
579 {
580 if (matchItem(vMethods, sCommandLine.substr(nPos, n-nPos))
581 || matchItem(vMethodsArgs, sCommandLine.substr(nPos, n-nPos)))
582 colors.replace(nPos, n-nPos, n-nPos, '0'+SYNTAX_METHODS);
583 nPos = n+1;
584 }
585
586 if (n+1 == i+nLen)
587 {
588 if (matchItem(vMethods, sCommandLine.substr(nPos, n-nPos+1))
589 || matchItem(vMethodsArgs, sCommandLine.substr(nPos, n-nPos+1)))
590 colors.replace(nPos, n-nPos+1, n-nPos+1, '0'+SYNTAX_METHODS);
591 nPos = n+1;
592 }
593 }
594 }
595 else if (i+nLen < sCommandLine.length()
596 && sCommandLine[i+nLen] == '('
597 && matchItem(vFunctions, sCommandLine.substr(i,nLen)))
598 {
599 // Functions
600 colors.replace(i, nLen, nLen, '0'+SYNTAX_FUNCTION);
601 }
602 else if (matchItem(vOptions, sCommandLine.substr(i,nLen)))
603 {
604 // Command line options
605 colors.replace(i, nLen, nLen, '0'+SYNTAX_OPTION);
606 }
607 else if (matchItem(vConstants, sCommandLine.substr(i,nLen)))
608 {
609 // Constants
610 colors.replace(i, nLen, nLen, '0'+SYNTAX_CONSTANT);
611 }
612 else if (matchItem(vSpecialValues, sCommandLine.substr(i,nLen)))
613 {
614 // Special variables
615 colors.replace(i, nLen, nLen, '0'+SYNTAX_SPECIALVAL);
616 }
617
618 i += nLen;
619 }
620 }
621
622 // Return the color string
623 return colors;
624}
625
626
636std::string NumeReSyntax::highlightError(const std::string& sCommandLine)
637{
638 std::string colors;
639 colors.assign(sCommandLine.length(),'0'+SYNTAX_OPERATOR);
640 return colors;
641}
642
643
653std::string NumeReSyntax::highlightWarning(const std::string& sCommandLine)
654{
655 std::string colors;
656 colors.assign(sCommandLine.length(),'0'+SYNTAX_NUMBER);
657 return colors;
658}
659
660
670std::string NumeReSyntax::getAutoCompList(std::string sFirstChars, bool useSmartSense)
671{
672 std::string sAutoCompList;
673
674 // Only of the autocompletion map is not filled
675 if (!mAutoCompList.size())
676 {
677 // Insert every element in the map
678 for (size_t i = 0; i < vNSCRCommands.size(); i++)
679 mAutoCompList[toLowerCase(vNSCRCommands[i])] = std::make_pair(vNSCRCommands[i] + "?" + toString((int)SYNTAX_COMMAND),
681
682 for (size_t i = 0; i < vNPRCCommands.size(); i++)
683 mAutoCompList[toLowerCase(vNPRCCommands[i])] = std::make_pair(vNPRCCommands[i] + "?" + toString((int)SYNTAX_COMMAND),
685
686 for (size_t i = 0; i < vFunctions.size(); i++)
687 mAutoCompList[toLowerCase(vFunctions[i])+"("] = std::make_pair(vFunctions[i]+"(?" + toString((int)SYNTAX_FUNCTION),
689
690 for (size_t i = 0; i < vMethods.size(); i++)
691 mAutoCompList[toLowerCase(vMethods[i])+"."] = std::make_pair(vMethods[i]+"?" + toString((int)SYNTAX_METHODS),
692 SYNTAX_METHODS); // Methods shall not override functions
693
694 for (size_t i = 0; i < vMethodsArgs.size(); i++)
695 mAutoCompList[toLowerCase(vMethodsArgs[i])+"(."] = std::make_pair(vMethodsArgs[i]+"(?" + toString((int)SYNTAX_METHODS),
696 SYNTAX_METHODS); // Methods shall not override functions
697
698 for (size_t i = 0; i < vOptions.size(); i++)
699 mAutoCompList[toLowerCase(vOptions[i])] = std::make_pair(vOptions[i] + "?" + toString((int)SYNTAX_OPTION),
701
702 for (size_t i = 0; i < vConstants.size(); i++)
703 mAutoCompList[toLowerCase(vConstants[i])] = std::make_pair(vConstants[i] + "?" + toString((int)SYNTAX_CONSTANT),
705
706 for (size_t i = 0; i < vSpecialValues.size(); i++)
707 {
708 if (vSpecialValues[i] == "ans")
709 mAutoCompList["ans"] = std::make_pair("ans{?" + toString((int)SYNTAX_SPECIALVAL), SYNTAX_SPECIALVAL);
710 else if (vSpecialValues[i] == "table" || vSpecialValues[i] == "data" || vSpecialValues[i] == "string")
713 else
716 }
717
718 for (size_t i = 0; i < vOperators.size(); i++)
719 mAutoCompList[toLowerCase(vOperators[i])] = std::make_pair(vOperators[i] + "?" + toString((int)SYNTAX_OPERATOR),
721 }
722
723 // Transform the passed first characters to lower case to avoid case sensitivity
724 sFirstChars = toLowerCase(sFirstChars);
725
726 bool selectMethods = useSmartSense && sFirstChars.front() == '.';
727
728 if (selectMethods)
729 sFirstChars.erase(0, 1);
730
731 // Try to find the correspondig elements in the map
732 for (auto iter = mAutoCompList.begin(); iter != mAutoCompList.end(); ++iter)
733 {
734 if ((iter->first).front() == sFirstChars.front())
735 {
736 if (useSmartSense)
737 {
738 if (selectMethods && iter->second.second != SYNTAX_METHODS)
739 continue;
740 else if (!selectMethods && iter->second.second == SYNTAX_METHODS)
741 continue;
742 }
743
744 if (sFirstChars == (iter->first).substr(0, sFirstChars.length()))
745 sAutoCompList += iter->second.first + " ";
746 }
747 else if ((iter->first).front() > sFirstChars.front())
748 break;
749 }
750
751 // return the created list
752 return sAutoCompList;
753}
754
755
764std::string NumeReSyntax::getAutoCompListMATLAB(std::string sFirstChars)
765{
766 std::string sAutoCompList;
767
768 if (!mAutoCompListMATLAB.size())
769 {
770 for (size_t i = 0; i < vMatlabKeyWords.size(); i++)
772
773 for (size_t i = 0; i < vMatlabFunctions.size(); i++)
775 }
776
777 sFirstChars = toLowerCase(sFirstChars);
778
779 for (auto iter = mAutoCompListMATLAB.begin(); iter != mAutoCompListMATLAB.end(); ++iter)
780 {
781 if ((iter->first).front() == sFirstChars.front())
782 {
783 if (sFirstChars == (iter->first).substr(0,sFirstChars.length()))
784 sAutoCompList += (iter->first).substr((iter->first).find('|')+1) + "?" + toString((int)(iter->second)) + " ";
785 }
786 else if ((iter->first).front() > sFirstChars.front())
787 break;
788 }
789
790 return sAutoCompList;
791}
792
793
802std::string NumeReSyntax::getAutoCompListCPP(std::string sFirstChars)
803{
804 std::string sAutoCompList;
805
806 if (!mAutoCompListCPP.size())
807 {
808 for (size_t i = 0; i < vCppKeyWords.size(); i++)
810
811 for (size_t i = 0; i < vCppFunctions.size(); i++)
813 }
814
815 sFirstChars = toLowerCase(sFirstChars);
816
817 for (auto iter = mAutoCompListCPP.begin(); iter != mAutoCompListCPP.end(); ++iter)
818 {
819 if ((iter->first).front() == sFirstChars.front())
820 {
821 if (sFirstChars == (iter->first).substr(0,sFirstChars.length()))
822 sAutoCompList += (iter->first).substr((iter->first).find('|')+1) + "?" + toString((int)(iter->second)) + " ";
823 }
824 else if ((iter->first).front() > sFirstChars.front())
825 break;
826 }
827
828 return sAutoCompList;
829}
830
831
840std::string NumeReSyntax::getAutoCompListTeX(std::string sFirstChars)
841{
842 std::string sAutoCompList;
843
844 if (!mAutoCompListTeX.size())
845 {
846 for (size_t i = 0; i < vTeXKeyWords.size(); i++)
848 }
849
850 sFirstChars = toLowerCase(sFirstChars);
851
852 for (auto iter = mAutoCompListTeX.begin(); iter != mAutoCompListTeX.end(); ++iter)
853 {
854 if ((iter->first).front() == sFirstChars.front())
855 {
856 if (sFirstChars == (iter->first).substr(0,sFirstChars.length()))
857 sAutoCompList += (iter->first).substr((iter->first).find('|')+1) + "?" + toString((int)(iter->second)) + " ";
858 }
859 else if ((iter->first).front() > sFirstChars.front())
860 break;
861 }
862
863 return sAutoCompList;
864}
865
866
878std::string NumeReSyntax::getProcAutoCompList(std::string sFirstChars, std::string sBaseNameSpace, std::string sSelectedNameSpace)
879{
880 if (!vProcedureTree.size())
881 return "";
882
883 std::string sProcName;
884 static std::string sStandardNamespaces[] = {"main~", "this~", "thisfile~"};
885
886 // Try to detect the actual name of the procedure including its namespace
887 if (sSelectedNameSpace.length())
888 {
889 if (sSelectedNameSpace.back() != '~')
890 sProcName = sSelectedNameSpace + "~" + sFirstChars;
891 else
892 sProcName = sSelectedNameSpace + sFirstChars;
893 }
894 else if (sBaseNameSpace.length())
895 {
896 if (sBaseNameSpace.back() != '~')
897 sProcName = sBaseNameSpace + "~" + sFirstChars;
898 else
899 sProcName = sBaseNameSpace + sFirstChars;
900 }
901 else
902 sProcName = sFirstChars;
903
904 sFirstChars = toLowerCase(sFirstChars);
905 sProcName = toLowerCase(sProcName);
906
907 std::string sAutoCompList = " ";
908
909 // If no namespace was pre-selected, provide the standard namespaces
910 if (!sSelectedNameSpace.length())
911 {
912 for (size_t i = 0; i < 3; i++)
913 {
914 if (sStandardNamespaces[i].substr(0, sFirstChars.length()) == sFirstChars)
915 sAutoCompList += sStandardNamespaces[i] + "?" + toString((int)(SYNTAX_PROCEDURE)) + " ";
916 }
917 }
918
919 std::string sToken;
920
921 // Go through the complete procedure tree
922 for (size_t i = 0; i < vProcedureTree.size(); i++)
923 {
924 // If the procedure in the tree corresponds to the passed procedure fragment
925 if (toLowerCase(vProcedureTree[i].substr(0, sProcName.length())) == sProcName)
926 {
927 // Only add the namespace if it's needed
928 if (sSelectedNameSpace.length())
929 {
930 sToken = vProcedureTree[i].substr(sSelectedNameSpace.length());
931
932 if (sToken.find('~', sFirstChars.length()) != std::string::npos)
933 {
934 sToken.erase(sToken.find('~', sFirstChars.length())+1);
935 sToken += "?" + toString((int)(SYNTAX_PROCEDURE)) + " ";
936 }
937 else
938 sToken += "(?" + toString((int)(SYNTAX_PROCEDURE)) + " ";
939 }
940 else if (vProcedureTree[i].find('~', sProcName.length()) != std::string::npos)
941 sToken = vProcedureTree[i].substr(0, vProcedureTree[i].find('~', sFirstChars.length())+1) + "?" + toString((int)(SYNTAX_PROCEDURE)) + " ";
942 else
943 sToken = vProcedureTree[i] + "(?" + toString((int)(SYNTAX_PROCEDURE)) + " ";
944
945 // Remove the base namespace, if it is available in the current token
946 if (!sSelectedNameSpace.length())
947 {
948 if (toLowerCase(sToken.substr(0, sBaseNameSpace.length())) == toLowerCase(sBaseNameSpace))
949 sToken.erase(0, sBaseNameSpace.length());
950
951 if (sToken.front() == '~')
952 sToken.erase(0,1);
953 }
954
955 // Only add the current element to the autocompletion list, if it is not already available
956 if (sAutoCompList.find(" " + sToken) == std::string::npos)
957 sAutoCompList += sToken;
958 }
959 }
960
961 // Return the autocompletion list (the first character is always a whitespace character)
962 return sAutoCompList.substr(1);
963}
964
965
974std::string NumeReSyntax::getNameSpaceAutoCompList(std::string sFirstChars)
975{
976 if (!vProcedureTree.size())
977 return "";
978
979 std::string sProcName;
980 static std::string sStandardNamespaces[] = {"main~", "this~", "thisfile~"};
981 sProcName = sFirstChars;
982 std::string sAutoCompList = " ";
983
984 // Provide the standard namespaces first
985 for (size_t i = 0; i < 3; i++)
986 {
987 if (sStandardNamespaces[i].substr(0, sFirstChars.length()) == sFirstChars)
988 sAutoCompList += sStandardNamespaces[i] + "?" + toString((int)(SYNTAX_PROCEDURE)) + " ";
989 }
990
991 // Append all available namespaces
992 std::string sToken;
993
994 for (size_t i = 0; i < vProcedureTree.size(); i++)
995 {
996 if (vProcedureTree[i].substr(0, sProcName.length()) == sProcName)
997 {
998 if (vProcedureTree[i].find('~', sProcName.length()) != std::string::npos)
999 sToken = vProcedureTree[i].substr(0, vProcedureTree[i].find('~', sProcName.length())+1) + "?" + toString((int)(SYNTAX_PROCEDURE)) + " ";
1000
1001 if (sAutoCompList.find(" " + sToken) == std::string::npos)
1002 sAutoCompList += sToken;
1003 }
1004 }
1005
1006 // Return the autocompletion list (the first character is always a whitespace character)
1007 return sAutoCompList.substr(1);
1008}
1009
1010
std::string toLowerCase(const std::string &)
Converts uppercase to lowercase letters.
This class extends the std::vector for endlessness.
Definition: structures.hpp:838
This class contains all needed keywords to highlight their occurences correspondingly....
Definition: syntax.hpp:55
std::map< std::string, int > mAutoCompListCPP
Definition: syntax.hpp:79
void addPlugins(const std::vector< std::string > &vPlugins)
Add the plugin definitions to the command strings. Will reload the standard settings in advance to re...
Definition: syntax.cpp:165
std::vector< std::string > vProcedureTree
Definition: syntax.hpp:74
std::string sSingleOperators
Definition: syntax.hpp:76
bool matchItem(const std::vector< std::string > &vVector, const std::string &sString)
This function searches for a match of the passed string in the passed vector.
Definition: syntax.cpp:340
void loadSyntax(const std::string &_sPath="")
Member function for loading the syntax element definitions.
Definition: syntax.cpp:89
std::vector< std::string > vCppFunctions
Definition: syntax.hpp:71
void setProcedureTree(const std::vector< std::string > &vTree)
Set the procedure tree (used for autocompleting).
Definition: syntax.cpp:182
std::vector< SyntaxBlockDefinition > vBlockDefs
Definition: syntax.hpp:67
std::string getAutoCompListCPP(std::string sFirstChars)
The same as above but specialized for C++ Commands.
Definition: syntax.cpp:802
std::vector< SyntaxBlockDefinition > splitDefs(std::string sDefString)
Converts a block def string into actual syntax block definitions.
Definition: syntax.cpp:300
std::vector< std::string > splitString(std::string sString)
This function splits the passed string up into single string tokens.
Definition: syntax.cpp:256
std::string highlightLine(const std::string &sCommandLine)
This function applies the highlighting colors to the command line (only used in the terminal).
Definition: syntax.cpp:363
std::vector< std::string > vOptions
Definition: syntax.hpp:59
std::vector< std::string > vMethods
Definition: syntax.hpp:61
std::vector< std::string > vFunctions
Definition: syntax.hpp:60
std::vector< std::string > vOperators
Definition: syntax.hpp:65
@ SYNTAX_OPTION
Definition: syntax.hpp:92
@ SYNTAX_FUNCTION
Definition: syntax.hpp:93
@ SYNTAX_METHODS
Definition: syntax.hpp:102
@ SYNTAX_STRING
Definition: syntax.hpp:96
@ SYNTAX_PROCEDURE
Definition: syntax.hpp:99
@ SYNTAX_COMMENT
Definition: syntax.hpp:103
@ SYNTAX_CONSTANT
Definition: syntax.hpp:94
@ SYNTAX_COMMAND
Definition: syntax.hpp:91
@ SYNTAX_SPECIALVAL
Definition: syntax.hpp:95
@ SYNTAX_OPERATOR
Definition: syntax.hpp:98
std::string getAutoCompListMATLAB(std::string sFirstChars)
The same as above but specialized for MATLAB commands.
Definition: syntax.cpp:764
std::vector< std::string > vDocKeyWords
Definition: syntax.hpp:66
std::string getNameSpaceAutoCompList(std::string sFirstChars)
This function returns the autocompletion list for the namespaces.
Definition: syntax.cpp:974
NumeReSyntax()
Default constructor.
Definition: syntax.cpp:27
std::vector< std::string > vConstants
Definition: syntax.hpp:63
std::vector< std::string > vNSCRCommands
Definition: syntax.hpp:57
std::map< std::string, std::pair< std::string, int > > mAutoCompList
Definition: syntax.hpp:77
std::string getAutoCompListTeX(std::string sFirstChars)
The same as above but specialized for LaTeX Commands.
Definition: syntax.cpp:840
std::vector< std::string > vMethodsArgs
Definition: syntax.hpp:62
std::string getBlockDefs() const
Returns all block definitions as a folding string for the lexers.
Definition: syntax.cpp:210
std::string constructString(const std::vector< std::string > &vVector) const
This member function concatenates the passed vector elements to a whitespace- separated single string...
Definition: syntax.cpp:234
std::string getAutoCompList(std::string sFirstChars, bool useSmartSense=false)
This function returns the autocompletion list for the editor.
Definition: syntax.cpp:670
std::map< std::string, int > mAutoCompListTeX
Definition: syntax.hpp:80
std::vector< std::string > vMatlabKeyWords
Definition: syntax.hpp:68
std::string highlightError(const std::string &sCommandLine)
Highlight an error message. We simply use the color of the operators (which is red as default).
Definition: syntax.cpp:636
std::vector< std::string > vMatlabFunctions
Definition: syntax.hpp:69
std::vector< std::string > vTeXKeyWords
Definition: syntax.hpp:72
std::vector< std::string > vSpecialValues
Definition: syntax.hpp:64
std::string sPath
Definition: syntax.hpp:82
std::string highlightWarning(const std::string &sCommandLine)
Highlight a warning message. We simply use the color of numbers (which is orange as default).
Definition: syntax.cpp:653
std::string getProcAutoCompList(std::string sFirstChars, std::string sBaseNameSpace, std::string sSelectedNameSpace)
This function will return the autocompletion list for the procedures based upon the provided procedur...
Definition: syntax.cpp:878
std::vector< std::string > vCppKeyWords
Definition: syntax.hpp:70
std::vector< std::string > vNPRCCommands
Definition: syntax.hpp:58
std::map< std::string, int > mAutoCompListMATLAB
Definition: syntax.hpp:78
A simple structure to define a syntax block for folding, etc.
Definition: syntax.hpp:35
std::string middleWord2
Definition: syntax.hpp:39
std::string endWord
Definition: syntax.hpp:37
std::string middleWord1
Definition: syntax.hpp:38
std::string startWord
Definition: syntax.hpp:36
std::string toString(int)
Converts an integer to a string without the Settings bloat.
EndlessVector< string > getAllSemiColonSeparatedTokens(string sArgList)
Splits up the complete index list and returns them as an EndlessVector.
Definition: tools.cpp:2403