cmExprParserHelper.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = 0;
  20. }
  21. cmExprParserHelper::~cmExprParserHelper()
  22. {
  23. this->CleanupParser();
  24. }
  25. int cmExprParserHelper::ParseString(const char* str, int verb)
  26. {
  27. if ( !str)
  28. {
  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. {
  44. //str << "CAL_Parser returned: " << res << std::endl;
  45. //std::cerr << "When parsing: [" << str << "]" << std::endl;
  46. return 0;
  47. }
  48. this->CleanupParser();
  49. if ( Verbose )
  50. {
  51. std::cerr << "Expanding [" << str << "] produced: ["
  52. << this->Result << "]" << std::endl;
  53. }
  54. return 1;
  55. }
  56. void cmExprParserHelper::CleanupParser()
  57. {
  58. }
  59. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  60. {
  61. //std::cout << "JPLexInput ";
  62. //std::cout.write(buf, maxlen);
  63. //std::cout << std::endl;
  64. if ( maxlen < 1 )
  65. {
  66. return 0;
  67. }
  68. if ( this->InputBufferPos < this->InputBuffer.size() )
  69. {
  70. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  71. if ( buf[0] == '\n' )
  72. {
  73. this->CurrentLine ++;
  74. }
  75. return(1);
  76. }
  77. else
  78. {
  79. buf[0] = '\n';
  80. return( 0 );
  81. }
  82. }
  83. void cmExprParserHelper::Error(const char* str)
  84. {
  85. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  86. std::ostringstream ostr;
  87. ostr << str << " (" << pos << ")";
  88. this->ErrorString = ostr.str();
  89. }
  90. void cmExprParserHelper::SetResult(int value)
  91. {
  92. this->Result = value;
  93. }