1
0

cmExprParserHelper.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmExprParserHelper.h"
  4. #include "cmExprLexer.h"
  5. #include <iostream>
  6. #include <sstream>
  7. #include <stdexcept>
  8. #include <utility>
  9. int cmExpr_yyparse(yyscan_t yyscanner);
  10. //
  11. cmExprParserHelper::cmExprParserHelper()
  12. {
  13. this->FileLine = -1;
  14. this->FileName = nullptr;
  15. this->Result = 0;
  16. }
  17. cmExprParserHelper::~cmExprParserHelper() = default;
  18. int cmExprParserHelper::ParseString(const char* str, int verb)
  19. {
  20. if (!str) {
  21. return 0;
  22. }
  23. // printf("Do some parsing: %s\n", str);
  24. this->Verbose = verb;
  25. this->InputBuffer = str;
  26. this->InputBufferPos = 0;
  27. this->CurrentLine = 0;
  28. this->Result = 0;
  29. yyscan_t yyscanner;
  30. cmExpr_yylex_init(&yyscanner);
  31. cmExpr_yyset_extra(this, yyscanner);
  32. try {
  33. int res = cmExpr_yyparse(yyscanner);
  34. if (res != 0) {
  35. std::string e = "cannot parse the expression: \"" + InputBuffer + "\": ";
  36. e += ErrorString;
  37. e += ".";
  38. this->SetError(std::move(e));
  39. }
  40. } catch (std::runtime_error const& fail) {
  41. std::string e =
  42. "cannot evaluate the expression: \"" + InputBuffer + "\": ";
  43. e += fail.what();
  44. e += ".";
  45. this->SetError(std::move(e));
  46. } catch (std::out_of_range const&) {
  47. std::string e = "cannot evaluate the expression: \"" + InputBuffer +
  48. "\": a numeric value is out of range.";
  49. this->SetError(std::move(e));
  50. } catch (...) {
  51. std::string e = "cannot parse the expression: \"" + InputBuffer + "\".";
  52. this->SetError(std::move(e));
  53. }
  54. cmExpr_yylex_destroy(yyscanner);
  55. if (!this->ErrorString.empty()) {
  56. return 0;
  57. }
  58. if (Verbose) {
  59. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  60. << std::endl;
  61. }
  62. return 1;
  63. }
  64. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  65. {
  66. // std::cout << "JPLexInput ";
  67. // std::cout.write(buf, maxlen);
  68. // std::cout << std::endl;
  69. if (maxlen < 1) {
  70. return 0;
  71. }
  72. if (this->InputBufferPos < this->InputBuffer.size()) {
  73. buf[0] = this->InputBuffer[this->InputBufferPos++];
  74. if (buf[0] == '\n') {
  75. this->CurrentLine++;
  76. }
  77. return (1);
  78. }
  79. buf[0] = '\n';
  80. return (0);
  81. }
  82. void cmExprParserHelper::Error(const char* str)
  83. {
  84. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  85. std::ostringstream ostr;
  86. ostr << str << " (" << pos << ")";
  87. this->ErrorString = ostr.str();
  88. }
  89. void cmExprParserHelper::UnexpectedChar(char c)
  90. {
  91. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  92. std::ostringstream ostr;
  93. ostr << "Unexpected character in expression at position " << pos << ": " << c
  94. << "\n";
  95. this->WarningString += ostr.str();
  96. }
  97. void cmExprParserHelper::SetResult(KWIML_INT_int64_t value)
  98. {
  99. this->Result = value;
  100. }
  101. void cmExprParserHelper::SetError(std::string errorString)
  102. {
  103. this->ErrorString = std::move(errorString);
  104. }