cmExprParserHelper.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmExprParserHelper.h"
  11. #include <cmConfigure.h>
  12. #include "cmExprLexer.h"
  13. #include <iostream>
  14. #include <sstream>
  15. int cmExpr_yyparse(yyscan_t yyscanner);
  16. //
  17. cmExprParserHelper::cmExprParserHelper()
  18. {
  19. this->FileLine = -1;
  20. this->FileName = CM_NULLPTR;
  21. }
  22. cmExprParserHelper::~cmExprParserHelper()
  23. {
  24. this->CleanupParser();
  25. }
  26. int cmExprParserHelper::ParseString(const char* str, int verb)
  27. {
  28. if (!str) {
  29. return 0;
  30. }
  31. // printf("Do some parsing: %s\n", str);
  32. this->Verbose = verb;
  33. this->InputBuffer = str;
  34. this->InputBufferPos = 0;
  35. this->CurrentLine = 0;
  36. this->Result = 0;
  37. yyscan_t yyscanner;
  38. cmExpr_yylex_init(&yyscanner);
  39. cmExpr_yyset_extra(this, yyscanner);
  40. int res = cmExpr_yyparse(yyscanner);
  41. cmExpr_yylex_destroy(yyscanner);
  42. if (res != 0) {
  43. // str << "CAL_Parser returned: " << res << std::endl;
  44. // std::cerr << "When parsing: [" << str << "]" << std::endl;
  45. return 0;
  46. }
  47. this->CleanupParser();
  48. if (Verbose) {
  49. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  50. << std::endl;
  51. }
  52. return 1;
  53. }
  54. void cmExprParserHelper::CleanupParser()
  55. {
  56. }
  57. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  58. {
  59. // std::cout << "JPLexInput ";
  60. // std::cout.write(buf, maxlen);
  61. // std::cout << std::endl;
  62. if (maxlen < 1) {
  63. return 0;
  64. }
  65. if (this->InputBufferPos < this->InputBuffer.size()) {
  66. buf[0] = this->InputBuffer[this->InputBufferPos++];
  67. if (buf[0] == '\n') {
  68. this->CurrentLine++;
  69. }
  70. return (1);
  71. }
  72. buf[0] = '\n';
  73. return (0);
  74. }
  75. void cmExprParserHelper::Error(const char* str)
  76. {
  77. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  78. std::ostringstream ostr;
  79. ostr << str << " (" << pos << ")";
  80. this->ErrorString = ostr.str();
  81. }
  82. void cmExprParserHelper::SetResult(int value)
  83. {
  84. this->Result = value;
  85. }