在 javacc 中正确跳过单行注释的另一种方法? <“//” (~[“\n”])*“\n”>导致多行注释跳过中断

发布于 2024-12-12 02:09:38 字数 742 浏览 0 评论 0 原文

所以我在 javacc 和跳过注释方面遇到了问题。我有一个多行注释跳过,它本身可以包含多个注释(注释是 /**/ 中出现的任何内容),我也使用此代码段<"//" (~["\n"])* "\n"> 跳过单行注释。两者的功能彼此独立,但当组合起来时,单行注释似乎打破了我的多行注释。

解析器不再识别多行注释,而是将其解析为 OTHER(/*)、ID 等的组合。

下面是我的多行注释和单行注释跳过的代码:

SKIP:
{
    "/*" {commentnesting++;} : IN_COMMENT
}

<IN_COMMENT> SKIP :
{
    "/*" {commentnesting++;} 
    | "*/" {commentnesting--;
        if(commentnesting == 0) {
            SwitchTo(DEFAULT);
        }
    }
    | <~[]>
}

SKIP :
{
    <"//" (~["\n"])* "\n">
}

我的问题是:

  • 当我相对较新的眼睛看来单行注释似乎具有完全不同的正则表达式时,单行注释如何导致多行注释中断?
  • 他们是否有一种编写单行注释skip的方法,以便它执行与以前相同的功能,但当两者一起使用时不会破坏多行注释?

So I am having a problem with javacc and skipping comments. I have a multi line comment skip that can contain multiply comments within itself (A comment is anything that appears within /* and a */), I also use this code segement <"//" (~["\n"])* "\n"> to skip a single line comment. Both function independently of one another but when combined, the single line comment seems to break my multi line comment.

The parser no longer recognises the multi line comment and instead parses it as a combination of OTHER(/*), ID etc.

Below is my code for the multi line comment and single line comment skip:

SKIP:
{
    "/*" {commentnesting++;} : IN_COMMENT
}

<IN_COMMENT> SKIP :
{
    "/*" {commentnesting++;} 
    | "*/" {commentnesting--;
        if(commentnesting == 0) {
            SwitchTo(DEFAULT);
        }
    }
    | <~[]>
}

SKIP :
{
    <"//" (~["\n"])* "\n">
}

My Questions are:

  • How can the single line comment cause the multi line comment to break, when they to my relatively new eyes appear to have completely different regexes?
  • Is their a way to write the single line comment skip , so that it performs the same function as before but doesn't break the multi line comment when the two are used together?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错々过的事 2024-12-19 02:09:38

看起来应该可行,但你似乎缺少一些尖括号。而不是:

"/*" {commentnesting++;} : IN_COMMENT

...不应该是:

<"/*"> {commentnesting++;} : IN_COMMENT

...与您的第二条规则类似吗?

Looks like that should work, except you seem to be missing some angle brackets. Instead of:

"/*" {commentnesting++;} : IN_COMMENT

...shouldn't it be:

<"/*"> {commentnesting++;} : IN_COMMENT

...and similarly with your second rule?

薄荷梦 2024-12-19 02:09:38

我最终做了类似的事情:

TOKEN: {
  < COMMENT_END : "*/" >
}
SPECIAL_TOKEN: {
 < COMMENT_START : "/*" >  {         
        /*currently commented contents are dropped, but they can be attached to the special token*/
        do {
            Token nextToken = this.getNextToken();
                if ("*/".equals(nextToken.image)) {
                        break;
                }
        } while (true);
 }
}

这有点hacky,但在注释包含其他注释甚至字符串的代码时它也有效:“/* */”....

I ended up doing something like:

TOKEN: {
  < COMMENT_END : "*/" >
}
SPECIAL_TOKEN: {
 < COMMENT_START : "/*" >  {         
        /*currently commented contents are dropped, but they can be attached to the special token*/
        do {
            Token nextToken = this.getNextToken();
                if ("*/".equals(nextToken.image)) {
                        break;
                }
        } while (true);
 }
}

It is a bit hacky but it also works when commenting code that contains other comments and even strings like: "/* */"....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文