/usr/share/lua/5.1/lyaml/implicit.lua is in lua-yaml 6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | -- LYAML parse implicit type tokens.
-- Written by Gary V. Vaughan, 2015
--
-- Copyright (C) 2015-2016 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.
--- @module lyaml.implicit
local NULL = require "lyaml.functional".NULL
local is_null = {
[""] = true, ["~"] = true, null = true, Null = true, NULL = true,
}
--- Parse a null token to a null value.
-- @param value token
-- @return[1] lyaml.null, for an empty string or literal ~
-- @return[2] nil otherwise, nil
-- @usage maybe_null = implicit.null (token)
local function null (value)
if is_null[value] then
return NULL
end
end
local to_bool = {
["true"] = true, True = true, TRUE = true,
["false"] = false, False = false, FALSE = false,
yes = true, Yes = true, YES = true,
no = false, No = false, NO = false,
on = true, On = true, ON = true,
off = false, Off = false, OFF = false,
}
--- Parse a boolean token to the equivalent value.
-- Treats capilalized, lower and upper-cased variants of true/false,
-- yes/no or on/off tokens as boolean `true` and `false` values.
-- @param value token
-- @treturn[1] bool if a valid boolean token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_bool = implicit.bool (token)
local function bool (value)
return to_bool[value]
end
--- Parse a binary token, such as "0b1010\_0111\_0100\_1010\_1110".
-- @tparam string value token
-- @treturn[1] int integer equivalent, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_int = implicit.binary (value)
local function binary (value)
local r
value:gsub ("^([+-]?)0b_*([01][01_]+)$", function (sign, rest)
r = 0
rest:gsub ("_*(.)", function (digit)
r = r * 2 + tonumber (digit)
end)
if sign == "-" then r = r * -1 end
end)
return r
end
--- Parse an octal token, such as "012345".
-- @tparam string value token
-- @treturn[1] int integer equivalent, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_int = implicit.octal (value)
local function octal (value)
local r
value:gsub ("^([+-]?)0_*([0-7][0-7_]*)$", function (sign, rest)
r = 0
rest:gsub ("_*(.)", function (digit)
r = r * 8 + tonumber (digit)
end)
if sign == "-" then r = r * -1 end
end)
return r
end
--- Parse a decimal token, such as "0" or "12345".
-- @tparam string value token
-- @treturn[1] int integer equivalent, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_int = implicit.decimal (value)
local function decimal (value)
local r
value:gsub ("^([+-]?)_*([0-9][0-9_]*)$", function (sign, rest)
rest = rest:gsub ("_", "")
if rest == "0" or #rest > 1 or rest:sub (1, 1) ~= "0" then
r = tonumber (rest)
if sign == "-" then r = r * -1 end
end
end)
return r
end
--- Parse a hexadecimal token, such as "0xdeadbeef".
-- @tparam string value token
-- @treturn[1] int integer equivalent, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_int = implicit.hexadecimal (value)
local function hexadecimal (value)
local r
value:gsub ("^([+-]?)(0x_*[0-9a-fA-F][0-9a-fA-F_]*)$",
function (sign, rest)
rest = rest:gsub ("_", "")
r = tonumber (rest)
if sign == "-" then r = r * -1 end
end
)
return r
end
--- Parse a sexagesimal token, such as "190:20:30".
-- Useful for times and angles.
-- @tparam string value token
-- @treturn[1] int integer equivalent, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_int = implicit.sexagesimal (value)
local function sexagesimal (value)
local r
value:gsub ("^([+-]?)([0-9]+:[0-5]?[0-9][:0-9]*)$", function (sign, rest)
r = 0
rest:gsub ("([0-9]+):?", function (digit)
r = r * 60 + tonumber (digit)
end)
if sign == "-" then r = r * -1 end
end)
return r
end
local isnan = {
[".nan"] = true, [".NaN"] = true, [".NAN"] = true,
}
--- Parse a `nan` token.
-- @tparam string value token
-- @treturn[1] nan not-a-number, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_nan = implicit.nan (value)
local function nan (value)
if isnan[value] then return 0/0 end
end
local isinf = {
[".inf"] = math.huge, [".Inf"] = math.huge, [".INF"] = math.huge,
["+.inf"] = math.huge, ["+.Inf"] = math.huge, ["+.INF"] = math.huge,
["-.inf"] = -math.huge, ["-.Inf"] = -math.huge, ["-.INF"] = -math.huge,
}
--- Parse a signed `inf` token.
-- @tparam string value token
-- @treturn[1] number plus/minus-infinity, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_inf = implicit.inf (value)
local function inf (value)
return isinf[value]
end
--- Parse a floating point number token, such as "1e-3" or "-0.12".
-- @tparam string value token
-- @treturn[1] number float equivalent, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_float = implicit.float (value)
local function float (value)
local r = tonumber ((value:gsub ("_", "")))
if r and value:find "[%.eE]" then return r end
end
--- Parse a sexagesimal float, such as "190:20:30.15".
-- Useful for times and angles.
-- @tparam string value token
-- @treturn[1] number float equivalent, if a valid token was recognized
-- @treturn[2] nil otherwise, nil
-- @usage maybe_float = implicit.sexfloat (value)
local function sexfloat (value)
local r
value:gsub ("^([+-]?)([0-9]+:[0-5]?[0-9][:0-9]*)(%.[0-9]+)$",
function (sign, rest, float)
r = 0
rest:gsub ("([0-9]+):?", function (digit)
r = r * 60 + tonumber (digit)
end)
r = r + tonumber (float)
if sign == "-" then r = r * -1 end
end
)
return r
end
--- @export
return {
binary = binary,
decimal = decimal,
float = float,
hexadecimal = hexadecimal,
inf = inf,
nan = nan,
null = null,
octal = octal,
sexagesimal = sexagesimal,
sexfloat = sexfloat,
bool = bool,
}
|