cmExprParserHelper.cxx 3.0 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 <iostream>
  5. #include <sstream>
  6. #include <stdexcept>
  7. #include <utility>
  8. #include "cmExprLexer.h"
  9. #include "cmStringAlgorithms.h"
  10. int cmExpr_yyparse(yyscan_t yyscanner);
  11. //
  12. cmExprParserHelper::cmExprParserHelper()
  13. {
  14. this->FileLine = -1;
  15. this->FileName = nullptr;
  16. this->Result = 0;
  17. }
  18. cmExprParserHelper::~cmExprParserHelper() = default;
  19. int cmExprParserHelper::ParseString(const char* str, int verb)
  20. {
  21. if (!str) {
  22. return 0;
  23. }
  24. // printf("Do some parsing: %s\n", str);
  25. this->Verbose = verb;
  26. this->InputBuffer = str;
  27. this->InputBufferPos = 0;
  28. this->CurrentLine = 0;
  29. this->Result = 0;
  30. yyscan_t yyscanner;
  31. cmExpr_yylex_init(&yyscanner);
  32. cmExpr_yyset_extra(this, yyscanner);
  33. try {
  34. int res = cmExpr_yyparse(yyscanner);
  35. if (res != 0) {
  36. std::string e =
  37. cmStrCat("cannot parse the expression: \"", this->InputBuffer,
  38. "\": ", this->ErrorString, '.');
  39. this->SetError(std::move(e));
  40. }
  41. } catch (std::runtime_error const& fail) {
  42. std::string e = cmStrCat("cannot evaluate the expression: \"",
  43. this->InputBuffer, "\": ", fail.what(), '.');
  44. this->SetError(std::move(e));
  45. } catch (std::out_of_range const&) {
  46. std::string e = "cannot evaluate the expression: \"" + this->InputBuffer +
  47. "\": a numeric value is out of range.";
  48. this->SetError(std::move(e));
  49. } catch (...) {
  50. std::string e =
  51. "cannot parse the expression: \"" + this->InputBuffer + "\".";
  52. this->SetError(std::move(e));
  53. }
  54. cmExpr_yylex_destroy(yyscanner);
  55. if (!this->ErrorString.empty()) {
  56. return 0;
  57. }
  58. if (this->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. }