Sha256: 24e5635640cbb460de04d17b80e4108286b033ba3d9e67afa439e3b3bdf644ae

Contents?: true

Size: 1.12 KB

Versions: 10

Compression:

Stored size: 1.12 KB

Contents

/**
 * The MIT License (MIT)
 * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
 */

'use strict';

/**
 * A regexp-tree plugin to replace different range-based quantifiers
 * with their symbol equivalents.
 *
 * a{0,} -> a*
 * a{1,} -> a+
 * a{1} -> a
 *
 * NOTE: the following is automatically handled in the generator:
 *
 * a{3,3} -> a{3}
 */

module.exports = {
  Quantifier: function Quantifier(path) {
    var node = path.node;


    if (node.kind !== 'Range') {
      return;
    }

    // a{0,} -> a*
    rewriteOpenZero(path);

    // a{1,} -> a+
    rewriteOpenOne(path);

    // a{1} -> a
    rewriteExactOne(path);
  }
};

function rewriteOpenZero(path) {
  var node = path.node;


  if (node.from !== 0 || node.to) {
    return;
  }

  node.kind = '*';
  delete node.from;
}

function rewriteOpenOne(path) {
  var node = path.node;


  if (node.from !== 1 || node.to) {
    return;
  }

  node.kind = '+';
  delete node.from;
}

function rewriteExactOne(path) {
  var node = path.node;


  if (node.from !== 1 || node.to !== 1) {
    return;
  }

  path.parentPath.replace(path.parentPath.node.expression);
}

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
condenser-0.0.8 lib/condenser/processors/node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
jester-data-8.0.0 node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
ezii-os-5.2.1 node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
ezii-os-2.0.1 node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
ezii-os-1.1.0 node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
ezii-os-1.0.0 node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
condenser-0.0.7 lib/condenser/processors/node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
ezii-os-0.0.0.1.0 node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
ezii-os-0.0.0.0.1 node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js
condenser-0.0.5 lib/condenser/processors/node_modules/regexp-tree/dist/optimizer/transforms/quantifier-range-to-symbol-transform.js