From ac8529c9f55da7fdc1186e3f34313cf411de6e71 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Tue, 16 Sep 2014 22:04:54 -0700
Subject: Re-added backtracking and memoization.

Gives better results for things like

    **foo*
---
 js/stmd.js | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)

(limited to 'js')

diff --git a/js/stmd.js b/js/stmd.js
index c5268d8..ea72b9e 100755
--- a/js/stmd.js
+++ b/js/stmd.js
@@ -289,6 +289,7 @@
         }
 
         this.pos += numdelims;
+        var delimpos = this.pos;
 
         var next_inline;
         var first = [];
@@ -472,36 +473,31 @@
             }
 
         }
+        this.pos = startpos;
+        return null;
 
         switch (state) {
         case 1: // ***a
-            return [{t: 'Str', c: c+c+c}].concat(first);
+            return [{t: 'Emph', c: [{t: 'Str', c: c}]}].concat(first);
         case 2: // **a
             return [{t: 'Str', c: c+c}].concat(first);
         case 3: // *a
             return [{t: 'Str', c: c}].concat(first);
         case 4: // ***a**b
         case 6: // ***a** b
-            return [{t: 'Str', c: c+c+c}]
-                .concat(first,
-                        [{t: 'Str', c: c+c}],
-                        second);
+            return [{t: 'Strong', c:
+                     [{t: 'Str', c: c}].concat(first)}].concat(second);
         case 5: // ***a*b
         case 7: // ***a* b
-            return [{t: 'Str', c: c+c+c}]
-                .concat(first,
-                        [{t: 'Str', c: c}],
-                        second);
+            return [{t: 'Emph', c:
+                     [{t: 'Str', c: c+c}].concat(first)}].concat(second);
         case 8: // **a *b
             return [{t: 'Str', c: c+c}]
                 .concat(first,
                         [{t: 'Str', c: c}],
                         second);
         case 9: // *a **b
-            return [{t: 'Str', c: c}]
-                .concat(first,
-                        [{t: 'Str', c: c+c}],
-                        second);
+            return [{t: 'Emph', c: first.concat([{t: 'Str', c: c}])}].concat(second);
         default:
             console.log("Unknown state, parseEmphasis");
             // shouldn't happen
@@ -783,13 +779,11 @@
     // and returning the inline parsed.
     var parseInline = function() {
         var startpos = this.pos;
-        /*
         var memoized = this.memo[startpos];
         if (memoized) {
             this.pos = memoized.endpos;
             return memoized.inline;
         }
-        */
         var c = this.peek();
         if (!c) {
             return null;
@@ -830,12 +824,10 @@
             this.pos += 1;
             res = [{t: 'Str', c: c}];
         }
-        /*
         if (res) {
             this.memo[startpos] = { inline: res,
                                     endpos: this.pos };
         }
-         */
         return res;
     };
 
@@ -844,7 +836,7 @@
         this.subject = s;
         this.pos = 0;
         this.refmap = refmap || {};
-        // this.memo = {};
+        this.memo = {};
         this.last_emphasis_closer = null;
         var inlines = [];
         var next_inline;
@@ -862,7 +854,7 @@
             last_emphasis_closer: null,  // used by parseEmphasis method
             pos: 0,
             refmap: {},
-            // memo: {},
+            memo: {},
             match: match,
             peek: peek,
             spnl: spnl,
-- 
cgit v1.2.3