@@ -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 ) {
0 commit comments