1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
-- Transform between YAML 1.1 streams and Lua table representations.
-- Written by Gary V. Vaughan, 2013
--
-- Copyright (c) 2013-2014 Gary V. Vaughan
--
-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
-- THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--
-- Portions of this software were inspired by an earlier LibYAML binding by
-- Andrew Danforth <acd@weirdness.net>
local yaml = require "yaml"
local TAG_PREFIX = "tag:yaml.org,2002:"
local null = setmetatable ({}, { _type = "LYAML null" })
local function isnull (x)
return (getmetatable (x) or {})._type == "LYAML null"
end
-- Metatable for Dumper objects.
local dumper_mt = {
__index = {
-- Emit EVENT to the LibYAML emitter.
emit = function (self, event)
return self.emitter.emit (event)
end,
-- Look up an anchor for a repeated document element.
get_anchor = function (self, value)
local r = self.anchors[value]
if r then
self.aliased[value], self.anchors[value] = self.anchors[value], nil
end
return r
end,
-- Look up an already anchored repeated document element.
get_alias = function (self, value)
return self.aliased[value]
end,
-- Dump ALIAS into the event stream.
dump_alias = function (self, alias)
return self:emit {
type = "ALIAS",
anchor = alias,
}
end,
-- Dump MAP into the event stream.
dump_mapping = function (self, map)
local alias = self:get_alias (map)
if alias then
return self:dump_alias (alias)
end
self:emit {
type = "MAPPING_START",
anchor = self:get_anchor (map),
style = "BLOCK",
}
for k, v in pairs (map) do
self:dump_node (k)
self:dump_node (v)
end
return self:emit {type = "MAPPING_END"}
end,
-- Dump SEQUENCE into the event stream.
dump_sequence = function (self, sequence)
local alias = self:get_alias (sequence)
if alias then
return self:dump_alias (alias)
end
self:emit {
type = "SEQUENCE_START",
anchor = self:get_anchor (sequence),
style = "BLOCK",
}
for _, v in ipairs (sequence) do
self:dump_node (v)
end
return self:emit {type = "SEQUENCE_END"}
end,
-- Dump a null into the event stream.
dump_null = function (self)
return self:emit {
type = "SCALAR",
value = "~",
plain_implicit = true,
quoted_implicit = true,
style = "PLAIN",
}
end,
-- Dump VALUE into the event stream.
dump_scalar = function (self, value)
local alias = self:get_alias (value)
if alias then
return self:dump_alias (alias)
end
local anchor = self:get_anchor (value)
local itsa = type (value)
local style = "PLAIN"
if value == "true" or value == "false" or
value == "yes" or value == "no" or value == "~" or
(type (value) ~= "number" and tonumber (value) ~= nil) then
style = "SINGLE_QUOTED"
elseif itsa == "number" or itsa == "boolean" then
value = tostring (value)
end
return self:emit {
type = "SCALAR",
anchor = anchor,
value = value,
plain_implicit = true,
quoted_implicit = true,
style = style,
}
end,
-- Decompose NODE into a stream of events.
dump_node = function (self, node)
local itsa = type (node)
if isnull (node) then
return self:dump_null ()
elseif itsa == "string" or itsa == "boolean" or itsa == "number" then
return self:dump_scalar (node)
elseif itsa == "table" then
if #node > 0 then
return self:dump_sequence (node)
else
return self:dump_mapping (node)
end
else -- unsupported Lua type
error ("cannot dump object of type '" .. itsa .. "'", 2)
end
end,
-- Dump DOCUMENT into the event stream.
dump_document = function (self, document)
self:emit {type = "DOCUMENT_START"}
self:dump_node (document)
return self:emit {type = "DOCUMENT_END"}
end,
},
}
-- Emitter object constructor.
local function Dumper (anchors)
local t = {}
for k, v in pairs (anchors or {}) do t[v] = k end
local object = {
anchors = t,
aliased = {},
emitter = yaml.emitter (),
}
return setmetatable (object, dumper_mt)
end
local function dump (documents, anchors)
local dumper = Dumper (anchors)
dumper:emit { type = "STREAM_START", encoding = "UTF8" }
for _, document in ipairs (documents) do
dumper:dump_document (document)
end
local ok, stream = dumper:emit { type = "STREAM_END" }
return stream
end
-- Metatable for Parser objects.
local parser_mt = {
__index = {
-- Return the type of the current event.
type = function (self)
return tostring (self.event.type)
end,
-- Raise a parse error.
error = function (self, errmsg)
error (self.mark.line .. ":" .. self.mark.column ..
": " .. errmsg, 0)
end,
-- Save node in the anchor table for reference in future ALIASes.
add_anchor = function (self, node)
if self.event.anchor ~= nil then
self.anchors[self.event.anchor] = node
end
end,
-- Fetch the next event.
parse = function (self)
local ok, event = pcall (self.next)
if not ok then
-- if ok is nil, then event is a parser error from libYAML
self:error (event:gsub (" at document: .*$", ""))
end
self.event = event
self.mark = {
line = tostring (self.event.start_mark.line + 1),
column = tostring (self.event.start_mark.column + 1),
}
return self:type ()
end,
-- Construct a Lua hash table from following events.
load_map = function (self)
local map = {}
self:add_anchor (map)
while true do
local key = self:load_node ()
if key == nil then break end
local value, event = self:load_node ()
if value == nil then
self:error ("unexpected " .. self:type () .. "event")
end
map[key] = value
end
return map
end,
-- Construct a Lua array table from following events.
load_sequence = function (self)
local sequence = {}
self:add_anchor (sequence)
while true do
local node = self:load_node ()
if node == nil then break end
sequence[#sequence + 1] = node
end
return sequence
end,
-- Construct a primitive type from the current event.
load_scalar = function (self)
local value = self.event.value
local tag = self.event.tag
if tag then
tag = tag:match ("^" .. TAG_PREFIX .. "(.*)$")
if tag == "str" then
-- value is already a string
elseif tag == "int" or tag == "float" then
value = tonumber (value)
elseif tag == "bool" then
value = (value == "true" or value == "yes")
end
elseif self.event.style == "PLAIN" then
if value == "~" then
value = null
elseif value == "true" or value == "yes" then
value = true
elseif value == "false" or value == "no" then
value = false
else
local number = tonumber (value)
if number then value = number end
end
end
self:add_anchor (value)
return value
end,
load_alias = function (self)
local anchor = self.event.anchor
if self.anchors[anchor] == nil then
self:error ("invalid reference: " .. tostring (anchor))
end
return self.anchors[anchor]
end,
load_node = function (self)
local dispatch = {
SCALAR = self.load_scalar,
ALIAS = self.load_alias,
MAPPING_START = self.load_map,
SEQUENCE_START = self.load_sequence,
MAPPING_END = function () end,
SEQUENCE_END = function () end,
DOCUMENT_END = function () end,
}
local event = self:parse ()
if dispatch[event] == nil then
self:error ("invalid event: " .. self:type ())
end
return dispatch[event] (self)
end,
},
}
-- Parser object constructor.
local function Parser (s)
local object = {
anchors = {},
mark = { line = "0", column = "0" },
next = yaml.parser (s),
}
return setmetatable (object, parser_mt)
end
local function load (s, all)
local documents = {}
local parser = Parser (s)
if parser:parse () ~= "STREAM_START" then
error ("expecting STREAM_START event, but got " .. parser:type (), 2)
end
while parser:parse () ~= "STREAM_END" do
local document = parser:load_node ()
if document == nil then
error ("unexpected " .. parser:type () .. " event")
end
if parser:parse () ~= "DOCUMENT_END" then
error ("expecting DOCUMENT_END event, but got " .. parser:type (), 2)
end
-- save document
documents[#documents + 1] = document
-- reset anchor table
parser.anchors = {}
end
return all and documents or documents[1]
end
--[[ ----------------- ]]--
--[[ Public Interface. ]]--
--[[ ----------------- ]]--
local M = {
dump = dump,
load = load,
null = null,
_VERSION = yaml.version,
}
return M
|