perlparsingparse-recdescent

how to skip all single- and multi-line comments in a Parse::RecDescent parser


In Parse::RecDescent, how do I effectively ignore C++/Java style comments? This includes single-line ('//' until the end of the line) and multi-line (/everything between here/).


Solution

  • <skip> defines what the parser considers whitespace.

    parse: <skip: qr{(?xs:
              (?: \s+                       # Whitespace
              |   /[*] (?:(?![*]/).)* [*]/  # Inline comment
              |   // [^\n]* \n?             # End of line comment
              )
           )*}>
           main_rule
           /\Z/
           { $item[2] }
    

    Unlike Nate Glenn's solution, mine


    Note: (?:(?!STRING).)* is to (?:STRING) as [^CHAR] is to CHAR.