microsoft / monaco-editor

A browser based code editor
https://microsoft.github.io/monaco-editor/
MIT License
40.45k stars 3.6k forks source link

Monarch embedded @pop rule requires two carets #1127

Open EvilCoincoin opened 6 years ago

EvilCoincoin commented 6 years ago

version: 0.14.3

If an embedded mode @pop rule requires to match only at the beginning of a line, one must insert two ^^ carets instead of one for it to take effect.

This is probably caused by _findLeavingNestedModeOffset ignoring matchOnlyAtLineStart when it rebuilds its regex.

alexdima commented 6 years ago

@EvilCoincoin Can you please show a grammar/example that illustrates the problem?

EvilCoincoin commented 6 years ago

@alexandrudima

gxmonarch.txt

In the provided example, the grammar lets you switch between two languages (ttx and lua) using lines starting with @ttx or @lua. The root rules are so that it defaults to ttx embed.

To make sure the "@" is only matching at beginning of line (for the two pop rules), we need to add a second caret else it will match the pop in the middle of the line.

To find the ^^ workaround I pin pointed the bug to the _findLeavingNestedModeOffset method. It does a pre-pass on every line to find if the embedded language is ending on that line. The problem is, this method tries to reconstruct its whole line regex using the normal parsing one by striping the ^ caret and $ but it doesn't add a caret back if matchOnlyAtLineStart was specified for that rule. Adding a second caret in the rule works around that problem.