%start start;
start: TOPLEVEL_DHCP6 syntax_map
-| TOPLEVEL_GENERIC_JSON map;
+| TOPLEVEL_GENERIC_JSON map2;
// ---- generic JSON parser ---------------------------------
| BOOLEAN { $$ = ElementPtr(new BoolElement($1)); }
| STRING { $$ = ElementPtr(new StringElement($1)); }
| NULL_TYPE { $$ = ElementPtr(new NullElement()); }
- | map { $$ = ctx.stack_.back(); ctx.stack_.pop_back(); }
- | list { $$ = ctx.stack_.back(); ctx.stack_.pop_back(); }
+ | map2 { $$ = ctx.stack_.back(); ctx.stack_.pop_back(); }
+ | list_generic { $$ = ctx.stack_.back(); ctx.stack_.pop_back(); }
;
-map: LCURLY_BRACKET {
+map2: LCURLY_BRACKET {
// This code is executed when we're about to start parsing
// the content of the map
ElementPtr m(new MapElement());
}
;
-list: LSQUARE_BRACKET {
+list_generic: LSQUARE_BRACKET {
+ ElementPtr l(new ListElement());
+ ctx.stack_.push_back(l);
+ } list_content RSQUARE_BRACKET {
+
+ }
+
+// This one is used in syntax parser.
+list2: LSQUARE_BRACKET {
// List parsing about to start
} list_content RSQUARE_BRACKET {
// list parsing complete. Put any sanity checking here
+ //ctx.stack_.pop_back();
};
list_content: { /* do nothing, it's an empty list */ }
ElementPtr l(new ListElement());
ctx.stack_.back()->set("interfaces", l);
ctx.stack_.push_back(l);
- } COLON list {
+ } COLON list2 {
ctx.stack_.pop_back();
}
ElementPtr l(new ListElement());
ctx.stack_.back()->set("relay-supplied-options", l);
ctx.stack_.push_back(l);
-} COLON list {
+} COLON list2 {
ctx.stack_.pop_back();
};
ctx.stack_.pop_back();
};
-hooks_libraries_list: { }
+hooks_libraries_list: { }
| hooks_library
| hooks_libraries_list COMMA hooks_library;
ElementPtr l(new ListElement());
ctx.stack_.back()->set("ip-addresses", l);
ctx.stack_.push_back(l);
-} list {
+} list2 {
ctx.stack_.pop_back();
};
ElementPtr l(new ListElement());
ctx.stack_.back()->set("prefixes", l);
ctx.stack_.push_back(l);
-} list {
+} list2 {
ctx.stack_.pop_back();
};
ElementPtr c(new ListElement());
ctx.stack_.back()->set("client-classes", c);
ctx.stack_.push_back(c);
-} list {
+} list2 {
ctx.stack_.pop_back();
};
ASSERT_TRUE(a);
ASSERT_TRUE(b);
if (print) {
- std::cout << "JSON A: -----" << endl << a->str() << std::endl;
- std::cout << "JSON B: -----" << endl << b->str() << std::endl;
- cout << "---------" << endl << endl;
+ // std::cout << "JSON A: -----" << endl << a->str() << std::endl;
+ // std::cout << "JSON B: -----" << endl << b->str() << std::endl;
+ // cout << "---------" << endl << endl;
}
EXPECT_EQ(a->str(), b->str());
}
ElementPtr reference_json;
ConstElementPtr test_json;
- EXPECT_NO_THROW(reference_json = Element::fromJSON(txt, true));
- EXPECT_NO_THROW({
+ ASSERT_NO_THROW(reference_json = Element::fromJSON(txt, true));
+ ASSERT_NO_THROW({
+ try {
Parser6Context ctx;
test_json = ctx.parseString(txt, parser_type);
+ } catch (const std::exception &e) {
+ cout << "EXCEPTION: " << e.what() << endl;
+ throw;
+ }
+
});
// Now compare if both representations are the same.
void testParser2(const std::string& txt, Parser6Context::ParserType parser_type) {
ConstElementPtr test_json;
- EXPECT_NO_THROW({
+ ASSERT_NO_THROW({
+ try {
Parser6Context ctx;
test_json = ctx.parseString(txt, parser_type);
+ } catch (const std::exception &e) {
+ cout << "EXCEPTION: " << e.what() << endl;
+ throw;
+ }
});
/// @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;
+ /// std::cout << "Original text:" << txt << endl;
+ /// std::cout << "Parsed text :" << test_json->str() << endl;
}
TEST(ParserTest, mapInMap) {
- string txt = "{ \"Dhcp6\": { \"foo\": 123, \"baz\": 456 } }";
+ string txt = "{ \"xyzzy\": { \"foo\": 123, \"baz\": 456 } }";
testParser(txt, Parser6Context::PARSER_GENERIC_JSON);
}
}
TEST(ParserTest, mapsInLists) {
- string txt = "{ \"solar-system\": [ { \"name\": \"earth\", \"gravity\": 1.0 },"
- " { \"name\": \"mars\", \"gravity\": 0.376 } ] }";
+ string txt = "{ \"solar-system\": [ { \"body\": \"earth\", \"gravity\": 1.0 },"
+ " { \"body\": \"mars\", \"gravity\": 0.376 } ] }";
testParser(txt, Parser6Context::PARSER_GENERIC_JSON);
}
}
TEST(ParserTest, bashComments) {
- string txt= "{ \"interfaces-config\": {"
+ string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
" \"interfaces\": [ \"*\" ]"
"},\n"
"\"preferred-lifetime\": 3000,\n"
"\"subnet6\": [ { "
" \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
" \"subnet\": \"2001:db8:1::/48\", "
- " \"interface-id\": \"\","
" \"interface\": \"eth0\""
" } ],"
- "\"valid-lifetime\": 4000 }";
- testParser(txt, Parser6Context::PARSER_GENERIC_JSON);
+ "\"valid-lifetime\": 4000 } }";
+ testParser2(txt, Parser6Context::PARSER_DHCP6);
}
TEST(ParserTest, cComments) {
- string txt= "{ \"interfaces-config\": {"
+ string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
" \"interfaces\": [ \"*\" ]"
"},\n"
"\"preferred-lifetime\": 3000, // this is a comment \n"
"\"subnet6\": [ { "
" \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
" \"subnet\": \"2001:db8:1::/48\", "
- " \"interface-id\": \"\","
" \"interface\": \"eth0\""
" } ],"
- "\"valid-lifetime\": 4000 }";
- testParser2(txt, Parser6Context::PARSER_GENERIC_JSON);
+ "\"valid-lifetime\": 4000 } }";
+ testParser2(txt, Parser6Context::PARSER_DHCP6);
}
-TEST(ParserTest, bashComments2) {
- string txt= "{ \"interfaces-config\": {"
+TEST(ParserTest, bashCommentsInline) {
+ string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
" \"interfaces\": [ \"*\" ]"
"},\n"
"\"preferred-lifetime\": 3000, # this is a comment \n"
"\"subnet6\": [ { "
" \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
" \"subnet\": \"2001:db8:1::/48\", "
- " \"interface-id\": \"\","
" \"interface\": \"eth0\""
" } ],"
- "\"valid-lifetime\": 4000 }";
- testParser2(txt, Parser6Context::PARSER_GENERIC_JSON);
+ "\"valid-lifetime\": 4000 } }";
+ testParser2(txt, Parser6Context::PARSER_DHCP6);
}
TEST(ParserTest, multilineComments) {
- string txt= "{ \"interfaces-config\": {"
+ string txt= "{ \"Dhcp6\": { \"interfaces-config\": {"
" \"interfaces\": [ \"*\" ]"
"},\n"
"\"preferred-lifetime\": 3000, /* this is a C style comment\n"
"\"subnet6\": [ { "
" \"pools\": [ { \"pool\": \"2001:db8:1::/64\" } ],"
" \"subnet\": \"2001:db8:1::/48\", "
- " \"interface-id\": \"\","
" \"interface\": \"eth0\""
" } ],"
- "\"valid-lifetime\": 4000 }";
- testParser2(txt, Parser6Context::PARSER_GENERIC_JSON);
+ "\"valid-lifetime\": 4000 } }";
+ testParser2(txt, Parser6Context::PARSER_DHCP6);
}
EXPECT_NO_THROW(reference_json = Element::fromJSONFile(fname, true));
+ EXPECT_NO_THROW(
try {
Parser6Context ctx;
test_json = ctx.parseFile(fname, Parser6Context::PARSER_DHCP6);
} catch (const std::exception &x) {
cout << "EXCEPTION: " << x.what() << endl;
- }
+ throw;
+ });
ASSERT_TRUE(reference_json);
ASSERT_TRUE(test_json);