Allow job names to be arbitrary OCaml expressions.
[whenjobs.git] / lib / pa_when.ml
index da51e20..6e0592d 100644 (file)
@@ -84,8 +84,10 @@ let lift_expr = M.Expr.meta_expr
 
 (* Handle a top level statement. *)
 let rec call_stmt name (_loc, stmt, sh) =
-  let name = if name <> "" then name else unique_job_name () in
-  let name = <:expr< $str:name$ >> in
+  let name =
+    match name with
+    | None -> let name = unique_job_name () in <:expr< $str:name$ >>
+    | Some name -> name in
   match stmt with
   | `When e -> when_stmt _loc name e sh
   | `Every p -> every_stmt _loc name p sh
@@ -177,26 +179,6 @@ let period_parser =
         )
       | _ -> raise Stream.Failure
     )
-
-(*
-(* This hand-written parser looks for "job <name>" before a statement. *)
-let optjob =
-  Gram.Entry.of_parser "optjob"
-    (fun stream ->
-      let info, name =
-        match Stream.npeek 2 stream with
-        | [ LIDENT "job", info; STRING (_,name), _ ] ->
-          Stream.junk stream;
-          Stream.junk stream;
-          info, name
-        | (_, info) :: _ ->
-          (* Job is unnamed so generate a unique internal name. *)
-          info, unique_job_name ()
-        | _ -> assert false in
-      let _loc = Gram.token_location info in
-      <:expr< $str:name$ >>
-    )
-*)
 ;;
 
 EXTEND Gram
@@ -231,8 +213,8 @@ EXTEND Gram
 
   (* "str_item" is a top level statement in an OCaml program. *)
   str_item: LEVEL "top" [
-    [ s = statement -> call_stmt "" s ]
-  | [ "job"; name = STRING; s = statement -> call_stmt name s ]
+    [ s = statement -> call_stmt None s ]
+  | [ "job"; name = expr; s = statement -> call_stmt (Some name) s ]
   ];
 
 END