本文介绍了在PEG.js上为STRING.STRING.STRING进行语法表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找与以下对象匹配的peg.js语法表达式:
I am looking for a peg.js grammar expression for matching against:
-
"variable"
#失败 -
"variable."
#失败 -
""
#失败 -
"variable.variable"
#确定 -
"variable.variable.variable.variable.variable"
#确定
"variable"
# Fails"variable."
# Fails""
# Fails"variable.variable"
# Ok"variable.variable.variable.variable.variable"
#Ok
-
{PATH: "variable.variable"}
-
{PATH: "variable.variable.variable.variable.variable"}
{PATH: "variable.variable"}
{PATH: "variable.variable.variable.variable.variable"}
start =
PATH_EXP
STRING_EXP =
chars:[0-9a-zA-Z_]+ { return chars.join(""); }
PATH_EXP =
path:(STRING_EXP "." STRING_EXP) { return {PATH: path.join("")}; }
我不知道如何使表达式重复,但也使其成为可选.
I don't know how to make the expression repeat, but also make it optional.
推荐答案
这就是我想出的摆脱."的原因.人物.我承认我以前从未使用过peg.js:)
Here's what I came up with to get rid of the "." characters. I'll admit that I've never used peg.js before :)
PATH_EXP =
(first:STRING_EXP rest:("." STRING_EXP)*) {
return {
PATH: first +
rest.map(function(v) {
return v[1];
}).join("")
};
}
edit —哦,等等,这样更好:
edit — oh wait this is better:
PATH_EXP =
first:STRING_EXP rest:("." s:STRING_EXP { return "." + s; })+ {
return {
PATH: first + rest.join('')
};
}
edit —清楚地知道是否要使用."您会在第二部分的动作中将其包括在内的字符.错过了那部分问题.
edit — clearly if you want the "." characters you'd include them in the action inside that second part. Missed that part of the question.
这篇关于在PEG.js上为STRING.STRING.STRING进行语法表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!