Imported Upstream version 0.10.0
[platform/upstream/augeas.git] / lenses / util.aug
1 (*
2 Module: Util
3   Generic module providing useful primitives
4
5 Author: David Lutterkort
6
7 About: License
8   This file is licensed under the LGPLv2+, like the rest of Augeas.
9 *)
10
11
12 module Util =
13
14
15 (*
16 Variable: del_str
17   Delete a string and default to it
18
19   Parameters:
20      s:string - the string to delete and default to
21 *)
22   let del_str (s:string) = del s s
23
24 (*
25 Variable: del_ws
26   Delete mandatory whitespace
27 *)
28   let del_ws = del /[ \t]+/
29
30 (*
31 Variable: del_ws_spc
32   Delete mandatory whitespace, default to single space
33 *)
34   let del_ws_spc = del_ws " "
35
36 (*
37 Variable: del_ws_tab
38   Delete mandatory whitespace, default to single tab
39 *)
40   let del_ws_tab = del_ws "\t"
41
42
43 (*
44 Variable: del_opt_ws
45   Delete optional whitespace
46 *)
47   let del_opt_ws = del /[ \t]*/
48
49
50 (*
51 Variable: eol
52   Delete end of line, including optional trailing whitespace
53 *)
54   let eol = del /[ \t]*\n/ "\n"
55
56 (*
57 Variable: indent
58   Delete indentation, including leading whitespace
59 *)
60   let indent = del /[ \t]*/ ""
61
62 (* Group: Comment
63      This is a general definition of comment
64      It allows indentation for comments, removes the leading and trailing spaces
65      of comments and stores them in nodes, except for empty comments which are
66      ignored together with empty lines
67
68 View: comment_generic
69   Map comments and set default comment sign
70 *)
71
72   let comment_generic (r:regexp) (d:string) =
73     [ label "#comment" . del r d
74         . store /([^ \t\n].*[^ \t\n]|[^ \t\n])/ . eol ]
75
76 (* View: comment
77   Map comments into "#comment" nodes
78 *)
79   let comment = comment_generic /[ \t]*#[ \t]*/ "# "
80
81 (* View: comment_eol
82   Map eol comments into "#comment" nodes
83   Add a space before # for end of line comments
84 *)
85   let comment_eol = comment_generic /[ \t]*#[ \t]*/ " # "
86
87 (* View: comment_or_eol
88     A <comment_eol> or <eol>, with an optional empty comment *)
89  let comment_or_eol = comment_eol | (del /[ \t]*(#[ \t]*)?\n/ "\n")
90
91 (* View: comment_multiline
92     A C-style multiline comment *)
93   let comment_multiline =
94      let mline_re = (/[^ \t\n].*[^ \t\n]|[^ \t\n]/ - /.*\*\/.*/) in
95      let mline = [ seq "mline"
96                  . del /[ \t\n]*/ "\n"
97                  . store mline_re ] in
98      [ label "#mcomment" . del /[ \t]*\/\*/ "/*"
99        . counter "mline"
100        . mline . (eol . mline)*
101        . del /[ \t\n]*\*\/[ \t]*\n/ "\n*/\n" ]
102
103 (* View: comment_c_style
104     A comment line, C-style *)
105   let comment_c_style =
106     comment_generic /[ \t]*\/\/[ \t]*/ "// "
107
108 (* View: empty_generic
109   A generic definition of <empty>
110   Map empty lines, including empty comments *)
111   let empty_generic (r:regexp) =
112     [ del r "" . del_str "\n" ]
113
114 (* View: empty
115   Map empty lines, including empty comments *)
116   let empty = empty_generic /[ \t]*#?[ \t]*/
117
118 (* View: empty_c_style
119   Map empty lines, including C-style empty comment *)
120   let empty_c_style =
121     empty_generic /[ \t]*((\/\/)|(\/\*[ \t]*\*\/))?[ \t]*/
122
123
124 (* View: Split *)
125 (* Split (SEP . ELT)* into an array-like tree where each match for ELT *)
126 (* appears in a separate subtree. The labels for the subtrees are      *)
127 (* consecutive numbers, starting at 0                                  *)
128   let split (elt:lens) (sep:lens) =
129     let sym = gensym "split" in
130     counter sym . ( [ seq sym . sep . elt ] ) *
131
132 (* Group: Exclusions
133
134 Variable: stdexcl
135   Exclusion for files that are commonly not wanted/needed
136 *)
137   let stdexcl = (excl "*~") .
138     (excl "*.rpmnew") .
139     (excl "*.rpmsave") .
140     (excl "*.dpkg-old") .
141     (excl "*.dpkg-new") .
142     (excl "*.dpkg-bak") .
143     (excl "*.dpkg-dist") .
144     (excl "*.augsave") .
145     (excl "*.augnew")