[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] More generic constructions in util.aug



Hi all,

As shared with David on IRC, I think we should go even more towards sharing generic constructions in lenses to make writing lenses easier, and adopt generic ways of implementing common patterns.

Here are some propositions already, taken from monit.aug and exports.aug and how these two lenses can be simplified by using the newly defined types :


diff --git a/lenses/util.aug b/lenses/util.aug
index 0dced14..40a9ec3 100644
--- a/lenses/util.aug
+++ b/lenses/util.aug
 @@ -97,6 +97,27 @@ Variable: stdexcl
     (excl "*.augsave") .
     (excl "*.augnew")
 
+
+(* Lists constructions *)
+  let list (lns:lens) (sep:lens) = lns . ( sep . lns ) *
+
+(* Stores *)
+  let sto_to_spc = store /([^ \t\n].*[^ \t\n]|[^ \t\n])/
+  let sto_no_spc = store /[^ ^\t\n]+/
+
+(* Labels *)
+  let xchg (m:regexp) (d:string) (l:string) = del m d . label l
+  let key_value_line (kw: regexp) (sep:lens) (sto:lens) =
+                                   [ key kw . sep . sto . eol ]
+  let key_value (kw: regexp) (sep:lens) (sto:lens) =
+                                   [ key kw . sep . sto ]
+
+  let brackets (l:lens) (r:lens) (lns:lens) = l . lns . r
+
+(* Regexps *)
+  let word = /[A-Za-z0-9_.-]+/
+
+
 (* Local Variables: *)
 (* mode: caml       *)
 (* End:             *)


diff --git a/lenses/exports.aug b/lenses/exports.aug
index 499a3af..d3103e1 100644
--- a/lenses/exports.aug
+++ b/lenses/exports.aug
@@ -78,19 +78,20 @@ module Exports =
 
   let client_re = /[a-zA-Z0-9\-\ \*\?\/]+/
 
-  let eol = del /[ \t]*\n/ "\n"
+  let eol       = Util.eol
+  let lbracket  = Util.del_str "("
+  let rbracket  = Util.del_str ")"
+  let sep_com   = Util.del_str ","
+  let sep_spc   = Util.del_ws_spc
  
 let option = [ label "option" . store /[^,)]+/ ]
 
-  let client = [ label "client" . store client_re .
-                    ( Util.del_str "(" .
-                        option .
-                        ( Util.del_str "," . option ) * .
-                      Util.del_str ")" )? ]
+  let client    = [ label "client" . store client_re .
+                    ( Util.brackets lbracket rbracket
+                         ( Util.list option sep_com ) )? ]
 
-  let entry = [ label "dir" . store /\/[^ \t]*/ .
-                Util.del_ws_spc .
-                client . (Util.del_ws_spc . client)* . eol ]
+  let entry = [ label "dir" . store /\/[^ \t]*/
+                . sep_spc . Util.list client sep_spc . eol ]
 
   let lns = (Hosts.empty | Hosts.comment | entry)*
 
diff --git a/lenses/monit.aug b/lenses/monit.aug
index bd4e63e..24b69ae 100644
--- a/lenses/monit.aug
+++ b/lenses/monit.aug
@@ -38,24 +38,24 @@ let spc        = Util.del_ws_spc
 let comment    = Util.comment
 let empty      = Util.empty
 
-let sto_to_eol = store /([^ \t\n].*[^ \t\n]|[^ \t\n])/
-let sto_to_spc = store /[^ \t\n]+/
+let sto_to_eol = Util.sto_to_spc
+let sto_no_spc = Util.sto_no_spc
 
-let word       = /[A-Za-z0-9_.-]+/
-let value      = [ key word . spc . sto_to_eol . eol ]
+let word       = Util.word
+let value      = Util.key_value_line word spc sto_to_eol
 
 (************************************************************************
  *                               ENTRIES
  *************************************************************************)
 
 (* set statement *)
-let set        = [ key "set" . spc . value  ]
+let set        = Util.key_value "set" spc value
 
 (* include statement *)
-let include    = [ key "include" . spc . sto_to_eol . eol ]
+let include    = Util.key_value_line "include" spc sto_to_eol
 
 (* service statement *)
-let service    = [ key "check" . spc . value . (spc . value)+ ]
+let service    = Util.key_value_line "check" spc (Util.list value spc)
 
 let entry      = (set|include|service)
 



Any ideas/comments are welcome :)


Raphaël

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]