parser: consolidate tokens
[m6w6/ext-psi] / src / types / impl_def_val.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include <assert.h>
30
31 struct psi_impl_def_val *psi_impl_def_val_init(token_t t, const char *text)
32 {
33 struct psi_impl_def_val *def = calloc(1, sizeof(*def));
34
35 def->type = t;
36 def->text = text ? strdup(text) : NULL;
37
38 return def;
39 }
40
41 void psi_impl_def_val_free(struct psi_impl_def_val **def_ptr)
42 {
43 if (*def_ptr) {
44 struct psi_impl_def_val *def = *def_ptr;
45
46 *def_ptr = NULL;
47 if (def->token) {
48 free(def->token);
49 }
50 switch (def->type) {
51 case PSI_T_STRING:
52 case PSI_T_QUOTED_STRING:
53 if (def->ival.zend.str) {
54 zend_string_release(def->ival.zend.str);
55 }
56 break;
57 default:
58 break;
59 }
60 if (def->text) {
61 free(def->text);
62 }
63 free(def);
64 }
65 }
66
67 bool psi_impl_def_val_validate(struct psi_data *data,
68 struct psi_impl_def_val *def, token_t type_t, const char *type_name)
69 {
70 if (def->type != PSI_T_NULL && def->text) {
71 switch (type_t) {
72 case PSI_T_BOOL:
73 def->ival.zend.bval = def->type == PSI_T_TRUE ? 1 : 0;
74 break;
75 case PSI_T_INT:
76 def->ival.zend.lval = zend_atol(def->text, strlen(def->text));
77 break;
78 case PSI_T_FLOAT:
79 case PSI_T_DOUBLE:
80 def->ival.dval = zend_strtod(def->text, NULL);
81 break;
82 case PSI_T_STRING:
83 case PSI_T_QUOTED_STRING:
84 def->ival.zend.str = zend_string_init(def->text, strlen(def->text), 1);
85 break;
86 default:
87 data->error(data, def->token, PSI_WARNING,
88 "Invalid default value type '%s', expected one of bool, int, double, string.",
89 type_name);
90 return false;
91 }
92 }
93 return true;
94 }
95
96 void psi_impl_def_val_dump(int fd, struct psi_impl_def_val *val) {
97 switch (val->type) {
98 case PSI_T_BOOL:
99 dprintf(fd, "%s", val->ival.zend.bval ? "true" : "false");
100 break;
101 case PSI_T_INT:
102 dprintf(fd, "%ld", val->ival.zend.lval);
103 break;
104 case PSI_T_FLOAT:
105 case PSI_T_DOUBLE:
106 dprintf(fd, "%f", val->ival.dval);
107 break;
108 case PSI_T_STRING:
109 dprintf(fd, "\"%s\"", val->ival.zend.str->val);
110 break;
111 case PSI_T_QUOTED_STRING:
112 dprintf(fd, "\"%s\"", val->text);
113 break;
114 default:
115 if (val->text) {
116 dprintf(fd, "%s", val->text);
117 } else {
118 assert(0);
119 }
120 break;
121 }
122 }