gpt4 book ai didi

javascript - 自定义规则解析器

转载 作者:行者123 更新时间:2023-11-29 20:18:21 27 4
gpt4 key购买 nike

我有一套面具

面具是这样的

'09{2,9}n(6)'
//read as 09
//[a number between 2 and 9]
//[a random number][repeat expression 6 times]

'029n(7,10)'
//read as 029
//[a random number][repeat expression between 7 and 10 times]

'029n(2,5){8,15}(7,10)n'
//read as 029
//[a random number][repeat expression between 2 and 5 times]
//[a random number between 8 and 15][repeat expression between 7 and 10 times]
//[a random number]

as an example expession 3 would work out as
'029n(4){4,9}(7)n'
'029nnnn{4,9}{4,9}{4,9}{4,9}{4,9}{4,9}{4,9}n
'029nnnn{5}{9}{4}{8}{5}{9}{9}n
'029nnnn5948599n'
'029023559485999'

我需要用 javascript 编写一个解析器,它可以根据这些规则生成一个字符串。请注意,这不是验证,而是字符串生成。

最好的方法是什么?

最佳答案

试用自定义解析器。用作,

var generator = new PatternGenerator('09{2,9}n(6)');
generator.generate(); // 096555555
generator.generate(); // 095000000

检查这个 example .

和构造函数,

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);
}
}

关于javascript - 自定义规则解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5240959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com