cmExprParserHelper.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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()
  18. {
  19. this->CleanupParser();
  20. }
  21. int cmExprParserHelper::ParseString(const char* str, int verb)
  22. {
  23. if (!str) {
  24. return 0;
  25. }
  26. // printf("Do some parsing: %s\n", str);
  27. this->Verbose = verb;
  28. this->InputBuffer = str;
  29. this->InputBufferPos = 0;
  30. this->CurrentLine = 0;
  31. this->Result = 0;
  32. yyscan_t yyscanner;
  33. cmExpr_yylex_init(&yyscanner);
  34. cmExpr_yyset_extra(this, yyscanner);
  35. int res;
  36. try {
  37. res = cmExpr_yyparse(yyscanner);
  38. if (res != 0) {
  39. std::string e = "cannot parse the expression: \"" + InputBuffer + "\": ";
  40. e += ErrorString;
  41. e += ".";
  42. this->SetError(std::move(e));
  43. }
  44. } catch (std::runtime_error const& fail) {
  45. std::string e =
  46. "cannot evaluate the expression: \"" + InputBuffer + "\": ";
  47. e += fail.what();
  48. e += ".";
  49. this->SetError(std::move(e));
  50. res = 1;
  51. } catch (std::out_of_range const&) {
  52. std::string e = "cannot evaluate the expression: \"" + InputBuffer +
  53. "\": a numeric value is out of range.";
  54. this->SetError(std::move(e));
  55. res = 1;
  56. } catch (...) {
  57. std::string e = "cannot parse the expression: \"" + InputBuffer + "\".";
  58. this->SetError(std::move(e));
  59. res = 1;
  60. }
  61. cmExpr_yylex_destroy(yyscanner);
  62. if (res != 0) {
  63. // str << "CAL_Parser returned: " << res << std::endl;
  64. // std::cerr << "When parsing: [" << str << "]" << std::endl;
  65. return 0;
  66. }
  67. this->CleanupParser();
  68. if (Verbose) {
  69. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  70. << std::endl;
  71. }
  72. return 1;
  73. }
  74. void cmExprParserHelper::CleanupParser()
  75. {
  76. }
  77. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  78. {
  79. // std::cout << "JPLexInput ";
  80. // std::cout.write(buf, maxlen);
  81. // std::cout << std::endl;
  82. if (maxlen < 1) {
  83. return 0;
  84. }
  85. if (this->InputBufferPos < this->InputBuffer.size()) {
  86. buf[0] = this->InputBuffer[this->InputBufferPos++];
  87. if (buf[0] == '\n') {
  88. this->CurrentLine++;
  89. }
  90. return (1);
  91. }
  92. buf[0] = '\n';
  93. return (0);
  94. }
  95. void cmExprParserHelper::Error(const char* str)
  96. {
  97. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  98. std::ostringstream ostr;
  99. ostr << str << " (" << pos << ")";
  100. this->ErrorString = ostr.str();
  101. }
  102. void cmExprParserHelper::SetResult(KWIML_INT_int64_t value)
  103. {
  104. this->Result = value;
  105. }
  106. void cmExprParserHelper::SetError(std::string errorString)
  107. {
  108. this->ErrorString = std::move(errorString);
  109. }