1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmExprParserHelper.h"
4 
5 #include <iostream>
6 #include <sstream>
7 #include <stdexcept>
8 #include <utility>
9 
10 #include "cmExprLexer.h"
11 #include "cmStringAlgorithms.h"
12 
13 int cmExpr_yyparse(yyscan_t yyscanner);
14 //
cmExprParserHelper()15 cmExprParserHelper::cmExprParserHelper()
16 {
17   this->FileLine = -1;
18   this->FileName = nullptr;
19   this->Result = 0;
20 }
21 
22 cmExprParserHelper::~cmExprParserHelper() = default;
23 
ParseString(const char * str,int verb)24 int cmExprParserHelper::ParseString(const char* str, int verb)
25 {
26   if (!str) {
27     return 0;
28   }
29   // printf("Do some parsing: %s\n", str);
30 
31   this->Verbose = verb;
32   this->InputBuffer = str;
33   this->InputBufferPos = 0;
34   this->CurrentLine = 0;
35 
36   this->Result = 0;
37 
38   yyscan_t yyscanner;
39   cmExpr_yylex_init(&yyscanner);
40   cmExpr_yyset_extra(this, yyscanner);
41 
42   try {
43     int res = cmExpr_yyparse(yyscanner);
44     if (res != 0) {
45       std::string e =
46         cmStrCat("cannot parse the expression: \"", this->InputBuffer,
47                  "\": ", this->ErrorString, '.');
48       this->SetError(std::move(e));
49     }
50   } catch (std::runtime_error const& fail) {
51     std::string e = cmStrCat("cannot evaluate the expression: \"",
52                              this->InputBuffer, "\": ", fail.what(), '.');
53     this->SetError(std::move(e));
54   } catch (std::out_of_range const&) {
55     std::string e = "cannot evaluate the expression: \"" + this->InputBuffer +
56       "\": a numeric value is out of range.";
57     this->SetError(std::move(e));
58   } catch (...) {
59     std::string e =
60       "cannot parse the expression: \"" + this->InputBuffer + "\".";
61     this->SetError(std::move(e));
62   }
63   cmExpr_yylex_destroy(yyscanner);
64   if (!this->ErrorString.empty()) {
65     return 0;
66   }
67 
68   if (this->Verbose) {
69     std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
70               << std::endl;
71   }
72   return 1;
73 }
74 
LexInput(char * buf,int maxlen)75 int cmExprParserHelper::LexInput(char* buf, int maxlen)
76 {
77   // std::cout << "JPLexInput ";
78   // std::cout.write(buf, maxlen);
79   // std::cout << std::endl;
80   if (maxlen < 1) {
81     return 0;
82   }
83   if (this->InputBufferPos < this->InputBuffer.size()) {
84     buf[0] = this->InputBuffer[this->InputBufferPos++];
85     if (buf[0] == '\n') {
86       this->CurrentLine++;
87     }
88     return (1);
89   }
90   buf[0] = '\n';
91   return (0);
92 }
93 
Error(const char * str)94 void cmExprParserHelper::Error(const char* str)
95 {
96   unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
97   std::ostringstream ostr;
98   ostr << str << " (" << pos << ")";
99   this->ErrorString = ostr.str();
100 }
101 
UnexpectedChar(char c)102 void cmExprParserHelper::UnexpectedChar(char c)
103 {
104   unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
105   std::ostringstream ostr;
106   ostr << "Unexpected character in expression at position " << pos << ": " << c
107        << "\n";
108   this->WarningString += ostr.str();
109 }
110 
SetResult(KWIML_INT_int64_t value)111 void cmExprParserHelper::SetResult(KWIML_INT_int64_t value)
112 {
113   this->Result = value;
114 }
115 
SetError(std::string errorString)116 void cmExprParserHelper::SetError(std::string errorString)
117 {
118   this->ErrorString = std::move(errorString);
119 }
120