AOS json tokenizer allows C++ style comments
More options for developers.
Change-Id: I8267b5d0b78c3614d2f9af259c370e828d0433c9
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/json_to_flatbuffer_test.cc b/aos/json_to_flatbuffer_test.cc
index d2b88f0..a145364 100644
--- a/aos/json_to_flatbuffer_test.cc
+++ b/aos/json_to_flatbuffer_test.cc
@@ -41,7 +41,11 @@
printf("Back to string via TypeTable: %s\n", back_typetable.c_str());
printf("Back to string via reflection: %s\n", back_reflection.c_str());
- return back_typetable == out && back_reflection == out;
+ const bool as_expected = back_typetable == out && back_reflection == out;
+ if (!as_expected) {
+ printf("But expected: %s\n", out.c_str());
+ }
+ return as_expected;
}
};
@@ -230,9 +234,33 @@
EXPECT_TRUE(JsonAndBack("{ }"));
}
-// Tests that comments get stripped.
-TEST_F(JsonToFlatbufferTest, Comments) {
- EXPECT_TRUE(JsonAndBack("{ /* foo */ \"vector_foo_double\": [ 9, 7, 1 ] }",
+// Tests that C style comments get stripped.
+TEST_F(JsonToFlatbufferTest, CStyleComments) {
+ EXPECT_TRUE(JsonAndBack(R"({
+ /* foo */
+ "vector_foo_double": [ 9, 7, 1 ] /* foo */
+} /* foo */)",
+ "{ \"vector_foo_double\": [ 9.0, 7.0, 1.0 ] }"));
+}
+
+// Tests that C++ style comments get stripped.
+TEST_F(JsonToFlatbufferTest, CppStyleComments) {
+ EXPECT_TRUE(JsonAndBack(R"({
+ // foo
+ "vector_foo_double": [ 9, 7, 1 ] // foo
+} // foo)",
+ "{ \"vector_foo_double\": [ 9.0, 7.0, 1.0 ] }"));
+}
+
+// Tests that mixed style comments get stripped.
+TEST_F(JsonToFlatbufferTest, MixedStyleComments) {
+ // Weird comments do not throw us off.
+ EXPECT_TRUE(JsonAndBack(R"({
+ // foo /* foo */
+ "vector_foo_double": [ 9, 7, 1 ] /* // foo */
+}
+// foo
+/* foo */)",
"{ \"vector_foo_double\": [ 9.0, 7.0, 1.0 ] }"));
}
diff --git a/aos/json_tokenizer.cc b/aos/json_tokenizer.cc
index b3c6620..eab7fcc 100644
--- a/aos/json_tokenizer.cc
+++ b/aos/json_tokenizer.cc
@@ -23,6 +23,19 @@
}
ConsumeChar();
}
+ } else if (Consume("//")) {
+ // C++ style comment. Keep consuming chars until newline, or until the
+ // end of the file if this is the last line (no newline at end of file).
+ while (true) {
+ ConsumeChar();
+ if (AtEnd()) {
+ return;
+ }
+ if (Char() == '\n') {
+ ++linenumber_;
+ break;
+ }
+ }
} else {
// There is no fail. Once we are out of whitespace (including 0 of it),
// declare success.