1717
1818/* The pushdown automata */
1919
20+ //Provides: caml_parser_trace
21+ var caml_parser_trace = 0 ;
22+
2023//Provides: caml_parse_engine
21- //Requires: caml_lex_array
24+ //Requires: caml_lex_array, caml_parser_trace,caml_jsstring_of_string
25+ //Requires: caml_ml_output, caml_ml_string_length, caml_string_of_jsbytes
26+ //Requires: caml_jsbytes_of_string, MlBytes
2227function caml_parse_engine ( tables , env , cmd , arg )
2328{
2429 var ERRCODE = 256 ;
@@ -73,8 +78,42 @@ function caml_parse_engine(tables, env, cmd, arg)
7378 var tbl_table = 12 ;
7479 var tbl_check = 13 ;
7580 // var _tbl_error_function = 14;
76- // var _tbl_names_const = 15;
77- // var _tbl_names_block = 16;
81+ var tbl_names_const = 15 ;
82+ var tbl_names_block = 16 ;
83+
84+
85+ function log ( x ) {
86+ var s = caml_string_of_jsbytes ( x + "\n" ) ;
87+ caml_ml_output ( 2 , s , 0 , caml_ml_string_length ( s ) ) ;
88+ }
89+
90+ function token_name ( names , number )
91+ {
92+ var str = caml_jsstring_of_string ( names ) ;
93+ if ( str [ 0 ] == '\x00' )
94+ return "<unknown token>" ;
95+ return str . split ( '\x00' ) [ number ] ;
96+ }
97+
98+ function print_token ( state , tok )
99+ {
100+ var token , kind ;
101+ if ( tok instanceof Array ) {
102+ token = token_name ( tables [ tbl_names_block ] , tok [ 0 ] ) ;
103+ if ( typeof tok [ 1 ] == "number" )
104+ kind = "" + tok [ 1 ] ;
105+ else if ( typeof tok [ 1 ] == "string" )
106+ kind = tok [ 1 ]
107+ else if ( tok [ 1 ] instanceof MlBytes )
108+ kind = caml_jsbytes_of_string ( tok [ 1 ] )
109+ else
110+ kind = "_"
111+ log ( "State " + state + ": read token " + token + "(" + kind + ")" ) ;
112+ } else {
113+ token = token_name ( tables [ tbl_names_const ] , tok ) ;
114+ log ( "State " + state + ": read token " + token ) ;
115+ }
116+ }
78117
79118 if ( ! tables . dgoto ) {
80119 tables . defred = caml_lex_array ( tables [ tbl_defred ] ) ;
@@ -118,6 +157,7 @@ function caml_parse_engine(tables, env, cmd, arg)
118157 env [ env_curr_char ] = tables [ tbl_transl_const ] [ arg + 1 ] ;
119158 env [ env_lval ] = 0 ;
120159 }
160+ if ( caml_parser_trace ) print_token ( state , arg ) ;
121161 // Fall through
122162
123163 case 7 ://testshift:
@@ -149,16 +189,26 @@ function caml_parse_engine(tables, env, cmd, arg)
149189 n2 = n1 + ERRCODE ;
150190 if ( n1 != 0 && n2 >= 0 && n2 <= tables [ tbl_tablesize ] &&
151191 tables . check [ n2 ] == ERRCODE ) {
192+ if ( caml_parser_trace )
193+ log ( "Recovering in state " + state1 ) ;
152194 cmd = shift_recover ; break next;
153195 } else {
154- if ( sp <= env [ env_stackbase ] ) return RAISE_PARSE_ERROR ;
196+ if ( caml_parser_trace )
197+ log ( "Discarding state " + state1 ) ;
198+ if ( sp <= env [ env_stackbase ] ) {
199+ if ( caml_parser_trace )
200+ log ( "No more states to discard" ) ;
201+ return RAISE_PARSE_ERROR ;
202+ }
155203 /* The ML code raises Parse_error */
156204 sp -- ;
157205 }
158206 }
159207 } else {
160- if ( env [ env_curr_char ] == 0 ) return RAISE_PARSE_ERROR ;
161- /* The ML code raises Parse_error */
208+ if ( env [ env_curr_char ] == 0 )
209+ return RAISE_PARSE_ERROR ; /* The ML code raises Parse_error */
210+ if ( caml_parser_trace )
211+ log ( "Discarding last token read" ) ;
162212 env [ env_curr_char ] = - 1 ;
163213 cmd = loop ; break ;
164214 }
@@ -168,6 +218,8 @@ function caml_parse_engine(tables, env, cmd, arg)
168218 if ( errflag > 0 ) errflag -- ;
169219 // Fall through
170220 case 9 ://shift_recover:
221+ if ( caml_parser_trace )
222+ log ( "State " + state + ": shift to state " + tables . table [ n2 ] ) ;
171223 state = tables . table [ n2 ] ;
172224 sp ++ ;
173225 if ( sp >= env [ env_stacksize ] ) {
@@ -185,6 +237,8 @@ function caml_parse_engine(tables, env, cmd, arg)
185237 break ;
186238
187239 case 10 ://reduce:
240+ if ( caml_parser_trace )
241+ log ( "State " + state + ": reduce by rule " + n ) ;
188242 var m = tables . len [ n ] ;
189243 env [ env_asp ] = sp ;
190244 env [ env_rule_number ] = n ;
@@ -232,5 +286,9 @@ function caml_parse_engine(tables, env, cmd, arg)
232286}
233287
234288//Provides: caml_set_parser_trace const
235- //Dummy function!
236- function caml_set_parser_trace ( ) { return 0 ; }
289+ //Requires: caml_parser_trace
290+ function caml_set_parser_trace ( bool ) {
291+ var oldflag = caml_parser_trace ;
292+ caml_parser_trace = bool ;
293+ return oldflag ;
294+ }
0 commit comments