如何使用MGrammar解析行注释块?
我想要解析行注释块。应在MGraph输出中对每个注释旁边的行注释进行分组。
我在将行注释块组合在一起时遇到问题。我当前的语法使用"\r\n\r\n“来终止一个块,但这并不是在所有情况下都有效,例如在文件末尾或当我引入其他语法时。
示例输入可能如下所示:
/// This is block
/// number one
/// This is block
/// number two我现在的语法是这样的:
module MyModule
{
language MyLanguage
{
syntax Main = CommentLineBlock*;
token CommentContent = !(
'\u000A' // New Line
|'\u000D' // Carriage Return
|'\u0085' // Next Line
|'\u2028' // Line Separator
|'\u2029' // Paragraph Separator
);
token CommentLine = "///" c:CommentContent* => c;
syntax CommentLineBlock = (CommentLine)+ "\r\n\r\n";
interleave Whitespace = " " | "\r" | "\n";
}
}发布于 2010-07-20 14:06:14
问题是,您交错了所有的空格-所以在解析令牌并进入词法分析器之后,它们就“不再存在了”。
在您的例子中,CommentLineBlock是syntax,但是您需要在tokens中完全使用注释块...
language MyLanguage
{
syntax Main = CommentLineBlock*;
token LineBreak = '\u000D\u000A'
| '\u000A' // New Line
|'\u000D' // Carriage Return
|'\u0085' // Next Line
|'\u2028' // Line Separator
|'\u2029' // Paragraph Separator
;
token CommentContent = !(
'\u000A' // New Line
|'\u000D' // Carriage Return
|'\u0085' // Next Line
|'\u2028' // Line Separator
|'\u2029' // Paragraph Separator
);
token CommentLine = "//" c:CommentContent*;
token CommentLineBlock = c:(CommentLine LineBreak?)+ => Block {c};
interleave Whitespace = " " | "\r" | "\n";
}但问题是,CommentLine中的子令牌规则将不会被处理-您得到的是普通字符串的解析。
Main[
[
Block{
"/// This is block\r\n/// number one\r\n"
},
Block{
"/// This is block\r\n/// number two"
}
]
]今晚我可能会试着找到一个更好的方式:-)
https://stackoverflow.com/questions/3000211
复制相似问题