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