是否可以在 MGrammar 中解析多行 c 风格注释?

发布于 2024-08-08 00:55:21 字数 876 浏览 9 评论 0 原文

我一直在研究 May09 Oslo 位,尝试对一些源代码进行标记。不过,我似乎不知道如何正确处理多行 C 风格注释。 例如: /*comment*/

有些情况让我困惑:

/***/

或者

/**//**/

我可以使其中之一起作用,但不能同时起作用。 语法是:

    module Test {
    language Comments {

        token Comment =
            MultiLineComment;

        token MultiLineComment =
            "/*" MultiLineCommentChar* "*/";

        token MultiLineCommentChar =
            ^ "*" |
            "*" PostAsteriskChar;

        token PostAsteriskChar =
            ^ "*" |
            "*" ^("*" | "/"); 

        /*    
        token PostAsteriskChar =
            ^ "*" |
            "*" PostAsteriskChar; 
        */

        syntax Main = Comment*;
    }
}

注释掉的标记是我认为我想做的,但是不允许使用递归标记。 MGrammar 本身具有“损坏的”多行注释(它无法处理 /***/)这一事实让我相信这是不可能的。

还有人知道吗?

I've been hacking around with the May09 Oslo bits, experimented with tokenizing some source code. I can't seem to figure out how to correctly handle multiline C-style comments though.
For example: /*comment*/

Some cases that elude me:

/***/

or

/**//**/

I can make one or the other work, but not both.
The grammar was:

    module Test {
    language Comments {

        token Comment =
            MultiLineComment;

        token MultiLineComment =
            "/*" MultiLineCommentChar* "*/";

        token MultiLineCommentChar =
            ^ "*" |
            "*" PostAsteriskChar;

        token PostAsteriskChar =
            ^ "*" |
            "*" ^("*" | "/"); 

        /*    
        token PostAsteriskChar =
            ^ "*" |
            "*" PostAsteriskChar; 
        */

        syntax Main = Comment*;
    }
}

The commented out token is what I think I want to do, however recursive tokens are not permitted.
The fact that MGrammar itself has "broken" multiline comments (it can't handle /***/) leads me to believe this isn't possible.

Does anyone know otherwise?

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

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

发布评论

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

评论(1

情感失落者 2024-08-15 00:55:21

我的做法如下(并非全部是我自己的代码,但我找不到对原作者的引用)。

interleave Skippable = Whitespace | Comment;
interleave Comment = CommentToken;
@{Classification["Comment"]}
token CommentToken = CommentDelimited
| CommentLine;
token CommentDelimited = "/*" CommentDelimitedContent* "*/";
token CommentDelimitedContent
= ^('*')
| '*'  ^('/');
token CommentLine = "//" CommentLineContent*;
token CommentLineContent
= ^(
'\u000A' // New Line
|  '\u000D' // Carriage Return
|  '\u0085' // Next Line
|  '\u2028' // Line Separator
|  '\u2029' // Paragraph Separator
);

这允许单行 (//) 注释和多行 (/* */) 注释。

The way I have done it is as follows (not all my own code but I can't find a referance to the original author).

interleave Skippable = Whitespace | Comment;
interleave Comment = CommentToken;
@{Classification["Comment"]}
token CommentToken = CommentDelimited
| CommentLine;
token CommentDelimited = "/*" CommentDelimitedContent* "*/";
token CommentDelimitedContent
= ^('*')
| '*'  ^('/');
token CommentLine = "//" CommentLineContent*;
token CommentLineContent
= ^(
'\u000A' // New Line
|  '\u000D' // Carriage Return
|  '\u0085' // Next Line
|  '\u2028' // Line Separator
|  '\u2029' // Paragraph Separator
);

This allows for both single line (//) comments as well as multiline (/* */) comments.

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