lib/rule.js
function Rule (subrules, type, handler, props, groupProps, encoding) {
  var self = this
  var n = subrules.length

  this.props = props

  this.debug = false

  // Used for cloning
  this.subrules = subrules

  // Required by Atok#_resolveRules
  for (var p in groupProps)
    this[p] = groupProps[p]

  // Runtime values for continue props
  this.continue = this.props.continue[0]
  this.continueOnFail = this.props.continue[1]

  this.type = type
  this.handler = handler

  // For debug
  this.prevHandler = null
  this.id = this.type !== null ? this.type : handler
  // Id for debug
  this._id = (handler !== null ? (handler.name || '#emit()') : this.type)

  // Subrule pattern index that matched (-1 if only 1 pattern)
  this.idx = -1

  // Single subrule: special case for trimRight
  this.single = (n === 1)

  // First subrule
  var subrule = this.first = n > 0
    ? SubRule.firstSubRule( subrules[0], this.props, encoding, this.single )
    // Special case: no rule given -> passthrough
    : SubRule.emptySubRule

  // Special case: one empty rule -> tokenize whole buffer
  if (n === 1 && subrule.length === 0) {
    subrule = this.first = SubRule.allSubRule
    // Make infinite loop detection ignore this
    this.length = -1
  } else {
    // First subrule pattern length (max of all patterns if many)
    // - used in infinite loop detection
    this.length = this.first.length
  }

  // Instantiate and link the subrules
  // { test: {Function}
  // , next: {SubRule|undefined}
  // }
  var prev = subrule
  // Many subrules or none
  for (var i = 1; i < n; i++) {
    subrule = SubRule.SubRule( subrules[i], this.props, encoding )
    prev.next = subrule
    subrule.prev = prev // Useful in some edge cases
    prev = subrule
    if (this.length < subrule.length) this.length = subrule.length
  }

  // Last subrule (used for trimRight and matched idx)
  this.last = subrule

  // Set the first and last subrules length based on trim properties
  if (!this.props.trimLeft) this.first.length = 0
  if (!this.single && !this.props.trimRight) this.last.length = 0

  this.next = null
  this.nextFail = null
  this.currentRule = null
}
function Rule (subrules, type, handler, props, groupProps, encoding) {
  var self = this
  var n = subrules.length

  this.props = props

  this.debug = false

  // Used for cloning
  this.subrules = subrules

  // Required by Atok#_resolveRules
  for (var p in groupProps)
    this[p] = groupProps[p]

  // Runtime values for continue props
  this.continue = this.props.continue[0]
  this.continueOnFail = this.props.continue[1]

  this.type = type
  this.handler = handler

  // For debug
  this.prevHandler = null
  this.id = this.type !== null ? this.type : handler
  // Id for debug
  this._id = (handler !== null ? (handler.name || '#emit()') : this.type)

  // Subrule pattern index that matched (-1 if only 1 pattern)
  this.idx = -1

  // Single subrule: special case for trimRight
  this.single = (n === 1)

  // First subrule
  var subrule = this.first = n > 0
    ? SubRule.firstSubRule( subrules[0], this.props, encoding, this.single )
    // Special case: no rule given -> passthrough
    : SubRule.emptySubRule

  // Special case: one empty rule -> tokenize whole buffer
  if (n === 1 && subrule.length === 0) {
    subrule = this.first = SubRule.allSubRule
    // Make infinite loop detection ignore this
    this.length = -1
  } else {
    // First subrule pattern length (max of all patterns if many)
    // - used in infinite loop detection
    this.length = this.first.length
  }

  // Instantiate and link the subrules
  // { test: {Function}
  // , next: {SubRule|undefined}
  // }
  var prev = subrule
  // Many subrules or none
  for (var i = 1; i < n; i++) {
    subrule = SubRule.SubRule( subrules[i], this.props, encoding )
    prev.next = subrule
    subrule.prev = prev // Useful in some edge cases
    prev = subrule
    if (this.length < subrule.length) this.length = subrule.length
  }

  // Last subrule (used for trimRight and matched idx)
  this.last = subrule

  // Set the first and last subrules length based on trim properties
  if (!this.props.trimLeft) this.first.length = 0
  if (!this.single && !this.props.trimRight) this.last.length = 0

  this.next = null
  this.nextFail = null
  this.currentRule = null
}
function Rule ( subrules , type , handler , props , groupProps , encoding ) { var self = this var n = subrules . length this . props = props this . debug = false // Used for cloning this . subrules = subrules // Required by Atok#_resolveRules for ( var p in groupProps ) this [ p ] = groupProps [ p ] // Runtime values for continue props this . continue = this . props . continue [ 0 ] this . continueOnFail = this . props . continue [ 1 ] this . type = type this . handler = handler // For debug this . prevHandler = null this . id = this . type !== null ? this . type : handler // Id for debug this . _id = ( handler !== null ? ( handler . name || '#emit()' ) : this . type ) // Subrule pattern index that matched (-1 if only 1 pattern) this . idx = - 1 // Single subrule: special case for trimRight this . single = ( n === 1 ) // First subrule var subrule = this . first = n > 0 ? SubRule . firstSubRule ( subrules [ 0 ] , this . props , encoding , this . single ) // Special case: no rule given -> passthrough : SubRule . emptySubRule // Special case: one empty rule -> tokenize whole buffer if ( n === 1 && subrule . length === 0 ) { subrule = this . first = SubRule . allSubRule // Make infinite loop detection ignore this this . length = - 1 } else { // First subrule pattern length (max of all patterns if many) // - used in infinite loop detection this . length = this . first . length } // Instantiate and link the subrules // { test: {Function} // , next: {SubRule|undefined} // } var prev = subrule // Many subrules or none for ( var i = 1 ; i < n ; i ++ ) { subrule = SubRule . SubRule ( subrules [ i ] , this . props , encoding ) prev . next = subrule subrule . prev = prev // Useful in some edge cases prev = subrule if ( this . length < subrule . length ) this . length = subrule . length } // Last subrule (used for trimRight and matched idx) this . last = subrule // Set the first and last subrules length based on trim properties if ( ! this . props . trimLeft ) this . first . length = 0 if ( ! this . single && ! this . props . trimRight ) this . last . length = 0 this . next = null this . nextFail = null this . currentRule = null }
Atok Rule constructor

@param {array} list of subrules
@param {string|number|null} rule type (set if handler is not)
@param {function} rule handler (set if type is not)
@param {Object} atok instance
@constructor
@api private
Atok Rule constructor