[gtksourceview] sh.lang: Improve variable definition; cleaning



commit 733d126bbd19febc5bb89c169eee8399868edf23
Author: neyfag <11970-neyfag users noreply gitlab gnome org>
Date:   Tue Nov 5 16:31:17 2019 +0100

    sh.lang: Improve variable definition; cleaning

 data/language-specs/sh.lang       | 109 +++++++++++++++++++++++++-------------
 tests/syntax-highlighting/file.sh |  10 +++-
 2 files changed, 81 insertions(+), 38 deletions(-)
---
diff --git a/data/language-specs/sh.lang b/data/language-specs/sh.lang
index dc123d58..9e99228d 100644
--- a/data/language-specs/sh.lang
+++ b/data/language-specs/sh.lang
@@ -33,7 +33,8 @@
     <style id="function"            name="Function"            map-to="def:function"/>
     <style id="string"              name="String"              map-to="def:string"/>
     <style id="keyword"             name="Keyword"             map-to="def:keyword"/>
-    <style id="variable"            name="Variable"            map-to="def:type"/> <!-- FIXME: need to sort 
out proper styles for variables -->
+    <!-- FIXME: need to sort out proper styles for variables -->
+    <style id="variable"            name="Variable"            map-to="def:type"/>
     <style id="variable-definition" name="Variable Definition" map-to="sh:variable"/>
     <style id="others"              name="Others"              map-to="def:keyword"/>
     <style id="common-command"      name="Common Commands"     map-to="def:keyword"/>
@@ -43,6 +44,10 @@
 
   <definitions>
 
+    <define-regex id="assignment">\+?=</define-regex>
+    <define-regex id="boundary-operator">[&amp;|;]</define-regex>
+    <define-regex id="identifier">[a-zA-Z_][a-zA-Z0-9_]*</define-regex>
+
     <!-- we cannot use def:shell-like-comment, because
          m4.lang needs to replace this context -->
     <context id="line-comment" style-ref="comment" end-at-line-end="true" class="comment" 
class-disabled="no-spell-check">
@@ -100,11 +105,11 @@
     </context>
 
     <context id="punctuator" style-ref="others">
-      <match>[;\|&amp;/]</match>
+      <match>\%{boundary-operator}|/</match>
     </context>
 
     <context id="function" style-ref="function">
-      <match>\b[a-zA-Z_][a-zA-Z0-9_]+[ \t]*\(\)</match>
+      <match>\b\%{identifier}[ \t]*\(\)</match>
     </context>
 
     <context id="redirection" style-ref="others">
@@ -180,7 +185,7 @@
     </context>
 
     <context id="short-parameter-expansion" style-ref="variable">
-      <match>\$([!@?*#\$0-9]|[a-zA-Z0-9_]+)</match>
+      <match>\$([!@?*#\$0-9]|\%{identifier})</match>
     </context>
 
     <context id="parameter-expansion" style-ref="variable">
@@ -201,10 +206,40 @@
       </include>
     </context>
 
+    <define-regex id="keyword">(?&lt;=if|then|else|while|until|do)</define-regex>
+    <context id="stand-alone-variable-definition">
+      <match extended="true">
+        (?:
+          ^ \s* |
+          (?&lt;=\%{boundary-operator}) \s* |
+          (?&lt;=if|then|else|while|until|do) \s+
+        )
+        (\%{identifier})
+        \%{assignment}
+      </match>
+      <include>
+        <context sub-pattern="1" style-ref="variable-definition"/>
+      </include>
+    </context>
+
+    <context id="variable-definition-command" end-at-line-end="true">
+      <start>\b(declare|local|typeset|readonly|export)\b</start>
+      <end>&gt;|\%{boundary-operator}</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="keyword"/>
+        <context ref="def:line-continue"/>
+        <context ref="double-quoted-string"/>
+        <context ref="single-quoted-string"/>
+        <context style-ref="variable-definition">
+          <match>(?&lt;=\s)\%{identifier}</match>
+        </context>
+      </include>
+    </context>
+
     <context id="variable-definition">
-      
<match>(^\s*|(?&lt;=if|then|else|do|export)\s+|(?&lt;=&amp;&amp;|\|\||\||;)\s*)([a-zA-Z_][a-zA-Z0-9_]*)\=</match>
       <include>
-        <context sub-pattern="2" style-ref="variable-definition"/>
+        <context ref="stand-alone-variable-definition"/>
+        <context ref="variable-definition-command"/>
       </include>
     </context>
 
@@ -218,18 +253,8 @@
       <keyword>(?&lt;=\s)\.(?=\s)</keyword>
     </context>
 
-    <context id="for-command" end-at-line-end="true">
-      <start>\bfor\b</start>
-      <end>(?=\S)</end>
-      <include>
-        <context sub-pattern="0" where="start" style-ref="keyword"/>
-        <context ref="def:line-continue"/>
-        <context style-ref="variable-definition" once-only="true">
-          <match>\w+</match>
-        </context>
-      </include>
-    </context>
-
+    <!-- To prevent conflicts, keywords below should be removed from the list
+         before being used in any container context -->
     <context id="built-in-command-2" style-ref="keyword">
       <prefix>(?&lt;![^\s;\(\)\`])</prefix>
       <suffix>(?![^\s;\(\)\`])</suffix>
@@ -238,7 +263,6 @@
       <keyword>elif</keyword>
       <keyword>else</keyword>
       <keyword>fi</keyword>
-      <keyword>for</keyword>
       <keyword>function</keyword>
       <keyword>if</keyword>
       <keyword>in</keyword>
@@ -256,7 +280,6 @@
       <keyword>compgen</keyword>
       <keyword>complete</keyword>
       <keyword>continue</keyword>
-      <keyword>declare</keyword>
       <keyword>dirs</keyword>
       <keyword>disown</keyword>
       <keyword>echo</keyword>
@@ -264,7 +287,6 @@
       <keyword>eval</keyword>
       <keyword>exec</keyword>
       <keyword>exit</keyword>
-      <keyword>export</keyword>
       <keyword>fc</keyword>
       <keyword>fg</keyword>
       <keyword>getopts</keyword>
@@ -273,13 +295,11 @@
       <keyword>history</keyword>
       <keyword>jobs</keyword>
       <keyword>let</keyword>
-      <keyword>local</keyword>
       <keyword>logout</keyword>
       <keyword>popd</keyword>
       <keyword>printf</keyword>
       <keyword>pushd</keyword>
       <keyword>read</keyword>
-      <keyword>readonly</keyword>
       <keyword>return</keyword>
       <keyword>set</keyword>
       <keyword>shift</keyword>
@@ -290,7 +310,6 @@
       <keyword>times</keyword>
       <keyword>trap</keyword>
       <keyword>type</keyword>
-      <keyword>typeset</keyword>
       <keyword>ulimit</keyword>
       <keyword>umask</keyword>
       <keyword>unalias</keyword>
@@ -298,11 +317,40 @@
       <keyword>wait</keyword>
     </context>
 
+    <context id="case-command">
+      <start>\bcase\b</start>
+      <end>\besac\b</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="keyword"/>
+        <context sub-pattern="0" where="end" style-ref="keyword"/>
+        <context ref="sh"/>
+        <context>
+          <match>(?&lt;=\))\s*(\%{identifier})(\%{assignment})</match>
+          <include>
+            <context sub-pattern="1" style-ref="variable-definition"/>
+          </include>
+        </context>
+      </include>
+    </context>
+
+    <context id="for-command" end-at-line-end="true">
+      <start>\bfor\b</start>
+      <end>(?=\S)</end>
+      <include>
+        <context sub-pattern="0" where="start" style-ref="keyword"/>
+        <context ref="def:line-continue"/>
+        <context style-ref="variable-definition" once-only="true">
+          <match>\%{identifier}</match>
+        </context>
+      </include>
+    </context>
+
     <context id="built-in-command">
       <include>
         <context ref="built-in-command-1"/>
-        <context ref="for-command"/>
         <context ref="built-in-command-2"/>
+        <context ref="case-command"/>
+        <context ref="for-command"/>
       </include>
     </context>
 
@@ -475,16 +523,6 @@
       <keyword>znew</keyword>
     </context>
 
-    <context id="case">
-      <start>\bcase\b</start>
-      <end>\besac\b</end>
-      <include>
-        <context sub-pattern="0" where="start" style-ref="keyword"/>
-        <context sub-pattern="0" where="end" style-ref="keyword"/>
-        <context ref="sh"/>
-      </include>
-    </context>
-
     <context id="sh" class="no-spell-check">
       <include>
         <context ref="def:shebang"/>
@@ -494,7 +532,6 @@
         <context ref="double-quoted-string"/>
         <context ref="subshell"/>
         <context ref="backtick-subshell"/>
-        <context ref="case"/>
         <context ref="punctuator"/>
         <context ref="function"/>
         <context ref="here-doc"/>
diff --git a/tests/syntax-highlighting/file.sh b/tests/syntax-highlighting/file.sh
index c424142a..c6b9b172 100644
--- a/tests/syntax-highlighting/file.sh
+++ b/tests/syntax-highlighting/file.sh
@@ -32,14 +32,20 @@ xxx${x#"*"}xxx
 
 # Variable definitions
 var1=val1; var2=val2
+local var=xxx
 if var=$(cmd); then some; fi
 test -f xxx && var=xxx || var=yyy
-echo text | var=xxx cmd
+echo text | var=xxx cmd & var=yyy
+declare -i '-r' "-x" var1=val1 var2=val2
+case $1 in
+  item) var=xxx;;
+  *)var=yyy;;
+esac
 
 # For statements
 for word in hello world
 do
-    echo $word
+  echo $word
 done
 
 for arg; do echo $arg; done


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