cmExprParserHelper.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "cmMakefile.h"
  12. #include "cmSystemTools.h"
  13. #include "cmExprLexer.h"
  14. int cmExpr_yyparse(yyscan_t yyscanner);
  15. //
  16. cmExprParserHelper::cmExprParserHelper()
  17. {
  18. this->FileLine = -1;
  19. this->FileName = CM_NULLPTR;
  20. }
  21. cmExprParserHelper::~cmExprParserHelper()
  22. {
  23. this->CleanupParser();
  24. }
  25. int cmExprParserHelper::ParseString(const char* str, int verb)
  26. {
  27. if (!str) {
  28. return 0;
  29. }
  30. // printf("Do some parsing: %s\n", str);
  31. this->Verbose = verb;
  32. this->InputBuffer = str;
  33. this->InputBufferPos = 0;
  34. this->CurrentLine = 0;
  35. this->Result = 0;
  36. yyscan_t yyscanner;
  37. cmExpr_yylex_init(&yyscanner);
  38. cmExpr_yyset_extra(this, yyscanner);
  39. int res = cmExpr_yyparse(yyscanner);
  40. cmExpr_yylex_destroy(yyscanner);
  41. if (res != 0) {
  42. // str << "CAL_Parser returned: " << res << std::endl;
  43. // std::cerr << "When parsing: [" << str << "]" << std::endl;
  44. return 0;
  45. }
  46. this->CleanupParser();
  47. if (Verbose) {
  48. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  49. << std::endl;
  50. }
  51. return 1;
  52. }
  53. void cmExprParserHelper::CleanupParser()
  54. {
  55. }
  56. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  57. {
  58. // std::cout << "JPLexInput ";
  59. // std::cout.write(buf, maxlen);
  60. // std::cout << std::endl;
  61. if (maxlen < 1) {
  62. return 0;
  63. }
  64. if (this->InputBufferPos < this->InputBuffer.size()) {
  65. buf[0] = this->InputBuffer[this->InputBufferPos++];
  66. if (buf[0] == '\n') {
  67. this->CurrentLine++;
  68. }
  69. return (1);
  70. } else {
  71. buf[0] = '\n';
  72. return (0);
  73. }
  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. }