Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<a href="https://github.com/mshang/syntree"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
<div id="accordion">
<h3><a href="#">Syntax Tree Generator</a></h3><div style="text-align:left">
<textarea id="i" rows="4">[S [NP This] [VP [V is] [^NP a wug]]]</textarea>
<textarea id="i" rows="4">[S [NP This] [VP [V \[is\]] [^NP a wug]]]</textarea>
(C) 2011 by <a href="http://mshang.ca/">Miles Shang</a>, see <a href="LICENSE.txt">license</a>.
</div>

Expand Down Expand Up @@ -206,4 +206,4 @@ <h3>Examples</h3>
</script>

</body>
</html>
</html>
22 changes: 14 additions & 8 deletions js/syntree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand All @@ -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)));
Expand Down