Merge branch 'release-3.29'
[kiteware-cmake.git] / Source / cmExprParserHelper.cxx
blobcc8b8b7e873ebeb558875fa11244c5aaca47e1b3
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"
5 #include <iostream>
6 #include <sstream>
7 #include <stdexcept>
8 #include <utility>
10 #include "cmExprLexer.h"
11 #include "cmStringAlgorithms.h"
13 int cmExpr_yyparse(yyscan_t yyscanner);
15 cmExprParserHelper::cmExprParserHelper()
17 this->FileLine = -1;
18 this->FileName = nullptr;
19 this->Result = 0;
22 cmExprParserHelper::~cmExprParserHelper() = default;
24 int cmExprParserHelper::ParseString(const char* str, int verb)
26 if (!str) {
27 return 0;
29 // printf("Do some parsing: %s\n", str);
31 this->Verbose = verb;
32 this->InputBuffer = str;
33 this->InputBufferPos = 0;
34 this->CurrentLine = 0;
36 this->Result = 0;
38 yyscan_t yyscanner;
39 cmExpr_yylex_init(&yyscanner);
40 cmExpr_yyset_extra(this, yyscanner);
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));
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));
63 cmExpr_yylex_destroy(yyscanner);
64 if (!this->ErrorString.empty()) {
65 return 0;
68 if (this->Verbose) {
69 std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
70 << std::endl;
72 return 1;
75 int cmExprParserHelper::LexInput(char* buf, int maxlen)
77 // std::cout << "JPLexInput ";
78 // std::cout.write(buf, maxlen);
79 // std::cout << std::endl;
80 if (maxlen < 1) {
81 return 0;
83 if (this->InputBufferPos < this->InputBuffer.size()) {
84 buf[0] = this->InputBuffer[this->InputBufferPos++];
85 if (buf[0] == '\n') {
86 this->CurrentLine++;
88 return (1);
90 buf[0] = '\n';
91 return (0);
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
107 << "\n";
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);