【发布时间】:2021-06-21 19:32:13
【问题描述】:
有一段文字:
this is title. {this is id} [this is type] (this is description)
我要获取关注对象
{
id: 'this is id',
title: 'this is title',
type: 'this is type',
description: 'this is description',
}
这是我的 pegjs 规则:
start = Text
_ "whitespace"
= [ \t\n\r]*
Text
= _ body:Element? _ {
return {
type: "Text",
body: body || [],
}
}
Element
= _ title:Title _ id:Id _ type:Type _ description:Description _ {
return {
id: id,
title: title,
type: type,
description: description,
}
}
Type
= "[" type: Literal "]" {
return type;
}
Id
= '{' id: Literal '}' {
return id;
}
Title
= Literal
Description
= "(" description: Literal ")" {
return description;
}
Literal "Literal"
= '"' char:DoubleStringCharacter* '"' {
return char.join("");
}
DoubleStringCharacter
= !'"' . {
return text();
}
还有个问题,不知道怎么匹配没有环绕语法的字符串?
我只知道Literal语法错了,不知道怎么改进,谁能帮帮我?
【问题讨论】:
标签: pegjs