1*2bc180efSBaptiste DaroussinLYAML 2*2bc180efSBaptiste Daroussin===== 3*2bc180efSBaptiste Daroussin 4*2bc180efSBaptiste DaroussinCopyright (C) 2013-2022 Gary V. Vaughan 5*2bc180efSBaptiste Daroussin 6*2bc180efSBaptiste Daroussin[](https://mit-license.org) 7*2bc180efSBaptiste Daroussin[](https://github.com/gvvaughan/lyaml/actions) 8*2bc180efSBaptiste Daroussin[](https://codecov.io/github/gvvaughan/lyaml?branch=release-v6.2.8) 9*2bc180efSBaptiste Daroussin 10*2bc180efSBaptiste Daroussin[LibYAML] binding for [Lua], with a fast C implementation 11*2bc180efSBaptiste Daroussinfor converting between [%YAML 1.1][yaml11] and [Lua] tables, 12*2bc180efSBaptiste Daroussinand a low-level [YAML] event parser for implementing more 13*2bc180efSBaptiste Daroussinintricate [YAML] document loading. 14*2bc180efSBaptiste Daroussin 15*2bc180efSBaptiste DaroussinUsage 16*2bc180efSBaptiste Daroussin----- 17*2bc180efSBaptiste Daroussin 18*2bc180efSBaptiste Daroussin### High Level API 19*2bc180efSBaptiste Daroussin 20*2bc180efSBaptiste DaroussinThese functions quickly convert back and forth between Lua tables 21*2bc180efSBaptiste Daroussinand [%YAML 1.1][yaml11] format strings. 22*2bc180efSBaptiste Daroussin 23*2bc180efSBaptiste Daroussin```lua 24*2bc180efSBaptiste Daroussinlocal lyaml = require "lyaml" 25*2bc180efSBaptiste Daroussinlocal t = lyaml.load (YAML-STRING, [OPTS-TABLE]) 26*2bc180efSBaptiste Daroussinlocal yamlstr = lyaml.dump (LUA-TABLE, [OPTS-TABLE]) 27*2bc180efSBaptiste Daroussinlocal null = lyaml.null () 28*2bc180efSBaptiste Daroussin``` 29*2bc180efSBaptiste Daroussin 30*2bc180efSBaptiste Daroussin#### `lyaml.load` 31*2bc180efSBaptiste Daroussin 32*2bc180efSBaptiste Daroussin`lyaml.load` accepts a YAML string for parsing. If the YAML string contains 33*2bc180efSBaptiste Daroussinmultiple documents, only the first document will be returned by default. To 34*2bc180efSBaptiste Daroussinreturn multiple documents as a table, set `all = true` in the second 35*2bc180efSBaptiste Daroussinargument OPTS-TABLE. 36*2bc180efSBaptiste Daroussin 37*2bc180efSBaptiste Daroussin```lua 38*2bc180efSBaptiste Daroussinlyaml.load("foo: bar") 39*2bc180efSBaptiste Daroussin--> { foo = "bar" } 40*2bc180efSBaptiste Daroussin 41*2bc180efSBaptiste Daroussinlyaml.load("foo: bar", { all = true }) 42*2bc180efSBaptiste Daroussin--> { { foo = "bar" } } 43*2bc180efSBaptiste Daroussin 44*2bc180efSBaptiste Daroussinmulti_doc_yaml = [[ 45*2bc180efSBaptiste Daroussin--- 46*2bc180efSBaptiste Daroussinone 47*2bc180efSBaptiste Daroussin... 48*2bc180efSBaptiste Daroussin--- 49*2bc180efSBaptiste Daroussintwo 50*2bc180efSBaptiste Daroussin... 51*2bc180efSBaptiste Daroussin]] 52*2bc180efSBaptiste Daroussin 53*2bc180efSBaptiste Daroussinlyaml.load(multi_doc_yaml) 54*2bc180efSBaptiste Daroussin--> "one" 55*2bc180efSBaptiste Daroussin 56*2bc180efSBaptiste Daroussinlyaml.load(multi_doc_yaml, { all = true }) 57*2bc180efSBaptiste Daroussin--> { "one", "two" } 58*2bc180efSBaptiste Daroussin``` 59*2bc180efSBaptiste Daroussin 60*2bc180efSBaptiste DaroussinYou can supply an alternative function for converting implicit plain 61*2bc180efSBaptiste Daroussinscalar values in the `implicit_scalar` field of the OPTS-TABLE argument; 62*2bc180efSBaptiste Daroussinotherwise a default is composed from the functions in the `lyaml.implicit` 63*2bc180efSBaptiste Daroussinmodule. 64*2bc180efSBaptiste Daroussin 65*2bc180efSBaptiste DaroussinYou can also supply an alternative table for coverting explicitly tagged 66*2bc180efSBaptiste Daroussinscalar values in the `explicit_scalar` field of the OPTS-TABLE argument; 67*2bc180efSBaptiste Daroussinotherwise all supported tags are parsed by default using the functions 68*2bc180efSBaptiste Daroussinfrom the `lyaml.explicit` module. 69*2bc180efSBaptiste Daroussin 70*2bc180efSBaptiste Daroussin#### `lyaml.dump` 71*2bc180efSBaptiste Daroussin 72*2bc180efSBaptiste Daroussin`lyaml.dump` accepts a table of values to dump. Each value in the table 73*2bc180efSBaptiste Daroussinrepresents a single YAML document. To dump a table of lua values this means 74*2bc180efSBaptiste Daroussinthe table must be wrapped in another table (the outer table represents the 75*2bc180efSBaptiste DaroussinYAML documents, the inner table is the single document table to dump). 76*2bc180efSBaptiste Daroussin 77*2bc180efSBaptiste Daroussin```lua 78*2bc180efSBaptiste Daroussinlyaml.dump({ { foo = "bar" } }) 79*2bc180efSBaptiste Daroussin--> --- 80*2bc180efSBaptiste Daroussin--> foo: bar 81*2bc180efSBaptiste Daroussin--> ... 82*2bc180efSBaptiste Daroussin 83*2bc180efSBaptiste Daroussinlyaml.dump({ "one", "two" }) 84*2bc180efSBaptiste Daroussin--> --- one 85*2bc180efSBaptiste Daroussin--> ... 86*2bc180efSBaptiste Daroussin--> --- two 87*2bc180efSBaptiste Daroussin--> ... 88*2bc180efSBaptiste Daroussin``` 89*2bc180efSBaptiste Daroussin 90*2bc180efSBaptiste DaroussinIf you need to round-trip load a dumped document, and you used a custom 91*2bc180efSBaptiste Daroussinfunction for converting implicit scalars, then you should pass that same 92*2bc180efSBaptiste Daroussinfunction in the `implicit_scalar` field of the OPTS-TABLE argument to 93*2bc180efSBaptiste Daroussin`lyaml.dump` so that it can quote strings that might otherwise be 94*2bc180efSBaptiste Daroussinimplicitly converted on reload. 95*2bc180efSBaptiste Daroussin 96*2bc180efSBaptiste Daroussin#### Nil Values 97*2bc180efSBaptiste Daroussin 98*2bc180efSBaptiste Daroussin[Lua] tables treat `nil` valued keys as if they were not there, 99*2bc180efSBaptiste Daroussinwhere [YAML] explicitly supports `null` values (and keys!). Lyaml 100*2bc180efSBaptiste Daroussinwill retain [YAML] `null` values as `lyaml.null ()` by default, 101*2bc180efSBaptiste Daroussinthough it is straight forward to wrap the low level APIs to use `nil`, 102*2bc180efSBaptiste Daroussinsubject to the usual caveats of how nil values work in [Lua] tables. 103*2bc180efSBaptiste Daroussin 104*2bc180efSBaptiste Daroussin 105*2bc180efSBaptiste Daroussin### Low Level APIs 106*2bc180efSBaptiste Daroussin 107*2bc180efSBaptiste Daroussin```lua 108*2bc180efSBaptiste Daroussinlocal emitter = require ("yaml").emitter () 109*2bc180efSBaptiste Daroussin 110*2bc180efSBaptiste Daroussinemitter.emit {type = "STREAM_START"} 111*2bc180efSBaptiste Daroussinfor _, event in ipairs (event_list) do 112*2bc180efSBaptiste Daroussin emitter.emit (event) 113*2bc180efSBaptiste Daroussinend 114*2bc180efSBaptiste Daroussinstr = emitter.emit {type = "STREAM_END"} 115*2bc180efSBaptiste Daroussin``` 116*2bc180efSBaptiste Daroussin 117*2bc180efSBaptiste DaroussinThe `yaml.emitter` function returns an emitter object that has a 118*2bc180efSBaptiste Daroussinsingle emit function, which you call with event tables, the last 119*2bc180efSBaptiste Daroussin`STREAM_END` event returns a string formatted as a [YAML 1.1][yaml11] 120*2bc180efSBaptiste Daroussindocument. 121*2bc180efSBaptiste Daroussin 122*2bc180efSBaptiste Daroussin```lua 123*2bc180efSBaptiste Daroussinlocal iter = require ("yaml").scanner (YAML-STRING) 124*2bc180efSBaptiste Daroussin 125*2bc180efSBaptiste Daroussinfor token_table in iter () do 126*2bc180efSBaptiste Daroussin -- process token table 127*2bc180efSBaptiste Daroussinend 128*2bc180efSBaptiste Daroussin``` 129*2bc180efSBaptiste Daroussin 130*2bc180efSBaptiste DaroussinEach time the iterator returned by `scanner` is called, it returns 131*2bc180efSBaptiste Daroussina table describing the next token of YAML-STRING. See LibYAML's 132*2bc180efSBaptiste Daroussin[yaml.h] for details of the contents and semantics of the various 133*2bc180efSBaptiste Daroussintokens produced by `yaml_parser_scan`, the underlying call made by 134*2bc180efSBaptiste Daroussinthe iterator. 135*2bc180efSBaptiste Daroussin 136*2bc180efSBaptiste Daroussin[LibYAML] implements a fast parser in C using `yaml_parser_scan`, which 137*2bc180efSBaptiste Daroussinis also bound to lyaml, and easier to use than the token API above: 138*2bc180efSBaptiste Daroussin 139*2bc180efSBaptiste Daroussin```lua 140*2bc180efSBaptiste Daroussinlocal iter = require ("yaml").parser (YAML-STRING) 141*2bc180efSBaptiste Daroussin 142*2bc180efSBaptiste Daroussinfor event_table in iter () do 143*2bc180efSBaptiste Daroussin -- process event table 144*2bc180efSBaptiste Daroussinend 145*2bc180efSBaptiste Daroussin``` 146*2bc180efSBaptiste Daroussin 147*2bc180efSBaptiste DaroussinEach time the iterator returned by `parser` is called, it returns 148*2bc180efSBaptiste Daroussina table describing the next event from the "Parse" process of the 149*2bc180efSBaptiste Daroussin"Parse, Compose, Construct" processing model described in the 150*2bc180efSBaptiste Daroussin[YAML 1.1][yaml11] specification using [LibYAML]. 151*2bc180efSBaptiste Daroussin 152*2bc180efSBaptiste DaroussinImplementing the remaining "Compose" and "Construct" processes in 153*2bc180efSBaptiste Daroussin[Lua] is left as an exercise for the reader -- though, unlike the 154*2bc180efSBaptiste Daroussinhigh-level API, `lyaml.parser` exposes all details of the input 155*2bc180efSBaptiste Daroussinstream events, such as line and column numbers. 156*2bc180efSBaptiste Daroussin 157*2bc180efSBaptiste Daroussin 158*2bc180efSBaptiste DaroussinInstallation 159*2bc180efSBaptiste Daroussin------------ 160*2bc180efSBaptiste Daroussin 161*2bc180efSBaptiste DaroussinThere's no need to download an [lyaml] release, or clone the git repo, 162*2bc180efSBaptiste Daroussinunless you want to modify the code. If you use [LuaRocks], you can 163*2bc180efSBaptiste Daroussinuse it to install the latest release from its repository: 164*2bc180efSBaptiste Daroussin 165*2bc180efSBaptiste Daroussin luarocks --server=http://rocks.moonscript.org install lyaml 166*2bc180efSBaptiste Daroussin 167*2bc180efSBaptiste DaroussinOr from the rockspec in a release tarball: 168*2bc180efSBaptiste Daroussin 169*2bc180efSBaptiste Daroussin luarocks make lyaml-?-1.rockspec 170*2bc180efSBaptiste Daroussin 171*2bc180efSBaptiste DaroussinTo install current git master from [GitHub][lyaml] (for testing): 172*2bc180efSBaptiste Daroussin 173*2bc180efSBaptiste Daroussin luarocks install http://raw.github.com/gvvaughan/lyaml/master/lyaml-git-1.rockspec 174*2bc180efSBaptiste Daroussin 175*2bc180efSBaptiste DaroussinTo install without [LuaRocks], clone the sources from the 176*2bc180efSBaptiste Daroussin[repository][lyaml], and then run the following commands: 177*2bc180efSBaptiste Daroussin 178*2bc180efSBaptiste Daroussin```sh 179*2bc180efSBaptiste Daroussincd lyaml 180*2bc180efSBaptiste Daroussinbuild-aux/luke LYAML_DIR=LIBYAML-INSTALL-PREFIX 181*2bc180efSBaptiste Daroussinsudo build-aux/luke PREFIX=LYAML-INSTALL-PREFIX install 182*2bc180efSBaptiste Daroussinspecl -v1freport spec/*_spec.yaml 183*2bc180efSBaptiste Daroussin``` 184*2bc180efSBaptiste Daroussin 185*2bc180efSBaptiste DaroussinThe dependencies are listed in the dependencies entry of the file 186*2bc180efSBaptiste Daroussin[rockspec][L15]. 187*2bc180efSBaptiste Daroussin 188*2bc180efSBaptiste Daroussin 189*2bc180efSBaptiste DaroussinBug reports and code contributions 190*2bc180efSBaptiste Daroussin---------------------------------- 191*2bc180efSBaptiste Daroussin 192*2bc180efSBaptiste DaroussinThis library is maintained by its users. 193*2bc180efSBaptiste Daroussin 194*2bc180efSBaptiste DaroussinPlease make bug reports and suggestions as [GitHub Issues][issues]. 195*2bc180efSBaptiste DaroussinPull requests are especially appreciated. 196*2bc180efSBaptiste Daroussin 197*2bc180efSBaptiste DaroussinBut first, please check that your issue has not already been reported by 198*2bc180efSBaptiste Daroussinsomeone else, and that it is not already fixed by [master][lyaml] in 199*2bc180efSBaptiste Daroussinpreparation for the next release (see Installation section above for how 200*2bc180efSBaptiste Daroussinto temporarily install master with [LuaRocks][]). 201*2bc180efSBaptiste Daroussin 202*2bc180efSBaptiste DaroussinThere is no strict coding style, but please bear in mind the following 203*2bc180efSBaptiste Daroussinpoints when proposing changes: 204*2bc180efSBaptiste Daroussin 205*2bc180efSBaptiste Daroussin0. Follow existing code. There are a lot of useful patterns and avoided 206*2bc180efSBaptiste Daroussin traps there. 207*2bc180efSBaptiste Daroussin 208*2bc180efSBaptiste Daroussin1. 3-character indentation using SPACES in Lua sources: It makes rogue 209*2bc180efSBaptiste Daroussin TABs easier to see, and lines up nicely with 'if' and 'end' keywords. 210*2bc180efSBaptiste Daroussin 211*2bc180efSBaptiste Daroussin2. Simple strings are easiest to type using single-quote delimiters, 212*2bc180efSBaptiste Daroussin saving double-quotes for where a string contains apostrophes. 213*2bc180efSBaptiste Daroussin 214*2bc180efSBaptiste Daroussin3. Save horizontal space by only using SPACEs where the parser requires 215*2bc180efSBaptiste Daroussin them. 216*2bc180efSBaptiste Daroussin 217*2bc180efSBaptiste Daroussin4. Use vertical space to separate out compound statements to help the 218*2bc180efSBaptiste Daroussin coverage reports discover untested lines. 219*2bc180efSBaptiste Daroussin 220*2bc180efSBaptiste Daroussin5. Prefer explicit string function calls over object methods, to mitigate 221*2bc180efSBaptiste Daroussin issues with monkey-patching in caller environment. 222*2bc180efSBaptiste Daroussin 223*2bc180efSBaptiste Daroussin 224*2bc180efSBaptiste Daroussin[issues]: http://github.com/gvvaughas/lyaml/issues 225*2bc180efSBaptiste Daroussin[libyaml]: http://pyyaml.org/wiki/LibYAML 226*2bc180efSBaptiste Daroussin[lua]: http://www.lua.org 227*2bc180efSBaptiste Daroussin[luarocks]: http://www.luarocks.org 228*2bc180efSBaptiste Daroussin[lyaml]: http://github.com/gvvaughan/lyaml 229*2bc180efSBaptiste Daroussin[L15]: http://github.com/gvvaughan/lyaml/blob/master/lyaml-git-1.rockspec#L15 230*2bc180efSBaptiste Daroussin[yaml.h]: http://pyyaml.org/browser/libyaml/branches/stable/include/yaml.h 231*2bc180efSBaptiste Daroussin[yaml]: http://yaml.org 232*2bc180efSBaptiste Daroussin[yaml11]: http://yaml.org/spec/1.1/ 233