Browse Source

[5014] Implemented comments

 - addresses tickets #3450 (part about comments), #3960
Tomek Mrugalski 8 years ago
parent
commit
7df8902127
2 changed files with 108 additions and 0 deletions
  1. 20 0
      src/bin/dhcp6/dhcp6_lexer.ll
  2. 88 0
      src/bin/dhcp6/tests/parser_unittest.cc

+ 20 - 0
src/bin/dhcp6/dhcp6_lexer.ll

@@ -12,6 +12,7 @@
 #include <dhcp6/parser_context.h>
 #include <asiolink/io_address.h>
 #include <boost/lexical_cast.hpp>
+#include <exceptions/exceptions.h>
 
 // Work around an incompatibility in flex (at least versions
 // 2.5.31 through 2.5.33): it generates code that does
@@ -55,6 +56,8 @@ static isc::dhcp::location loc;
    useful in more complex cases. */
 %option yylineno
 
+%x COMMENT
+
 /* These are not token expressions yet, just convenience expressions that
    can be used during actual token definitions. Note some can match
    incorrect inputs (e.g., IP addresses) which must be checked. */
@@ -80,8 +83,25 @@ JSONString                              \"{JSONStringCharacter}*\"
 %{
     // Code run each time yylex is called.
     loc.step();
+
+    int comment_start_line = 0;
 %}
 
+#.* ;
+
+"//"(.*) ;
+
+"/*" {
+  BEGIN(COMMENT);
+  comment_start_line = yylineno;
+}
+
+<COMMENT>"*/" BEGIN(INITIAL);
+<COMMENT>.|"\n" ;
+<COMMENT><<EOF>> {
+    isc_throw(isc::BadValue, "Comment not closed. (/* in line " << comment_start_line);
+}
+
 {blank}+   {
     // Ok, we found a with space. Let's ignore it and update loc variable.
     loc.step();

+ 88 - 0
src/bin/dhcp6/tests/parser_unittest.cc

@@ -14,6 +14,8 @@ using namespace std;
 namespace {
 
 void compareJSON(ConstElementPtr a, ConstElementPtr b) {
+    ASSERT_TRUE(a);
+    ASSERT_TRUE(b);
     std::cout << a->str() << std::endl;
     std::cout << b->str() << std::endl;
     EXPECT_EQ(a->str(), b->str());
@@ -33,6 +35,20 @@ void testParser(const std::string& txt) {
     compareJSON(reference_json, test_json);
 }
 
+void testParser2(const std::string& txt) {
+    ConstElementPtr test_json;
+
+    EXPECT_NO_THROW({
+        Parser6Context ctx;
+        test_json = ctx.parseString(txt);
+    });
+    /// @todo: Implement actual validation here. since the original
+    /// Element::fromJSON does not support several comment types, we don't
+    /// have anything to compare with.
+    std::cout << "Original text:" << txt << endl;
+    std::cout << "Parsed text  :" << test_json->str() << endl;
+}
+
 TEST(ParserTest, mapInMap) {
     string txt = "{ \"Dhcp6\": { \"foo\": 123, \"baz\": 456 } }";
     testParser(txt);
@@ -76,4 +92,76 @@ TEST(ParserTest, types) {
     testParser(txt);
 }
 
+TEST(ParserTest, bashComments) {
+    string txt= "{ \"interfaces-config\": {"
+                "  \"interfaces\": [ \"*\" ]"
+                "},\n"
+                "\"preferred-lifetime\": 3000,\n"
+                "# this is a comment\n"
+                "\"rebind-timer\": 2000, \n"
+                "# lots of comments here\n"
+                "# and here\n"
+                "\"renew-timer\": 1000, \n"
+                "\"subnet6\": [ { "
+                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
+                "    \"subnet\": \"2001:db8:1::/48\", "
+                "    \"interface-id\": \"\","
+                "    \"interface\": \"eth0\""
+                " } ],"
+                "\"valid-lifetime\": 4000 }";
+    testParser(txt);
+}
+
+TEST(ParserTest, cComments) {
+    string txt= "{ \"interfaces-config\": {"
+                "  \"interfaces\": [ \"*\" ]"
+                "},\n"
+                "\"preferred-lifetime\": 3000, // this is a comment \n"
+                "\"rebind-timer\": 2000, // everything after // is ignored\n"
+                "\"renew-timer\": 1000, // this will be ignored, too\n"
+                "\"subnet6\": [ { "
+                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
+                "    \"subnet\": \"2001:db8:1::/48\", "
+                "    \"interface-id\": \"\","
+                "    \"interface\": \"eth0\""
+                " } ],"
+                "\"valid-lifetime\": 4000 }";
+    testParser2(txt);
+}
+
+TEST(ParserTest, bashComments2) {
+    string txt= "{ \"interfaces-config\": {"
+                "  \"interfaces\": [ \"*\" ]"
+                "},\n"
+                "\"preferred-lifetime\": 3000, # this is a comment \n"
+                "\"rebind-timer\": 2000, # everything after # is ignored\n"
+                "\"renew-timer\": 1000, # this will be ignored, too\n"
+                "\"subnet6\": [ { "
+                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
+                "    \"subnet\": \"2001:db8:1::/48\", "
+                "    \"interface-id\": \"\","
+                "    \"interface\": \"eth0\""
+                " } ],"
+                "\"valid-lifetime\": 4000 }";
+    testParser2(txt);
+}
+
+TEST(ParserTest, multilineComments) {
+    string txt= "{ \"interfaces-config\": {"
+                "  \"interfaces\": [ \"*\" ]"
+                "},\n"
+                "\"preferred-lifetime\": 3000, /* this is a C style comment\n"
+                "that\n can \n span \n multiple \n lines */ \n"
+                "\"rebind-timer\": 2000,\n"
+                "\"renew-timer\": 1000, \n"
+                "\"subnet6\": [ { "
+                "    \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
+                "    \"subnet\": \"2001:db8:1::/48\", "
+                "    \"interface-id\": \"\","
+                "    \"interface\": \"eth0\""
+                " } ],"
+                "\"valid-lifetime\": 4000 }";
+    testParser2(txt);
+}
+
 };