]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
77d37419 | 3 | ** Copyright (C) 1994-2002 University of Virginia, |
616915dd | 4 | ** Massachusetts Institute of Technology |
5 | ** | |
6 | ** This program is free software; you can redistribute it and/or modify it | |
7 | ** under the terms of the GNU General Public License as published by the | |
8 | ** Free Software Foundation; either version 2 of the License, or (at your | |
9 | ** option) any later version. | |
10 | ** | |
11 | ** This program is distributed in the hope that it will be useful, but | |
12 | ** WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | ** General Public License for more details. | |
15 | ** | |
16 | ** The GNU General Public License is available from http://www.gnu.org/ or | |
17 | ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
18 | ** MA 02111-1307, USA. | |
19 | ** | |
155af98d | 20 | ** For information on splint: info@splint.org |
21 | ** To report a bug: splint-bug@splint.org | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 23 | */ |
24 | /* | |
25 | ** ltokenList.c | |
26 | ** | |
27 | ** based on list_template.c | |
28 | ** | |
29 | ** where T has T_equal (or change this) and T_unparse | |
30 | */ | |
31 | ||
1b8ae690 | 32 | # include "splintMacros.nf" |
616915dd | 33 | # include "llbasic.h" |
34 | ||
35 | /*@notnull@*/ /*@only@*/ ltokenList | |
36 | ltokenList_new () | |
37 | { | |
38 | ltokenList s = (ltokenList) dmalloc (sizeof (*s)); | |
39 | ||
40 | s->nelements = 0; | |
41 | s->nspace = ltokenListBASESIZE; | |
42 | s->elements = (ltoken *) | |
43 | dmalloc (sizeof (*s->elements) * ltokenListBASESIZE); | |
44 | s->current = 0; | |
45 | ||
46 | return (s); | |
47 | } | |
48 | ||
49 | /*@notnull@*/ /*@only@*/ ltokenList | |
50 | ltokenList_singleton (ltoken l) | |
51 | { | |
52 | ltokenList s = (ltokenList) dmalloc (sizeof (*s)); | |
53 | ||
54 | s->nelements = 1; | |
55 | s->nspace = ltokenListBASESIZE - 1; | |
56 | s->elements = (ltoken *) dmalloc (sizeof (*s->elements) * ltokenListBASESIZE); | |
57 | s->elements[0] = l; | |
58 | s->current = 0; | |
59 | ||
60 | return (s); | |
61 | } | |
62 | ||
63 | static void | |
64 | ltokenList_grow (/*@notnull@*/ ltokenList s) | |
65 | { | |
66 | int i; | |
67 | ltoken *newelements; | |
68 | ||
69 | s->nspace += ltokenListBASESIZE; | |
70 | ||
71 | newelements = (ltoken *) dmalloc (sizeof (*newelements) | |
72 | * (s->nelements + s->nspace)); | |
73 | ||
74 | for (i = 0; i < s->nelements; i++) | |
75 | { | |
76 | newelements[i] = s->elements[i]; | |
77 | } | |
78 | ||
79 | sfree (s->elements); | |
80 | s->elements = newelements; | |
81 | } | |
82 | ||
83 | ltokenList | |
84 | ltokenList_push (/*@returned@*/ ltokenList s, ltoken el) | |
85 | { | |
86 | ltokenList_addh (s, el); | |
87 | return s; | |
88 | } | |
89 | ||
90 | void | |
91 | ltokenList_addh (ltokenList s, ltoken el) | |
92 | { | |
93 | llassert (ltokenList_isDefined (s)); | |
94 | ||
95 | if (s->nspace <= 0) | |
96 | ltokenList_grow (s); | |
97 | ||
98 | s->nspace--; | |
99 | s->elements[s->nelements] = el; | |
100 | s->nelements++; | |
101 | } | |
102 | ||
103 | void | |
104 | ltokenList_reset (ltokenList s) | |
105 | { | |
106 | if (ltokenList_isDefined (s)) | |
107 | { | |
108 | s->current = 0; | |
109 | } | |
110 | } | |
111 | ||
112 | bool | |
113 | ltokenList_isFinished (ltokenList s) | |
114 | { | |
115 | return (ltokenList_isUndefined(s) || (s->current == s->nelements)); | |
116 | } | |
117 | ||
118 | void | |
119 | ltokenList_advance (ltokenList s) | |
120 | { | |
121 | if (ltokenList_isDefined (s)) | |
122 | { | |
123 | s->current++; | |
124 | llassert (s->current <= s->nelements); | |
125 | } | |
126 | } | |
127 | ||
128 | ltoken | |
129 | ltokenList_head (ltokenList s) | |
130 | { | |
131 | llassert (ltokenList_isDefined (s) && s->nelements > 0); | |
132 | return (s->elements[0]); | |
133 | } | |
134 | ||
135 | bool | |
136 | ltokenList_equal (ltokenList s1, ltokenList s2) | |
137 | { | |
138 | if (ltokenList_isUndefined (s1)) | |
139 | { | |
140 | return (ltokenList_isEmpty (s2)); | |
141 | } | |
142 | else | |
143 | { | |
144 | if (ltokenList_isUndefined (s2)) | |
145 | { | |
146 | return ltokenList_isEmpty (s1); | |
147 | } | |
148 | else | |
149 | { | |
150 | int i; | |
151 | int size = s1->nelements; | |
152 | ||
153 | if (s2->nelements != size) | |
154 | return FALSE; | |
155 | ||
156 | for (i = 0; i < size; i++) | |
157 | { | |
158 | if (!ltoken_similar (s1->elements[i], s2->elements[i])) | |
159 | return FALSE; | |
160 | } | |
161 | return TRUE; | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | /*@only@*/ ltokenList | |
167 | ltokenList_copy (ltokenList s) | |
168 | { | |
169 | ltokenList r = ltokenList_new (); | |
170 | ||
171 | ltokenList_elements (s, x) | |
172 | { | |
173 | ltokenList_addh (r, ltoken_copy (x)); | |
174 | } end_ltokenList_elements; | |
175 | ||
176 | return r; | |
177 | } | |
178 | ||
179 | void | |
180 | ltokenList_removeCurrent (ltokenList s) | |
181 | { | |
182 | int i; | |
183 | llassert (ltokenList_isDefined (s) && s->current >= 0 && s->current < s->nelements); | |
184 | ||
185 | for (i = s->current; i < s->nelements - 1; i++) | |
186 | { | |
187 | s->elements[i] = s->elements[i+1]; | |
188 | } | |
189 | ||
190 | s->nelements--; | |
191 | s->nspace++; | |
192 | } | |
193 | ||
194 | ltoken | |
195 | ltokenList_current (ltokenList s) | |
196 | { | |
197 | llassert (ltokenList_isDefined (s) && s->current >= 0 && s->current < s->nelements); | |
198 | return (s->elements[s->current]); | |
199 | } | |
200 | ||
201 | /*@only@*/ cstring | |
202 | ltokenList_unparse (ltokenList s) | |
203 | { | |
204 | int i; | |
205 | cstring st = cstring_undefined; | |
206 | ||
207 | if (ltokenList_isDefined (s)) | |
208 | { | |
209 | for (i = 0; i < s->nelements; i++) | |
210 | { | |
211 | if (i == 0) | |
212 | { | |
213 | st = cstring_copy (ltoken_unparse (s->elements[i])); | |
214 | } | |
215 | else | |
216 | st = message ("%q, %s", st, ltoken_unparse (s->elements[i])); | |
217 | } | |
218 | } | |
219 | ||
220 | return st; | |
221 | } | |
222 | ||
223 | void | |
224 | ltokenList_free (ltokenList s) | |
225 | { | |
226 | if (ltokenList_isDefined (s)) | |
227 | { | |
228 | sfree (s->elements); | |
229 | sfree (s); | |
230 | } | |
231 | } |