如何使用pegjs语法解析注释?

时间:2016-07-22 15:02:50

标签: parsing pegjs

我写了一个pegjs语法,应该解析任何类型的js / 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 个答案:

答案 0 :(得分:0)

在考虑评论(单行或多行)之前,您需要重构以专门捕获行内容,如:

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"
]