Commits

camlspotter  committed 401df83

<:qx<...>> or $`...` now uses Spotlib.Spot.Unix.Command.shell_command directly. If you need a different behaviour just use <:qq<...>> then give the result to something appropriate

  • Participants
  • Parent commits 5dd57e2

Comments (0)

Files changed (22)

 LIBFILES[] = 
   regexp
   cformat
+  qx
   std
 
 LIB = orakuda

File pa/OMakefile

-OCAMLPACKS[] = pcre camlp4
+OCAMLPACKS[] = pcre camlp4 spotlib
 
 HAS_EXTENSION=
 match $(string $(shell ocamlc -version))

File pa/pa_command.ml

   include Syntax
 
   module Pa_format = Pa_format.Make(Syntax) 
-
+    
   let parse stopat _loc _loc_var_opt s =
     match Pa_format.parse stopat _loc _loc_var_opt s with
     | (`Const _ | `Fun _), Some rem ->
         let pos = String.length s - String.length rem in
         raise (Lexformat.Error (pos, pos + 1,
 		                Printf.sprintf "Unescaped special character %C found" rem.[0]))
-    | `Const s, _ -> <:expr<command $s$>>
+    | `Const s, _ -> <:expr<Orakuda.Qx.command $s$>>
     | `Fun (_abss, f), _ -> 
 	f <:expr<
-	    Printf.ksprintf command
+	    Printf.ksprintf Orakuda.Qx.command
 	  >>
   ;;
 

File pa/pa_sprintf.ml

 open Camlp4                                             (* -*- camlp4of -*- *)
 
 module Id : Sig.Id = struct
-  let name = "pa_xprintf"
+  let name = "pa_sprintf"
   let version = "1.0"
 end
 
+let command s k = Spotlib.Spot.Unix.Command.(shell s |> get_stdout |> k)
+val command : string -> (Unix.process_status * string list -> 'a) -> 'a

File test-requires-patch/Error_regexp_double_named_group.ml

+let _ = $/(?P<x>[a-z]+)(?P<x>[0-9]+)/;;

File test-requires-patch/OMakefile

+OCAMLPACKS[] =
+	pcre
+	camlp4
+	spotlib
+
+OCAMLPPFLAGS=-pp camlp4of
+
+# CR jfuruse: pcre dir is hard coded
+OCAML_TEST(f) = 
+   println(testing $(f))
+   ocaml -I .. -I ../pa -I $(shell ocamlfind query pcre) -I $(shell ocamlfind query spotlib) -I $(shell ocamlfind query pa_ounit) dynlink.cma camlp4o.cma pa_orakuda.cma pcre.cma unix.cma str.cma pa_ounit_lib.cma spotlib.cma orakuda.cmo $(f)
+   println(test $(f) done)
+
+.PHONY: test 
+test: ../orakuda.cmo ../pa/pa_orakuda.cma
+	$(OCAML_TEST test_regexp.ml)
+	$(OCAML_TEST test_regexp_case.ml)
+	$(OCAML_TEST test_regexp_subst.ml)
+	$(OCAML_TEST test_cformat.ml)
+	$(OCAML_TEST test_sprintf.ml)
+	$(OCAML_TEST test_command.ml)
+        println(test done)
+
+.PHONY: top
+top: ../orakuda.cmo
+	ocaml -I +camlp4 -I ../pa -I .. -I +site-lib/pcre dynlink.cma camlp4o.cma pa_orakuda.cma pcre.cma unix.cma orakuda.cmo
+
+CAMLP4_TEST= camlp4of -I ../pa pa_orakuda.cma pcre.cma 
+
+%.out.ml: %.ml ../pa/pa_orakuda.cma
+	camlp4of -printer OCaml -I ../pa pa_orakuda.cma $< > $@
+
+clean:
+	rm -f *.out.ml
+

File test-requires-patch/test_cformat.ml

+(*
+  Pre-compiled printf format $%"....." and its use by Cformat
+
+  Cformat.sprintf $%"..." is actually equivalent with $"...".
+*)
+
+open Orakuda.Std
+
+let _ = 
+  assert (Cformat.sprintf $%"hello%dworld" 2 = "hello2world");
+
+  let x = 1 and y = 2 in 
+  assert (Cformat.sprintf $%"%d %${x}d %d %${y}d %d" 3 4 5 = "3 1 4 2 5");
+
+  assert (Cformat.sprintf $%"%1.F" 123.456 = "123.");
+;;
+
+(* people who cannot use the p4 patch *)
+let _ = 
+  assert (Cformat.sprintf <:fmt<hello%dworld>> 2 = "hello2world");
+
+  let x = 1 and y = 2 in 
+  assert (Cformat.sprintf <:fmt<%d %${x}d %d %${y}d %d>> 3 4 5 = "3 1 4 2 5")
+;;
+
+let _ = $%"%*.*d"
+
+(* special char '"' escaping *)
+
+let _ = 
+  prerr_endline "special char escaping test";
+  assert (Cformat.sprintf $%"hello\"%d\"world" 2 = "hello\"2\"world");
+  assert (Cformat.sprintf <:fmt<hello\"%d\"world>> 2 = "hello\"2\"world");
+  assert (Cformat.sprintf <:fmt<h"ello\"%d\"world>> 2 = "h\"ello\"2\"world"); (* CR jfuruse: BUGGY! *)

File test-requires-patch/test_command.ml

+(*
+
+  Sub-shell call $`...` which calls `command'. The command variable must be bound manually.
+
+*)
+
+open Spotlib.Spot
+let command s = Spotlib.Spot.Unix.shell_command s
+
+let _ = $`ls .`
+;;
+    
+let _ = <:qx<ls .>>
+;;
+
+
+    

File test-requires-patch/test_regexp.ml

+(*
+  Regexp creation $/.../
+*)
+
+open Orakuda.Std
+open Orakuda.Regexp.Infix
+
+(* str_item *)
+$/([0-9]+)(?P<x>[a-z]+)|([A-Z]+)/;;
+
+(* CR jfuruse: CamlP4 problem: the following is not parsable. *) 
+(* $/([0-9]+)(?P<x>[a-z]+)|([A-Z]+)/, 1;; *)
+($/([0-9]+)(?P<x>[a-z]+)|([A-Z]+)/, 1);;
+
+let rex = $/([0-9]+)(?P<x>[a-z]+)|([A-Z]+)/;;
+
+let res =
+  match Regexp.exec rex "abc123def456" with
+  | None -> assert false
+  | Some res ->
+      assert (res#_0 = "123def");
+      assert (res#_1 = "123");
+      assert (res#_2 = "def");
+      assert (res#_2 = res#x);
+      assert (res#_left = "abc");
+      assert (res#_right = "456");
+      assert (res#_3 = "");
+      assert (res#_last = "def");
+      res
+;;
+
+let rex = <:m<([0-9]+)(?P<x>[a-z]+)|([A-Z]+)>>;;
+
+let res =
+  match Regexp.exec rex "abc123def456" with
+  | None -> assert false
+  | Some res ->
+      assert (res#_0 = "123def");
+      assert (res#_1 = "123");
+      assert (res#_2 = "def");
+      assert (res#_2 = res#x);
+      assert (res#_left = "abc");
+      assert (res#_right = "456");
+      assert (res#_3 = "");
+      assert (res#_last = "def");
+      res
+;;
+
+let _ = assert (("HeLlO" =~ $/hello/i) <> None)
+
+let _ = assert (("HeLlO" =~ $/hello/) = None)
+
+let _ = assert (("HeLlO" =~ <:m<hello/i>>) <> None)
+
+let _ = assert (("HeLlO" =~ <:m<hello>>) = None)
+
+let () =
+  match "http://www.www.com" =~ <:m<http:\/\/([^.]+)\.([^.]+)\.([^.]+)>> with
+  | None -> assert false
+  | Some res -> 
+      assert (res#_1 = "www"
+           && res#_2 = "www"
+           && res#_3 = "com")

File test-requires-patch/test_regexp_case.ml

+(*
+  Regexp case match
+
+  string |! $/regexp/ as x -> ... | $/regexp/ as x -> ... | _ -> ...
+
+*)
+
+open Spotlib.Spot
+open Orakuda.Std
+
+let r = $/hello/ -> "world" | $/bye/ -> "universe" | _ -> "default"
+;;
+
+let _ = 
+  assert (r "hello world" = "world");
+  assert (r "bye universe" = "universe");
+  assert (r "42" = "default")
+;;
+
+(* CR jfuruse: hmmm. Precedence seems glitchy *)
+
+let _ = 
+  let res = 
+    "world bye universe" 
+    |! $/hello/ -> "world" 
+    | $/bye/ as x -> x#_left ^ x#_right
+    | _ -> "default"
+  in
+  assert ("world  universe" = res)
+;;
+
+let _ = 
+  let res = 
+    "world bye universe" 
+    |! $/([^ ]+) ([^ ]+) ([^ ]+)/ as x -> x#_1
+    |! $/([^ ]+) ([^ ]+) ([^ ]+)/ as x -> (match x#_1opt with Some v -> v | None -> "")
+    | $/(.*)/ as x -> x#_1
+    | _ -> "default"
+  in
+  assert ("world" = res)
+;;
+
+let _ =
+  "hello"
+  |! $/^S_|\/S_/ -> true 
+  | _ -> false
+;;
+
+let _ =
+  "hello"
+  |! <:m<^S_|\/S_>> -> true 
+  | _ -> false

File test-requires-patch/test_regexp_subst.ml

+(*
+  Regexp substitution $s/.../.../
+*)
+
+let s = $s/l+/x/g "hello world";;
+let () = assert (s = "hexo worxd");;
+
+let s = $s/l+/$&$0/g "hello world";;
+let () = assert (s = "hellllo worlld");;
+
+let s = <:s<l+/$&$0>> "hello world";;
+let () = assert (s = "hellllo world");;
+
+let s = <:s<l+/$&$0/g>> "hello world";;
+let () = assert (s = "hellllo worlld");;

File test-requires-patch/test_sprintf.ml

+(*
+  pre-compiled efficient sprintf $"xxx"
+*)
+
+let _ =
+  assert ("hello" = $"hello");
+  let w = "world" in
+  assert ("hello world" = $"hello %s" w);
+  assert ("hello world" = $"hello $w");
+  assert ("hello worldworld" = $"hello ${w ^ w}");
+
+  let x = 42 in
+  assert ("answer = 42" = $"answer = %d" x);
+  assert ("answer = 42" = $"answer = %${x}d");
+  assert ("answer^2 = 1764" = $"answer^2 = %${x * x}d");
+  (* assert ("123." = $"%${123.456}1.F"); (* it fails if ocaml bug in printf.ml is not fixed *) *)
+
+  assert ("hello" = <:qq<hello>>);
+  let w = "world" in
+  assert ("hello world" = <:qq<hello %s>> w);
+  assert ("hello world" = <:qq<hello $w>>);
+  assert ("hello worldworld" = <:qq<hello ${w ^ w}>>);
+
+  let x = 42 in
+  assert ("answer = 42" = <:qq<answer = %d>> x);
+  assert ("answer = 42" = <:qq<answer = %${x}d>>);
+  assert ("answer^2 = 1764" = <:qq<answer^2 = %${x * x}d>>);
+  (* assert ("123." = <:qq<%${123.456}1.F>>); (* it fails if ocaml bug in printf.ml is not fixed *) *)
+;;
+

File test-requires-patch/x.ml

+let _ = <:m<xxx>>

File test/OMakefile

 # CR jfuruse: pcre dir is hard coded
 OCAML_TEST(f) = 
    println(testing $(f))
-   ocaml -I .. -I ../pa -I $(shell ocamlfind query pcre) -I $(shell ocamlfind query spotlib) -I $(shell ocamlfind query pa_ounit) dynlink.cma camlp4o.cma pa_orakuda.cma pcre.cma unix.cma str.cma pa_ounit_lib.cma spotlib.cma orakuda.cmo $(f)
+   ocaml -I .. -I ../pa -I $(shell ocamlfind query pcre) -I $(shell ocamlfind query spotlib) -I $(shell ocamlfind query pa_ounit) dynlink.cma camlp4o.cma pa_ounit_lib.cma str.cma unix.cma spotlib.cma pa_orakuda.cma pcre.cma orakuda.cmo $(f)
    println(test $(f) done)
 
 .PHONY: test 

File test/test_cformat.ml

 open Orakuda.Std
 
 let _ = 
-  assert (Cformat.sprintf $%"hello%dworld" 2 = "hello2world");
-
-  let x = 1 and y = 2 in 
-  assert (Cformat.sprintf $%"%d %${x}d %d %${y}d %d" 3 4 5 = "3 1 4 2 5");
-
-  assert (Cformat.sprintf $%"%1.F" 123.456 = "123.");
-;;
-
-(* people who cannot use the p4 patch *)
-let _ = 
   assert (Cformat.sprintf <:fmt<hello%dworld>> 2 = "hello2world");
 
   let x = 1 and y = 2 in 
-  assert (Cformat.sprintf <:fmt<%d %${x}d %d %${y}d %d>> 3 4 5 = "3 1 4 2 5")
+  assert (Cformat.sprintf <:fmt<%d %${x}d %d %${y}d %d>> 3 4 5 = "3 1 4 2 5");
+
+  assert (Cformat.sprintf <:fmt<%1.F>> 123.456 = "123.");
 ;;
 
-let _ = $%"%*.*d"
+let _ = <:fmt<%*.*d>>
 
 (* special char '"' escaping *)
 
 let _ = 
   prerr_endline "special char escaping test";
-  assert (Cformat.sprintf $%"hello\"%d\"world" 2 = "hello\"2\"world");
   assert (Cformat.sprintf <:fmt<hello\"%d\"world>> 2 = "hello\"2\"world");
-  assert (Cformat.sprintf <:fmt<h"ello\"%d\"world>> 2 = "h\"ello\"2\"world"); (* CR jfuruse: BUGGY! *)
+  assert (Cformat.sprintf <:fmt<hello\"%d\"world>> 2 = "hello\"2\"world");
+  assert (Cformat.sprintf <:fmt<h"ello\"%d\"world>> 2 = "h\"ello\"2\"world")

File test/test_command.ml

 *)
 
 open Spotlib.Spot
-let command s = Spotlib.Spot.Unix.shell_command s
 
-let _ = $`ls .`
+let _ = <:qx<ls .>> & fun (_, xs) -> prerr_string & String.concat "" xs
 ;;
-    
-let _ = <:qx<ls .>>
-;;
-
-
-    

File test/test_regexp.ml

 open Orakuda.Regexp.Infix
 
 (* str_item *)
-$/([0-9]+)(?P<x>[a-z]+)|([A-Z]+)/;;
+<:m<([0-9]+)(?P<x>[a-z]+)|([A-Z]+)>>;;
 
 (* CR jfuruse: CamlP4 problem: the following is not parsable. *) 
 (* $/([0-9]+)(?P<x>[a-z]+)|([A-Z]+)/, 1;; *)
-($/([0-9]+)(?P<x>[a-z]+)|([A-Z]+)/, 1);;
-
-let rex = $/([0-9]+)(?P<x>[a-z]+)|([A-Z]+)/;;
-
-let res =
-  match Regexp.exec rex "abc123def456" with
-  | None -> assert false
-  | Some res ->
-      assert (res#_0 = "123def");
-      assert (res#_1 = "123");
-      assert (res#_2 = "def");
-      assert (res#_2 = res#x);
-      assert (res#_left = "abc");
-      assert (res#_right = "456");
-      assert (res#_3 = "");
-      assert (res#_last = "def");
-      res
-;;
+(<:m<([0-9]+)(?P<x>[a-z]+)|([A-Z]+)>>, 1);;
 
 let rex = <:m<([0-9]+)(?P<x>[a-z]+)|([A-Z]+)>>;;
 
       res
 ;;
 
-let _ = assert (("HeLlO" =~ $/hello/i) <> None)
-
-let _ = assert (("HeLlO" =~ $/hello/) = None)
-
 let _ = assert (("HeLlO" =~ <:m<hello/i>>) <> None)
 
 let _ = assert (("HeLlO" =~ <:m<hello>>) = None)

File test/test_regexp_case.ml

 open Spotlib.Spot
 open Orakuda.Std
 
-let r = $/hello/ -> "world" | $/bye/ -> "universe" | _ -> "default"
+let r = <:m<hello>> -> "world" | <:m<bye>> -> "universe" | _ -> "default"
 ;;
 
 let _ = 
 let _ = 
   let res = 
     "world bye universe" 
-    |! $/hello/ -> "world" 
-    | $/bye/ as x -> x#_left ^ x#_right
+    |! <:m<hello>> -> "world" 
+    | <:m<bye>> as x -> x#_left ^ x#_right
     | _ -> "default"
   in
   assert ("world  universe" = res)
 let _ = 
   let res = 
     "world bye universe" 
-    |! $/([^ ]+) ([^ ]+) ([^ ]+)/ as x -> x#_1
-    |! $/([^ ]+) ([^ ]+) ([^ ]+)/ as x -> (match x#_1opt with Some v -> v | None -> "")
-    | $/(.*)/ as x -> x#_1
+    |! <:m<([^ ]+) ([^ ]+) ([^ ]+)>> as x -> x#_1
+    |! <:m<([^ ]+) ([^ ]+) ([^ ]+)>> as x -> (match x#_1opt with Some v -> v | None -> "")
+    | <:m<(.*)>> as x -> x#_1
     | _ -> "default"
   in
   assert ("world" = res)
 
 let _ =
   "hello"
-  |! $/^S_|\/S_/ -> true 
+  |! <:m<^S_|\/S_>> -> true 
   | _ -> false
 ;;
 
-let _ =
-  "hello"
-  |! <:m<^S_|\/S_>> -> true 
-  | _ -> false

File test/test_regexp_subst.ml

   Regexp substitution $s/.../.../
 *)
 
-let s = $s/l+/x/g "hello world";;
-let () = assert (s = "hexo worxd");;
-
-let s = $s/l+/$&$0/g "hello world";;
-let () = assert (s = "hellllo worlld");;
-
 let s = <:s<l+/$&$0>> "hello world";;
 let () = assert (s = "hellllo world");;
 

File test/test_sprintf.ml

 *)
 
 let _ =
-  assert ("hello" = $"hello");
-  let w = "world" in
-  assert ("hello world" = $"hello %s" w);
-  assert ("hello world" = $"hello $w");
-  assert ("hello worldworld" = $"hello ${w ^ w}");
-
-  let x = 42 in
-  assert ("answer = 42" = $"answer = %d" x);
-  assert ("answer = 42" = $"answer = %${x}d");
-  assert ("answer^2 = 1764" = $"answer^2 = %${x * x}d");
-  (* assert ("123." = $"%${123.456}1.F"); (* it fails if ocaml bug in printf.ml is not fixed *) *)
-
   assert ("hello" = <:qq<hello>>);
   let w = "world" in
   assert ("hello world" = <:qq<hello %s>> w);