cmExprParserHelper.cxx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. int cmExpr_yyparse(yyscan_t yyscanner);
  8. //
  9. cmExprParserHelper::cmExprParserHelper()
  10. {
  11. this->FileLine = -1;
  12. this->FileName = nullptr;
  13. }
  14. cmExprParserHelper::~cmExprParserHelper()
  15. {
  16. this->CleanupParser();
  17. }
  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. int res = cmExpr_yyparse(yyscanner);
  33. cmExpr_yylex_destroy(yyscanner);
  34. if (res != 0) {
  35. // str << "CAL_Parser returned: " << res << std::endl;
  36. // std::cerr << "When parsing: [" << str << "]" << std::endl;
  37. return 0;
  38. }
  39. this->CleanupParser();
  40. if (Verbose) {
  41. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  42. << std::endl;
  43. }
  44. return 1;
  45. }
  46. void cmExprParserHelper::CleanupParser()
  47. {
  48. }
  49. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  50. {
  51. // std::cout << "JPLexInput ";
  52. // std::cout.write(buf, maxlen);
  53. // std::cout << std::endl;
  54. if (maxlen < 1) {
  55. return 0;
  56. }
  57. if (this->InputBufferPos < this->InputBuffer.size()) {
  58. buf[0] = this->InputBuffer[this->InputBufferPos++];
  59. if (buf[0] == '\n') {
  60. this->CurrentLine++;
  61. }
  62. return (1);
  63. }
  64. buf[0] = '\n';
  65. return (0);
  66. }
  67. void cmExprParserHelper::Error(const char* str)
  68. {
  69. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  70. std::ostringstream ostr;
  71. ostr << str << " (" << pos << ")";
  72. this->ErrorString = ostr.str();
  73. }
  74. void cmExprParserHelper::SetResult(int value)
  75. {
  76. this->Result = value;
  77. }