Browse Source

[4088] General smallish improvements in evaluation code

Tomek Mrugalski 9 years ago
parent
commit
e2896aa6b3

+ 18 - 4
src/lib/eval/eval_context.cc

@@ -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;
 }
 

+ 60 - 26
src/lib/eval/eval_context.h

@@ -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

+ 6 - 6
src/lib/eval/lexer.cc

@@ -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);
 }
 

+ 6 - 6
src/lib/eval/lexer.ll

@@ -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);
 }

+ 2 - 2
src/lib/eval/location.hh

@@ -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

+ 12 - 12
src/lib/eval/parser.cc

@@ -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"
 
@@ -137,7 +137,7 @@
 #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,

+ 3 - 3
src/lib/eval/parser.h

@@ -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
 

+ 14 - 0
src/lib/eval/parser.yy

@@ -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

+ 2 - 2
src/lib/eval/position.hh

@@ -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

+ 2 - 2
src/lib/eval/stack.hh

@@ -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