xref: /freebsd/contrib/lyaml/lib/lyaml/explicit.lua (revision 2bc180ef045e5911cce0cea1c2a139cffd2b577a)
1-- LYAML parse explicit token values.
2-- Written by Gary V. Vaughan, 2015
3--
4-- Copyright(C) 2015-2022 Gary V. Vaughan
5--
6-- Permission is hereby granted, free of charge, to any person obtaining
7-- a copy of this software and associated documentation files(the
8-- "Software"), to deal in the Software without restriction, including
9-- without limitation the rights to use, copy, modify, merge, publish,
10-- distribute, sublicense, and/or sell copies of the Software, and to
11-- permit persons to whom the Software is furnished to do so, subject to
12-- the following conditions:
13--
14-- The above copyright notice and this permission notice shall be
15-- included in all copies or substantial portions of the Software.
16--
17-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25--- @module lyaml.explicit
26
27local functional = require 'lyaml.functional'
28local implicit = require 'lyaml.implicit'
29
30local NULL = functional.NULL
31local anyof = functional.anyof
32local id = functional.id
33
34
35local yn = {y=true, Y=true, n=false, N=false}
36
37
38--- Parse the value following an explicit `!!bool` tag.
39-- @function bool
40-- @param value token
41-- @treturn[1] bool boolean equivalent, if a valid value was recognized
42-- @treturn[2] nil otherwise, nil
43-- @usage maybe_bool = explicit.bool(tagarg)
44local bool = anyof {
45   implicit.bool,
46   function(x) return yn[x] end,
47}
48
49
50--- Return a function that converts integer results to equivalent float.
51-- @tparam function fn token parsing function
52-- @treturn function new function that converts int results to float
53-- @usage maybe_float = maybefloat(implicit.decimal)(tagarg)
54local function maybefloat(fn)
55   return function(...)
56      local r = fn(...)
57      if type(r) == 'number' then
58         return r + 0.0
59      end
60   end
61end
62
63
64--- Parse the value following an explicit `!!float` tag.
65-- @function float
66-- @param value token
67-- @treturn[1] number float equivalent, if a valid value was recognized
68-- @treturn[2] nil otherwise, nil
69-- @usage maybe_float = explicit.float(tagarg)
70local float = anyof {
71   implicit.float,
72   implicit.nan,
73   implicit.inf,
74   maybefloat(implicit.octal),
75   maybefloat(implicit.decimal),
76   maybefloat(implicit.hexadecimal),
77   maybefloat(implicit.binary),
78   implicit.sexfloat,
79}
80
81
82--- Parse the value following an explicit `!!int` tag.
83-- @function int
84-- @param value token
85-- @treturn[1] int integer equivalent, if a valid value was recognized
86-- @treturn[2] nil otherwise, nil
87-- @usage maybe_int = explicit.int(tagarg)
88local int = anyof {
89   implicit.octal,
90   implicit.decimal,
91   implicit.hexadecimal,
92   implicit.binary,
93   implicit.sexagesimal,
94}
95
96
97--- Parse an explicit `!!null` tag.
98-- @treturn lyaml.null
99-- @usage null = explicit.null(tagarg)
100local function null()
101   return NULL
102end
103
104
105--- Parse the value following an explicit `!!str` tag.
106-- @function str
107-- @tparam string value token
108-- @treturn string *value* which was a string already
109-- @usage tagarg = explicit.str(tagarg)
110local str = id
111
112
113--- @export
114return {
115   bool = bool,
116   float = float,
117   int = int,
118   null = null,
119   str = str,
120}
121