support continuation lines
authorEvan Martin <martine@danga.com>
Fri, 22 Oct 2010 16:28:22 +0000 (09:28 -0700)
committerEvan Martin <martine@danga.com>
Fri, 22 Oct 2010 16:28:22 +0000 (09:28 -0700)
manifest_parser.h
ninja_test.cc

index 05bf67e..82eb9a8 100644 (file)
@@ -101,8 +101,20 @@ bool Parser::ReadToken(string* out) {
 bool Parser::ReadToNewline(string* text, string* err) {
   token_.clear();
   while (cur_ < end_ && *cur_ != '\n') {
-    text->push_back(*cur_);
-    ++cur_;
+    if (*cur_ == '\\') {
+      ++cur_;
+      if (cur_ >= end_)
+        return Error("unexpected eof", err);
+      if (!Newline(err))
+        return false;
+      SkipWhitespace();
+      // Collapse whitespace, but make sure we get at least one space.
+      if (text->size() > 0 && text->at(text->size() - 1) != ' ')
+        text->push_back(' ');
+    } else {
+      text->push_back(*cur_);
+      ++cur_;
+    }
   }
   return Newline(err);
 }
index 87236f4..0eb5884 100644 (file)
@@ -49,6 +49,22 @@ TEST(Parser, Variables) {
   EXPECT_EQ("ld -pthread -o a b c", edge->EvaluateCommand());
 }
 
+TEST(Parser, Continuation) {
+  State state;
+  ASSERT_NO_FATAL_FAILURE(AssertParse(&state,
+"rule link\n"
+"command foo bar \\\n"
+"    baz\n"
+"\n"
+"extra = \\\n"
+"xyz\n"));
+
+  ASSERT_EQ(1, state.rules_.size());
+  Rule* rule = state.rules_.begin()->second;
+  EXPECT_EQ("link", rule->name_);
+  EXPECT_EQ("foo bar baz", rule->command_.unparsed());
+}
+
 TEST(Parser, Errors) {
   State state;