From Michal Labedzki via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9333 :
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 */
29
30 #include "config.h"
31
32 #include <stdio.h>
33 #include <glib.h>
34 #include <assert.h>
35 #include "dtd.h"
36 #include "dtd_parse.h"
37
38 static dtd_named_list_t* dtd_named_list_new(gchar* name, GPtrArray* list) {
39         dtd_named_list_t* nl = g_new(dtd_named_list_t,1);
40
41         nl->name = name;
42         nl->list = list;
43         
44         return nl;
45 }
46
47 static GPtrArray* g_ptr_array_join(GPtrArray* a, GPtrArray* b){
48         
49         while(b->len > 0) {
50                 g_ptr_array_add(a,g_ptr_array_remove_index_fast(b,0));
51         }
52         
53         g_ptr_array_free(b,TRUE);
54
55         return a;
56 }
57
58 }
59
60 %name DtdParse
61
62 %extra_argument { dtd_build_data_t *bd }
63
64 %token_destructor { 
65         if ($$) {
66                 if ($$->text) g_free($$->text);
67                 if ($$->location) g_free($$->location);
68                 g_free($$);
69         }
70 }
71
72 %syntax_error {
73         if (!TOKEN)
74                 g_string_append_printf(bd->error,"syntax error at end of file");
75         else 
76                 g_string_append_printf(bd->error,"syntax error in %s at or before '%s': \n", TOKEN->location,TOKEN->text);
77 }
78
79 %parse_failure {
80         g_string_append_printf(bd->error,"DTD parsing failure\n");
81 }
82
83 %token_prefix TOKEN_
84
85 %token_type { dtd_token_data_t* }
86
87 dtd ::= doctype.
88 dtd ::= dtd_parts.
89
90 doctype ::= TAG_START DOCTYPE_KW NAME(Name) OPEN_BRACKET dtd_parts CLOSE_BRACKET TAG_STOP. {
91     dtd_named_list_t* root;
92     GPtrArray* root_elems = g_ptr_array_new();
93     guint i;
94     gchar *name;
95
96     if(! bd->proto_name) {
97         bd->proto_name = Name->text;
98     }
99
100     if(bd->proto_root)
101         g_free(bd->proto_root);
102
103         bd->proto_root = Name->text;
104     
105     name = g_ascii_strdown(bd->proto_name, -1);
106     g_free(bd->proto_name);
107         bd->proto_name = name;
108     
109     for( i = 0; i< bd->elements->len; i++) {
110         dtd_named_list_t* el = (dtd_named_list_t*)g_ptr_array_index(bd->elements,i);
111         
112         g_ptr_array_add(root_elems,g_strdup(el->name));
113     }
114     
115     root = dtd_named_list_new(g_strdup(Name->text),root_elems);
116     
117     g_ptr_array_add(bd->elements,root);
118     
119     g_free(Name->location);
120     g_free(Name);
121
122 }
123
124 dtd_parts ::= dtd_parts element(Element). { g_ptr_array_add(bd->elements,Element); }
125 dtd_parts ::= dtd_parts attlist(Attlist). { g_ptr_array_add(bd->attributes,Attlist); }
126 dtd_parts ::= element(Element). { g_ptr_array_add(bd->elements,Element); }
127 dtd_parts ::= attlist(Attlist). { g_ptr_array_add(bd->attributes,Attlist); }
128
129 %type   attlist                         { dtd_named_list_t* }
130 attlist(A) ::= TAG_START ATTLIST_KW NAME(B) attrib_list(TheList) TAG_STOP. {
131     A = dtd_named_list_new(g_ascii_strdown(B->text, -1),TheList);
132     g_free(B->text);
133     g_free(B->location);
134     g_free(B);
135 }
136
137 %type element { dtd_named_list_t* }
138 element(A) ::= TAG_START ELEMENT_KW NAME(B) sub_elements(C) TAG_STOP. {
139     A = dtd_named_list_new(g_ascii_strdown(B->text, -1),C);
140     g_free(B->text);
141     g_free(B->location);
142     g_free(B);
143 }
144
145 %type   attrib_list                     { GPtrArray* }
146 attrib_list(A) ::= attrib_list(B) attrib(C). { g_ptr_array_add(B,C); A = B; }
147 attrib_list(A) ::= attrib(B).  { A = g_ptr_array_new(); g_ptr_array_add(A,B);  }
148
149 %type   attrib                          { gchar* }
150 attrib(A) ::= NAME(B) att_type att_default. {
151         A = g_ascii_strdown(B->text, -1);
152     g_free(B->text);
153     g_free(B->location);
154     g_free(B);
155 }
156
157 att_type ::= ATT_TYPE.
158 att_type ::= enumeration.
159
160 att_default ::= ATT_DEF.
161 att_default ::= ATT_DEF_WITH_VALUE QUOTED. 
162 att_default ::= QUOTED.
163 att_default ::= IMPLIED_KW.
164 att_default ::= REQUIRED_KW.
165
166 enumeration ::= OPEN_PARENS enum_list CLOSE_PARENS.
167
168 enum_list ::= enum_list PIPE enum_item.
169 enum_list ::= enum_item.
170 enum_list ::= enumeration.
171 enum_list ::= enum_list PIPE enumeration.
172
173 enum_item ::= NAME.
174 enum_item ::= QUOTED.
175
176
177 %type   sub_elements            { GPtrArray* }
178 sub_elements(A) ::= sub_elements(B) STAR. {A=B;}
179 sub_elements(A) ::= sub_elements(B) PLUS. {A=B;}
180 sub_elements(A) ::= sub_elements(B) QUESTION. {A=B;}
181 sub_elements(A) ::= OPEN_PARENS ELEM_DATA CLOSE_PARENS. { A = g_ptr_array_new(); }
182 sub_elements(A) ::= OPEN_PARENS element_list(B) COMMA ELEM_DATA CLOSE_PARENS.   { A = B; }
183 sub_elements(A) ::= OPEN_PARENS element_list(B) PIPE ELEM_DATA CLOSE_PARENS.    { A = B; }
184 sub_elements(A) ::= OPEN_PARENS element_list(B) CLOSE_PARENS. { A = B; }
185 sub_elements(A) ::= EMPTY_KW. { A = g_ptr_array_new(); }
186
187 %type   element_list    { GPtrArray* }
188 element_list(A) ::= element_list(B) COMMA element_child(C).     { g_ptr_array_add(B,C); A = B; }
189 element_list(A) ::= element_list(B) PIPE element_child(C).      { g_ptr_array_add(B,C); A = B; }
190 element_list(A) ::= element_child(B).                                           { A = g_ptr_array_new(); g_ptr_array_add(A,B); }
191 element_list(A) ::= sub_elements(B).                                            { A = B; }
192 element_list(A) ::= element_list(B) COMMA sub_elements(C).   { A = g_ptr_array_join(B,C); }
193 element_list(A) ::= element_list(B) PIPE sub_elements(C).   { A = g_ptr_array_join(B,C); }
194
195 %type   element_child           { gchar* }
196 element_child(A) ::= NAME(B).                   {
197         A = g_ascii_strdown(B->text, -1);
198         g_free(B->text);
199     g_free(B->location);
200     g_free(B);
201 }
202
203 element_child(A) ::= NAME(B) STAR.              {
204         A = g_ascii_strdown(B->text, -1);
205         g_free(B->text);
206     g_free(B->location);
207     g_free(B);
208 }
209
210 element_child(A) ::= NAME(B) QUESTION.  {
211         A = g_ascii_strdown(B->text, -1);
212         g_free(B->text);
213     g_free(B->location);
214     g_free(B);
215 }
216
217 element_child(A) ::= NAME(B) PLUS.              {
218         A = g_ascii_strdown(B->text, -1);
219         g_free(B->text);
220     g_free(B->location);
221     g_free(B);
222 }
223