Search code examples
jison

Handling clike comments in Jison


I'm writing a compiler of clike language in JS using Jison as an lexer/parser generator with angular frontend. I nearly got the result I expected, but there is one thing that is puzzling me - how to make Jison ignore comments (both /* block */ and // line)?

Is there any easy way to achieve it? Keeping in mind that the comment can potentially be inserted in the middle of any statement/expression?


Solution

  • You ignore comments the same way you ignore whitespace: with a lexer rule that has no action.

    For example:

    \s+                                   /* IGNORE */
    "//".*                                /* IGNORE */
    [/][*][^*]*[*]+([^/*][^*]*[*]+)*[/]   /* IGNORE */
    

    The first line ignores whitespace. The second ignores single-line comments. And the third ignores block comments.