Imported Upstream version 1.7.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: doseol
58   Delete end of line with optional carriage return,
59   including optional trailing whitespace
60 *)
61   let doseol = del /[ \t]*\r?\n/ "\n"
62    
63
64 (*
65 Variable: indent
66   Delete indentation, including leading whitespace
67 *)
68   let indent = del /[ \t]*/ ""
69
70 (* Group: Comment
71      This is a general definition of comment
72      It allows indentation for comments, removes the leading and trailing spaces
73      of comments and stores them in nodes, except for empty comments which are
74      ignored together with empty lines
75 *)
76
77
78 (* View: comment_generic_seteol
79   Map comments and set default comment sign
80 *)
81
82   let comment_generic_seteol (r:regexp) (d:string) (eol:lens) =
83     [ label "#comment" . del r d
84         . store /([^ \t\r\n].*[^ \t\r\n]|[^ \t\r\n])/ . eol ]
85
86 (* View: comment_generic
87   Map comments and set default comment sign
88 *)
89
90   let comment_generic (r:regexp) (d:string) =
91     comment_generic_seteol r d doseol
92
93 (* View: comment
94   Map comments into "#comment" nodes
95 *)
96   let comment = comment_generic /[ \t]*#[ \t]*/ "# "
97
98 (* View: comment_noindent
99   Map comments into "#comment" nodes, without indentation
100 *)
101   let comment_noindent = comment_generic /#[ \t]*/ "# "
102
103 (* View: comment_eol
104   Map eol comments into "#comment" nodes
105   Add a space before # for end of line comments
106 *)
107   let comment_eol = comment_generic /[ \t]*#[ \t]*/ " # "
108
109 (* View: comment_or_eol
110     A <comment_eol> or <eol>, with an optional empty comment *)
111  let comment_or_eol = comment_eol | (del /[ \t]*(#[ \t]*)?\n/ "\n")
112
113 (* View: comment_multiline
114     A C-style multiline comment *)
115   let comment_multiline =
116      let mline_re = (/[^ \t\r\n].*[^ \t\r\n]|[^ \t\r\n]/ - /.*\*\/.*/) in
117      let mline = [ seq "mline"
118                  . del /[ \t\r\n]*/ "\n"
119                  . store mline_re ] in
120      [ label "#mcomment" . del /[ \t]*\/\*/ "/*"
121        . counter "mline"
122        . mline . (eol . mline)*
123        . del /[ \t\r\n]*\*\/[ \t]*\r?\n/ "\n*/\n" ]
124
125 (* View: comment_c_style
126     A comment line, C-style *)
127   let comment_c_style =
128     comment_generic /[ \t]*\/\/[ \t]*/ "// "
129
130 (* View: comment_c_style_or_hash
131     A comment line, C-style or hash *)
132   let comment_c_style_or_hash =
133     comment_generic /[ \t]*((\/\/)|#)[ \t]*/ "// "
134
135 (* View: empty_generic
136   A generic definition of <empty>
137   Map empty lines, including empty comments *)
138   let empty_generic (r:regexp) =
139     [ del r "" . del_str "\n" ]
140
141 (* Variable: empty_generic_re *)
142   let empty_generic_re = /[ \t]*#?[ \t]*/
143
144 (* View: empty
145   Map empty lines, including empty comments *)
146   let empty = empty_generic empty_generic_re
147
148 (* Variable: empty_c_style_re *)
149   let empty_c_style_re = /[ \t]*((\/\/)|(\/\*[ \t]*\*\/))?[ \t]*/
150
151 (* View: empty_c_style
152   Map empty lines, including C-style empty comment *)
153   let empty_c_style = empty_generic empty_c_style_re
154
155 (* View: empty_any
156   Either <empty> or <empty_c_style> *)
157   let empty_any = empty_generic (empty_generic_re | empty_c_style_re)
158
159 (* View: empty_generic_dos
160   A generic definition of <empty> with dos newlines
161   Map empty lines, including empty comments *)
162   let empty_generic_dos (r:regexp) =
163     [ del r "" . del /\r?\n/ "\n" ]
164
165 (* View: empty_dos *)
166   let empty_dos =
167     empty_generic_dos /[ \t]*#?[ \t]*/
168
169
170 (* View: Split *)
171 (* Split (SEP . ELT)* into an array-like tree where each match for ELT *)
172 (* appears in a separate subtree. The labels for the subtrees are      *)
173 (* consecutive numbers, starting at 0                                  *)
174   let split (elt:lens) (sep:lens) =
175     let sym = gensym "split" in
176     counter sym . ( [ seq sym . sep . elt ] ) *
177
178 (* View: delim *)
179   let delim (op:string) = del (/[ \t]*/ . op . /[ \t]*/)
180                               (" " . op . " ")
181
182 (* Group: Exclusions
183
184 Variable: stdexcl
185   Exclusion for files that are commonly not wanted/needed
186 *)
187   let stdexcl = (excl "*~") .
188     (excl "*.rpmnew") .
189     (excl "*.rpmsave") .
190     (excl "*.dpkg-old") .
191     (excl "*.dpkg-new") .
192     (excl "*.dpkg-bak") .
193     (excl "*.dpkg-dist") .
194     (excl "*.augsave") .
195     (excl "*.augnew") .
196     (excl "*.bak") .
197     (excl "*.old") .
198     (excl "#*#")