Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added options.antlr to support antlr syntax #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ console.log(re.test('96')); //=> false

## Options

### options.antlr

**Type**: `boolean`

**Deafault**: `undefined`

Enable generate regex to support antlr

```js
var toRegexRange = require('to-regex-range');
var source = toRegexRange('-128', '127', {capture: true, antlr: true});
console.log("-128 -> 127 = "+source);

-128 -> 127 = ('-'[1-9]
|'-''12'[0-8]
|'-'?[1-9][0-9]
|'-'?'1'[01][0-9]
|[0-9]
|'12'[0-7])
```

### options.capture

**Type**: `boolean`
Expand Down Expand Up @@ -282,4 +303,4 @@ Released under the [MIT License](LICENSE).

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.7.0, on July 25, 2018._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.7.0, on July 25, 2018._
70 changes: 58 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

'use strict';

const isNumber = require('is-number');
//const isNumber = require('is-number');

function isNumber(n){
var re = new RegExp('^-?[0-9]+$');
//console.log(n+'='+re.test(n));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove all temporary code comments

return re.test(n);
}

function toRegexRange(min, max, options) {
toRegexRange.cache = {};
if (isNumber(min) === false) {
throw new TypeError('toRegexRange: expected the first argument to be a number');
}
Expand Down Expand Up @@ -75,14 +82,25 @@ function toRegexRange(min, max, options) {
return tok.result;
}

toRegexRange.cache = {};

function siftPatterns(neg, pos, options) {
let onlyNegative = filterPatterns(neg, pos, '-', false, options) || [];
let onlyPositive = filterPatterns(pos, neg, '', false, options) || [];
let intersected = filterPatterns(neg, pos, '-?', true, options) || [];
let onlyNegative;
let onlyPositive;
let intersected;
if (options.antlr){
onlyNegative = filterPatterns(neg, pos, "'-'", false, options) || [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this would be better to do in filterPatterns, so it only needs to be done one time.

onlyPositive = filterPatterns(pos, neg, '', false, options) || [];
intersected = filterPatterns(neg, pos, "'-'?", true, options) || [];
}else{
onlyNegative = filterPatterns(neg, pos, '-', false, options) || [];
onlyPositive = filterPatterns(pos, neg, '', false, options) || [];
intersected = filterPatterns(neg, pos, '-?', true, options) || [];
}
let subpatterns = onlyNegative.concat(intersected).concat(onlyPositive);
return subpatterns.join('|');
if (options.antlr){
return subpatterns.join('\n|');
}else{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run eslint to get reports on formatting inconsistencies.

return subpatterns.join('|');
}
}

function splitToRanges(min, max) {
Expand Down Expand Up @@ -129,24 +147,51 @@ function rangeToPattern(start, stop, options) {

let pattern = '';
let digits = 0;
var singleQuote=false;

while (++i < len) {
let numbers = zipped[i];
let startDigit = numbers[0];
let stopDigit = numbers[1];

//console.log("startDigit="+startDigit);
if (startDigit === stopDigit) {
if (options.antlr) {
if (!singleQuote){
pattern="'";
singleQuote=true;
}
}
pattern += startDigit;

} else if (startDigit !== '0' || stopDigit !== '9') {
if (options.antlr) {
if (singleQuote){
pattern+="'";
singleQuote=false;
}
}
pattern += toCharacterClass(startDigit, stopDigit);

} else {
digits += 1;
if (options.antlr){
if (options.antlr) {
if (singleQuote){
pattern+="'";
singleQuote=false;
}
}
pattern += toCharacterClass(startDigit, stopDigit);
}else{
digits += 1;
}
}
}

if (digits) {
if (options.antlr) {
if (singleQuote){
pattern+="'";
}
}
pattern += options.shorthand ? '\\d' : '[0-9]';
}

Expand Down Expand Up @@ -208,11 +253,11 @@ function filterPatterns(arr, comparison, prefix, intersection, options) {
}

if (!intersection && !contains(comparison, 'string', ele)) {
res.push(prefix + ele);
res.push(prefix+ ele);
}

if (intersection && contains(comparison, 'string', ele)) {
res.push(prefix + ele);
res.push(prefix+ ele);
}
}
return res;
Expand Down Expand Up @@ -262,6 +307,7 @@ function toQuantifier(digits) {
if (!stop && (!start || start === 1)) {
return '';
}
//console.log("digits="+digits+' == '+'{' + start + stop + '}');
return '{' + start + stop + '}';
}

Expand Down
4 changes: 4 additions & 0 deletions peter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const toRegexRange = require('./');
var source = toRegexRange('-128', '127', {capture: true, antlr: true});
console.log("-128 -> 127 = "+source);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests should use assertions. Please see the test/test.js file for examples of how tests are done. I would be happy to help if you have questions.