2010-06-08 49 views
0

如何解析使用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)+ "\r\n\r\n"; 

     interleave Whitespace = " " | "\r" | "\n"; 
    } 
} 

回答

1

的问题是,你交错所有空格 - 这样解析令牌和来之后词法分析器,他们只是“不存在”了。

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的subtoken规则不会被处理 - 你得到纯净的字符串解析。

Main[ 
    [ 
    Block{ 
     "/// This is block\r\n/// number one\r\n" 
    }, 
    Block{ 
     "/// This is block\r\n/// number two" 
    } 
    ] 
] 

我可能会尝试找到一种更好的方式,今晚:-)