Испытание пользовательского парсера.Используйте как,
var generator = new PatternGenerator('09{2,9}n(6)');
generator.generate(); // 096555555
generator.generate(); // 095000000
Оформить заказ пример .
И функция конструктора,
function PatternGenerator(pattern) {
var tokens = null;
this.generate = function() {
var stack = [];
tokens = pattern.split('');
// Read each token and add
while (tokens.length) {
var token = lookahead();
if (isDigit(token)) {
stack.push(consumeNumber());
}
else if (token == "n") {
stack.push(consumeVariableNumber());
}
else if (token == "(") {
var topObject = stack.pop();
stack.push(consumeRepetition(topObject));
}
else if (token == "{") {
stack.push(consumeVariableRangeNumber());
}
else {
throw new Error("Invalid input");
}
}
return stack.join('');
}
// [0-9]+
function consumeNumber() {
var number = "";
while (isDigit(lookahead())) {
number += consume();
}
return number;
}
// "n"
function VariableNumber() {
var number = generateRandomNumber();
this.toString = function() {
return Number(number);
};
}
function consumeVariableNumber() {
consume();
return new VariableNumber();
}
// {x, y}
function VariableRangeNumber(start, end) {
var number = generateRandomNumberBetween(start, end);
this.toString = function() {
return Number(number);
};
}
function consumeVariableRangeNumber() {
consume(); // {
var firstNumber = consumeNumber();
consume(); // ,
var secondNumber = consumeNumber();
consume(); // }
return new VariableRangeNumber(firstNumber, secondNumber);
}
// <expression>(x)
function Repeat(object, times) {
this.toString = function() {
var string = "";
for (var i = 0; i < times; i++) {
string += object;
}
return string;
};
}
// <expression>(x, y)
function RepeatWithRange(object, start, end) {
var times = generateRandomNumberBetween(start, end);
this.toString = function() {
return new Repeat(object, times).toString();
};
}
function consumeRepetition(object) {
consume(); // (
var firstNumber, secondNumber;
var firstNumber = consumeNumber();
if (lookahead() == ",") {
consume(); // ,
secondNumber = consumeNumber();
}
consume(); // )
if (typeof secondNumber == 'undefined') {
return new Repeat(objectToRepeat, firstNumber);
}
else {
return new RepeatWithRange(object, firstNumber, secondNumber);
}
}
// Helpers to generate random integers
function generateRandomNumber() {
var MAX = Math.pow(2, 52);
return generateRandomNumberBetween(0, MAX);
}
function generateRandomNumberBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function lookahead() {
return tokens[0];
}
function consume() {
return tokens.shift();
}
function isDigit(character) {
return /\d/.test(character);
}
}