about summary refs log tree commit diff stats
path: root/cpp
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-03-14 16:53:51 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-03-14 16:53:51 -0700
commitad796a56e574e91b8760714f54a08bbfec0a6ede (patch)
tree82658e4c2e78c7c6c3844f724330352de120ef16 /cpp
parent580f63893e13900384bdcd7ac21510c3fb0fca87 (diff)
downloadmu-ad796a56e574e91b8760714f54a08bbfec0a6ede.tar.gz
908 - tangle: support nested patterns
Diffstat (limited to 'cpp')
-rw-r--r--cpp/tangle/030tangle.cc20
-rw-r--r--cpp/tangle/030tangle.test.cc7
2 files changed, 27 insertions, 0 deletions
diff --git a/cpp/tangle/030tangle.cc b/cpp/tangle/030tangle.cc
index bf2d3c66..2d1bc191 100644
--- a/cpp/tangle/030tangle.cc
+++ b/cpp/tangle/030tangle.cc
@@ -76,6 +76,19 @@ void process_next_hunk(istream& in, const string& directive, list<string>& out)
       return;
     }
 
+    if (next_tangle_token(directive_stream) == "then") {
+      string pat2 = next_tangle_token(directive_stream);
+      if (pat2 == "") {
+        RAISE << "No target for then directive following " << cmd << ".\n" << die();
+        return;
+      }
+      target = find_substr(out, target, pat2);
+      if (target == out.end()) {
+        RAISE << "Couldn't find target " << pat << " then " << pat2 << '\n' << die();
+        return;
+      }
+    }
+
     indent_all(hunk, target);
 
     if (cmd == "before") {
@@ -264,6 +277,13 @@ list<string>::iterator find_substr(list<string>& in, const string& pat) {
   return in.end();
 }
 
+list<string>::iterator find_substr(list<string>& in, list<string>::iterator p, const string& pat) {
+  for (; p != in.end(); ++p)
+    if (p->find(pat) != NOT_FOUND)
+      return p;
+  return in.end();
+}
+
 list<string>::iterator find_trim(list<string>& in, const string& pat) {
   for (list<string>::iterator p = in.begin(); p != in.end(); ++p)
     if (trim(*p) == pat)
diff --git a/cpp/tangle/030tangle.test.cc b/cpp/tangle/030tangle.test.cc
index 663161ba..cf42d71c 100644
--- a/cpp/tangle/030tangle.test.cc
+++ b/cpp/tangle/030tangle.test.cc
@@ -76,6 +76,13 @@ void test_tangle_replace_tracks_old_lines() {
   CHECK_TRACE_DOESNT_CONTAIN("tangle", "b {");
 }
 
+void test_tangle_nested_patterns() {
+  istringstream in("a\nc\nb\nc\nd\n:(after \"b\" then \"c\")\ne");
+  list<string> dummy;
+  tangle(in, dummy);
+  CHECK_TRACE_CONTENTS("tangle", "acbced");
+}
+
 // todo: include line numbers in tangle errors