0

如何使用 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";   
    }
}
4

1 回答 1

1

问题是,你交错了所有的空格——所以在解析标记并进入词法分析器之后,它们就“不存在”了。

CommentLineBlocksyntax在您的情况下,但您需要在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"
    }
  ]
]

今晚我可能会尝试找到更好的方法:-)

于 2010-07-20T06:06:14.270 回答