使用MGrammar解析行注释块

如何用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)+ "rnrn";

        interleave Whitespace = " " | "r" | "n";   
    }
}
    
已邀请:
问题是,你交错了所有的空格 - 所以在解析了令牌并来到词法分析器之后,它们只是“不存在”了。 CommentLineBlock在你的情况下是
syntax
,但你需要在
tokens
中完全消耗注释块...
language MyLanguage
{       
    syntax Main = CommentLineBlock*;

    token LineBreak = 'u000Du000A'
                         | '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中的suboken-rules将不会被处理 - 你可以解析普通的字符串。
Main[
  [
    Block{
      "/// This is blockrn/// number onern"
    },
    Block{
      "/// This is blockrn/// number two"
    }
  ]
]
我今晚可能会尝试找到更好的方式:-)     

要回复问题请先登录注册