简体   繁体   English

帮助创建peg.js解析器

[英]help creating peg.js parser

I am wanting to create the parser, and expression syntax in peg.js that would allow me to do these things 我想在peg.js中创建解析器和表达式语法,这将允许我做这些事情

basically I want to pass in a mask and have a number output. 基本上我想传递一个掩码并有一个数字输出。

the mask has these abilities. 面具有这些能力。

1) generate a random number between 0-9 (character n for expression? ) 1)生成0-9之间的随机数(表达式的字符n ?)
2) generate a random number between x and y( (x,y) for expression?) 2)在x和y之间生成一个随机数((x,y)用于表达式?)
3) literal numbers are valid (hopefully nothing needed for expession?) 3)字面数字是有效的(希望没有任何需要的东西?)
4) repeat previous expression x times({x} for expression?) 4)重复前面的表达式x次(表达式为{x}?)
5) repeat previous expression between x and y times({x,y} for expression?) 5)重复x和y次之间的前一个表达式({x,y}表达式?)

so an example expression could be 所以一个例子表达式可能是

027n(5,9){4}n12{2,8}(2,4)

the proposed expression syntax above is only an example, it can change. 上面提出的表达式语法只是一个例子,它可以改变。

can anyone provide help in creating the parser for this in peg.js ? 任何人都可以在peg.js中为此创建解析器提供帮助吗?

The idea is to make it generate a JavaScript function, which when executed, will return a random string according to the mask. 我们的想法是让它生成一个JavaScript函数,该函数在执行时将根据掩码返回一个随机字符串。

A literal number is any character between 0 to 9, so make it generate a function that returns itself. 文字数字是介于0到9之间的任何字符,因此要生成一个返回自身的函数。

literal_number
 = num:[0-9]
 { return function() {
     return num;
 }; }

Then n is for a random number. 那么n是一个随机数。 Again, this generates a function to return a random number. 同样,这会生成一个返回随机数的函数。 I added + '' to convert it to a string before returning. 在返回之前,我添加了+ ''将其转换为字符串。

random_number
 = "n"
 { return function() {
     return Math.floor(Math.random() * 10) + '';
 }; }

In the (a,b) syntax, a and b are numbers, so we need to make it parse and return a number. (a,b)语法中, ab是数字,因此我们需要对其进行解析并返回一个数字。 Using the declaration from the calculator example: 使用计算器示例中的声明:

number
 = digits:[0-9]+ { return parseInt(digits.join(""), 10); }

Then we can move on to create a rule for (a,b) syntax. 然后我们可以继续为(a,b)语法创建规则。

random_number_between
 = "(" a:number "," b:number ")"
 { return function() {
     return a + Math.floor(Math.random() * (b - a + 1)) + ''
 }; }

So, these 3 things (literal_number, random_number, random_number_between) combine into a single expression that generates a valid function. 因此,这三件事(literal_number,random_number,random_number_between)组合成一个生成有效函数的表达式。

single_expression
 = random_number
 / random_number_between
 / literal_number

A single expression, followed by {n} or {a,b} forms a repeated expression. 单个表达式,后跟{n}{a,b}形成重复表达式。 A single expression is also a repeated expression, repeated one time. 单个表达式也是重复表达式,重复一次。

The idea of a repeated expression is, given a function, return a function that calls the input function N times, collect the result, and return it. 重复表达式的思想是,给定一个函数,返回一个函数,该函数调用输入函数N次,收集结果并返回它。

repeated_expression
 = ex:single_expression "{" n:number "}" {
       return function() {
           var result = '';
           for (var i = 0; i < n; i ++) {
               result += ex();
           }
           return result;
       };
   }
 / ex:single_expression "{" a:number "," b:number "}" {
       return function() {
           var result = '';
           var n = a + Math.floor(Math.random() * (b - a + 1))
           for (var i = 0; i < n; i ++) {
               result += ex();
           }
           return result;
       };
   }
 / ex:single_expression

Finally, repeated expressions can be put next to each other to concatenate. 最后,重复的表达式可以彼此相邻以进行连接。

expression
 = list:repeated_expression* {
       return function() {
           var result = '';
           for (var i = 0; i < list.length; i ++) {
               result += list[i]();
           }
           return result;
       };
   }

Finally, you need a starting point, which defines a mask. 最后,您需要一个定义遮罩的起点。 This final bit scans the expression which returns a generates a function, and call it. 最后一位扫描表达式,返回a生成一个函数,然后调用它。 Put the following at the top , and when you try it online, it will generate a string of numbers according to your mask. 将以下内容放在顶部 ,当您在线尝试时,它将根据您的掩码生成一串数字。

mask
 = ex:expression
 { return ex() }

Example run: 027n(5,9){4}n12{2,8}(2,4) gives 0271568891222224 . 实施例操作: 027n(5,9){4}n12{2,8}(2,4)得到0271568891222224

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM