Browse Source

[4204fd] Moved convert_option_code to context code

Francis Dupont 9 years ago
parent
commit
1666189a61
3 changed files with 38 additions and 37 deletions
  1. 26 0
      src/lib/eval/eval_context.cc
  2. 10 0
      src/lib/eval/eval_context.h
  3. 2 37
      src/lib/eval/parser.yy

+ 26 - 0
src/lib/eval/eval_context.cc

@@ -52,3 +52,29 @@ EvalContext::error (const std::string& what)
 {
     isc_throw(EvalParseError, what);
 }
+
+uint16_t
+EvalContext::convert_option_code(const std::string& option_code,
+                                 const isc::eval::location& loc)
+{
+    int n = 0;
+    try {
+        n  = boost::lexical_cast<int>(option_code);
+    } catch (const boost::bad_lexical_cast &) {
+        // This can't happen...
+        error(loc, "Option code has invalid value in " + option_code);
+    }
+    if (option_universe_ == Option::V6) {
+        if (n < 0 || n > 65535) {
+            error(loc, "Option code has invalid value in "
+                      + option_code + ". Allowed range: 0..65535");
+        }
+    } else {
+        if (n < 0 || n > 255) {
+            error(loc, "Option code has invalid value in "
+                      + option_code + ". Allowed range: 0..255");
+        }
+    }
+    return (static_cast<uint16_t>(n));
+}
+

+ 10 - 0
src/lib/eval/eval_context.h

@@ -85,6 +85,16 @@ public:
     /// cases when the EvalParser is not able to handle the packet.
     void error(const std::string& what);
 
+    /// @brief Option code convertion
+    ///
+    /// @param option_code a string representing the integer code
+    /// @param loc the location of the token
+    /// @result the option code
+    /// @throw calls the syntax error function if the value is no in
+    ///        the range 0..255 or 0..65535
+    uint16_t convert_option_code(const std::string& option_code,
+                                 const isc::eval::location& loc);
+
     /// @brief Option universe: DHCPv4 or DHCPv6.
     ///
     /// This is used by the parser to determine which option definitions

+ 2 - 37
src/lib/eval/parser.yy

@@ -66,41 +66,6 @@ using namespace isc::eval;
 
 %printer { yyoutput << $$; } <*>;
 
-%code
-{
-namespace {
-
-/* Convert option code specified as string to an 16 bit unsigned
-   representation. If the option code is not within the range of
-   0..65535 an error is reported. */
-uint16_t
-convert_option_code(const std::string& option_code,
-                    const isc::eval::EvalParser::location_type& loc,
-                    EvalContext& ctx) {
-    int n = 0;
-    try {
-        n  = boost::lexical_cast<int>(option_code);
-    } catch (const boost::bad_lexical_cast &) {
-        // This can't happen...
-        ctx.error(loc, "Option code has invalid value in " + option_code);
-    }
-    if (ctx.option_universe_ == Option::V6) {
-        if (n < 0 || n > 65535) {
-            ctx.error(loc, "Option code has invalid value in "
-                          + option_code + ". Allowed range: 0..65535");
-        }
-    } else {
-        if (n < 0 || n > 255) {
-            ctx.error(loc, "Option code has invalid value in "
-                          + option_code + ". Allowed range: 0..255");
-        }
-    }
-    return (static_cast<uint16_t>(n));
-}
-}
-
-}
-
 %%
 
 // The whole grammar starts with an expression.
@@ -130,13 +95,13 @@ string_expr : STRING
                   }
             | OPTION "[" INTEGER "]" DOT TEXT
                   {
-                      uint16_t numeric_code = convert_option_code($3, @3, ctx);
+                      uint16_t numeric_code = ctx.convert_option_code($3, @3);
                       TokenPtr opt(new TokenOption(numeric_code, TokenOption::TEXTUAL));
                       ctx.expression.push_back(opt);
                   }
             | OPTION "[" INTEGER "]" DOT HEX
                   {
-                      uint16_t numeric_code = convert_option_code($3, @3, ctx);
+                      uint16_t numeric_code = ctx.convert_option_code($3, @3);
                       TokenPtr opt(new TokenOption(numeric_code, TokenOption::HEXADECIMAL));
                       ctx.expression.push_back(opt);
                   }