1%-
2% Copyright (c) 2012-2014 Yakaz
3% Copyright (c) 2016-2018 Jean-Sébastien Pédron <jean-sebastien.pedron@dumbbell.fr>
4% All rights reserved.
5%
6% Redistribution and use in source and binary forms, with or without
7% modification, are permitted provided that the following conditions
8% are met:
9% 1. Redistributions of source code must retain the above copyright
10%    notice, this list of conditions and the following disclaimer.
11% 2. Redistributions in binary form must reproduce the above copyright
12%    notice, this list of conditions and the following disclaimer in the
13%    documentation and/or other materials provided with the distribution.
14%
15% THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18% ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19% FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21% OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22% HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25% SUCH DAMAGE.
26
27%% @private
28
29-module(yamerl_node_bool).
30
31-include("yamerl_tokens.hrl").
32-include("yamerl_nodes.hrl").
33-include("internal/yamerl_constr.hrl").
34
35%% Public API.
36-export([
37    tags/0,
38    try_construct_token/3,
39    construct_token/3,
40    node_pres/1
41  ]).
42
43-define(TAG, "tag:yaml.org,2002:bool").
44
45-define(IS_TRUE(S),
46  S == "true" orelse S == "True" orelse S == "TRUE").
47
48-define(IS_FALSE(S),
49  S == "false" orelse S == "False" orelse S == "FALSE").
50
51%% -------------------------------------------------------------------
52%% Public API.
53%% -------------------------------------------------------------------
54
55tags() -> [?TAG].
56
57try_construct_token(Constr, Node,
58  #yamerl_scalar{tag = #yamerl_tag{uri = {non_specific, "?"}},
59  text = Text} = Token) when ?IS_TRUE(Text) orelse ?IS_FALSE(Text) ->
60    construct_token(Constr, Node, Token);
61try_construct_token(_, _, _) ->
62    unrecognized.
63
64construct_token(#yamerl_constr{detailed_constr = false},
65  undefined, #yamerl_scalar{text = Text}) when ?IS_TRUE(Text) ->
66    {finished, true};
67construct_token(#yamerl_constr{detailed_constr = false},
68  undefined, #yamerl_scalar{text = Text}) when ?IS_FALSE(Text) ->
69    {finished, false};
70construct_token(#yamerl_constr{detailed_constr = true},
71  undefined, #yamerl_scalar{text = Text} = Token) when ?IS_TRUE(Text) ->
72    Pres = yamerl_constr:get_pres_details(Token),
73    Node = #yamerl_bool{
74      module = ?MODULE,
75      tag    = ?TAG,
76      pres   = Pres,
77      value  = true
78    },
79    {finished, Node};
80construct_token(#yamerl_constr{detailed_constr = true},
81  undefined, #yamerl_scalar{text = Text} = Token) when ?IS_FALSE(Text) ->
82    Pres = yamerl_constr:get_pres_details(Token),
83    Node = #yamerl_bool{
84      module = ?MODULE,
85      tag    = ?TAG,
86      pres   = Pres,
87      value  = false
88    },
89    {finished, Node}.
90
91node_pres(Node) ->
92    ?NODE_PRES(Node).
93