From a1e443ad657fa44081e7445e3b7f4c2a3543a171 Mon Sep 17 00:00:00 2001 From: Kevin Albert Date: Thu, 29 Oct 2015 17:36:08 -0700 Subject: [PATCH 1/2] Brackets can be escaped --- js/syntree.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/js/syntree.js b/js/syntree.js index e5ac5a3..05d8095 100644 --- a/js/syntree.js +++ b/js/syntree.js @@ -293,9 +293,14 @@ function go(str, font_size, term_font, nonterm_font, vert_space, hor_space, colo // Clean up the string str = str.replace(/^\s+/, ""); var open = 0; + var esc = false; for (var i = 0; i < str.length; i++) { - if (str[i] == "[") open++; - if (str[i] == "]") open--; + if (!esc) { + if (str[i] == "[") open++; + else if (str[i] == "]") open--; + else if (str[i] == "\\") esc = true; + } + esc = false; } while (open < 0) { str = "[" + str; @@ -390,15 +395,16 @@ function parse(str) { function(match, tail) { n.tail = tail; return " "; - }); - str = str.replace(/^\s+/, ""); - str = str.replace(/\s+$/, ""); + }) + .replace(/^\s+/, "") + .replace(/\s+$/, "") + .replace(/\\([\[\]])/g, "$1"); n.value = str; return n; } var i = 1; - while ((str[i] != " ") && (str[i] != "[") && (str[i] != "]")) i++; + while ((str[i] != " ") && (str[i] != "[" || str[i-1] == "\\") && (str[i] != "]" || str[i-1] == "\\")) i++; n.value = str.substr(1, i-1) n.value = n.value.replace(/\^/, function () { @@ -419,8 +425,8 @@ function parse(str) { var start = i; for (; i < str.length; i++) { var temp = level; - if (str[i] == "[") level++; - if (str[i] == "]") level--; + if (str[i] == "[" && str[i-1] != "\\") level++; + if (str[i] == "]" && str[i-1] != "\\") level--; if (((temp == 1) && (level == 2)) || ((temp == 1) && (level == 0))) { if (str.substring(start, i).search(/[^\s]/) > -1) n.children.push(parse(str.substring(start, i))); From e205e28d3a98a437e3f2132b3be61374353abbaf Mon Sep 17 00:00:00 2001 From: Kevin Albert Date: Thu, 29 Oct 2015 17:47:33 -0700 Subject: [PATCH 2/2] . --- index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 40bdcb4..3636040 100644 --- a/index.html +++ b/index.html @@ -22,7 +22,7 @@ Fork me on GitHub

Syntax Tree Generator

- + (C) 2011 by Miles Shang, see license.
@@ -206,4 +206,4 @@

Examples

- \ No newline at end of file +