2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <ecoli_tk_seq.h>
37 static struct ec_parsed_tk *parse(const struct ec_tk *tk,
38 const char *str, size_t off)
40 struct ec_tk_seq *seq = (struct ec_tk_seq *)tk;
41 struct ec_parsed_tk *parsed_tk, *child_parsed_tk;
45 parsed_tk = ec_parsed_tk_new(tk);
46 if (parsed_tk == NULL)
49 for (i = 0; i < seq->len; i++) {
50 child_parsed_tk = ec_tk_parse(seq->table[i], str, off + len);
51 if (child_parsed_tk == NULL)
54 len += strlen(child_parsed_tk->str);
55 ec_parsed_tk_add_child(parsed_tk, child_parsed_tk);
58 parsed_tk->str = strndup(str + off, len);
63 ec_parsed_tk_free(parsed_tk);
67 static struct ec_tk_ops seq_ops = {
71 struct ec_tk *ec_tk_seq_new(const char *id)
73 struct ec_tk_seq *tk = NULL;
75 tk = (struct ec_tk_seq *)ec_tk_new(id, &seq_ops, sizeof(*tk));
89 struct ec_tk *ec_tk_seq_new_list(const char *id, ...)
91 struct ec_tk_seq *tk = NULL;
97 tk = (struct ec_tk_seq *)ec_tk_seq_new(id);
101 for (child = va_arg(ap, struct ec_tk *);
102 child != EC_TK_ENDLIST;
103 child = va_arg(ap, struct ec_tk *)) {
107 ec_tk_seq_add(&tk->gen, child);
114 free(tk); // XXX use tk_free? we need to delete all child on error
119 int ec_tk_seq_add(struct ec_tk *tk, struct ec_tk *child)
121 struct ec_tk_seq *seq = (struct ec_tk_seq *)tk;
122 struct ec_tk **table;
125 assert(child != NULL);
127 table = realloc(seq->table, seq->len + 1);
132 table[seq->len] = child;