PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tryocaml/js_of_ocaml-patched/runtime/parsing.js

http://github.com/cago/tryocaml
JavaScript | 236 lines | 182 code | 16 blank | 38 comment | 38 complexity | 219f3883d33489065c5935fee9ab14eb MD5 | raw file
Possible License(s): GPL-2.0
  1/***********************************************************************/
  2/*                                                                     */
  3/*                           Objective Caml                            */
  4/*                                                                     */
  5/*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
  6/*                                                                     */
  7/*  Copyright 1996 Institut National de Recherche en Informatique et   */
  8/*  en Automatique.  All rights reserved.  This file is distributed    */
  9/*  under the terms of the GNU Library General Public License, with    */
 10/*  the special exception on linking described in file ../LICENSE.     */
 11/*                                                                     */
 12/***********************************************************************/
 13
 14/* $Id: parsing.c 8983 2008-08-06 09:38:25Z xleroy $ */
 15
 16/* The PDA automaton for parsers generated by camlyacc */
 17
 18/* The pushdown automata */
 19
 20//Provides: caml_parse_engine
 21//Requires: caml_lex_array
 22function caml_parse_engine(tables, env, cmd, arg)
 23{
 24  var ERRCODE = 256;
 25
 26  var START = 0;
 27  var TOKEN_READ = 1;
 28  var STACKS_GROWN_1 = 2;
 29  var STACKS_GROWN_2 = 3;
 30  var SEMANTIC_ACTION_COMPUTED = 4;
 31  var ERROR_DETECTED = 5;
 32  var loop = 6;
 33  var testshift = 7;
 34  var shift = 8;
 35  var shift_recover = 9;
 36  var reduce = 10;
 37
 38  var READ_TOKEN = 0;
 39  var RAISE_PARSE_ERROR = 1;
 40  var GROW_STACKS_1 = 2;
 41  var GROW_STACKS_2 = 3;
 42  var COMPUTE_SEMANTIC_ACTION = 4;
 43  var CALL_ERROR_FUNCTION = 5;
 44
 45  var env_s_stack = 1;
 46  var env_v_stack = 2;
 47  var env_symb_start_stack = 3;
 48  var env_symb_end_stack = 4;
 49  var env_stacksize = 5;
 50  var env_stackbase = 6;
 51  var env_curr_char = 7;
 52  var env_lval = 8;
 53  var env_symb_start = 9;
 54  var env_symb_end = 10;
 55  var env_asp = 11;
 56  var env_rule_len = 12;
 57  var env_rule_number = 13;
 58  var env_sp = 14;
 59  var env_state = 15;
 60  var env_errflag = 16;
 61
 62  var tbl_actions = 1;
 63  var tbl_transl_const = 2;
 64  var tbl_transl_block = 3;
 65  var tbl_lhs = 4;
 66  var tbl_len = 5;
 67  var tbl_defred = 6;
 68  var tbl_dgoto = 7;
 69  var tbl_sindex = 8;
 70  var tbl_rindex = 9;
 71  var tbl_gindex = 10;
 72  var tbl_tablesize = 11;
 73  var tbl_table = 12;
 74  var tbl_check = 13;
 75  var tbl_error_function = 14;
 76  var tbl_names_const = 15;
 77  var tbl_names_block = 16;
 78
 79  if (!tables.dgoto) {
 80    tables.defred = caml_lex_array (tables[tbl_defred]);
 81    tables.sindex = caml_lex_array (tables[tbl_sindex]);
 82    tables.check  = caml_lex_array (tables[tbl_check]);
 83    tables.rindex = caml_lex_array (tables[tbl_rindex]);
 84    tables.table  = caml_lex_array (tables[tbl_table]);
 85    tables.len    = caml_lex_array (tables[tbl_len]);
 86    tables.lhs    = caml_lex_array (tables[tbl_lhs]);
 87    tables.gindex = caml_lex_array (tables[tbl_gindex]);
 88    tables.dgoto  = caml_lex_array (tables[tbl_dgoto]);
 89  }
 90
 91  var res = 0, n, n1, n2, state1;
 92
 93  // RESTORE
 94  var sp = env[env_sp];
 95  var state = env[env_state];
 96  var errflag = env[env_errflag];
 97
 98  exit:for (;;) {
 99    switch(cmd) {
100    case START:
101      state = 0;
102      errflag = 0;
103      // Fall through
104
105    case loop:
106      n = tables.defred[state];
107      if (n != 0) { cmd = reduce; break; }
108      if (env[env_curr_char] >= 0) { cmd = testshift; break; }
109      res = READ_TOKEN;
110      break exit;
111                                  /* The ML code calls the lexer and updates */
112                                  /* symb_start and symb_end */
113    case TOKEN_READ:
114      if (arg instanceof Array) {
115        env[env_curr_char] = tables[tbl_transl_block][arg[0] + 1];
116        env[env_lval] = arg[1];
117      } else {
118        env[env_curr_char] = tables[tbl_transl_const][arg + 1];
119        env[env_lval] = 0;
120      }
121      // Fall through
122
123    case testshift:
124      n1 = tables.sindex[state];
125      n2 = n1 + env[env_curr_char];
126      if (n1 != 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] &&
127          tables.check[n2] == env[env_curr_char]) {
128        cmd = shift; break;
129      }
130      n1 = tables.rindex[state];
131      n2 = n1 + env[env_curr_char];
132      if (n1 != 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] &&
133          tables.check[n2] == env[env_curr_char]) {
134        n = tables.table[n2];
135        cmd = reduce; break;
136      }
137      if (errflag <= 0) {
138        res = CALL_ERROR_FUNCTION;
139        break exit;
140      }
141      // Fall through
142                                  /* The ML code calls the error function */
143    case ERROR_DETECTED:
144      if (errflag < 3) {
145        errflag = 3;
146        for (;;) {
147          state1 = env[env_s_stack][sp + 1];
148          n1 = tables.sindex[state1];
149          n2 = n1 + ERRCODE;
150          if (n1 != 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] &&
151              tables.check[n2] == ERRCODE) {
152            cmd = shift_recover; break;
153          } else {
154            if (sp <= env[env_stackbase]) return RAISE_PARSE_ERROR;
155                                    /* The ML code raises Parse_error */
156            sp--;
157          }
158        }
159      } else {
160        if (env[env_curr_char] == 0) return RAISE_PARSE_ERROR;
161                                    /* The ML code raises Parse_error */
162        env[env_curr_char] = -1;
163        cmd = loop; break;
164      }
165      // Fall through
166    case shift:
167      env[env_curr_char] = -1;
168      if (errflag > 0) errflag--;
169      // Fall through
170    case shift_recover:
171      state = tables.table[n2];
172      sp++;
173      if (sp >= env[env_stacksize]) {
174        res = GROW_STACKS_1;
175        break exit;
176      }
177      // Fall through
178                                   /* The ML code resizes the stacks */
179    case STACKS_GROWN_1:
180      env[env_s_stack][sp + 1] = state;
181      env[env_v_stack][sp + 1] = env[env_lval];
182      env[env_symb_start_stack][sp + 1] = env[env_symb_start];
183      env[env_symb_end_stack][sp + 1] = env[env_symb_end];
184      cmd = loop;
185      break;
186
187    case reduce:
188      var m = tables.len[n];
189      env[env_asp] = sp;
190      env[env_rule_number] = n;
191      env[env_rule_len] = m;
192      sp = sp - m + 1;
193      m = tables.lhs[n];
194      state1 = env[env_s_stack][sp];
195      n1 = tables.gindex[m];
196      n2 = n1 + state1;
197      if (n1 != 0 && n2 >= 0 && n2 <= tables[tbl_tablesize] &&
198          tables.check[n2] == state1)
199        state = tables.table[n2];
200      else
201        state = tables.dgoto[m];
202      if (sp >= env[env_stacksize]) {
203        res = GROW_STACKS_2;
204        break exit;
205      }
206      // Fall through
207                                  /* The ML code resizes the stacks */
208    case STACKS_GROWN_2:
209      res = COMPUTE_SEMANTIC_ACTION;
210      break exit;
211                                  /* The ML code calls the semantic action */
212    case SEMANTIC_ACTION_COMPUTED:
213      env[env_s_stack][sp + 1] = state;
214      env[env_v_stack][sp + 1] = arg;
215      var asp = env[env_asp];
216      env[env_symb_end_stack][sp + 1] = env[env_symb_end_stack][asp + 1];
217      if (sp > asp) {
218        /* This is an epsilon production. Take symb_start equal to symb_end. */
219        env[env_symb_start_stack][sp + 1] = env[env_symb_end_stack][asp + 1];
220      }
221      cmd = loop; break;
222                                  /* Should not happen */
223    default:
224      return RAISE_PARSE_ERROR;
225    }
226  }
227  // SAVE
228  env[env_sp] = sp;
229  env[env_state] = state;
230  env[env_errflag] = errflag;
231  return res;
232}
233
234//Provides: caml_set_parser_trace const
235//Dummy function!
236function caml_set_parser_trace() { return 0; }