[gtksourceview] javascript.lang: Fix escape sequences



commit 0681c09c372500c990f3682187c94c90cb56a484
Author: Jeffery To <jeffery to gmail com>
Date:   Tue Jun 26 04:33:41 2018 +0800

    javascript.lang: Fix escape sequences
    
    * Update the escape context to match actual Javascript escape
      sequences[1][2]
    * Add a control-escape context to match control escape sequences valid
      in JS regular expressions[3]
    * Copy the string and single-quoted-string contexts from def.lang so
      that JS escape sequences are highlighted correctly
    * Remove def:line-continue from the regex-simple context (AFAIK line
      continations are not allowed inside JS regular expressions)
    
    [1] https://mathiasbynens.be/notes/javascript-escapes
    [2] 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Escape_notation
    [3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-control

 data/language-specs/javascript.lang | 42 +++++++++++++++++++++++++++++++++----
 tests/syntax-highlighting/file.js   |  8 +++++++
 2 files changed, 46 insertions(+), 4 deletions(-)
---
diff --git a/data/language-specs/javascript.lang b/data/language-specs/javascript.lang
index 0b39a9ae..684cbc71 100644
--- a/data/language-specs/javascript.lang
+++ b/data/language-specs/javascript.lang
@@ -8,6 +8,7 @@
  Copyright (C) 2005 Stef Walter (formerly Nate Nielsen) <stef memberwebs com>
  Copyright (C) 2005-2007 Marco Barisione <barisione gmail com>
  Copyright (C) 2005-2007 Emanuele Aina
+ Copyright (C) 2018 Jeffery To <jeffery to gmail com>
 
  GtkSourceView is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
@@ -54,18 +55,51 @@
 
     <!--contexts NOT used in the main context-->
     <context id="escape" style-ref="escape">
-      <match>\\((0-7){3}|(x[a-fA-F0-9]{2})|(c\S)|([CM]-\S)|(M-C-\S)|.)</match>
+      <match extended="true">
+        \\
+        (?:
+          (?: [1-7][0-7]{0,2} | [0-7]{2,3} ) |  # octal escape (\0 is the null character, not octal)
+          x[0-9a-fA-F]{2} |                     # hexadecimal escape
+          u[0-9a-fA-F]{4} |                     # unicode escape
+          u\{[0-9a-fA-F]{1,}\} |                # unicode code point escape
+          .                                     # single character escape
+        )
+      </match>
+    </context>
+
+    <!-- only valid in regular expressions -->
+    <context id="control-escape" style-ref="escape">
+      <match>\\c[a-zA-Z]</match>
     </context>
 
     <context id="regex-bracketed" style-ref="escape" style-inside="true">
       <start>\[</start>
       <end>]</end>
       <include>
+        <context ref="control-escape"/>
         <context ref="escape"/>
       </include>
     </context>
 
     <!--contexts used in the main context-->
+    <context id="string" style-ref="string" end-at-line-end="true" class="string" 
class-disabled="no-spell-check">
+      <start>"</start>
+      <end>"</end>
+      <include>
+        <context ref="escape"/>
+        <context ref="def:line-continue"/>
+      </include>
+    </context>
+
+    <context id="single-quoted-string" style-ref="string" end-at-line-end="true" class="string" 
class-disabled="no-spell-check">
+      <start>'</start>
+      <end>'</end>
+      <include>
+        <context ref="escape"/>
+        <context ref="def:line-continue"/>
+      </include>
+    </context>
+
     <context id="regex-simple" style-ref="regex">
       <start extended="true">
         ((?&lt;=([(]|\s))|^)
@@ -80,8 +114,8 @@
         )</start>
       <end>\/\%{regex-opts}</end>
       <include>
+        <context ref="control-escape"/>
         <context ref="escape"/>
-        <context ref="def:line-continue"/>
         <context ref="regex-bracketed"/>
       </include>
     </context>
@@ -331,8 +365,8 @@
         <context ref="def:c-like-comment"/>
         <context ref="def:c-like-comment-multiline"/>
         <context ref="def:c-like-close-comment-outside-comment"/>
-        <context ref="def:string" style-ref="string"/>
-        <context ref="def:single-quoted-string" style-ref="string"/>
+        <context ref="string" />
+        <context ref="single-quoted-string" />
         <context ref="template-string"/>
         <context ref="def:float"/>
         <context ref="def:decimal"/>
diff --git a/tests/syntax-highlighting/file.js b/tests/syntax-highlighting/file.js
index 66f0a281..ca30228f 100644
--- a/tests/syntax-highlighting/file.js
+++ b/tests/syntax-highlighting/file.js
@@ -19,6 +19,14 @@ x = a /b/ c / d;
 /\\[ab]/ // a or b preceded by backslash
 /\[ab]/  // Literally "[ab]"
 
+// Escape sequences:
+'\b\f\n\r\t\v\0\'\"\\' // Single character escape
+"\1\01\001"            // Octal escape
+'\xA9'                 // Hexadecimal escape
+"\u00a9"               // Unicode escape
+'\u{1D306}'            // Unicode code point escape
+/\cJ/                  // Control escape
+
 // Template strings
 // ----------------
 // Template strings are delimited by back-ticks (grave accent) and


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]