3 This lua module allows to parse objects from strings and to store data into
4 ucl objects. It uses `libucl` C library to parse and manipulate with ucl objects.
9 local ucl = require("ucl")
11 local parser = ucl.parser()
12 local res,err = parser:parse_string('{key=value}')
15 print('parser error: ' .. err)
17 local obj = parser:get_object()
18 local got = ucl.to_format(obj, 'json')
30 print(ucl.to_format(table, 'ucl'))
44 > [`ucl_object_push_lua(L, obj, allow_array)`](#function-ucl_object_push_lual-obj-allow_array)
46 > [`ucl.to_format(var, format)`](#function-uclto_formatvar-format)
52 > [`parser:parse_file(name)`](#method-parserparse_filename)
54 > [`parser:parse_string(input)`](#method-parserparse_stringinput)
56 > [`parser:get_object()`](#method-parserget_object)
61 The module `ucl` defines the following functions.
63 ### Function `ucl_object_push_lua(L, obj, allow_array)`
65 This is a `C` function to push `UCL` object as lua variable. This function
66 converts `obj` to lua representation using the following conversions:
68 - *scalar* values are directly presented by lua objects
69 - *userdata* values are converted to lua function objects using `LUA_REGISTRYINDEX`,
70 this can be used to pass functions from lua to c and vice-versa
71 - *arrays* are converted to lua tables with numeric indicies suitable for `ipairs` iterations
72 - *objects* are converted to lua tables with string indicies
76 - `L {lua_State}`: lua state pointer
77 - `obj {ucl_object_t}`: object to push
78 - `allow_array {bool}`: expand implicit arrays (should be true for all but partial arrays)
82 - `{int}`: `1` if an object is pushed to lua
84 Back to [module description](#module-ucl).
86 ### Function `ucl.to_format(var, format)`
88 Converts lua variable `var` to the specified `format`. Formats supported are:
90 - `json` - fine printed json
91 - `json-compact` - compacted json
92 - `config` - fine printed configuration
93 - `ucl` - same as `config`
94 - `yaml` - embedded yaml
96 If `var` contains function, they are called during output formatting and if
97 they return string value, then this value is used for ouptut.
101 - `var {variant}`: any sort of lua variable (if userdata then metafield `__to_ucl` is searched for output)
102 - `format {string}`: any available format
106 - `{string}`: string representation of `var` in the specific `format`.
120 print(ucl.to_format(table, 'ucl'))
130 Back to [module description](#module-ucl).
135 The module `ucl` defines the following methods.
137 ### Method `parser:parse_file(name)`
139 Parse UCL object from file.
143 - `name {string}`: filename to parse
147 - `{bool[, string]}`: if res is `true` then file has been parsed successfully, otherwise an error string is also returned
152 local parser = ucl.parser()
153 local res,err = parser:parse_file('/some/file.conf')
156 print('parser error: ' .. err)
158 -- Do something with object
162 Back to [module description](#module-ucl).
164 ### Method `parser:parse_string(input)`
166 Parse UCL object from file.
170 - `input {string}`: string to parse
174 - `{bool[, string]}`: if res is `true` then file has been parsed successfully, otherwise an error string is also returned
176 Back to [module description](#module-ucl).
178 ### Method `parser:get_object()`
180 Get top object from parser and export it to lua representation.
188 - `{variant or nil}`: ucl object as lua native variable
190 Back to [module description](#module-ucl).