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"
10 #include "cmExprLexer.h"
11 #include "cmStringAlgorithms.h"
13 int cmExpr_yyparse(yyscan_t yyscanner
);
15 cmExprParserHelper::cmExprParserHelper()
18 this->FileName
= nullptr;
22 cmExprParserHelper::~cmExprParserHelper() = default;
24 int cmExprParserHelper::ParseString(const char* str
, int verb
)
29 // printf("Do some parsing: %s\n", str);
32 this->InputBuffer
= str
;
33 this->InputBufferPos
= 0;
34 this->CurrentLine
= 0;
39 cmExpr_yylex_init(&yyscanner
);
40 cmExpr_yyset_extra(this, yyscanner
);
43 int res
= cmExpr_yyparse(yyscanner
);
46 cmStrCat("cannot parse the expression: \"", this->InputBuffer
,
47 "\": ", this->ErrorString
, '.');
48 this->SetError(std::move(e
));
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
));
60 "cannot parse the expression: \"" + this->InputBuffer
+ "\".";
61 this->SetError(std::move(e
));
63 cmExpr_yylex_destroy(yyscanner
);
64 if (!this->ErrorString
.empty()) {
69 std::cerr
<< "Expanding [" << str
<< "] produced: [" << this->Result
<< "]"
75 int cmExprParserHelper::LexInput(char* buf
, int maxlen
)
77 // std::cout << "JPLexInput ";
78 // std::cout.write(buf, maxlen);
79 // std::cout << std::endl;
83 if (this->InputBufferPos
< this->InputBuffer
.size()) {
84 buf
[0] = this->InputBuffer
[this->InputBufferPos
++];
94 void cmExprParserHelper::Error(const char* str
)
96 unsigned long pos
= static_cast<unsigned long>(this->InputBufferPos
);
97 std::ostringstream ostr
;
98 ostr
<< str
<< " (" << pos
<< ")";
99 this->ErrorString
= ostr
.str();
102 void cmExprParserHelper::UnexpectedChar(char c
)
104 unsigned long pos
= static_cast<unsigned long>(this->InputBufferPos
);
105 std::ostringstream ostr
;
106 ostr
<< "Unexpected character in expression at position " << pos
<< ": " << c
108 this->WarningString
+= ostr
.str();
111 void cmExprParserHelper::SetResult(KWIML_INT_int64_t value
)
113 this->Result
= value
;
116 void cmExprParserHelper::SetError(std::string errorString
)
118 this->ErrorString
= std::move(errorString
);