From: Tomek Mrugalski Date: Fri, 4 Nov 2016 13:22:53 +0000 (+0100) Subject: [5014] Implemented comments X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c0b0556b593120540f396e25eb4b1fd2946dcae;p=thirdparty%2Fkea.git [5014] Implemented comments - addresses tickets #3450 (part about comments), #3960 --- diff --git a/src/bin/dhcp6/dhcp6_lexer.ll b/src/bin/dhcp6/dhcp6_lexer.ll index 54e5c6e018..780ccc805e 100644 --- a/src/bin/dhcp6/dhcp6_lexer.ll +++ b/src/bin/dhcp6/dhcp6_lexer.ll @@ -12,6 +12,7 @@ #include #include #include +#include // 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; +} + +"*/" BEGIN(INITIAL); +.|"\n" ; +<> { + 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(); diff --git a/src/bin/dhcp6/tests/parser_unittest.cc b/src/bin/dhcp6/tests/parser_unittest.cc index b2dbb62e75..3490f8dd0d 100644 --- a/src/bin/dhcp6/tests/parser_unittest.cc +++ b/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); +} + };