IniFile

Generic module to create INI files lenses

Author: Raphael Pinson rap.nosp@m.hink@gmai.nosp@m.l.com

Summary
IniFileGeneric module to create INI files lenses
LicenseThis file is licensed under the GPL.
TODOThings to add in the future
Lens usageThis lens is made to provide generic primitives to construct INI File lenses.
USEFUL PRIMITIVES
Internal primitives
eolEnd of line, inherited from Util.eol
emptyEmpty line, an eol subnode
Separators
sepGeneric separator
sep_reThe default regexp for a separator
sep_defaultThe default separator value
Stores
sto_to_eolStore until end of line
sto_to_commentStore until comment
Define comment and defaults
commentMap comments into “#comment” nodes
comment_reDefault regexp for comment pattern
comment_defaultDefault value for comment pattern
ENTRY
entry includes comments
entryGeneric INI File entry
indented_entryGeneric INI File entry that might be indented with an arbitrary amount of whitespace
entry_reDefault regexp for entry keyword
RECORD
Title definition
titleTitle for record.
indented_titleTitle for record.
title_labelTitle for record.
record_reDefault regexp for title keyword pattern
record_label_reDefault regexp for title_label keyword pattern
Record definition
record_noemptyINI File Record with no empty lines allowed.
recordGeneric INI File record
LENS
Lens definition
lns_noemptyGeneric INI File lens with no empty lines
lnsGeneric INI File lens

License

This file is licensed under the GPL.

TODO

Things to add in the future

  • Support double quotes in value
  • Support multiline values (is it standard?)

Lens usage

This lens is made to provide generic primitives to construct INI File lenses.  See <Puppet>, <PHP>, <MySQL> or <Dput> for examples of real life lenses using it.

USEFUL PRIMITIVES

Internal primitives

eol

let eol = Util.eol

End of line, inherited from Util.eol

empty

let empty = [ eol ]

Empty line, an eol subnode

Separators

sep

let sep (pat:regexp) (default:string) = Util.del_opt_ws "" . del pat default

Generic separator

Parameters

pat:regexpthe pattern to delete
default:stringthe default string to use

sep_re

let sep_re = /[=:]/

The default regexp for a separator

sep_default

let sep_default = "="

The default separator value

Stores

sto_to_eol

let sto_to_eol = Util.del_opt_ws "" . store /([^ \t\n].*[^ \t\n]|[^ \t\n])/

Store until end of line

sto_to_comment

let sto_to_comment = Util.del_opt_ws "" . store /[^;# \t\n][^;#\n]*[^;# \t\n]|[^;# \t\n]/

Store until comment

Define comment and defaults

comment

let comment (
   pat:regexp
) (default:string) = [ label "#comment" . sep pat default . sto_to_eol? . eol ]

Map comments into “#comment” nodes

Parameters

pat:regexppattern to delete before commented data
default:stringdefault pattern before commented data

Sample Usage

let comment  = IniFile.comment "#" "#"
let comment  = IniFile.comment IniFile.comment_re IniFile.comment_default

comment_re

let comment_re = /[;#]/

Default regexp for comment pattern

comment_default

let comment_default = ";"

Default value for comment pattern

ENTRY

entry includes comments

entry

let entry (
   kw:regexp
) (sep:lens) (comment:lens) = [ key kw . sep . sto_to_comment? . (comment|eol) ] | comment

Generic INI File entry

Parameters

kw:regexpkeyword regexp for the label
sep:lenslens to use as key/value separator
comment:lenslens to use as comment

Sample Usage

let entry = IniFile.entry setting sep comment

indented_entry

let indented_entry (
   kw:regexp
) (sep:lens) (comment:lens) = [ Util.del_opt_ws "" . key kw . sep . sto_to_comment? . (comment|eol) ] | comment

Generic INI File entry that might be indented with an arbitrary amount of whitespace

Parameters

kw:regexpkeyword regexp for the label
sep:lenslens to use as key/value separator
comment:lenslens to use as comment

Sample Usage

let entry = IniFile.indented_entry setting sep comment

entry_re

let entry_re = (/[A-Za-z][A-Za-z0-9\._-]+/)

Default regexp for entry keyword

RECORD

Title definition

title

let title (kw:regexp) = Util.del_str "[" . key kw . Util.del_str "]". eol

Title for record.  This maps the title of a record as a node in the abstract tree.

Parameters

kw:regexpkeyword regexp for the label

Sample Usage

let title   = IniFile.title IniFile.record_re

indented_title

let indented_title (
   kw:regexp
) = Util.del_opt_ws "" . Util.del_str "[" . key kw . Util.del_str "]". eol

Title for record.  This maps the title of a record as a node in the abstract tree.  The title may be indented with arbitrary amounts of whitespace

Parameters

kw:regexpkeyword regexp for the label

Sample Usage

let title   = IniFile.title IniFile.record_re

title_label

let title_label (
   name:string
) (kw:regexp) = label name . Util.del_str "[" . store kw . Util.del_str "]". eol

Title for record.  This maps the title of a record as a value in the abstract tree.

Parameters

name:stringname for the title label
kw:regexpkeyword regexp for the label

Sample Usage

let title   = IniFile.title_label "target" IniFile.record_label_re

record_re

let record_re = (/[^]\n\/]+/ /#comment/)

Default regexp for title keyword pattern

record_label_re

let record_label_re = /[^]\n]+/

Default regexp for title_label keyword pattern

Record definition

record_noempty

let record_noempty (title:lens) (entry:lens) = [ title . entry* ]

INI File Record with no empty lines allowed.

Parameters

title:lenslens to use for title.  Use either title or title_label.
entry:lenslens to use for entries in the record.  See entry.

record

let record (title:lens) (entry:lens) = record_noempty title ( entry | empty )

Generic INI File record

Parameters

title:lenslens to use for title.  Use either title or title_label.
entry:lenslens to use for entries in the record.  See entry.

Sample Usage

let record  = IniFile.record title entry

LENS

Lens definition

lns_noempty

let lns_noempty (record:lens) (comment:lens) = comment* . record*

Generic INI File lens with no empty lines

Parameters

record:lensrecord lens to use.  See record_noempty.
comment:lenscomment lens to use.  See comment.

Sample Usage

let lns     = IniFile.lns_noempty record comment

lns

let lns (record:lens) (comment:lens) = lns_noempty record (comment|empty)

Generic INI File lens

Parameters

record:lensrecord lens to use.  See record.
comment:lenscomment lens to use.  See comment.

Sample Usage

let lns     = IniFile.lns record comment
let eol = Util.eol
End of line, inherited from Util.eol
let eol = del /[ \t]*\n/ "\n"
Delete end of line, including optional trailing whitespace
let empty = [ eol ]
Empty line, an eol subnode
let sep (pat:regexp) (default:string) = Util.del_opt_ws "" . del pat default
Generic separator
let sep_re = /[=:]/
The default regexp for a separator
let sep_default = "="
The default separator value
let sto_to_eol = Util.del_opt_ws "" . store /([^ \t\n].*[^ \t\n]|[^ \t\n])/
Store until end of line
let sto_to_comment = Util.del_opt_ws "" . store /[^;# \t\n][^;#\n]*[^;# \t\n]|[^;# \t\n]/
Store until comment
let comment (
   pat:regexp
) (default:string) = [ label "#comment" . sep pat default . sto_to_eol? . eol ]
Map comments into “#comment” nodes
let comment_re = /[;#]/
Default regexp for comment pattern
let comment_default = ";"
Default value for comment pattern
let entry (
   kw:regexp
) (sep:lens) (comment:lens) = [ key kw . sep . sto_to_comment? . (comment|eol) ] | comment
Generic INI File entry
let indented_entry (
   kw:regexp
) (sep:lens) (comment:lens) = [ Util.del_opt_ws "" . key kw . sep . sto_to_comment? . (comment|eol) ] | comment
Generic INI File entry that might be indented with an arbitrary amount of whitespace
let entry_re = (/[A-Za-z][A-Za-z0-9\._-]+/)
Default regexp for entry keyword
let title (kw:regexp) = Util.del_str "[" . key kw . Util.del_str "]". eol
Title for record.
let record (title:lens) (entry:lens) = record_noempty title ( entry | empty )
Generic INI File record
let indented_title (
   kw:regexp
) = Util.del_opt_ws "" . Util.del_str "[" . key kw . Util.del_str "]". eol
Title for record.
let title_label (
   name:string
) (kw:regexp) = label name . Util.del_str "[" . store kw . Util.del_str "]". eol
Title for record.
let record_re = (/[^]\n\/]+/ /#comment/)
Default regexp for title keyword pattern
let record_label_re = /[^]\n]+/
Default regexp for title_label keyword pattern
let record_noempty (title:lens) (entry:lens) = [ title . entry* ]
INI File Record with no empty lines allowed.
let lns_noempty (record:lens) (comment:lens) = comment* . record*
Generic INI File lens with no empty lines
let lns (record:lens) (comment:lens) = lns_noempty record (comment|empty)
Generic INI File lens
Close