1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
|
/** \file
\brief Implementation of the token reader used to break the expression string up
into tokens.
<pre>
__________ ____ ___
_____ __ _\______ \_____ _______ ______ __________\ \/ /
/ \| | \ ___/\__ \\_ __ \/ ___// __ \_ __ \ /
| Y Y \ | / | / __ \| | \/\___ \\ ___/| | \/ \
|__|_| /____/|____| (____ /__| /____ >\___ >__| /___/\ \
\/ \/ \/ \/ \_/
Copyright (C) 2021 Ingo Berg, et al.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</pre>
*/
#include "mpTokenReader.h"
#include <cassert>
#include <cctype>
#include "mpParserBase.h"
#include "mpIValReader.h"
#include "mpIfThenElse.h"
#include "mpScriptTokens.h"
#include "mpOprtIndex.h"
#include "mpOprtMatrix.h"
MUP_NAMESPACE_START
//---------------------------------------------------------------------------
/** \brief Copy constructor.
\sa Assign
\throw nothrow
*/
TokenReader::TokenReader(const TokenReader &a_Reader)
{
Assign(a_Reader);
}
//---------------------------------------------------------------------------
/** \brief Assignement operator.
\param a_Reader Object to copy to this token reader.
\throw nothrow
Self assignement will be suppressed otherwise #Assign is called.
*/
TokenReader& TokenReader::operator=(const TokenReader &a_Reader)
{
if (&a_Reader != this)
Assign(a_Reader);
return *this;
}
//---------------------------------------------------------------------------
/** \brief Assign state of a token reader to this token reader.
\param a_Reader Object from which the state should be copied.
\throw nothrow
*/
void TokenReader::Assign(const TokenReader &obj)
{
m_pParser = obj.m_pParser;
m_sExpr = obj.m_sExpr;
m_nPos = obj.m_nPos;
m_nNumBra = obj.m_nNumBra;
m_nNumIndex = obj.m_nNumIndex;
m_nNumCurly = obj.m_nNumCurly;
m_nNumIfElse = obj.m_nNumIfElse;
m_nSynFlags = obj.m_nSynFlags;
m_UsedVar = obj.m_UsedVar;
m_pVarDef = obj.m_pVarDef;
m_pPostOprtDef = obj.m_pPostOprtDef;
m_pInfixOprtDef = obj.m_pInfixOprtDef;
m_pOprtDef = obj.m_pOprtDef;
m_pOprtShortcutDef = obj.m_pOprtShortcutDef;
m_pFunDef = obj.m_pFunDef;
m_pConstDef = obj.m_pConstDef;
m_pDynVarShadowValues = obj.m_pDynVarShadowValues;
m_vTokens = obj.m_vTokens;
// Reader klassen klonen
DeleteValReader();
std::size_t i, iSize = obj.m_vValueReader.size();
for (i = 0; i < iSize; ++i)
{
m_vValueReader.push_back(obj.m_vValueReader[i]->Clone(this));
}
}
//---------------------------------------------------------------------------
/** \brief Constructor.
Create a Token reader and bind it to a parser object.
\pre [assert] a_pParser may not be nullptr
\post #m_pParser==a_pParser
\param a_pParent Parent parser object of the token reader.
*/
TokenReader::TokenReader(ParserXBase *a_pParent)
: m_pParser(a_pParent)
, m_sExpr()
, m_nPos(0)
, m_nNumBra(0)
, m_nNumIndex(0)
, m_nNumCurly(0)
, m_nNumIfElse(0)
, m_nSynFlags(0)
, m_vTokens()
, m_eLastTokCode(cmUNKNOWN)
, m_pFunDef(nullptr)
, m_pOprtDef(nullptr)
, m_pOprtShortcutDef(nullptr)
, m_pInfixOprtDef(nullptr)
, m_pPostOprtDef(nullptr)
, m_pConstDef(nullptr)
, m_pDynVarShadowValues(nullptr)
, m_pVarDef(nullptr)
, m_vValueReader()
, m_UsedVar()
, m_fZero(0)
{
assert(m_pParser);
SetParent(m_pParser);
}
//---------------------------------------------------------------------------
/** \brief Destructor (trivial).
\throw nothrow
*/
TokenReader::~TokenReader()
{
DeleteValReader();
}
//---------------------------------------------------------------------------
void TokenReader::DeleteValReader()
{
int iSize = (int)m_vValueReader.size();
for (int i = 0; i < iSize; ++i)
delete m_vValueReader[i];
m_vValueReader.clear();
}
//---------------------------------------------------------------------------
/** \brief Create instance of a ParserTokenReader identical with this
and return its pointer.
This is a factory method the calling function must take care of the object destruction.
\return A new ParserTokenReader object.
\throw nothrow
*/
TokenReader* TokenReader::Clone(ParserXBase *a_pParent) const
{
std::unique_ptr<TokenReader> ptr(new TokenReader(*this));
ptr->SetParent(a_pParent);
return ptr.release();
}
//---------------------------------------------------------------------------
void TokenReader::AddValueReader(IValueReader *a_pReader)
{
a_pReader->SetParent(this);
m_vValueReader.push_back(a_pReader);
}
//---------------------------------------------------------------------------
void TokenReader::AddSynFlags(int flag)
{
m_nSynFlags |= flag;
}
//---------------------------------------------------------------------------
const TokenReader::token_buf_type& TokenReader::GetTokens() const
{
return m_vTokens;
}
//---------------------------------------------------------------------------
/** \brief Return the current position of the token reader in the formula string.
\return #m_nPos
\throw nothrow
*/
int TokenReader::GetPos() const
{
return m_nPos;
}
//---------------------------------------------------------------------------
/** \brief Return a reference to the formula.
\return #m_sExpr
\throw nothrow
*/
const string_type& TokenReader::GetExpr() const
{
return m_sExpr;
}
//---------------------------------------------------------------------------
/** \brief Return a map containing the used variables only. */
const var_maptype& TokenReader::GetUsedVar() const
{
return m_UsedVar;
}
//---------------------------------------------------------------------------
/** \brief Initialize the token Reader.
Sets the expression position index to zero and set Syntax flags to
default for initial parsing.
*/
void TokenReader::SetExpr(const string_type &a_sExpr)
{
if (a_sExpr.empty())
throw ParserError(_T("Expression is empty!"), ecUNEXPECTED_EOF);
if (a_sExpr.find_first_not_of(' ') == std::string::npos)
throw ParserError(_T("Expression is empty!"), ecUNEXPECTED_EOF);
if (std::all_of(a_sExpr.begin(), a_sExpr.end(), [](char_type c) { return !std::isgraph(c); }))
throw ParserError(_T("Non printable characters in expression found!"));
// Check maximum allowed expression length. An arbitrary value small enough so i can debug expressions sent to me
if (a_sExpr.length() >= 10000)
throw ParserError(_T("Expression longer than 10000 characters!"));
m_sExpr = a_sExpr;
ReInit();
}
//---------------------------------------------------------------------------
/** \brief Reset the token reader to the start of the formula.
\post #m_nPos==0, #m_nSynFlags = noOPT | noBC | noPOSTOP | noSTR
\throw nothrow
\sa ESynCodes
The syntax flags will be reset to a value appropriate for the
start of a formula.
*/
void TokenReader::ReInit()
{
m_nPos = 0;
m_nNumBra = 0;
m_nNumIndex = 0;
m_nNumCurly = 0;
m_nNumIfElse = 0;
m_nSynFlags = noOPT | noBC | noCBC | noPFX | noCOMMA | noIO | noIC | noIF | noELSE;
m_UsedVar.clear();
m_eLastTokCode = cmUNKNOWN;
m_vTokens.clear();
}
//---------------------------------------------------------------------------
const ptr_tok_type& TokenReader::Store(const ptr_tok_type &t, int token_pos)
{
m_eLastTokCode = t->GetCode();
t->SetExprPos(token_pos);
m_vTokens.push_back(t);
return t;
}
//---------------------------------------------------------------------------
void TokenReader::SkipCommentsAndWhitespaces()
{
bool bSkip = true;
while (m_nPos < static_cast<int>(m_sExpr.length()) && bSkip)
{
switch (m_sExpr[m_nPos])
{
// skip comments
case '#':
{
std::size_t i = m_sExpr.find_first_of('\n', m_nPos + 1);
m_nPos = static_cast<int>((i != string_type::npos) ? i : m_sExpr.length());
}
break;
// skip whitespaces
case ' ':
++m_nPos;
break;
default:
bSkip = false;
} // switch
} // while comment or whitespace
}
//---------------------------------------------------------------------------
/** \brief Read the next token from the string. */
ptr_tok_type TokenReader::ReadNextToken()
{
assert(m_pParser);
SkipCommentsAndWhitespaces();
int token_pos = m_nPos;
ptr_tok_type pTok;
// Check for end of expression
if (IsEOF(pTok))
return Store(pTok, token_pos);
if (IsNewline(pTok))
return Store(pTok, token_pos);
if (!(m_nSynFlags & noOPT) && IsShortCutOprt(pTok))
return Store(pTok, token_pos);
if (!(m_nSynFlags & noOPT) && IsOprt(pTok))
return Store(pTok, token_pos); // Check for user defined binary operator
if (!(m_nSynFlags & noIFX) && IsInfixOpTok(pTok))
return Store(pTok, token_pos); // Check for unary operators
if (IsValTok(pTok))
return Store(pTok, token_pos); // Check for values / constant tokens
if (IsBuiltIn(pTok))
return Store(pTok, token_pos); // Check built in operators / tokens
if (IsVarOrConstTok(pTok))
return Store(pTok, token_pos); // Check for variable tokens
if (IsFunTok(pTok))
return Store(pTok, token_pos);
if (!(m_nSynFlags & noPFX) && IsPostOpTok(pTok))
return Store(pTok, token_pos); // Check for unary operators
// 2.) We have found no token, maybe there is a token that we don't expect here.
// Again call the Identifier functions but this time only those we don't expect
// to find.
if ((m_nSynFlags & noOPT) && IsOprt(pTok))
return Store(pTok, token_pos); // Check for user defined binary operator
if ((m_nSynFlags & noIFX) && IsInfixOpTok(pTok))
return Store(pTok, token_pos); // Check for unary operators
if ((m_nSynFlags & noPFX) && IsPostOpTok(pTok))
return Store(pTok, token_pos); // Check for unary operators
// </ibg>
// Now we are in trouble because there is something completely unknown....
// Check the string for an undefined variable token. This is done
// only if a flag is set indicating to ignore undefined variables.
// This is a way to conditionally avoid an error if undefined variables
// occur. The GetExprVar function must supress the error for undefined
// variables in order to collect all variable names including the
// undefined ones.
if ((m_pParser->m_bIsQueryingExprVar || m_pParser->m_bAutoCreateVar) && IsUndefVarTok(pTok))
return Store(pTok, token_pos);
// Check for unknown token
//
// !!! From this point on there is no exit without an exception possible...
//
string_type sTok;
int iEnd = ExtractToken(m_pParser->ValidNameChars(), sTok, m_nPos);
ErrorContext err;
err.Errc = ecUNASSIGNABLE_TOKEN;
err.Expr = m_sExpr;
err.Pos = m_nPos;
if (iEnd != m_nPos)
err.Ident = sTok;
else
err.Ident = m_sExpr.substr(m_nPos);
throw ParserError(err);
}
//---------------------------------------------------------------------------
void TokenReader::SetParent(ParserXBase *a_pParent)
{
m_pParser = a_pParent;
m_pFunDef = &a_pParent->m_FunDef;
m_pOprtDef = &a_pParent->m_OprtDef;
m_pOprtShortcutDef = &a_pParent->m_OprtShortcutDef;
m_pInfixOprtDef = &a_pParent->m_InfixOprtDef;
m_pPostOprtDef = &a_pParent->m_PostOprtDef;
m_pVarDef = &a_pParent->m_varDef;
m_pConstDef = &a_pParent->m_valDef;
m_pDynVarShadowValues = &a_pParent->m_valDynVarShadow;
}
//---------------------------------------------------------------------------
/** \brief Extract all characters that belong to a certain charset.
\param a_szCharSet [in] Const char array of the characters allowed in the token.
\param a_strTok [out] The string that consists entirely of characters listed in a_szCharSet.
\param a_iPos [in] Position in the string from where to start reading.
\return The Position of the first character not listed in a_szCharSet.
\throw nothrow
*/
int TokenReader::ExtractToken(const char_type *a_szCharSet,
string_type &a_sTok,
int a_iPos) const
{
int iEnd = (int)m_sExpr.find_first_not_of(a_szCharSet, a_iPos);
if (iEnd == (int)string_type::npos)
iEnd = (int)m_sExpr.length();
if (iEnd != a_iPos)
a_sTok.assign(m_sExpr.begin() + a_iPos, m_sExpr.begin() + iEnd);
return iEnd;
}
//---------------------------------------------------------------------------
/** \brief Check if a built in operator or other token can be found.
*/
bool TokenReader::IsBuiltIn(ptr_tok_type &a_Tok)
{
const char_type **pOprtDef = m_pParser->GetOprtDef(),
*szFormula = m_sExpr.c_str();
int i;
try
{
// Compare token with function and operator strings
// check string for operator/function
for (i = 0; pOprtDef[i]; i++)
{
std::size_t len(std::char_traits<char_type>::length(pOprtDef[i]));
if (string_type(pOprtDef[i]) == string_type(szFormula + m_nPos, szFormula + m_nPos + len))
{
switch (i)
{
case cmARG_SEP:
if (m_nSynFlags & noCOMMA)
throw ecUNEXPECTED_COMMA;
m_nSynFlags = noBC | noCBC | noOPT | noEND | noNEWLINE | noCOMMA | noPFX | noIC | noIO | noIF | noELSE;
a_Tok = ptr_tok_type(new GenericToken((ECmdCode)i, pOprtDef[i]));
break;
case cmELSE:
if (m_nSynFlags & noELSE)
throw ecMISPLACED_COLON;
m_nNumIfElse--;
if (m_nNumIfElse < 0)
throw ecMISPLACED_COLON;
m_nSynFlags = noBC | noCBC | noIO | noIC | noPFX | noEND | noNEWLINE | noCOMMA | noOPT | noIF | noELSE;
a_Tok = ptr_tok_type(new TokenIfThenElse(cmELSE));
break;
case cmIF:
if (m_nSynFlags & noIF)
throw ecUNEXPECTED_CONDITIONAL;
m_nNumIfElse++;
m_nSynFlags = noBC | noCBC | noIO | noPFX | noIC | noEND | noNEWLINE | noCOMMA | noOPT | noIF | noELSE;
a_Tok = ptr_tok_type(new TokenIfThenElse(cmIF));
break;
case cmBO:
if (m_nSynFlags & noBO)
throw ecUNEXPECTED_PARENS;
if (m_eLastTokCode == cmFUNC)
{
m_nSynFlags = noOPT | noEND | noNEWLINE | noCOMMA | noPFX | noIC | noIO | noIF | noELSE | noCBC;
}
else
{
m_nSynFlags = noBC | noOPT | noEND | noNEWLINE | noCOMMA | noPFX | noIC | noIO | noIF | noELSE | noCBC;
}
m_nNumBra++;
a_Tok = ptr_tok_type(new GenericToken((ECmdCode)i, pOprtDef[i]));
break;
case cmBC:
if (m_nSynFlags & noBC)
throw ecUNEXPECTED_PARENS;
m_nSynFlags = noBO | noVAR | noVAL | noFUN | noIFX | noCBO;
m_nNumBra--;
if (m_nNumBra < 0)
throw ecUNEXPECTED_PARENS;
a_Tok = ptr_tok_type(new GenericToken((ECmdCode)i, pOprtDef[i]));
break;
case cmIO:
if (m_nSynFlags & noIO)
throw ecUNEXPECTED_SQR_BRACKET;
m_nSynFlags = noIC | noIO | noOPT | noPFX | noBC | noNEWLINE | noCBC | noCOMMA;
m_nNumIndex++;
a_Tok = ptr_tok_type(new GenericToken((ECmdCode)i, pOprtDef[i]));
break;
case cmIC:
if (m_nSynFlags & noIC)
throw ecUNEXPECTED_SQR_BRACKET;
m_nSynFlags = noBO | noIFX | noCBO;
m_nNumIndex--;
if (m_nNumIndex < 0)
throw ecUNEXPECTED_SQR_BRACKET;
a_Tok = ptr_tok_type(new OprtIndex());
break;
case cmCBO:
if (m_nSynFlags & noVAL)
throw ecUNEXPECTED_CURLY_BRACKET;
m_nSynFlags = noCBC | noIC | noIO | noOPT | noPFX | noBC | noNEWLINE | noCOMMA | noIF;
m_nNumCurly++;
a_Tok = ptr_tok_type(new GenericToken((ECmdCode)i, pOprtDef[i]));
break;
case cmCBC:
if (m_nSynFlags & noIC)
throw ecUNEXPECTED_CURLY_BRACKET;
m_nSynFlags = noBO | noCBO | noIFX;
m_nNumCurly--;
if (m_nNumCurly < 0)
throw ecUNEXPECTED_CURLY_BRACKET;
a_Tok = ptr_tok_type(new OprtCreateArray());
break;
default: // The operator is listed in c_DefaultOprt, but not here. This is a bad thing...
throw ecINTERNAL_ERROR;
} // switch operator id
m_nPos += (int)len;
return true;
} // if operator string found
} // end of for all operator strings
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Expr = m_sExpr;
err.Ident = pOprtDef[i];
err.Pos = m_nPos;
throw ParserError(err);
}
return false;
}
//---------------------------------------------------------------------------
/** \brief Check for End of expression
*/
bool TokenReader::IsNewline(ptr_tok_type &a_Tok)
{
// nicht nach: bionop, infixop, argumentseparator,
// erlaubt nach: Werten, variablen, schließenden klammern, schliessendem index
bool bRet(false);
try
{
if (m_sExpr[m_nPos] == '\n')
{
// Check if all brackets were closed
if (m_nSynFlags & noNEWLINE)
throw ecUNEXPECTED_NEWLINE;
if (m_nNumBra > 0)
throw ecMISSING_PARENS;
if (m_nNumIndex > 0)
throw ecMISSING_SQR_BRACKET;
if (m_nNumCurly > 0)
throw ecMISSING_CURLY_BRACKET;
if (m_nNumIfElse > 0)
throw(ecMISSING_ELSE_CLAUSE);
m_nPos++;
m_nSynFlags = sfSTART_OF_LINE;
a_Tok = ptr_tok_type(new TokenNewline());
bRet = true;
}
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Ident = _T("");
err.Expr = m_sExpr;
err.Pos = m_nPos;
throw ParserError(err);
}
return bRet;
}
//---------------------------------------------------------------------------
/** \brief Check for End of expression
*/
bool TokenReader::IsEOF(ptr_tok_type &a_Tok)
{
bool bRet(false);
try
{
if (m_sExpr.length() && m_nPos >= (int)m_sExpr.length())
{
if (m_nSynFlags & noEND)
throw ecUNEXPECTED_EOF;
if (m_nNumBra > 0)
throw ecMISSING_PARENS;
if (m_nNumCurly > 0)
throw ecMISSING_CURLY_BRACKET;
if (m_nNumIndex > 0)
throw ecMISSING_SQR_BRACKET;
if (m_nNumIfElse > 0)
throw ecMISSING_ELSE_CLAUSE;
m_nSynFlags = 0;
a_Tok = ptr_tok_type(new GenericToken(cmEOE));
bRet = true;
}
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Ident = _T("");
err.Expr = m_sExpr;
err.Pos = m_nPos;
throw ParserError(err);
}
return bRet;
}
//---------------------------------------------------------------------------
/** \brief Check if a string position contains a unary infix operator.
\return true if a function token has been found false otherwise.
*/
bool TokenReader::IsInfixOpTok(ptr_tok_type &a_Tok)
{
string_type sTok;
int iEnd = ExtractToken(m_pParser->ValidInfixOprtChars(), sTok, m_nPos);
if (iEnd == m_nPos)
return false;
try
{
// iteraterate over all infix operator strings
oprt_ifx_maptype::const_iterator item = m_pInfixOprtDef->begin();
for (item = m_pInfixOprtDef->begin(); item != m_pInfixOprtDef->end(); ++item)
{
if (sTok.find(item->first) != 0)
continue;
a_Tok = ptr_tok_type(item->second->Clone());
m_nPos += (int)item->first.length();
if (m_nSynFlags & noIFX)
throw ecUNEXPECTED_OPERATOR;
m_nSynFlags = noPFX | noIFX | noOPT | noBC | noIC | noIO | noEND | noCOMMA | noNEWLINE | noIF | noELSE;
return true;
}
return false;
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Pos = m_nPos;
err.Ident = a_Tok->GetIdent();
err.Expr = m_sExpr;
throw ParserError(err);
}
}
//---------------------------------------------------------------------------
/** \brief Check expression for function tokens. */
bool TokenReader::IsFunTok(ptr_tok_type &a_Tok)
{
if (m_pFunDef->size() == 0)
return false;
string_type sTok;
int iEnd = ExtractToken(m_pParser->ValidNameChars(), sTok, m_nPos);
if (iEnd == m_nPos)
return false;
try
{
fun_maptype::iterator item = m_pFunDef->find(sTok);
if (item == m_pFunDef->end())
return false;
m_nPos = (int)iEnd;
a_Tok = ptr_tok_type(item->second->Clone());
a_Tok->Compile(_T("xxx"));
if (m_nSynFlags & noFUN)
throw ecUNEXPECTED_FUN;
m_nSynFlags = sfALLOW_NONE ^ noBO;
return true;
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Pos = m_nPos - (int)a_Tok->GetIdent().length();
err.Ident = a_Tok->GetIdent();
err.Expr = m_sExpr;
throw ParserError(err);
}
}
//---------------------------------------------------------------------------
/** \brief Check if a string position contains a unary post value operator. */
bool TokenReader::IsPostOpTok(ptr_tok_type &a_Tok)
{
if (m_nSynFlags & noPFX)
{
// <ibg 2014-05-30/> Only look for postfix operators if they are allowed at the given position.
// This will prevent conflicts with variable names such as:
// "sin(n)" where n is the postfix for "nano"
return false;
// </ibg>
}
// Tricky problem with equations like "3m+5":
// m is a postfix operator, + is a valid sign for postfix operators and
// for binary operators parser detects "m+" as operator string and
// finds no matching postfix operator.
//
// This is a special case so this routine slightly differs from the other
// token readers.
// Test if there could be a postfix operator
string_type sTok;
int iEnd = ExtractToken(m_pParser->ValidOprtChars(), sTok, m_nPos);
if (iEnd == m_nPos)
return false;
try
{
// iteraterate over all postfix operator strings
oprt_pfx_maptype::const_iterator item;
for (item = m_pPostOprtDef->begin(); item != m_pPostOprtDef->end(); ++item)
{
if (sTok.find(item->first) != 0)
continue;
a_Tok = ptr_tok_type(item->second->Clone());
m_nPos += (int)item->first.length();
if (m_nSynFlags & noPFX)
throw ecUNEXPECTED_OPERATOR;
m_nSynFlags = noVAL | noVAR | noFUN | noBO | noPFX /*| noIO*/ | noIF;
return true;
}
return false;
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Pos = m_nPos - (int)a_Tok->GetIdent().length();
err.Ident = a_Tok->GetIdent();
err.Expr = m_sExpr;
throw ParserError(err);
}
}
//---------------------------------------------------------------------------
/** \brief Check if a string position contains a binary operator. */
bool TokenReader::IsOprt(ptr_tok_type &a_Tok)
{
string_type sTok;
int iEnd = ExtractToken(m_pParser->ValidOprtChars(), sTok, m_nPos);
if (iEnd == m_nPos)
return false;
oprt_bin_maptype::reverse_iterator item;
try
{
// Note:
// All tokens in oprt_bin_maptype are have been sorted by their length
// Long operators must come first! Otherwise short names (like: "add") that
// are part of long token names (like: "add123") will be found instead
// of the long ones.
// Length sorting is done with ascending length so we use a reverse iterator here.
for (item = m_pOprtDef->rbegin(); item != m_pOprtDef->rend(); ++item)
{
if (sTok.find(item->first) != 0)
continue;
// operator found, check if we expect one...
if (m_nSynFlags & noOPT)
{
// An operator was found but is not expected to occur at
// this position of the formula, maybe it is an infix
// operator, not a binary operator. Both operator types
// can use the same characters in their identifiers.
if (IsInfixOpTok(a_Tok))
return true;
// nope, it's no infix operator and we dont expect
// an operator
throw ecUNEXPECTED_OPERATOR;
}
else
{
a_Tok = ptr_tok_type(item->second->Clone());
m_nPos += (int)a_Tok->GetIdent().length();
m_nSynFlags = noBC | noIO | noIC | noOPT | noCOMMA | noEND | noNEWLINE | noPFX | noIF | noELSE;
return true;
}
}
return false;
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Pos = m_nPos; // - (int)item->first.length();
err.Ident = item->first;
err.Expr = m_sExpr;
throw ParserError(err);
}
}
//---------------------------------------------------------------------------
/** \brief Check if a string position contains a binary operator with short cut evaluation. */
bool TokenReader::IsShortCutOprt(ptr_tok_type &a_Tok)
{
string_type sTok;
int iEnd = ExtractToken(m_pParser->ValidOprtChars(), sTok, m_nPos);
if (iEnd == m_nPos)
return false;
oprt_bin_shortcut_maptype::reverse_iterator item;
try
{
// Note:
// All tokens in oprt_bin_maptype are have been sorted by their length
// Long operators must come first! Otherwise short names (like: "add") that
// are part of long token names (like: "add123") will be found instead
// of the long ones.
// Length sorting is done with ascending length so we use a reverse iterator here.
for (item = m_pOprtShortcutDef->rbegin(); item != m_pOprtShortcutDef->rend(); ++item)
{
if (sTok.find(item->first) != 0)
continue;
// operator found, check if we expect one...
a_Tok = ptr_tok_type(item->second->Clone());
m_nPos += (int)a_Tok->GetIdent().length();
m_nSynFlags = noBC | noIO | noIC | noOPT | noCOMMA | noEND | noNEWLINE | noPFX | noIF | noELSE;
return true;
}
return false;
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Pos = m_nPos; // - (int)item->first.length();
err.Ident = item->first;
err.Expr = m_sExpr;
throw ParserError(err);
}
}
//---------------------------------------------------------------------------
/** \brief Check whether the token at a given position is a value token.
Value tokens are either values or constants.
\param a_Tok [out] If a value token is found it will be placed here.
\return true if a value token has been found.
*/
bool TokenReader::IsValTok(ptr_tok_type &a_Tok)
{
if (m_vValueReader.size() == 0)
return false;
stringstream_type stream(m_sExpr.c_str() + m_nPos);
string_type sTok;
try
{
// call the value recognition functions provided by the user
// Call user defined value recognition functions
int iSize = (int)m_vValueReader.size();
Value val;
for (int i = 0; i < iSize; ++i)
{
int iStart = m_nPos;
if (m_vValueReader[i]->IsValue(m_sExpr.c_str(), m_nPos, val))
{
sTok.assign(m_sExpr.c_str(), iStart, m_nPos);
if (m_nSynFlags & noVAL)
throw ecUNEXPECTED_VAL;
m_nSynFlags = noVAL | noVAR | noFUN | noBO | noIFX | noIO;
a_Tok = ptr_tok_type(val.Clone());
a_Tok->SetIdent(string_type(sTok.begin(), sTok.begin() + (m_nPos - iStart)));
return true;
}
}
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Pos = m_nPos;
err.Ident = sTok;
err.Expr = m_sExpr;
err.Pos = m_nPos - (int)sTok.length();
throw ParserError(err);
}
return false;
}
//---------------------------------------------------------------------------
/** \brief Check wheter a token at a given position is a variable token.
\param a_Tok [out] If a variable token has been found it will be placed here.
\return true if a variable token has been found.
*/
bool TokenReader::IsVarOrConstTok(ptr_tok_type &a_Tok)
{
if (!m_pVarDef->size() && !m_pConstDef->size() && !m_pFunDef->size())
return false;
string_type sTok;
int iEnd;
try
{
iEnd = ExtractToken(m_pParser->ValidNameChars(), sTok, m_nPos);
if (iEnd == m_nPos || (sTok.size() > 0 && sTok[0] >= _T('0') && sTok[0] <= _T('9')))
return false;
// Check for variables
var_maptype::const_iterator item = m_pVarDef->find(sTok);
if (item != m_pVarDef->end())
{
if (m_nSynFlags & noVAR)
throw ecUNEXPECTED_VAR;
m_nPos = iEnd;
m_nSynFlags = noVAL | noVAR | noFUN | noBO | noIFX;
a_Tok = ptr_tok_type(item->second->Clone());
a_Tok->SetIdent(sTok);
m_UsedVar[item->first] = item->second; // Add variable to used-var-list
return true;
}
// Check for constants
item = m_pConstDef->find(sTok);
if (item != m_pConstDef->end())
{
if (m_nSynFlags & noVAL)
throw ecUNEXPECTED_VAL;
m_nPos = iEnd;
m_nSynFlags = noVAL | noVAR | noFUN | noBO | noIFX | noIO;
a_Tok = ptr_tok_type(item->second->Clone());
a_Tok->SetIdent(sTok);
return true;
}
}
catch (EErrorCodes e)
{
ErrorContext err;
err.Errc = e;
err.Pos = m_nPos;
err.Ident = sTok;
err.Expr = m_sExpr;
throw ParserError(err);
}
return false;
}
//---------------------------------------------------------------------------
bool TokenReader::IsComment()
{
return false;
}
//---------------------------------------------------------------------------
/** \brief Check wheter a token at a given position is an undefined variable.
\param a_Tok [out] If a variable tom_pParser->m_vStringBufken has been found it will be placed here.
\return true if a variable token has been found.
\throw nothrow
*/
bool TokenReader::IsUndefVarTok(ptr_tok_type &a_Tok)
{
string_type sTok;
int iEnd = ExtractToken(m_pParser->ValidNameChars(), sTok, m_nPos);
if (iEnd == m_nPos || (sTok.size() > 0 && sTok[0] >= _T('0') && sTok[0] <= _T('9')))
return false;
if (m_nSynFlags & noVAR)
{
ErrorContext err;
err.Errc = ecUNEXPECTED_VAR;
err.Ident = sTok;
err.Expr = m_sExpr;
err.Pos = m_nPos;
throw ParserError(err);
}
// Create a variable token
if (m_pParser->m_bAutoCreateVar)
{
ptr_val_type val(new Value); // Create new value token
m_pDynVarShadowValues->push_back(val); // push to the vector of shadow values
a_Tok = ptr_tok_type(new Variable(val.Get())); // bind variable to the new value item
(*m_pVarDef)[sTok] = a_Tok; // add new variable to the variable list
}
else
a_Tok = ptr_tok_type(new Variable(nullptr)); // bind variable to empty variable
a_Tok->SetIdent(sTok);
m_UsedVar[sTok] = a_Tok; // add new variable to used-var-list
m_nPos = iEnd;
m_nSynFlags = noVAL | noVAR | noFUN | noBO | noIFX;
return true;
}
} // namespace mu
|