Skip to content

JSON: highlight invalid escaped characters and invalid numbers

I'm thinking of an interesting feature that could be made to share contexts a little more.

JSON syntax contains 2 types of string: Key and String. Each can contain escape characters. This is how it's done:

      <context name="String_Key" lineEndContext="#stay" attribute="Style_String_Key"> <!-- ⁴ -->
<!--                        ~~~ or Value                                        ~~~ or Value -->
        <DetectChar char="&quot;"  context="#pop" attribute="Style_String_Key" />
<!--                                                                      ~~~ or Value -->
        <DetectChar char="\"       context="String_Key_Char" lookAhead="1"/> <!-- ² -->
<!--                                              ~~~ or Value -->
      </context>

      <context name="String_Key_Char" lineEndContext="#stay" attribute="Style_Normal"> <!-- ¹ -->
<!--                        ~~~ or Value -->
        <RegExpr String="\\(?:[&quot;\\/bfnrt]|u[0-9a-fA-f]{4})" context="#pop" attribute="Style_String_Key_Char" /> <!-- ³ -->
<!--                                                                                                    ~~~ or Value -->
        <RegExpr String="\\(u[0-9a-fA-f]+|.)?" context="#pop" attribute="Style_Error" />
      </context>

We can see here that String_Key_Char and String_Value_Char (¹) are identical except for the attribute and can be accessed through a rule using lookAhead=1 (²). lookAhead propagates the attribute from the context where it is located to the next context (until the next context change). So, if I remove attribute="Style_String_Key_Char" (³), the color of the regex will be Style_String_Key (⁴) and not Style_Normal (¹).

I propose that when a lookAhead=1 rule has attribute, the latter should be propagated (instead of the context one), which would allow String_Key_Char and String_Value_Char to be merged here.

Merge request reports