简体   繁体   English

JFlex Block Comment 规则说明

[英]Explanation of JFlex Block Comment rule

I was looking on how to implement block comments in JFlex for custom language support in intellij and found that it can be described as我正在研究如何在 JFlex 中实现块注释以在 intellij 中支持自定义语言,发现它可以描述为

"/*" !([^]* "*/" [^]*) ("*/")?

I don't quite understand how to read this and would like it if it were explained in plain English.我不太明白如何阅读这篇文章,如果用简单的英语解释,我希望它。

At the moment I'm reading this as目前我正在阅读这个

  • first expect a /* then首先期望/*然后
  • expect not期待不
    • any character?任何字符? (Not sure why they used [^] ) zero or more times (不知道他们为什么使用[^] )零次或多次
    • followed */跟随*/
    • Any character zero or more任何零个或多个字符
  • An optional */一个可选的*/

You've basically deciphered it correctly.你基本上已经正确地破译了它。 Here's a few explanatory notes:这里有一些解释性说明:

[^]* matches an arbitrary sequence of characters. [^]*匹配任意字符序列。 It's similar to .* except that .它类似于.*除了. doesn't match newlines or unpaired surrogates;不匹配换行符或未配对的代理; [^] matches absolutely anything. [^]绝对匹配任何东西。

So ([^]* "*/" [^]*) matches any sequence which includes */ .所以([^]* "*/" [^]*)匹配任何包含*/的序列。 And therefore !([^]* "*/" [^]*) matches anything except a sequence containing */ .因此!([^]* "*/" [^]*)匹配除了包含*/的序列之外的任何内容。 In other words, it matches anything up to but not including */ , which is the rest of the comment.换句话说,它匹配直到但不包括*/的任何内容,即评论的 rest。

Now what happens if the user makes a mistake and forgets to close the last comment?现在如果用户犯了错误并忘记关闭最后一条评论会发生什么? In that case, there is no */ and will match up to the end of input.在这种情况下,没有*/并且将匹配到输入的末尾。 Since there's no way to know where the comment should have ended (without being able to read the programmer's mind), the best we can do is to stop trying to parse.由于无法知道注释应该在哪里结束(无法读懂程序员的想法),我们能做的最好的就是停止尝试解析。 Thus, we accept the unterminated comment as a comment.因此,我们接受未终止的评论作为评论。 That's why the final "*/"?这就是为什么最后的"*/"? is optional.是可选的。 It will match the comment terminator if there is one, and otherwise it will match an empty sequence at the end of the input.如果有,它将匹配注释终止符,否则将匹配输入末尾的空序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM