-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
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'); | ||
} | ||
|
@@ -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) || []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this would be better to do in |
||
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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please run |
||
return subpatterns.join('|'); | ||
} | ||
} | ||
|
||
function splitToRanges(min, max) { | ||
|
@@ -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]'; | ||
} | ||
|
||
|
@@ -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; | ||
|
@@ -262,6 +307,7 @@ function toQuantifier(digits) { | |
if (!stop && (!start || start === 1)) { | ||
return ''; | ||
} | ||
//console.log("digits="+digits+' == '+'{' + start + stop + '}'); | ||
return '{' + start + stop + '}'; | ||
} | ||
|
||
|
There was a problem hiding this comment.
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