PEG.js - how to parse c style comments?

The implementation of the parser based on peg.js, I force you to add code to handle c-style / * style comments, like this * /.

I need to find the end marker without eating it.

this does not work:

multi = '/*' .* '*/' 

Message:

 line: 14 Expected "*/" or any character but end of input found. 

I understand why this does not work, but, unfortunately, I have no idea how to do a functional analysis of comments.

Here is the code:

 start = item* item = comment / content_line content_line = _ p:content _ {return ['CONTENT',p]} content = 'some' / 'legal' / 'values' comment = _ p:(single / multi) {return ['COMMENT',p]} single = '//' p:([^\n]*) {return p.join('')} multi = 'TODO' _ = [ \t\r\n]* {return null} 

and some sample input:

 // line comment, no problems here /* how to parse this ?? */ values // another comment some legal 
+7
comments parsing peg
source share
2 answers

Use a predicate that looks to the future and make sure that there is no "*/" in front of the character stream:

 comment = "/*" (!"*/" .)* "*/" 

The part (!"*/" .) Can be read as follows: when there is no '*/' in front, match any character.

This will cause such comments to match successfully: /* ... **/

+12
source share

full code:

Parser:

 start = item* item = comment / content_line content_line = _ p:content _ {return ['CONTENT',p]} content = 'all' / 'legal' / 'values' / 'Thanks!' comment = _ p:(single / multi) {return ['COMMENT',p]} single = '//' p:([^\n]*) {return p.join('')} multi = "/*" inner:(!"*/" i:. {return i})* "*/" {return inner.join('')} _ = [ \t\r\n]* {return null} 

Example:

 all // a comment values // another comment legal /*12 345 /* */ Thanks! 

Result:

 [ ["CONTENT","all"], ["COMMENT"," a comment"], ["CONTENT","values"], ["COMMENT"," another comment"], ["CONTENT","legal"], ["COMMENT","12\n345 /* \n"], ["CONTENT","Thanks!"] ] 
+5
source share

All Articles