Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmExprParserHelper.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmExprParserHelper.h"
13
14 #include "cmSystemTools.h"
15 #include "cmExprLexer.h"
16
17 #include "cmMakefile.h"
18
19 int cmExpr_yyparse( yyscan_t yyscanner );
20 //
21 cmExprParserHelper::cmExprParserHelper()
22 {
23   this->FileLine = -1;
24   this->FileName = 0;
25 }
26
27
28 cmExprParserHelper::~cmExprParserHelper()
29 {
30   this->CleanupParser();
31 }
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
41   this->Verbose = verb;
42   this->InputBuffer = str;
43   this->InputBufferPos = 0;
44   this->CurrentLine = 0;
45   
46   this->Result = 0;
47
48   yyscan_t yyscanner;
49   cmExpr_yylex_init(&yyscanner);
50   cmExpr_yyset_extra(this, yyscanner);
51   int res = cmExpr_yyparse(yyscanner);
52   cmExpr_yylex_destroy(yyscanner);
53   if ( res != 0 )
54     {
55     //str << "CAL_Parser returned: " << res << std::endl;
56     //std::cerr << "When parsing: [" << str << "]" << std::endl;
57     return 0;
58     }
59
60   this->CleanupParser();
61
62   if ( Verbose )
63     {
64     std::cerr << "Expanding [" << str << "] produced: [" 
65               << this->Result << "]" << std::endl;
66     }
67   return 1;
68 }
69
70 void cmExprParserHelper::CleanupParser()
71 {
72 }
73
74 int cmExprParserHelper::LexInput(char* buf, int maxlen)
75 {
76   //std::cout << "JPLexInput ";
77   //std::cout.write(buf, maxlen);
78   //std::cout << std::endl;
79   if ( maxlen < 1 )
80     {
81     return 0;
82     }
83   if ( this->InputBufferPos < this->InputBuffer.size() )
84     {
85     buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
86     if ( buf[0] == '\n' )
87       {
88       this->CurrentLine ++;
89       }
90     return(1);
91     }
92   else
93     {
94     buf[0] = '\n';
95     return( 0 );
96     }
97 }
98
99 void cmExprParserHelper::Error(const char* str)
100 {
101   unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
102   cmOStringStream ostr;
103   ostr << str << " (" << pos << ")";
104   this->ErrorString = ostr.str();
105 }
106
107 void cmExprParserHelper::SetResult(int value)
108 {
109   this->Result = value;
110 }
111
112