From f6607c869cf0f5113d657f713fc35fd66a13b8f1 Mon Sep 17 00:00:00 2001
From: Jonathan Poelen <jonathan.poelen@gmail.com>
Date: Sun, 6 Dec 2020 16:25:16 +0000
Subject: [PATCH] Bash: add (...), ||, && in [[ ... ]] ; add backquote in [ ...
 ] and [[ ... ]]

---
 data/syntax/bash.xml                  | 72 ++++++++++++++++++---------
 6 files changed, 84 insertions(+), 23 deletions(-)

diff --git a/data/syntax/bash.xml b/data/syntax/bash.xml
index f76b008c..3b8824df 100644
--- a/data/syntax/bash.xml
+++ b/data/syntax/bash.xml
@@ -28,7 +28,7 @@
         <!ENTITY heredocq "(?|&quot;([^&quot;]+)&quot;|'([^']+)'|\\(.[^&wordseps;&substseps;]*))">
 ]>
 
-<language name="Bash" version="15" kateversion="5.62" section="Scripts" extensions="*.sh;*.bash;*.ebuild;*.eclass;*.nix;.bashrc;.bash_profile;.bash_login;.profile;PKGBUILD;APKBUILD" mimetype="application/x-shellscript" casesensitive="1" author="Wilbert Berendsen (wilbert@kde.nl)" license="LGPL">
+<language name="Bash" version="16" kateversion="5.62" section="Scripts" extensions="*.sh;*.bash;*.ebuild;*.eclass;*.nix;.bashrc;.bash_profile;.bash_login;.profile;PKGBUILD;APKBUILD" mimetype="application/x-shellscript" casesensitive="1" author="Wilbert Berendsen (wilbert@kde.nl)" license="LGPL">
 
 <!-- (c) 2004 by Wilbert Berendsen (wilbert@kde.nl)
     Changes by Matthew Woehlke (mw_triad@users.sourceforge.net)
@@ -495,7 +495,7 @@
       </context>
       <context attribute="Normal Text" lineEndContext="#pop" name="MaybeBracketExpression" fallthroughContext="#pop!Command">
         <!-- start expression in double brackets -->
-        <RegExpr attribute="Keyword" context="ExprDblBracket" String="\[\[&eos;" beginRegion="expression"/>
+        <RegExpr attribute="Keyword" context="ExprDblBracket" String="\[\[(?=$|[ &tab;(])" beginRegion="expression"/>
         <!-- start expression in single brackets -->
         <RegExpr attribute="Builtin" context="ExprBracket" String="\[&eos;" beginRegion="expression"/>
       </context>
@@ -642,6 +642,9 @@
         <IncludeRules context="FindWord"/>
         <DetectChar context="NormalMaybeBraceExpansion" char="{" lookAhead="1"/>
         <IncludeRules context="FindPathThenPop"/>
+        <IncludeRules context="FindNormalTextOption"/>
+      </context>
+      <context attribute="Normal Text" lineEndContext="#stay" name="FindNormalTextOption">
         <RegExpr attribute="Normal Text" context="#stay" String="[^&wordseps;&substseps;]+"/>
       </context>
       <context attribute="Normal Text" lineEndContext="#pop" name="OptionMaybeBraceExpansion">
@@ -1146,33 +1149,40 @@
 
       <context attribute="Normal Text" lineEndContext="#stay" name="ExprBracketNot" fallthroughContext="#pop!ExprBracketParam1">
         <DetectSpaces attribute="Normal Text" context="#pop!ExprBracketParam1"/>
-        <DetectChar context="ExprBracketTestMaybeNot" char="!" lookAhead="1"/>
+        <Detect2Chars attribute="Expression" context="ExprBracketTestMaybeNot" char="!" char1=" " lookAhead="1"/>
+        <Detect2Chars attribute="Expression" context="ExprBracketTestMaybeNot" char="!" char1="&tab;" lookAhead="1"/>
       </context>
-      <context attribute="Normal Text" lineEndContext="#pop" name="ExprBracketTestMaybeNot" fallthroughContext="#pop#pop!ExprBracketParam1">
-        <RegExpr attribute="Expression" context="#pop" String="!(?=[ &tab;])"/>
+      <context attribute="Normal Text" lineEndContext="#pop" name="ExprBracketTestMaybeNot">
+        <DetectChar attribute="Expression" context="#pop" char="!"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="#stay" name="ExprBracketParam1" fallthroughContext="NormalOption">
+      <context attribute="Normal Text" lineEndContext="#stay" name="ExprBracketParam1" fallthroughContext="ExprBracketValue">
         <DetectSpaces attribute="Normal Text" context="#pop!ExprBracketParam2"/>
         <DetectChar context="TestMaybeUnary" char="-" lookAhead="1"/>
         <IncludeRules context="FindExprBracketEnd"/>
+      </context>
+
+      <context attribute="Normal Text" lineEndContext="#pop" name="ExprBracketValue">
         <AnyChar attribute="Error" context="#stay" String="&symbolseps;"/>
+        <AnyChar context="#pop" String=" &tab;" lookAhead="1"/>
+        <IncludeRules context="FindWord"/>
+        <DetectChar context="NormalMaybeBraceExpansion" char="{" lookAhead="1"/>
+        <IncludeRules context="FindPathThenPop"/>
+        <IncludeRules context="FindNormalTextOption"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="#stay" name="ExprBracketParam2" fallthroughContext="NormalOption">
+      <context attribute="Normal Text" lineEndContext="#stay" name="ExprBracketParam2" fallthroughContext="ExprBracketValue">
         <DetectSpaces attribute="Normal Text" context="#pop!ExprBracketParam3"/>
         <AnyChar context="TestMaybeBinary" String="-=!" lookAhead="1"/>
         <IncludeRules context="FindExprBracketEnd"/>
-        <AnyChar attribute="Error" context="#stay" String="&symbolseps;"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="ExprBracketFinal" name="ExprBracketParam3" fallthroughContext="NormalOption">
+      <context attribute="Normal Text" lineEndContext="ExprBracketFinal" name="ExprBracketParam3" fallthroughContext="ExprBracketValue">
         <DetectSpaces attribute="Normal Text" context="#pop!ExprBracketFinal"/>
         <IncludeRules context="FindExprBracketEnd"/>
-        <AnyChar attribute="Error" context="#stay" String="&symbolseps;"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="#stay" name="ExprBracketFinal" fallthroughContext="NormalOption">
+      <context attribute="Normal Text" lineEndContext="#stay" name="ExprBracketFinal" fallthroughContext="ExprBracketValue">
         <DetectSpaces attribute="Normal Text" context="#stay"/>
         <IncludeRules context="FindExprBracketEnd"/>
         <RegExpr attribute="Error" context="#pop" String="(?:[^] &tab;]++|\][^ &tab;])++" endRegion="expression"/>
@@ -1183,10 +1193,10 @@
         <RegExpr attribute="Builtin" context="#pop" String="\](?=($|[ &tab;;|&amp;&lt;>]))" endRegion="expression"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="#pop" name="TestMaybeUnary" fallthroughContext="#pop!NormalOption">
+      <context attribute="Normal Text" lineEndContext="#pop" name="TestMaybeUnary" fallthroughContext="#pop!ExprBracketValue">
         <RegExpr attribute="Expression" context="#pop" String="-[abcdefghkprstuwxGLNOSovRnz](?=[ &tab;])"/>
       </context>
-      <context attribute="Normal Text" lineEndContext="#pop" name="TestMaybeBinary" fallthroughContext="#pop!NormalOption">
+      <context attribute="Normal Text" lineEndContext="#pop" name="TestMaybeBinary" fallthroughContext="#pop!ExprBracketValue">
         <RegExpr attribute="Expression" context="#pop" String="(?:-(?:e[fq]|[nolg]t|[nlg]e)|==?|!=)(?=[ &tab;])"/>
       </context>
 
@@ -1202,40 +1212,56 @@
         <DetectChar context="ExprDblBracketTestMaybeNot" char="!" lookAhead="1"/>
       </context>
       <context attribute="Normal Text" lineEndContext="#pop" name="ExprDblBracketTestMaybeNot" fallthroughContext="#pop#pop!ExprDblBracketParam1">
-        <IncludeRules context="ExprBracketTestMaybeNot"/>
+        <RegExpr attribute="Expression" context="#pop" String="!(?=$|[ &tab;(])"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="#stay" name="ExprDblBracketParam1" fallthroughContext="NormalOption">
+      <context attribute="Normal Text" lineEndContext="#stay" name="ExprDblBracketParam1" fallthroughContext="ExprDblBracketValue">
         <DetectSpaces attribute="Normal Text" context="#pop!ExprDblBracketParam2"/>
         <DetectChar context="TestMaybeUnary" char="-" lookAhead="1"/>
         <AnyChar attribute="Expression" context="#pop!ExprDblBracketParam3Spe" String="&lt;>"/>
         <IncludeRules context="FindExprDblBracketEnd"/>
-        <AnyChar attribute="Error" context="#stay" String="&symbolseps;"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="#stay" name="ExprDblBracketParam2" fallthroughContext="NormalOption">
+      <context attribute="Normal Text" lineEndContext="#pop" name="ExprDblBracketValue">
+        <Detect2Chars attribute="Keyword" context="ExprDblParen" char="(" char1="(" beginRegion="expression"/>
+        <DetectChar context="ExprDblBracketSubValue" char="(" lookAhead="1"/>
+        <DetectChar attribute="Operator" context="#pop#pop" char=")"/>
+        <Detect2Chars attribute="Control" context="#pop#pop!ExprDblBracket" char="&amp;" char1="&amp;"/>
+        <Detect2Chars attribute="Control" context="#pop#pop!ExprDblBracket" char="|" char1="|"/>
+        <AnyChar attribute="Error" context="#stay" String="|&amp;;)"/>
+        <AnyChar context="#pop" String=" &tab;&lt;>" lookAhead="1"/>
+        <IncludeRules context="FindWord"/>
+        <DetectChar context="NormalMaybeBraceExpansion" char="{" lookAhead="1"/>
+        <IncludeRules context="FindPathThenPop"/>
+        <IncludeRules context="FindNormalTextOption"/>
+      </context>
+      <context attribute="Normal Text" lineEndContext="#pop" name="ExprDblBracketSubValue" fallthroughContext="#pop">
+        <DetectChar attribute="Operator" context="ExprDblBracketNot" char="("/>
+        <Detect2Chars context="#pop#pop" char="]" char1="]" lookAhead="1"/>
+      </context>
+
+      <context attribute="Normal Text" lineEndContext="#stay" name="ExprDblBracketParam2" fallthroughContext="ExprDblBracketValue">
         <DetectSpaces attribute="Normal Text" context="#pop!ExprDblBracketParam3"/>
         <AnyChar context="TestMaybeBinary2" String="-=!" lookAhead="1"/>
         <AnyChar attribute="Expression" context="#pop!ExprDblBracketParam3Spe" String="&lt;>"/>
         <IncludeRules context="FindExprDblBracketEnd"/>
-        <AnyChar attribute="Error" context="#stay" String="&symbolseps;"/>
       </context>
-      <context attribute="Normal Text" lineEndContext="#pop" name="TestMaybeBinary2" fallthroughContext="#pop!NormalOption">
+      <context attribute="Normal Text" lineEndContext="#pop" name="TestMaybeBinary2" fallthroughContext="#pop!ExprDblBracketValue">
         <IncludeRules context="TestMaybeBinary"/>
-        <RegExpr attribute="Expression" context="#pop#pop!ExprDblBracketRegex" String="=~(?=[ &tab;])"/>
+        <RegExpr attribute="Expression" context="#pop#pop!ExprDblBracketRegex" String="=~(?=[ &tab;(])"/>
       </context>
 
       <context attribute="Normal Text" lineEndContext="#stay" name="ExprDblBracketParam3Spe" fallthroughContext="#pop!ExprDblBracketParam3">
         <DetectSpaces attribute="Normal Text" context="#pop!ExprDblBracketParam3"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="ExprDblBracketFinal" name="ExprDblBracketParam3" fallthroughContext="NormalOption">
+      <context attribute="Normal Text" lineEndContext="ExprDblBracketFinal" name="ExprDblBracketParam3" fallthroughContext="ExprDblBracketValue">
         <DetectSpaces attribute="Normal Text" context="#pop!ExprDblBracketFinal"/>
         <IncludeRules context="FindExprDblBracketEnd"/>
-        <AnyChar attribute="Error" context="#stay" String="&symbolseps;"/>
+        <AnyChar attribute="Error" context="#stay" String="&lt;>"/>
       </context>
 
-      <context attribute="Normal Text" lineEndContext="#stay" name="ExprDblBracketFinal" fallthroughContext="NormalOption">
+      <context attribute="Normal Text" lineEndContext="#stay" name="ExprDblBracketFinal" fallthroughContext="ExprDblBracketValue">
         <DetectSpaces attribute="Normal Text" context="#stay"/>
         <IncludeRules context="FindExprDblBracketEnd"/>
         <RegExpr attribute="Error" context="#pop" String="(?:[^] &tab;]++|\](?:[^]]|\][^ &tab;]))++" endRegion="expression"/>
-- 
GitLab
