- Move setting _U_ into config.h, because
[obnox/wireshark/wip.git] / epan / dtd_grammar.lemon
1 %include {
2
3 /* dtd_parser.lemon
4 * XML dissector for wireshark 
5 * XML's DTD grammar
6 *
7 * Copyright 2005, Luis E. Garcia Ontanon <luis@ontanon.org>
8 *
9 * $Id$
10 *
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdio.h>
35 #include <glib.h>
36 #include <assert.h>
37 #include "dtd.h"
38 #include "dtd_parse.h"
39
40 static dtd_named_list_t* dtd_named_list_new(gchar* name, GPtrArray* list) {
41         dtd_named_list_t* nl = g_malloc(sizeof(dtd_named_list_t));
42
43         nl->name = name;
44         nl->list = list;
45         
46         return nl;
47 }
48
49 static GPtrArray* g_ptr_array_join(GPtrArray* a, GPtrArray* b){
50         
51         while(b->len > 0) {
52                 g_ptr_array_add(a,g_ptr_array_remove_index_fast(b,0));
53         }
54         
55         g_ptr_array_free(b,TRUE);
56
57         return a;
58 }
59
60 }
61
62 %name DtdParse
63
64 %extra_argument { dtd_build_data_t *bd }
65
66 %token_destructor { 
67         if ($$) {
68                 if ($$->text) g_free($$->text);
69                 if ($$->location) g_free($$->location);
70                 g_free($$);
71         }
72 }
73
74 %syntax_error {
75         if (!TOKEN)
76                 g_string_append_printf(bd->error,"syntax error at end of file");
77         else 
78                 g_string_append_printf(bd->error,"syntax error in %s at or before '%s': \n", TOKEN->location,TOKEN->text);
79 }
80
81 %parse_failure {
82         g_string_append_printf(bd->error,"DTD parsing failure\n");
83 }
84
85 %token_prefix TOKEN_
86
87 %token_type { dtd_token_data_t* }
88
89 dtd ::= doctype.
90 dtd ::= dtd_parts.
91
92 doctype ::= TAG_START DOCTYPE_KW NAME(Name) OPEN_BRACKET dtd_parts CLOSE_BRACKET TAG_STOP. {
93     dtd_named_list_t* root;
94     GPtrArray* root_elems = g_ptr_array_new();
95     guint i;
96     gchar *name;
97
98     if(! bd->proto_name) {
99         bd->proto_name = Name->text;
100     }
101
102     if(bd->proto_root)
103         g_free(bd->proto_root);
104
105         bd->proto_root = Name->text;
106     
107     name = g_ascii_strdown(bd->proto_name, -1);
108     g_free(bd->proto_name);
109         bd->proto_name = name;
110     
111     for( i = 0; i< bd->elements->len; i++) {
112         dtd_named_list_t* el = g_ptr_array_index(bd->elements,i);
113         
114         g_ptr_array_add(root_elems,g_strdup(el->name));
115     }
116     
117     root = dtd_named_list_new(g_strdup(Name->text),root_elems);
118     
119     g_ptr_array_add(bd->elements,root);
120     
121     g_free(Name->location);
122     g_free(Name);
123
124 }
125
126 dtd_parts ::= dtd_parts element(Element). { g_ptr_array_add(bd->elements,Element); }
127 dtd_parts ::= dtd_parts attlist(Attlist). { g_ptr_array_add(bd->attributes,Attlist); }
128 dtd_parts ::= element(Element). { g_ptr_array_add(bd->elements,Element); }
129 dtd_parts ::= attlist(Attlist). { g_ptr_array_add(bd->attributes,Attlist); }
130
131 %type   attlist                         { dtd_named_list_t* }
132 attlist(A) ::= TAG_START ATTLIST_KW NAME(B) attrib_list(TheList) TAG_STOP. {
133     A = dtd_named_list_new(g_ascii_strdown(B->text, -1),TheList);
134     g_free(B->text);
135     g_free(B->location);
136     g_free(B);
137 }
138
139 %type element { dtd_named_list_t* }
140 element(A) ::= TAG_START ELEMENT_KW NAME(B) sub_elements(C) TAG_STOP. {
141     A = dtd_named_list_new(g_ascii_strdown(B->text, -1),C);
142     g_free(B->text);
143     g_free(B->location);
144     g_free(B);
145 }
146
147 %type   attrib_list                     { GPtrArray* }
148 attrib_list(A) ::= attrib_list(B) attrib(C). { g_ptr_array_add(B,C); A = B; }
149 attrib_list(A) ::= attrib(B).  { A = g_ptr_array_new(); g_ptr_array_add(A,B);  }
150
151 %type   attrib                          { gchar* }
152 attrib(A) ::= NAME(B) att_type att_default. {
153         A = g_ascii_strdown(B->text, -1);
154     g_free(B->text);
155     g_free(B->location);
156     g_free(B);
157 }
158
159 att_type ::= ATT_TYPE.
160 att_type ::= enumeration.
161
162 att_default ::= ATT_DEF.
163 att_default ::= ATT_DEF_WITH_VALUE QUOTED. 
164 att_default ::= QUOTED.
165 att_default ::= IMPLIED_KW.
166 att_default ::= REQUIRED_KW.
167
168 enumeration ::= OPEN_PARENS enum_list CLOSE_PARENS.
169
170 enum_list ::= enum_list PIPE enum_item.
171 enum_list ::= enum_item.
172 enum_list ::= enumeration.
173 enum_list ::= enum_list PIPE enumeration.
174
175 enum_item ::= NAME.
176 enum_item ::= QUOTED.
177
178
179 %type   sub_elements            { GPtrArray* }
180 sub_elements(A) ::= sub_elements(B) STAR. {A=B;}
181 sub_elements(A) ::= sub_elements(B) PLUS. {A=B;}
182 sub_elements(A) ::= sub_elements(B) QUESTION. {A=B;}
183 sub_elements(A) ::= OPEN_PARENS ELEM_DATA CLOSE_PARENS. { A = g_ptr_array_new(); }
184 sub_elements(A) ::= OPEN_PARENS element_list(B) COMMA ELEM_DATA CLOSE_PARENS.   { A = B; }
185 sub_elements(A) ::= OPEN_PARENS element_list(B) PIPE ELEM_DATA CLOSE_PARENS.    { A = B; }
186 sub_elements(A) ::= OPEN_PARENS element_list(B) CLOSE_PARENS. { A = B; }
187 sub_elements(A) ::= EMPTY_KW. { A = g_ptr_array_new(); }
188
189 %type   element_list    { GPtrArray* }
190 element_list(A) ::= element_list(B) COMMA element_child(C).     { g_ptr_array_add(B,C); A = B; }
191 element_list(A) ::= element_list(B) PIPE element_child(C).      { g_ptr_array_add(B,C); A = B; }
192 element_list(A) ::= element_child(B).                                           { A = g_ptr_array_new(); g_ptr_array_add(A,B); }
193 element_list(A) ::= sub_elements(B).                                            { A = B; }
194 element_list(A) ::= element_list(B) COMMA sub_elements(C).   { A = g_ptr_array_join(B,C); }
195 element_list(A) ::= element_list(B) PIPE sub_elements(C).   { A = g_ptr_array_join(B,C); }
196
197 %type   element_child           { gchar* }
198 element_child(A) ::= NAME(B).                   {
199         A = g_ascii_strdown(B->text, -1);
200         g_free(B->text);
201     g_free(B->location);
202     g_free(B);
203 }
204
205 element_child(A) ::= NAME(B) STAR.              {
206         A = g_ascii_strdown(B->text, -1);
207         g_free(B->text);
208     g_free(B->location);
209     g_free(B);
210 }
211
212 element_child(A) ::= NAME(B) QUESTION.  {
213         A = g_ascii_strdown(B->text, -1);
214         g_free(B->text);
215     g_free(B->location);
216     g_free(B);
217 }
218
219 element_child(A) ::= NAME(B) PLUS.              {
220         A = g_ascii_strdown(B->text, -1);
221         g_free(B->text);
222     g_free(B->location);
223     g_free(B);
224 }
225