Commits

juster committed 42c4021

Many cosmetic code changes.

Removes superfluous arguments like all the extra 'str' and 'params'
arguments. Renames many ugly function names.

Comments (0)

Files changed (1)

     let parameter_expand str params =
 
       (* Recursive helper function for parameter_expand. *)
-      let rec pexpand str idx params =
+      let rec expand_at idx =
 
-        let rec expand_name str idx params =
+        (* Point expand_name to the index after a $. *)
+        let rec expand_name idx =
 
-          let inbrackets = fun str -> function (idx, name) ->
+          let expand_inbraces = function (idx, name) ->
 
-            (* Similiar to pexpand. Expand everything until closing bracket. *)
-            (* XXX: getting tired of passing params around, just use a ref? *)
-            let rec uptoclose str idx =
+            (* Similiar to expand_at. *)
+            (* Expand everything until closing bracket. *)
+            let rec expand_until_curly idx =
               try
                 (* Look ahead for the next } and $. *)
                 let brckidx = String.index_from str idx '}' in
                     let sigidx = String.index_from str idx '$' in
                     if sigidx > brckidx then raise Not_found
                     else
-                      (* XXX: Copy/pasted from pexpand below. Refactor? *)
+                      (* XXX: Copy/pasted from expand_at below. Refactor? *)
                       let donechunk = String.sub str idx (sigidx - idx) in
-                      match expand_name str (sigidx + 1) params
+                      match expand_name (sigidx + 1)
                       with (nextidx, expanded) ->
                         (* Repeat because the bracket could be gone now. *)
-                        match (uptoclose str nextidx) with (newidx, fin) ->
+                        match (expand_until_curly nextidx)
+                        with (newidx, fin) ->
                           (newidx, donechunk ^ expanded ^ fin)
-                    
                   with Not_found ->
                     (* No $ was found or it comes after the closing bracket. *)
                     (brckidx + 1, String.sub str idx (brckidx - idx))
               with Not_found -> raise (ExpandError idx)
             in
 
-            let inbrackets_subst name str idx =
+            let expand_param_subst name idx =
               (* XXX: What about badly formed input? *)
-              match uptoclose str (idx + 1) with (newidx, subst) ->
+              match expand_until_curly (idx + 1) with (newidx, subst) ->
                 let pval = Bashparams.string_of_param (get_param name params) in
                 let expstr =
                   match str.[idx] with
 
             match str.[idx] with
             | '}' -> (idx+1, Bashparams.string_of_param (get_param name params))
-            | ':' -> inbrackets_subst name str (idx+1)
+            | ':' -> expand_param_subst name (idx+1)
             |  _  -> raise Not_found
-          in (* end of inbrackets *)
+          in (* end of expand_inbraces *)
 
-          let extractname str idx =
+          let extract_name idx =
             if Str.string_match (Str.regexp "[a-zA-Z_-]+") str idx then
               (Str.match_end (), Str.matched_string str)
             else
           | '$' ->
               begin
                 try
-                  match expand_name str (idx+1) params with (newidx, name) ->
+                  match expand_name (idx+1) with (newidx, name) ->
                     (newidx, Bashparams.string_of_param (get_param name params))
                 with Invalid_argument(_) -> raise (ExpandError idx)
               end
           | '{' ->
               begin
-                try inbrackets str (expand_name str (idx+1) params)
+                try expand_inbraces (expand_name (idx+1))
                 with Invalid_argument(_) -> raise (UnbalancedBracket idx)
               end
           | _ ->
-              match extractname str idx with (newidx, name) ->
+              match extract_name idx with (newidx, name) ->
                 (newidx, Bashparams.string_of_param (get_param name params))
         in (* end of expand_name *)
 
           begin
             try
               let donechunk = String.sub str idx (sigidx - idx) in
-              match expand_name str (sigidx+1) params
+              match expand_name (sigidx+1)
               with (nextidx, expanded) ->
-                donechunk ^ expanded ^ (pexpand str nextidx params)
+                donechunk ^ expanded ^ (expand_at nextidx)
 
             (* This exception means the $ is at the end of the string. *)
             with Invalid_argument(_) -> raise (ExpandError sigidx)
         with Not_found ->
           (* There are no more $'s we are done. *)
           let len = String.length str in String.sub str idx (len - idx)
-      in (* end of pexpand *)
+      in (* end of expand_at *)
 
-      pexpand str 0 params
+      expand_at 0
 
   end
 
   let paramslist = [ ("FOO", Bashparams.param_of_string "BAR") ;
                      ("BAR", Bashparams.param_of_string "Hello, World!") ] in
   print_endline (Bashexpand.parameter_expand
-                   "Substitutions${BAZ:- IS WORKING}?" paramslist)
+                   "Substitutions${BAZ:- IS WORKING}!" paramslist)