cmExprParserHelper.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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: [" << this->Result << "]" << std::endl;
  60. }
  61. return 1;
  62. }
  63. void cmExprParserHelper::CleanupParser()
  64. {
  65. }
  66. int cmExprParserHelper::LexInput(char* buf, int maxlen)
  67. {
  68. //std::cout << "JPLexInput ";
  69. //std::cout.write(buf, maxlen);
  70. //std::cout << std::endl;
  71. if ( maxlen < 1 )
  72. {
  73. return 0;
  74. }
  75. if ( this->InputBufferPos < this->InputBuffer.size() )
  76. {
  77. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  78. if ( buf[0] == '\n' )
  79. {
  80. this->CurrentLine ++;
  81. }
  82. return(1);
  83. }
  84. else
  85. {
  86. buf[0] = '\n';
  87. return( 0 );
  88. }
  89. }
  90. void cmExprParserHelper::Error(const char* str)
  91. {
  92. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  93. //fprintf(stderr, "Argument Parser Error: %s (%lu / Line: %d)\n", str, pos, this->CurrentLine);
  94. cmOStringStream ostr;
  95. ostr << str << " (" << pos << ")";
  96. /*
  97. int cc;
  98. std::cerr << "String: [";
  99. for ( cc = 0; cc < 30 && *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
  100. cc ++ )
  101. {
  102. std::cerr << *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
  103. }
  104. std::cerr << "]" << std::endl;
  105. */
  106. this->ErrorString = ostr.str();
  107. }
  108. void cmExprParserHelper::SetResult(int value)
  109. {
  110. this->Result = value;
  111. }