cmExprParserHelper.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmExprParserHelper.h"
  14. #include "cmSystemTools.h"
  15. #include "cmExprLexer.h"
  16. #include "cmMakefile.h"
  17. int cmExpr_yyparse( yyscan_t yyscanner );
  18. //
  19. cmExprParserHelper::cmExprParserHelper()
  20. {
  21. this->FileLine = -1;
  22. this->FileName = 0;
  23. }
  24. cmExprParserHelper::~cmExprParserHelper()
  25. {
  26. this->CleanupParser();
  27. }
  28. void cmExprParserHelper::SetLineFile(long line, const char* file)
  29. {
  30. this->FileLine = line;
  31. this->FileName = file;
  32. }
  33. int cmExprParserHelper::ParseString(const char* str, int verb)
  34. {
  35. if ( !str)
  36. {
  37. return 0;
  38. }
  39. //printf("Do some parsing: %s\n", str);
  40. this->Verbose = verb;
  41. this->InputBuffer = str;
  42. this->InputBufferPos = 0;
  43. this->CurrentLine = 0;
  44. this->Result = 0;
  45. yyscan_t yyscanner;
  46. cmExpr_yylex_init(&yyscanner);
  47. cmExpr_yyset_extra(this, yyscanner);
  48. int res = cmExpr_yyparse(yyscanner);
  49. cmExpr_yylex_destroy(yyscanner);
  50. if ( res != 0 )
  51. {
  52. //str << "CAL_Parser returned: " << res << std::endl;
  53. //std::cerr << "When parsing: [" << str << "]" << std::endl;
  54. return 0;
  55. }
  56. this->CleanupParser();
  57. if ( Verbose )
  58. {
  59. std::cerr << "Expanding [" << str << "] produced: ["
  60. << this->Result << "]" << std::endl;
  61. }
  62. return 1;
  63. }
  64. void cmExprParserHelper::CleanupParser()
  65. {
  66. }
  67. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  68. {
  69. //std::cout << "JPLexInput ";
  70. //std::cout.write(buf, maxlen);
  71. //std::cout << std::endl;
  72. if ( maxlen < 1 )
  73. {
  74. return 0;
  75. }
  76. if ( this->InputBufferPos < this->InputBuffer.size() )
  77. {
  78. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  79. if ( buf[0] == '\n' )
  80. {
  81. this->CurrentLine ++;
  82. }
  83. return(1);
  84. }
  85. else
  86. {
  87. buf[0] = '\n';
  88. return( 0 );
  89. }
  90. }
  91. void cmExprParserHelper::Error(const char* str)
  92. {
  93. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  94. cmOStringStream ostr;
  95. ostr << str << " (" << pos << ")";
  96. this->ErrorString = ostr.str();
  97. }
  98. void cmExprParserHelper::SetResult(int value)
  99. {
  100. this->Result = value;
  101. }