samples/JavaScript/ccalc-lex.js
// resets the lexer, sets new input
resets the lexer, sets new input
resets the lexer sets new input
function lexer_setInput(input, yy) {
        this.yy = yy || this.yy || {};
        if (!this.__decompressed) {
          var rules = this.rules;
          for (var i = 0, len = rules.length; i < len; i++) {
            var rule_re = rules[i];
            if (typeof rule_re === 'number') {
              rules[i] = rules[rule_re];
            }
          }
          var conditions = this.conditions;
          for (var k in conditions) {
            var spec = conditions[k];
            var rule_ids = spec.rules;
            var len = rule_ids.length;
            var rule_regexes = new Array(len + 1);            
            var rule_new_ids = new Array(len + 1);
            if (this.rules_prefix1) {
                var rule_prefixes = new Array(65536);
                var first_catch_all_index = 0;
                for (var i = 0; i < len; i++) {
                  var idx = rule_ids[i];
                  var rule_re = rules[idx];
                  rule_regexes[i + 1] = rule_re;
                  rule_new_ids[i + 1] = idx;
                  var prefix = this.rules_prefix1[idx];
                  if (typeof prefix === 'number') {
                    prefix = this.rules_prefix1[prefix];
                  }
                  if (!prefix) {
                    if (!first_catch_all_index) {
                      first_catch_all_index = i + 1;
                    }
                  } else {
                    for (var j = 0, pfxlen = prefix.length; j < pfxlen; j++) {
                      var pfxch = prefix.charCodeAt(j);
                      if (!rule_prefixes[pfxch]) {
                        rule_prefixes[pfxch] = i + 1;
                      }  
                    }
                  }
                }
                To speed up their discovery at run-time while keeping the
                remainder of the lexer kernel code very simple (and fast),
                we point these to an 'illegal' rule set index *beyond*
                the end of the rule set.
                if (!first_catch_all_index) {
                  first_catch_all_index = len + 1;
                }
                for (var i = 0; i < 65536; i++) {
                  if (!rule_prefixes[i]) {
                    rule_prefixes[i] = first_catch_all_index; 
                  }
                }
                spec.__dispatch_lut = rule_prefixes;
            } else {
                for (var i = 0; i < len; i++) {
                  var idx = rule_ids[i];
                  var rule_re = rules[idx];
                  rule_regexes[i + 1] = rule_re;
                  rule_new_ids[i + 1] = idx;
                }
            }
            spec.rules = rule_new_ids;
            spec.__rule_regexes = rule_regexes;
            spec.__rule_count = len;
          }
          this.__decompressed = true;
        }
        this._input = input || '';
        this.clear();
        this._signaled_error_token = false;
        this.done = false;
        this.yylineno = 0;
        this.matched = '';
        this.conditionStack = ['INITIAL'];
        this.__currentRuleSet__ = null;
        this.yylloc = {
            first_line: 1,
            first_column: 0,
            last_line: 1,
            last_column: 0
        };
        if (this.options.ranges) {
            this.yylloc.range = [0, 0];
        }
        this.offset = 0;
        return this;
    }
function lexer_setInput ( input , yy ) { this . yy = yy || this . yy || { } ; if ( ! this . __decompressed ) { var rules = this . rules ; for ( var i = 0 , len = rules . length ; i < len ; i ++ ) { var rule_re = rules [ i ] ; if ( typeof rule_re === 'number' ) { rules [ i ] = rules [ rule_re ] ; } } var conditions = this . conditions ; for ( var k in conditions ) { var spec = conditions [ k ] ; var rule_ids = spec . rules ; var len = rule_ids . length ; var rule_regexes = new Array ( len + 1 ) ; var rule_new_ids = new Array ( len + 1 ) ; if ( this . rules_prefix1 ) { var rule_prefixes = new Array ( 65536 ) ; var first_catch_all_index = 0 ; for ( var i = 0 ; i < len ; i ++ ) { var idx = rule_ids [ i ] ; var rule_re = rules [ idx ] ; rule_regexes [ i + 1 ] = rule_re ; rule_new_ids [ i + 1 ] = idx ; var prefix = this . rules_prefix1 [ idx ] ; if ( typeof prefix === 'number' ) { prefix = this . rules_prefix1 [ prefix ] ; } if ( ! prefix ) { if ( ! first_catch_all_index ) { first_catch_all_index = i + 1 ; } } else { for ( var j = 0 , pfxlen = prefix . length ; j < pfxlen ; j ++ ) { var pfxch = prefix . charCodeAt ( j ) ; if ( ! rule_prefixes [ pfxch ] ) { rule_prefixes [ pfxch ] = i + 1 ; } } } } if ( ! first_catch_all_index ) { first_catch_all_index = len + 1 ; } for ( var i = 0 ; i < 65536 ; i ++ ) { if ( ! rule_prefixes [ i ] ) { rule_prefixes [ i ] = first_catch_all_index ; } } spec . __dispatch_lut = rule_prefixes ; } else { for ( var i = 0 ; i < len ; i ++ ) { var idx = rule_ids [ i ] ; var rule_re = rules [ idx ] ; rule_regexes [ i + 1 ] = rule_re ; rule_new_ids [ i + 1 ] = idx ; } } spec . rules = rule_new_ids ; spec . __rule_regexes = rule_regexes ; spec . __rule_count = len ; } this . __decompressed = true ; } this . _input = input || '' ; this . clear ( ) ; this . _signaled_error_token = false ; this . done = false ; this . yylineno = 0 ; this . matched = '' ; this . conditionStack = [ 'INITIAL' ] ; this . __currentRuleSet__ = null ; this . yylloc = { first_line : 1 , first_column : 0 , last_line : 1 , last_column : 0 } ; if ( this . options . ranges ) { this . yylloc . range = [ 0 , 0 ] ; } this . offset = 0 ; return this ; }
resets the lexer, sets new input
resets the lexer sets new input
// also check if we've fully initialized the lexer instance, // including expansion work to be done to go from a loaded // lexer to a usable lexer: // step 1: decompress the regex list: // compression: is the RE an xref to another RE slot in the rules[] table? // step 2: unfold the conditions[] set to make these ready for use: // slot 0 is unused; we use a 1-based index approach here to keep the hottest code in `lexer_next()` fast and simple! // compression: is the PREFIX-STRING an xref to another PREFIX-STRING slot in the rules_prefix1[] table? // init the prefix lookup table: first come, first serve... // first come, first serve: // if no catch-all prefix has been encountered yet, it means all // rules have limited prefix sets and it MAY be that particular // input characters won't be recognized by any rule in this  // condition state. //  // To speed up their discovery at run-time while keeping the // remainder of the lexer kernel code very simple (and fast), // we point these to an 'illegal' rule set index *beyond* // the end of the rule set.