]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4088fd] Fixed most of C++ problems trac4088fd_cpp
authorFrancis Dupont <fdupont@isc.org>
Fri, 6 Nov 2015 06:29:41 +0000 (07:29 +0100)
committerFrancis Dupont <fdupont@isc.org>
Fri, 6 Nov 2015 06:29:41 +0000 (07:29 +0100)
src/lib/eval/eval_context.cc
src/lib/eval/eval_context.h
src/lib/eval/lexer.cc
src/lib/eval/lexer.ll
src/lib/eval/tests/context_unittest.cc

index a22e18c72ddb153bf68f53160cafad98f451b2a2..9c6a9ddd7267ae7a14d5b8d3c294c7e19f02750a 100644 (file)
@@ -26,46 +26,39 @@ EvalContext::~EvalContext()
 {
 }
 
-int
+bool
 EvalContext::parseFile(const std::string &filename)
 {
     file_ = filename;
-    scanBegin();
+    scanFileBegin();
     isc::eval::EvalParser parser(*this);
     parser.set_debug_level(trace_parsing_);
     int res = parser.parse();
-    scanEnd();
-    return res;
+    scanFileEnd();
+    return (res == 0);
 }
 
-int
+bool
 EvalContext::parseString(const std::string& str)
 {
-    /// @todo: Is there any way for the parser to read from a stream,
-    /// rather than a file? It would be better to use stringstream,
-    /// but it seems that the lexer operates on FILE* interface.
-
-    // Put the content into a file and then open that file for reading.
-    remove("/tmp/eval");
-    std::fstream f("/tmp/eval", std::ios::out);
-
-    if (!f.good()) {
-        isc_throw(isc::Unexpected, "Can't write /tmp/eval file");
-    }
-    f << str;
-    f.close();
-
-    return (parseFile("/tmp/eval"));
+    file_ = "<string>";
+    string_ = str;
+    scanStringBegin();
+    isc::eval::EvalParser parser(*this);
+    parser.set_debug_level(trace_parsing_);
+    int res = parser.parse();
+    scanStringEnd();
+    return (res == 0);
 }
 
 void
-EvalContext::error(const isc::eval::location& l, const std::string& m)
+EvalContext::error(const isc::eval::location& loc, const std::string& what)
 {
-    isc_throw(EvalError, l << ": " << m);
+    isc_throw(EvalParseError, loc << ": " << what);
 }
 
 void
-EvalContext::error (const std::string& m)
+EvalContext::error (const std::string& what)
 {
-    isc_throw(EvalError, m);
+    isc_throw(EvalParseError, what);
 }
index 401c46b13b3491f3452bad4b9159fd3852175fc2..3cb31eddcf586c642848526031ee82e933754908 100644 (file)
@@ -30,9 +30,9 @@ namespace isc {
 namespace eval {
 
 /// @brief Evaluation error exception raised when trying to parse an axceptions.
-class EvalError : public isc::Exception {
+class EvalParseError : public isc::Exception {
 public:
-    EvalError(const char* file, size_t line, const char* what) :
+    EvalParseError(const char* file, size_t line, const char* what) :
         isc::Exception(file, line, what) { };
 };
 
@@ -50,32 +50,42 @@ public:
     /// @brief Parsed expression (output tokens are stored here)
     isc::dhcp::Expression expression;
 
-    /// @brief Method called before scanning starts.
-    void scanBegin();
+    /// @brief Method called before scanning starts on a file.
+    void scanFileBegin();
 
-    /// @brief Method called after the last tokens are scanned.
-    void scanEnd();
+    /// @brief Method called after the last tokens are scanned from a file.
+    void scanFileEnd();
+    
+    /// @brief Method called before scanning starts on a string.
+    void scanStringBegin();
+
+    /// @brief Method called after the last tokens are scanned from a string.
+    void scanStringEnd();
     
     /// @brief Runs the parser on specified file.
     ///
     /// @param filename
-    /// Return 0 on success.
-    int parseFile(const std::string& filename);
+    /// @return true on success.
+    bool parseFile(const std::string& filename);
 
     /// @brief Run the parser on the string specified.
     ///
     /// @param str string to be written
-    int parseString(const std::string& str);
+    /// @return true on success.
+    bool parseString(const std::string& str);
 
     /// @brief The name of the file being parsed.
     /// Used later to pass the file name to the location tracker.
     std::string file_;
 
+    /// @brief The string being parsed.
+    std::string string_;
+
     /// @brief Error handler
     ///
-    /// @param l location within the parsed file when experienced a problem.
+    /// @param loc location within the parsed file when experienced a problem.
     /// @param what string explaining the nature of the error.
-    void error(const isc::eval::location& l, const std::string& what);
+    void error(const isc::eval::location& loc, const std::string& what);
 
     /// @brief Error handler
     ///
index 6d976b7011a30cf7b48887c0b39f130b0a202493..953f05a313901b5a3c94f7c0349e05d75e123643 100644 (file)
@@ -2234,7 +2234,7 @@ void yyfree (void * ptr )
 using namespace isc::eval;
 
 void
-EvalContext::scanBegin()
+EvalContext::scanFileBegin()
 {
     yy_flex_debug = trace_scanning_;
     if (file_.empty () || file_ == "-") {
@@ -2247,8 +2247,26 @@ EvalContext::scanBegin()
 }
 
 void
-EvalContext::scanEnd()
+EvalContext::scanFileEnd()
 {
     fclose(yyin);
 }
 
+void
+EvalContext::scanStringBegin()
+{
+    YY_BUFFER_STATE buffer;
+    yy_flex_debug = trace_scanning_;
+    buffer = yy_scan_bytes(string_.c_str(),string_.size());
+    if (!buffer) {
+        error("cannot scan string");
+        exit(EXIT_FAILURE);
+    }
+}
+
+void
+EvalContext::scanStringEnd()
+{
+    yy_delete_buffer(YY_CURRENT_BUFFER);
+}
+
index 1d86d9db9064c3d5d134a6c2901824b58c6e8bdb..b9455b2da65ef2d9f75ef00cbb5d222debfff79b 100644 (file)
@@ -146,7 +146,7 @@ str   [a-zA-Z_0-9]*
 using namespace isc::eval;
 
 void
-EvalContext::scanBegin()
+EvalContext::scanFileBegin()
 {
     yy_flex_debug = trace_scanning_;
     if (file_.empty () || file_ == "-") {
@@ -159,7 +159,25 @@ EvalContext::scanBegin()
 }
 
 void
-EvalContext::scanEnd()
+EvalContext::scanFileEnd()
 {
     fclose(yyin);
 }
+
+void
+EvalContext::scanStringBegin()
+{
+    YY_BUFFER_STATE buffer;
+    yy_flex_debug = trace_scanning_;
+    buffer = yy_scan_bytes(string_.c_str(), string_.size());
+    if (!buffer) {
+        error("cannot scan string");
+        exit(EXIT_FAILURE);
+    }
+}
+
+void
+EvalContext::scanStringEnd()
+{
+    yy_delete_buffer(YY_CURRENT_BUFFER);
+}
index 38769740e242c61ead37ba0a02f7686172e4f8f9..077d74d40a223ed75e3460b9b543cb03b345211f 100644 (file)
@@ -77,19 +77,23 @@ public:
 
         EXPECT_EQ(expected_option, opt->getCode());
     }
+
+    bool parsed_; ///< Parsing status
 };
 
 TEST_F(EvalContextTest, basic) {
 
     EvalContext tmp;
 
-    EXPECT_NO_THROW(tmp.parseString("option[123] == 'MSFT'"));
+    EXPECT_NO_THROW(parsed_ = tmp.parseString("option[123] == 'MSFT'"));
+    EXPECT_TRUE(parsed_);
 }
 
 TEST_F(EvalContextTest, string) {
     EvalContext eval;
 
-    EXPECT_NO_THROW(eval.parseString("'foo'"));
+    EXPECT_NO_THROW(parsed_ = eval.parseString("'foo'"));
+    EXPECT_TRUE(parsed_);
 
     ASSERT_EQ(1, eval.expression.size());
 
@@ -101,7 +105,8 @@ TEST_F(EvalContextTest, string) {
 TEST_F(EvalContextTest, hexstring) {
     EvalContext eval;
 
-    EXPECT_NO_THROW(eval.parseString("0x666f6f"));
+    EXPECT_NO_THROW(parsed_ = eval.parseString("0x666f6f"));
+    EXPECT_TRUE(parsed_);
 
     ASSERT_EQ(1, eval.expression.size());
 
@@ -113,7 +118,8 @@ TEST_F(EvalContextTest, hexstring) {
 TEST_F(EvalContextTest, equal) {
     EvalContext eval;
 
-    EXPECT_NO_THROW(eval.parseString("'foo' == 'bar'"));
+    EXPECT_NO_THROW(parsed_ = eval.parseString("'foo' == 'bar'"));
+    EXPECT_TRUE(parsed_);
 
     ASSERT_EQ(3, eval.expression.size());
 
@@ -129,7 +135,8 @@ TEST_F(EvalContextTest, equal) {
 TEST_F(EvalContextTest, option) {
     EvalContext eval;
 
-    EXPECT_NO_THROW(eval.parseString("option[123]"));
+    EXPECT_NO_THROW(parsed_ = eval.parseString("option[123]"));
+    EXPECT_TRUE(parsed_);
     ASSERT_EQ(1, eval.expression.size());
     checkTokenOption(eval.expression.at(0), 123);
 }