Skip to content

Commit eda4c90

Browse files
committed
* forest.js: better choice of callback fns.
1 parent 9221817 commit eda4c90

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

forest.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ var forest_to_html, process_forest;
77
var result;
88
if(f.hasOwnProperty('next')) { // ambiguous derivation
99
result = [];
10-
while(f) { result.push(process_children(f, fns)); f = f.next; }
10+
while(f) {
11+
var d = fns.derivation(process_children(f, fns));
12+
result.push(d);
13+
f = f.next;
14+
}
1115
result = fns.choices(result);
1216
} else if(f.tag.rule) { // intermediate node
1317
result = process_children(f, fns);
1418
} else { // complete derivation
15-
result = process_children(f, fns);
16-
if(result) result = fns.non_terminal(f.tag, result);
17-
else result = fns.terminal(f.tag);
19+
var d = fns.derivation(process_children(f, fns));
20+
result = fns.symbol(f.tag, d);
1821
}
1922
return result;
2023
}
@@ -34,36 +37,35 @@ var forest_to_html, process_forest;
3437
// Convert parse forest to nested HTML tables
3538

3639
function html_derivation(d) {
37-
var row = tr();
38-
for(var i=0; i<d.length; ++i) row.appendChild(td(d[i]));
39-
return row;
40+
if(d) {
41+
var row = tr();
42+
for(var i=0; i<d.length; ++i) row.appendChild(td(d[i]));
43+
return row;
44+
}
4045
}
4146

4247
function html_choices(choices) {
4348
var html = table();
4449
for(var i=0; i<choices.length; ++i) {
45-
html.appendChild(tr(td(html_derivation(choices[i]))));
50+
html.appendChild(tr(td(choices[i])));
4651
}
4752
return html;
4853
}
4954

50-
function html_terminal(name) { return text(name); }
51-
52-
function html_non_terminal(name, derivation) {
55+
function html_symbol(name, derivation) {
5356
name = text(name);
54-
if(derivation && derivation.length) {
57+
if(derivation) {
5558
var symbol = td(name); symbol.colSpan = derivation.length;
56-
var data = html_derivation(derivation);
5759
var html = table(tr(symbol));
58-
html.appendChild(data);
60+
html.appendChild(derivation);
5961
return html;
60-
} else return symbol;
62+
} else return name;
6163
}
6264

6365
var html_fns = {
6466
choices: html_choices,
65-
terminal: html_terminal,
66-
non_terminal: html_non_terminal
67+
derivation: html_derivation,
68+
symbol: html_symbol
6769
}
6870

6971
forest_to_html = function forest_to_html(f) {

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ <h2><a name="demo">A Trivial Demonstration</a></h2>
320320
<h2><a name="future">Future Work</a></h2>
321321

322322
<p>I'm excited about finally getting this algorithm figured out and running, so
323-
I'm writing it up and posting it right away. But to be really usable at all it
323+
I'm writing it up and posting it right away. But to be at all "finished" it
324324
needs a means to enter grammars in a convenient notation, a way to choose a
325325
parse tree from the forest, and a way to attach actions to rules. I hope to
326326
get to this soon, but realistically it might be a month or three before I get

0 commit comments

Comments
 (0)