第一:这不是语法错误!我的语法文件没有任何错误!我想为我的程序编写C#语法。我已从CodePlex下载此语法:ANTLR C#4.0语法我没有找到用于ANTLR 4的更好的C#语法。此语法不支持对我的程序非常重要的文档注释。该语法会跳过所有注释,因此我删除了跳过文档注释的代码,并编写了用于文档注释的代码,但是我不知道怎么说,在“ \ n”之后必须为“ ///”。恐怕,词法分析器在识别出“ \ n”时会自动跳过词法符号,并且永远不会与此匹配:('\ n''///')?在我的解析器规则中。有谁知道,如何解决这个问题?还是有人可以向我解释我的代码是否正确?
这是我的文档注释解析器规则:
//documentation comments
doc_comment :
'///' ( summary remarks?
|remarks
);
summary :
'<summary' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* tag_body+ (('\n' '///')* comment_text ('\n' '///')*)* '</summary>';
remarks :
'<remarks' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</remarks>';
tag_body : '<c' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</c>'
|'<code' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</code>'
|'<example' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</example>'
|'<exception' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</exception>'
|'<include' 'file' '=' '\'' comment_text '\'' 'path' '=' '\'' comment_text ('[' '@name' '=' '"'identifier '"' ']')? '\'' '/' '>'
|'<list' 'type' '=' ('"bullet"' | '"number"' | '"table"') '>' ('\n' '///')* listheader? listitem? '</list>'
|'<para' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</para>'
|'<param' 'name' '=' '"' identifier '"' '>' (('\n' '///')* comment_text ('\n' '///')*)* '</param>'
|'<paramref' 'name' '=' '"' comment_text '"' '/' '>'
|'<permission' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</permission>'
|'<returns' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</returns>'
|'<see' cref '/' '>'
|'<seealso' cref '/' '>'
|'<typeparam' 'name' '=' '"' comment_text '"' '>' (('\n' '///')* comment_text ('\n' '///')*)* '</typeparam>'
|'<typeparamref' 'name' '=' '"' comment_text '"' '/' '>'
|'<value' cref? '>' (('\n' '///')* comment_text ('\n' '///')*)* '</value>';
cref : 'cref' '=' '"' comment_text '"' ;
listheader : '<listheader>' ('<term>' (('\n' '///')* comment_text ('\n' '///')*)* '</term>')? ('<description>' (('\n' '///')* comment_text ('\n' '///')*)* '</description>')? '</listheader>';
listitem : '<listitem>' ('<term>' (('\n' '///')* comment_text ('\n' '///')*)* '</term>')? ('<description>' (('\n' '///')* comment_text ('\n' '///')*)* '</description>')? '</listitem>';
这是空白(WS)的词法分析器规则,comment_text的解析器规则和注释字符(ANY_CHARS)的词法分析器规则:
WS:
(' ' | '\r' | '\t' | '\n' ) -> skip;
comment_text : ANY_CHARS;
fragment ANY_CHARS: (.)*;
感谢您的答复!
皮特
我确信从理论上讲可以与文件的其余部分同时解析XML文档注释,但这是维护的噩梦,尤其是对于词法分析器规则。例如,您将如何处理以下内容?
/// <see
/// cref="Something"
/// />
代替采取这种方法,您应该执行以下操作。
将所有文档注释标记移至其各自的通道。
@members {
public const int DocCommentsChannel = 2;
}
DOC_COMMENT : '///' ~[\r\n]* -> channel(DocCommentsChannel);
LINE_COMMENT : '//' ~[\r\n]* -> channel(HIDDEN);
解析之后,编写代码以检查通道ITokenStream
上所有令牌实例的,直到DocCommentsChannel
导致某些项目的声明,然后将它们作为一组进行处理。使用单独的解析器。通过///
从每行中删除前导字符并将所得的文本块作为根元素的主体(如)来创建注释的内存中XML表示,可以最轻松地执行此操作<Comment>{Your comment text here}</Comment>
。然后,您可以调用XDocument.Parse
以加载评论,并根据需要从那里进行处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句