【问题标题】:How to parse only comments using pegjs grammar?如何使用 pegjs 语法仅解析评论?
【发布时间】:2017-01-27 08:59:26
【问题描述】:

我编写了一个 pegjs 语法,它可以解析任何类型的 js/c 风格的 cmets。但是,它并不能很好地工作,因为我只设法捕获了评论本身,而忽略了其他所有内容。我应该如何更改此语法以仅从任何类型的输入中解析 cmets?

语法:

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.

【问题讨论】:

    标签: parsing pegjs


    【解决方案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"
    ]
    

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 2012-07-12
      • 2020-06-30
      • 2019-05-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多