cmExprParserHelper.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "cmSystemTools.h"
  12. #include "cmExprLexer.h"
  13. #include "cmMakefile.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. void cmExprParserHelper::SetLineFile(long line, const char* file)
  26. {
  27. this->FileLine = line;
  28. this->FileName = file;
  29. }
  30. int cmExprParserHelper::ParseString(const char* str, int verb)
  31. {
  32. if ( !str)
  33. {
  34. return 0;
  35. }
  36. //printf("Do some parsing: %s\n", str);
  37. this->Verbose = verb;
  38. this->InputBuffer = str;
  39. this->InputBufferPos = 0;
  40. this->CurrentLine = 0;
  41. this->Result = 0;
  42. yyscan_t yyscanner;
  43. cmExpr_yylex_init(&yyscanner);
  44. cmExpr_yyset_extra(this, yyscanner);
  45. int res = cmExpr_yyparse(yyscanner);
  46. cmExpr_yylex_destroy(yyscanner);
  47. if ( res != 0 )
  48. {
  49. //str << "CAL_Parser returned: " << res << std::endl;
  50. //std::cerr << "When parsing: [" << str << "]" << std::endl;
  51. return 0;
  52. }
  53. this->CleanupParser();
  54. if ( Verbose )
  55. {
  56. std::cerr << "Expanding [" << str << "] produced: ["
  57. << this->Result << "]" << std::endl;
  58. }
  59. return 1;
  60. }
  61. void cmExprParserHelper::CleanupParser()
  62. {
  63. }
  64. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  65. {
  66. //std::cout << "JPLexInput ";
  67. //std::cout.write(buf, maxlen);
  68. //std::cout << std::endl;
  69. if ( maxlen < 1 )
  70. {
  71. return 0;
  72. }
  73. if ( this->InputBufferPos < this->InputBuffer.size() )
  74. {
  75. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  76. if ( buf[0] == '\n' )
  77. {
  78. this->CurrentLine ++;
  79. }
  80. return(1);
  81. }
  82. else
  83. {
  84. buf[0] = '\n';
  85. return( 0 );
  86. }
  87. }
  88. void cmExprParserHelper::Error(const char* str)
  89. {
  90. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  91. cmOStringStream ostr;
  92. ostr << str << " (" << pos << ")";
  93. this->ErrorString = ostr.str();
  94. }
  95. void cmExprParserHelper::SetResult(int value)
  96. {
  97. this->Result = value;
  98. }