2 * Copyright (C) 2012 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 (* For general information about camlp4, see:
20 * http://brion.inria.fr/gallium/index.php/Camlp4
22 * For information about quotations, see:
23 * http://brion.inria.fr/gallium/index.php/Quotation
32 let unique = let i = ref 0 in fun () -> incr i; !i
34 let unique_job_name () = sprintf "job$%d" (unique ())
37 let len = String.length s in
38 let sublen = String.length sub in
40 if i <= len-sublen then (
43 if s.[i+j] = sub.[j] then loop2 (j+1)
49 if r = -1 then loop (i+1) else r
55 let rec replace_str s s1 s2 =
56 let len = String.length s in
57 let sublen = String.length s1 in
61 let s' = String.sub s 0 i in
62 let s'' = String.sub s (i+sublen) (len-i-sublen) in
63 s' ^ s2 ^ replace_str s'' s1 s2
66 (* Convert a _loc to an AST. *)
67 let expr_of_loc _loc loc =
69 start_line, start_bol, start_off,
70 stop_line, stop_bol, stop_off,
71 ghost = Loc.to_tuple loc in
72 <:expr< Camlp4.PreCast.Loc.of_tuple
74 $`int:start_line$, $`int:start_bol$, $`int:start_off$,
75 $`int:stop_line$, $`int:stop_bol$, $`int:stop_off$,
78 (* "Lift" an expression, turning it from an expression into an OCaml
79 * abstract syntax tree in the output. This is pretty obscure.
80 * http://caml.inria.fr/pub/ml-archives/caml-list/2008/09/591f7c4a8df9295d675a5adcb6802748.en.html
82 module M = Ast.Meta.Make (Ast.Meta.MetaGhostLoc)
83 let lift_expr = M.Expr.meta_expr
85 (* Handle a top level statement. *)
86 let rec call_stmt name (_loc, stmt, sh) =
87 let name = if name <> "" then name else unique_job_name () in
88 let name = <:expr< $str:name$ >> in
90 | `When e -> when_stmt _loc name e sh
91 | `Every p -> every_stmt _loc name p sh
93 (* Handle a top level "when" statement.
94 * e -> when expression
95 * sh -> the shell script to run
96 * Returns a top level statement (str_item) which when executed just
97 * adds the statement to a global list.
99 and when_stmt _loc name e sh =
100 let loc = expr_of_loc _loc _loc in
101 let e = lift_expr _loc e in
104 Whenfile.add_when_job $loc$ $name$ $e$ $sh$
107 (* Handle a top level "every" statement. *)
108 and every_stmt _loc name period sh =
109 let loc = expr_of_loc _loc _loc in
112 Whenfile.add_every_job $loc$ $name$ $period$ $sh$
116 (* Quotation expander for shell script. *)
117 let sh_quotation_expander _loc _ sh =
118 let loc = expr_of_loc _loc _loc in
120 (* Convert ">\>" to ">>" in code. *)
121 let sh = replace_str sh ">\\>" ">>" in
123 <:expr< { Whenutils.sh_loc = $loc$;
124 sh_script = $str:sh$ } >>
126 Quotation.add "sh" Quotation.DynAst.expr_tag sh_quotation_expander;
128 (* Default quotation expander (<< .. >>) should be shell script ("sh"). *)
129 Quotation.default := "sh"
131 (* For period expressions "<NN> (secs|mins|hours|...)" we cannot use
132 * the ordinary camlp4 parser since it only looks ahead by 1 symbol, so
133 * it gets "stuck" on the integer. Write a custom parser instead.
135 * Note the EXTEND macro implicitly reserves KEYWORDs.
138 Gram.Entry.of_parser "period"
140 match Stream.peek stream with
141 | Some (INT (_, i), info) ->
142 let i = int_of_string i in
143 let _loc = Gram.token_location info in
145 (match Stream.next stream with
146 | KEYWORD ("sec"|"secs"|"second"|"seconds"), _ ->
147 <:expr< Whenutils.Every_seconds $`int:i$ >>
148 | KEYWORD ("min"|"mins"|"minute"|"minutes"), _ ->
150 <:expr< Whenutils.Every_seconds $`int:i$ >>
151 | KEYWORD ("hour"|"hours"), _ ->
153 <:expr< Whenutils.Every_seconds $`int:i$ >>
154 | KEYWORD ("day"|"days"), _ ->
155 <:expr< Whenutils.Every_days $`int:i$ >>
156 | KEYWORD ("week"|"weeks"), _ ->
158 <:expr< Whenutils.Every_days $`int:i$ >>
159 | KEYWORD ("month"|"months"), _ ->
160 <:expr< Whenutils.Every_months $`int:i$ >>
161 | KEYWORD ("year"|"years"), _ ->
162 <:expr< Whenutils.Every_years $`int:i$ >>
163 | KEYWORD ("decade"|"decades"), _ ->
165 <:expr< Whenutils.Every_years $`int:i$ >>
166 | KEYWORD ("century"|"centuries"|"centurys"), _ ->
168 <:expr< Whenutils.Every_years $`int:i$ >>
169 | KEYWORD ("millenium"|"millenia"|"milleniums"), _ ->
171 <:expr< Whenutils.Every_years $`int:i$ >>
172 | (KEYWORD s | LIDENT s), _ ->
173 eprintf "period: failed to parse %d %s\n%!" i s;
178 | _ -> raise Stream.Failure
182 (* This hand-written parser looks for "job <name>" before a statement. *)
184 Gram.Entry.of_parser "optjob"
187 match Stream.npeek 2 stream with
188 | [ LIDENT "job", info; STRING (_,name), _ ] ->
193 (* Job is unnamed so generate a unique internal name. *)
194 info, unique_job_name ()
195 | _ -> assert false in
196 let _loc = Gram.token_location info in
197 <:expr< $str:name$ >>
205 (* A period expression (used in "every"). *)
207 [ ["sec"|"secs"|"second"|"seconds"] ->
208 <:expr< Whenutils.Every_seconds 1 >> ]
209 | [ ["min"|"mins"|"minute"|"minutes"] ->
210 <:expr< Whenutils.Every_seconds 60 >> ]
211 | [ ["hour"|"hours"] -> <:expr< Whenutils.Every_seconds 3600 >> ]
212 | [ ["day"|"days"] -> <:expr< Whenutils.Every_days 1 >> ]
213 | [ ["week"|"weeks"] -> <:expr< Whenutils.Every_days 7 >> ]
214 | [ ["month"|"months"] -> <:expr< Whenutils.Every_months 1 >> ]
215 | [ ["year"|"years"] -> <:expr< Whenutils.Every_years 1 >> ]
216 | [ ["decade"|"decades"] -> <:expr< Whenutils.Every_years 10 >> ]
217 | [ ["century"|"centuries"|"centurys"] ->
218 <:expr< Whenutils.Every_years 100 >> ]
219 | [ ["millenium"|"millenia"|"milleniums"] ->
220 <:expr< Whenutils.Every_years 1000 >> ]
221 | [ e = period_parser -> e ]
224 (* Top level statements. *)
226 [ "when"; e = expr; ":"; sh = expr ->
227 (_loc, `When e, sh) ]
228 | [ "every"; p = periodexpr; ":"; sh = expr ->
229 (_loc, `Every p, sh) ]
232 (* "str_item" is a top level statement in an OCaml program. *)
233 str_item: LEVEL "top" [
234 [ s = statement -> call_stmt "" s ]
235 | [ "job"; name = STRING; s = statement -> call_stmt name s ]