[m2sci] kernel code reorganization: clarify dependencies & ease code browsing
[scilab.git] / scilab / modules / m2sci / macros / kernel / m2sci_syntax.sci
index 87b3ada..b933417 100644 (file)
@@ -1,8 +1,8 @@
 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 // Copyright (C) ???? - INRIA - Scilab
 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
-//
 // Copyright (C) 2012 - 2016 - Scilab Enterprises
+// Copyright 2018 - Samuel GOUGEON
 //
 // This file is hereby licensed under the terms of the GNU GPL v2.0,
 // pursuant to article 5.3.4 of the CeCILL v.2.1.
@@ -484,3 +484,159 @@ function [helppart,txt,batch] = m2sci_syntax(txt)
         // END of BUG 2341 fix: function prototype with no comma between output parameters names
     end
 endfunction
+
+// ---------------------------------------------------------------------------
+
+function txt=replace_brackets(txt)
+
+    symbs=[",",";","=",")","]","("]
+    // This file will be use to deal with cells...
+    for k=1:size(txt,"r")
+
+        // select-case
+        if strindex(txt(k),"case")<>[] then
+            txt(k)=strsubst(strsubst(txt(k),"{","makecell("),"}",")")
+        else
+            tk=strsubst(txt(k)," ","")
+
+            ko=strindex(tk,"{")
+            if ko<>[] then
+                teq=strindex(tk,"=")
+
+                ///
+                if ko(1)==1 then
+                    txt(k)=strsubst(txt(k),"{}","makecell()")
+                    txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
+                elseif or(part(tk,ko(1)-1)==symbs) then
+                    txt(k)=strsubst(txt(k),"{}","makecell()")
+                    txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
+                else // Cell index
+                    txt(k)=strsubst(strsubst(txt(k),"{","("),"}",").entries")
+                end
+
+                ////
+
+                for kk=2:size(ko,"*")
+                    if or(part(tk,ko(kk)-1)==symbs) then // Cell creation
+                        txt(k)=strsubst(txt(k),"{}","makecell()")
+                        txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
+                    else // Cell index
+                        txt(k)=strsubst(strsubst(txt(k),"{","("),"}",").entries")
+                    end
+                end
+            elseif ~isempty(strindex(txt(k),"}")) then
+                txt(k)=strsubst(txt(k),"}","]))")
+            end
+        end
+    end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function txt = replace_end_dollar(txt)
+
+    patterns = ["/(?:\(|\-|\+|\*|\:|\,)\s*end\s*(\)|(\-|\+|\*|\/|\:|\,).*?\))/"
+                "/(?:\{|\-|\+|\*|\:|\,)\s*end\s*(\}|(\-|\+|\*|\/|\:|\,).*?\})/"
+               ]'
+    for pattern = patterns
+        rows = grep(txt, pattern, "r");
+        for i = rows
+            t = txt(i);
+            [d, f, M] = regexp(t, pattern);
+            Mr = strsubst(M, "end", "$");
+            for j = 1:size(M,1)
+                t = strsubst(t, M(j), Mr(j));
+            end
+            txt(i) = t;
+        end
+    end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function txt = i_notation(txt)
+    // This function changes 1i ,... by 1*i,...
+
+    // M2SCI kernel functions called :
+    //  - isinstring
+
+    // To succeed in this work, we successively suppress occurences which can be proved not to be complex notation
+    // Until we are 'sure' to have a complex notation
+
+    n=size(txt,"r")
+
+    I="i";J="j"
+    matches=[string(0:9)+I(ones(1,10)),".i",string(0:9)+J(ones(1,10)),".j"]
+    symbs=["+","-","*","/","\","(","["," ","^"," ",",",";","=","{"]
+    s1=["+","-","*","/","\",",",";"," ","^",".","&","|","''","]",")","}"]
+    s2=[string(0:9),"d","e","D","E","."]
+
+    for k=1:n
+        // Isolate a possible appended comment
+        st=strindex(txt(k),[";//","//"])
+        endComment = "";
+        tk = txt(k) + " "
+        if st<> [] then
+            for stk=1:size(st,"*")
+                if ~isinstring(txt(k),st(stk)) then
+                    endComment = part(txt(k), st(stk):$);
+                    tk = part(txt(k), 1:st(stk)-1)
+                    break
+                end
+            end
+        end
+
+        // Find possible occurence of complex notation
+        kc=strindex(tk,matches)
+
+        // Kill indexes which point to non complex values (e.g. : a1item...)
+        for kk=size(kc,"*"):-1:1
+            km=kc(kk)+2
+            if find(part(tk,km)==s1)==[] then kc(kk)=[],end
+        end
+
+        kc=[0 kc]
+
+        for kk=size(kc,"*"):-1:2
+            km=kc(kk)
+            num=%T
+            // Reads numeric value leading complex variable
+            while or(part(tk,km)==s2)
+                km=km-1
+                if km<=kc(kk-1)+1 then
+                    km=kc(kk-1);
+                    num=%F;
+                    break
+                end
+            end
+
+            tokill=%F
+            num=part(tk,km+1:kc(kk)-1)
+            ke=strindex(convstr(num),["e","d"])
+            kd=strindex(convstr(num),".")
+
+            // Searching for invalid numeric values (more than one dot...)
+            if size(ke,2)>1|size(kd,2)>1 then
+                tokill=%T
+            elseif size(ke,2)==1&size(kd,2)==1 then
+                if ke<kd then tokill=%T,end
+            end
+
+            if ~tokill then
+                // If char which follows supposed complex notation is not an operation symbol
+                if km<>kc(kk-1) then
+                    if and(part(tk,km)<>symbs) then tokill=%T,end
+                end
+            end
+
+            if ~tokill then
+                km=kc(kk)
+                // If supposed complex notation is not in a string
+                if ~isinstring(tk,km) then
+                    tk=part(tk,1:km)+"*%"+part(tk,km+1:length(tk))
+                end
+            end
+        end
+        txt(k) = tk + endComment
+    end
+endfunction