Search code examples
monaco-editor

Monaco Language Match Empty Line


I'm trying to create a language definition for the Monaco editor. The language is a very opinionated language to which I don't control the compiler, etc.

The "\" escape character is universal; single-line comments can be continued to the next line by ending in "\"

Some examples, using C++-style comments:

// This comment \
   continues on the following line

// This one does NOT continue \\
   because the escape char was itself escaped

// This one DOES continue because \\\
   the 1st '\' escapes the 2nd; the 3rd escapes EOL

// This comment continues to the following line \

But the line was empty. This should not be commented.

I have implemented all of the semantics except the last part, because it does not appear to me possible to match on an empty line. The closest I found in documentation was '@eos' but I cannot figure out how to use it. Here is my current implementation:

comments: [
  [/\/\/$/, 'comment'], // empty single-line comment
  [/\/\//, 'comment', '@comment_cpp'],
],

comment_cpp: [
  [/(?:[^\\]|(?:\\.))+$/, 'comment', '@pop'],
  [/.+$/, 'comment'],
],

Is there a rule I can add that will enable me to "@pop" out of the "@comment_cpp" rule on an empty line so the following is true?

// This comment continues to the following line \
   and this one continues to the following empty line \

But this line is not commented

Solution

  • This issue was since resolved; the monaco team added support for matching on /$/.

    https://github.com/microsoft/monaco-editor/issues/1235