我想对我的解析器说不要跳过“\ n”在“///”之前在文档评论中! [英] I want to say to my parser not to skip "\n" before "///" in documentation comments!

查看:95
本文介绍了我想对我的解析器说不要跳过“\ n”在“///”之前在文档评论中!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先:这不是语法错误!我的语法文件不包含任何错误!

我想为我的程序编写一个C#语法。我从CodePlex下载了这个语法: http://antlrcsharp.codeplex.com/ [ ^ ]。

我还没有找到更好的AN#4语法C#语法。这个语法不支持文档评论对我的计划非常重要。这个语法会跳过所有注释,所以我删除了文档注释的删除并编写了我的代码以备文档注释,但我不知道如何说,\ n之后必须是/// 在文档评论中。我担心,当他识别出\ n时,词法分析器会自动跳过词汇符号并且永远不会匹配:('\ n''///')?在我的解析器规则中。有谁知道,如何解决这个问题?或者任何人都可以向我解释,我的代码是正确的,如果是的话?



这是我的文档评论解析器规则:

Hi,
First: This is not a syntactic error! My grammar file does not contain any error!
I want to write a C# grammar for my program. I have downloaded this grammar from CodePlex: http://antlrcsharp.codeplex.com/[^].
I have not found better C# grammar for ANTLR 4. This grammar does not support documentation comments which are very important for my program. This grammar skips all the comments, so I have deleted the skiping of documentation comments and written my code for documentation comments, but I do not know how to say, that after "\n" has to be "///" in a documentation comment. I am afraid, that the lexer, when he recognizes "\n", skips the lexical symbol automatically and never match this: "('\n' '///')?" in my parser rule. Does anyone know, how to solve this problem? Or Could anyone explain to me, that my code is right, if it is?

Here is my parser rules for documentation comments:

//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):



And here is a lexer rules for white spaces (WS), a parser rule for comment_text and a lexer rule for comment chars (ANY_CHARS):

WS:
    (' '  |  '\r'  |  '\t'  |  '\n'  ) -> skip;
comment_text : ANY_CHARS;
fragment ANY_CHARS: (.)*;





感谢您的回复!

Pepin z Hane



Thanks for replies!
Pepin z Hane

推荐答案

我不太了解ANTLR,但我怀疑ANTLR有一个前瞻谓词,允许告诉词法分子只跳过新行*不*后跟可选的空格后跟///。

例如类似

I don't know ANTLR well enough, but my suspicion is that ANTLR has a lookahead predicates that allow to tell the lexer to only skip new-lines that are *not* followed by optional whitespaces followed by ///.
E.g. something like
WS1: ( ' '  |  '\r'  |  '\t' ) -> skip;
WS2: ( '\n' { _input.LT(1).getType() != COMMENT}? ) -> skip;



你当然需要一个评论制作。

我没试过,这只是一个可能的方向的暗示解决这个问题。

干杯

Andi


You need of course a COMMENT production.
I've not tried it out, this is only a hint of a possible direction to solve this.
Cheers
Andi


这篇关于我想对我的解析器说不要跳过“\ n”在“///”之前在文档评论中!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆