如何使用PEGJS语法仅解析注释?

3
我写了一个PegJS语法,可以解析任何类型的JavaScript/C风格的注释。但是它还不太完美,因为我只能捕获注释本身,并忽略其他所有内容。我应该如何修改这个语法,才能从任何输入中仅解析出注释呢?
语法:
Start
  = Comment

Character
  = .

Comment
  = MultiLineComment
  / SingleLineComment

LineTerminator
  = [\n\r\u2028\u2029]

MultiLineComment
  = "/*" (!"*/" Character)* "*/"

MultiLineCommentNoLineTerminator
  = "/*" (!("*/" / LineTerminator) Character)* "*/"

SingleLineComment
  = "//" (!LineTerminator Character)*

输入:

/**
 * Trending Content
 * Returns visible videos that have the largest view percentage increase over
 * the time period.
 */

Other text here

错误
Line 5, column 4: Expected end of input but "\n" found.
1个回答

1

在考虑单行或多行注释之前,您需要重构代码以特别捕获该行的内容,如下所示:

lines = result:line* {
  return result
}

line = WS* line:$( !'//' CHAR )* single_comment ( EOL / EOF ) { // single-comment line
  return line.replace(/^\s+|\s+$/g,'')
}
/ WS* line:$( !'/*' CHAR )* multi_comment ( EOL / EOF ) { // mult-comment line
  return line.replace(/^\s+|\s+$/g,'')
}
/ WS* line:$CHAR+ ( EOL / EOF ) { // non-blank line
  return line.replace(/^\s+|\s+$/g,'')
}
/ WS* EOL { // blank line
  return ''
}

single_comment = WS* '//' CHAR* WS*

multi_comment = WS* '/*' ( !'*/' ( CHAR / EOL ) )* '*/' WS*

CHAR = [^\n]
WS = [ \t]
EOF = !.
EOL = '\n'

当运行以下代码时:

no comment here

single line comment // single-comment HERE

test of multi line comment /*

  multi-comment HERE

*/

last line

返回:
[
  "no comment here",
  "",
  "single line comment",
  "",
  "test of multi line comment",
  "",
  "last line"
]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接