如何使用 peg.js 为更改行创建反斜杠?
How can i make a backslash for change line with peg.js?
目前,我使用'\n'来分割每一行(段落),但现在,我想为换行做一个像python这样的反斜杠。
这是我当前的规则:
Start
= _ head:Line tail:('\n'+ l:Line { return l })* _ {
return [head].concat(tail)
}
Line = (!'\n' .)+ { return { lines: [ text() ] } }
_ = [ \n\r\t]*
输入:
This is Line 1
This is \ Line
\ 2
当前输出:
[
{
"lines": [
"This is Line 1"
]
},
{
"lines": [
"This is \ Line"
]
},
{
"lines": [
"\ 2"
]
}
]
预期输出:
[
{
"lines": [
"This is Line 1"
]
},
{
"lines": [
"This is",
"Line",
"2"
]
},
]
哦,我自己实现了,这是我的代码:
{
var lines = [];
var lineText = "";
function resetLines() {
lines = [];
lineText = "";
}
}
Start
= _ head:Line tail:("\n"+ l:Line { return l; })* _ {
return [head].concat(tail);
}
Line
= ResetLines
chars:(
"\n"? "\" "\n"? {
lines.push(lineText);
lineText = "";
}
/ !"\n" . { lineText += text(); }
)+ {
lines.push(lineText);
return lines;
}
ResetLines = &" "* { (lines = []), (lineText = ""); }
_ = [ \n\r\t]*
输入:
This is Line 1
This is\Line
This is Line 3
输出:
[
[
"This is Line 1"
],
[
"This is",
"Line",
"2"
],
[
"This is Line 3"
]
]
目前,我使用'\n'来分割每一行(段落),但现在,我想为换行做一个像python这样的反斜杠。
这是我当前的规则:
Start
= _ head:Line tail:('\n'+ l:Line { return l })* _ {
return [head].concat(tail)
}
Line = (!'\n' .)+ { return { lines: [ text() ] } }
_ = [ \n\r\t]*
输入:
This is Line 1
This is \ Line
\ 2
当前输出:
[
{
"lines": [
"This is Line 1"
]
},
{
"lines": [
"This is \ Line"
]
},
{
"lines": [
"\ 2"
]
}
]
预期输出:
[
{
"lines": [
"This is Line 1"
]
},
{
"lines": [
"This is",
"Line",
"2"
]
},
]
哦,我自己实现了,这是我的代码:
{
var lines = [];
var lineText = "";
function resetLines() {
lines = [];
lineText = "";
}
}
Start
= _ head:Line tail:("\n"+ l:Line { return l; })* _ {
return [head].concat(tail);
}
Line
= ResetLines
chars:(
"\n"? "\" "\n"? {
lines.push(lineText);
lineText = "";
}
/ !"\n" . { lineText += text(); }
)+ {
lines.push(lineText);
return lines;
}
ResetLines = &" "* { (lines = []), (lineText = ""); }
_ = [ \n\r\t]*
输入:
This is Line 1
This is\Line
This is Line 3
输出:
[
[
"This is Line 1"
],
[
"This is",
"Line",
"2"
],
[
"This is Line 3"
]
]