]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4088] General smallish improvements in evaluation code
authorTomek Mrugalski <tomasz@isc.org>
Wed, 4 Nov 2015 15:34:12 +0000 (00:34 +0900)
committerTomek Mrugalski <tomasz@isc.org>
Wed, 4 Nov 2015 15:34:12 +0000 (00:34 +0900)
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/location.hh
src/lib/eval/parser.cc
src/lib/eval/parser.h
src/lib/eval/parser.yy
src/lib/eval/position.hh
src/lib/eval/stack.hh

index de01092fa0fade5cfff75e698ccaec4d7e25f8fa..812e41c58afd3e7c43081b4e3a456b80992409ec 100644 (file)
@@ -1,10 +1,24 @@
+// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
 #include <eval/eval_context.h>
 #include <eval/parser.h>
 #include <exceptions/exceptions.h>
 #include <fstream>
 
 EvalContext::EvalContext()
-  : trace_scanning (false), trace_parsing (false)
+  : trace_scanning_(false), trace_parsing_(false)
 {
 }
 
@@ -16,11 +30,11 @@ int
 EvalContext::parseFile(const std::string &filename)
 {
     file = filename;
-    scan_begin();
+    scanBegin();
     isc::eval::EvalParser parser(*this);
-    parser.set_debug_level(trace_parsing);
+    parser.set_debug_level(trace_parsing_);
     int res = parser.parse();
-    scan_end();
+    scanEnd();
     return res;
 }
 
index 64249752e99366eb63da2003ceac3024425ae8fc..7c963f0af49d5872aaee6adfc2bf701fc3f9a1e5 100644 (file)
@@ -1,47 +1,81 @@
+// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
 #ifndef EVAL_CONTEXT_H
 #define EVAL_CONTEXT_H
-# include <string>
-# include <map>
-# include "parser.h"
+#include <string>
+#include <map>
+#include <eval/parser.h>
 
 // Tell Flex the lexer's prototype ...
-# define YY_DECL \
-    isc::eval::EvalParser::symbol_type yylex (EvalContext& driver)
+#define YY_DECL isc::eval::EvalParser::symbol_type yylex (EvalContext& driver)
 
 // ... and declare it for the parser's sake.
 YY_DECL;
 
-// Conducting the whole scanning and parsing of Calc++.
+/// @brief Evaluation context, an interface to the expression evaluation.
 class EvalContext
 {
 public:
-  EvalContext ();
-  virtual ~EvalContext ();
+    /// @brief Default constructor.
+    EvalContext();
+
+    /// @brief destructor
+    virtual ~EvalContext();
+
+    /// @brief Parsed expression (output tokens are stored here)
+    isc::dhcp::Expression expression;
 
-  isc::dhcp::Expression expression;
+    /// @brief Method called before scanning starts.
+    void scanBegin();
 
-  int result;
+    /// @brief Method called after the last tokens are scanned.
+    void scanEnd();
+    
+    /// @brief Runs the parser on specified file.
+    ///
+    /// @param filename
+    /// Return 0 on success.
+    int parseFile(const std::string& filename);
 
-  // Handling the scanner.
-  void scan_begin ();
-  void scan_end ();
-  bool trace_scanning;
+    /// @brief Run the parser on the string specified.
+    ///
+    /// @param str string to be written
+    int parseString(const std::string& str);
 
-  // Run the parser on file F.
-  // Return 0 on success.
-  int parseFile(const std::string& filename);
+    /// @brief The name of the file being parsed.
+    /// Used later to pass the file name to the location tracker.
+    std::string file;
 
-  int parseString(const std::string& str);
+    /// @brief Error handler
+    ///
+    /// @param l 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);
 
-  // The name of the file being parsed.
-  // Used later to pass the file name to the location tracker.
+    /// @brief Error handler
+    ///
+    /// This is a simplified error reporting tool for possible future
+    /// cases when the EvalParser is not able to handle the packet.
+    void error(const std::string& what);
 
-  std::string file;
-  // Whether parser traces should be generated.
-  bool trace_parsing;
+ private:
+    /// @brief Flag determining scanner debugging.
+    bool trace_scanning_;
 
-  // Error handling.
-  void error (const isc::eval::location& l, const std::string& m);
-  void error (const std::string& m);
+    /// @brief Flag determing parser debugging.
+    bool trace_parsing_;
+  
 };
 #endif // ! EVALCONTEXT_HH
index b4a1f0d0d8005fe86efc3e4e82126aa9e636dfd4..3e01ff493c35066904631fd399dc817762e4556d 100644 (file)
@@ -2220,21 +2220,21 @@ void yyfree (void * ptr )
 
 
 void
-EvalContext::scan_begin()
+EvalContext::scanBegin()
 {
-    yy_flex_debug = trace_scanning;
+    yy_flex_debug = trace_scanning_;
     if (file.empty () || file == "-") {
         yyin = stdin;
     }
     else if (!(yyin = fopen(file.c_str (), "r"))) {
-        error ("cannot open " + file + ": " + strerror(errno));
-        exit (EXIT_FAILURE);
+        error("cannot open " + file + ": " + strerror(errno));
+        exit(EXIT_FAILURE);
     }
 }
 
 void
-EvalContext::scan_end()
+EvalContext::scanEnd()
 {
-    fclose (yyin);
+    fclose(yyin);
 }
 
index 9bcc58d4952a13b81f29436d79fad64a73403c62..14b5de0d690772306f6affec07b147b3f0841f06 100644 (file)
@@ -162,20 +162,20 @@ option\[{int}\] {
 %%
 
 void
-EvalContext::scan_begin()
+EvalContext::scanBegin()
 {
-    yy_flex_debug = trace_scanning;
+    yy_flex_debug = trace_scanning_;
     if (file.empty () || file == "-") {
         yyin = stdin;
     }
     else if (!(yyin = fopen(file.c_str (), "r"))) {
-        error ("cannot open " + file + ": " + strerror(errno));
-        exit (EXIT_FAILURE);
+        error("cannot open " + file + ": " + strerror(errno));
+        exit(EXIT_FAILURE);
     }
 }
 
 void
-EvalContext::scan_end()
+EvalContext::scanEnd()
 {
-    fclose (yyin);
+    fclose(yyin);
 }
index 413727d0cfbeff6e131e2647e010d010b5c09df1..8f859737487b5b2822c90a5f225ac4b3410477e0 100644 (file)
@@ -40,7 +40,7 @@
 
 # include "position.hh"
 
-#line 7 "parser.yy" // location.cc:337
+#line 21 "parser.yy" // location.cc:337
 namespace isc { namespace eval {
 #line 46 "location.hh" // location.cc:337
   /// Abstract a location.
@@ -186,7 +186,7 @@ namespace isc { namespace eval {
     return ostr;
   }
 
-#line 7 "parser.yy" // location.cc:337
+#line 21 "parser.yy" // location.cc:337
 } } // isc::eval
 #line 192 "location.hh" // location.cc:337
 #endif // !YY_YY_LOCATION_HH_INCLUDED
index 599d2922abc21e74ceef0900c89aa1f141b8d102..3abd2be298ad502f3dbbec93339d36ef78d7f6e9 100644 (file)
@@ -49,7 +49,7 @@
 
 #line 51 "parser.cc" // lalr1.cc:412
 // Unqualified %code blocks.
-#line 28 "parser.yy" // lalr1.cc:413
+#line 42 "parser.yy" // lalr1.cc:413
 
 # include "eval_context.h"
 
 #define YYERROR         goto yyerrorlab
 #define YYRECOVERING()  (!!yyerrstatus_)
 
-#line 7 "parser.yy" // lalr1.cc:479
+#line 21 "parser.yy" // lalr1.cc:479
 namespace isc { namespace eval {
 #line 143 "parser.cc" // lalr1.cc:479
 
@@ -320,14 +320,14 @@ namespace isc { namespace eval {
     {
             case 8: // "constant string"
 
-#line 42 "parser.yy" // lalr1.cc:636
+#line 56 "parser.yy" // lalr1.cc:636
         { yyoutput << yysym.value.template as< std::string > (); }
 #line 326 "parser.cc" // lalr1.cc:636
         break;
 
       case 9: // "option code"
 
-#line 42 "parser.yy" // lalr1.cc:636
+#line 56 "parser.yy" // lalr1.cc:636
         { yyoutput << yysym.value.template as< int > (); }
 #line 333 "parser.cc" // lalr1.cc:636
         break;
@@ -442,7 +442,7 @@ namespace isc { namespace eval {
 
 
     // User initialization code.
-    #line 21 "parser.yy" // lalr1.cc:745
+    #line 35 "parser.yy" // lalr1.cc:745
 {
   // Initialize the initial location.
   yyla.location.begin.filename = yyla.location.end.filename = &ctx.file;
@@ -564,7 +564,7 @@ namespace isc { namespace eval {
           switch (yyn)
             {
   case 2:
-#line 50 "parser.yy" // lalr1.cc:859
+#line 64 "parser.yy" // lalr1.cc:859
     {
     TokenPtr eq(new TokenEqual());
     ctx.expression.push_back(eq);
@@ -573,7 +573,7 @@ namespace isc { namespace eval {
     break;
 
   case 4:
-#line 57 "parser.yy" // lalr1.cc:859
+#line 71 "parser.yy" // lalr1.cc:859
     {
     TokenPtr str(new TokenString(yystack_[0].value.as< std::string > ()));
     ctx.expression.push_back(str);
@@ -582,7 +582,7 @@ namespace isc { namespace eval {
     break;
 
   case 5:
-#line 61 "parser.yy" // lalr1.cc:859
+#line 75 "parser.yy" // lalr1.cc:859
     {
     TokenPtr opt(new TokenOption(yystack_[0].value.as< int > ()));
     ctx.expression.push_back(opt);
@@ -591,7 +591,7 @@ namespace isc { namespace eval {
     break;
 
   case 6:
-#line 65 "parser.yy" // lalr1.cc:859
+#line 79 "parser.yy" // lalr1.cc:859
     {
     /* push back TokenSubstring */
   }
@@ -933,7 +933,7 @@ namespace isc { namespace eval {
   const unsigned char
   EvalParser::yyrline_[] =
   {
-       0,    50,    50,    54,    57,    61,    65
+       0,    64,    64,    68,    71,    75,    79
   };
 
   // Print the state stack on the debug stream.
@@ -966,10 +966,10 @@ namespace isc { namespace eval {
 #endif // YYDEBUG
 
 
-#line 7 "parser.yy" // lalr1.cc:1167
+#line 21 "parser.yy" // lalr1.cc:1167
 } } // isc::eval
 #line 972 "parser.cc" // lalr1.cc:1167
-#line 69 "parser.yy" // lalr1.cc:1168
+#line 83 "parser.yy" // lalr1.cc:1168
 
 void
 isc::eval::EvalParser::error(const location_type& l,
index 4c885c4bce292d0005c697834315e624ee029e4d..114f426f51198590c4594151d839e64a11567f08 100644 (file)
@@ -40,7 +40,7 @@
 #ifndef YY_YY_PARSER_H_INCLUDED
 # define YY_YY_PARSER_H_INCLUDED
 // //                    "%code requires" blocks.
-#line 10 "parser.yy" // lalr1.cc:392
+#line 24 "parser.yy" // lalr1.cc:392
 
 #include <string>
 #include <eval/token.h>
@@ -123,7 +123,7 @@ using namespace isc::dhcp;
 # define YYDEBUG 1
 #endif
 
-#line 7 "parser.yy" // lalr1.cc:392
+#line 21 "parser.yy" // lalr1.cc:392
 namespace isc { namespace eval {
 #line 129 "parser.h" // lalr1.cc:392
 
@@ -991,7 +991,7 @@ namespace isc { namespace eval {
   }
 
 
-#line 7 "parser.yy" // lalr1.cc:392
+#line 21 "parser.yy" // lalr1.cc:392
 } } // isc::eval
 #line 997 "parser.h" // lalr1.cc:392
 
index c93f4c714dd2e7de8cb0d5c497d1df61b776807f..ba500124c8d3ac835d70d85c66345381bbefd909 100644 (file)
@@ -1,3 +1,17 @@
+/* Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
+
+   Permission to use, copy, modify, and/or distribute this software for any
+   purpose with or without fee is hereby granted, provided that the above
+   copyright notice and this permission notice appear in all copies.
+
+   THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+   REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+   AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+   INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+   LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+   OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+   PERFORMANCE OF THIS SOFTWARE. */
+
 %skeleton "lalr1.cc" /* -*- C++ -*- */
 %require "3.0.0"
 %defines
index f0f06fa950bd40c598882e94c41605d4d871f0ad..fe32a47ff2e16c5cbb24100e26cf39a21416972e 100644 (file)
@@ -50,7 +50,7 @@
 #  endif
 # endif
 
-#line 7 "parser.yy" // location.cc:337
+#line 21 "parser.yy" // location.cc:337
 namespace isc { namespace eval {
 #line 56 "position.hh" // location.cc:337
   /// Abstract a position.
@@ -174,7 +174,7 @@ namespace isc { namespace eval {
     return ostr << pos.line << '.' << pos.column;
   }
 
-#line 7 "parser.yy" // location.cc:337
+#line 21 "parser.yy" // location.cc:337
 } } // isc::eval
 #line 180 "position.hh" // location.cc:337
 #endif // !YY_YY_POSITION_HH_INCLUDED
index 3353000a669daa8322019cd5f27d8abb0e70dfb7..c1120dccb957090fb77d338f5bcbf07ebf90549e 100644 (file)
@@ -40,7 +40,7 @@
 
 # include <vector>
 
-#line 7 "parser.yy" // stack.hh:151
+#line 21 "parser.yy" // stack.hh:151
 namespace isc { namespace eval {
 #line 46 "stack.hh" // stack.hh:151
   template <class T, class S = std::vector<T> >
@@ -150,7 +150,7 @@ namespace isc { namespace eval {
     unsigned int range_;
   };
 
-#line 7 "parser.yy" // stack.hh:151
+#line 21 "parser.yy" // stack.hh:151
 } } // isc::eval
 #line 156 "stack.hh" // stack.hh:151