aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dtd_grammar.lemon
blob: 2fcdd6de558537297375e95f02faf50110d5e846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
%include {

/* dtd_parser.lemon
* XML dissector for wireshark 
* XML's DTD grammar
*
* Copyright 2005, Luis E. Garcia Ontanon <luis@ontanon.org>
*
* $Id$
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <glib.h>
#include <assert.h>
#include "dtd.h"
#include "dtd_parse.h"

static dtd_named_list_t* dtd_named_list_new(gchar* name, GPtrArray* list) {
	dtd_named_list_t* nl = g_malloc(sizeof(dtd_named_list_t));

	nl->name = name;
	nl->list = list;
	
	return nl;
}

static GPtrArray* g_ptr_array_join(GPtrArray* a, GPtrArray* b){
	
	while(b->len > 0) {
		g_ptr_array_add(a,g_ptr_array_remove_index_fast(b,0));
	}
	
	g_ptr_array_free(b,TRUE);

	return a;
}

}

%name DtdParse

%extra_argument { dtd_build_data_t *bd }

%token_destructor { 
	if ($$) {
		if ($$->text) g_free($$->text);
		if ($$->location) g_free($$->location);
		g_free($$);
	}
}

%syntax_error {
	if (!TOKEN)
		g_string_append_printf(bd->error,"syntax error at end of file");
	else 
		g_string_append_printf(bd->error,"syntax error in %s at or before '%s': \n", TOKEN->location,TOKEN->text);
}

%parse_failure {
	g_string_append_printf(bd->error,"DTD parsing failure\n");
}

%token_prefix TOKEN_

%token_type { dtd_token_data_t* }

dtd ::= doctype.
dtd ::= dtd_parts.

doctype ::= TAG_START DOCTYPE_KW NAME(Name) OPEN_BRACKET dtd_parts CLOSE_BRACKET TAG_STOP. {
    dtd_named_list_t* root;
    GPtrArray* root_elems = g_ptr_array_new();
    guint i;
    gchar *name;

    if(! bd->proto_name) {
        bd->proto_name = Name->text;
    }

    if(bd->proto_root)
        g_free(bd->proto_root);

	bd->proto_root = Name->text;
    
    name = g_ascii_strdown(bd->proto_name, -1);
    g_free(bd->proto_name);
	bd->proto_name = name;
    
    for( i = 0; i< bd->elements->len; i++) {
        dtd_named_list_t* el = g_ptr_array_index(bd->elements,i);
        
        g_ptr_array_add(root_elems,g_strdup(el->name));
    }
    
    root = dtd_named_list_new(g_strdup(Name->text),root_elems);
    
    g_ptr_array_add(bd->elements,root);
    
    g_free(Name->location);
    g_free(Name);

}

dtd_parts ::= dtd_parts element(Element). { g_ptr_array_add(bd->elements,Element); }
dtd_parts ::= dtd_parts attlist(Attlist). { g_ptr_array_add(bd->attributes,Attlist); }
dtd_parts ::= element(Element). { g_ptr_array_add(bd->elements,Element); }
dtd_parts ::= attlist(Attlist). { g_ptr_array_add(bd->attributes,Attlist); }

%type   attlist				{ dtd_named_list_t* }
attlist(A) ::= TAG_START ATTLIST_KW NAME(B) attrib_list(TheList) TAG_STOP. {
    A = dtd_named_list_new(g_ascii_strdown(B->text, -1),TheList);
    g_free(B->text);
    g_free(B->location);
    g_free(B);
}

%type element { dtd_named_list_t* }
element(A) ::= TAG_START ELEMENT_KW NAME(B) sub_elements(C) TAG_STOP. {
    A = dtd_named_list_new(g_ascii_strdown(B->text, -1),C);
    g_free(B->text);
    g_free(B->location);
    g_free(B);
}

%type   attrib_list			{ GPtrArray* }
attrib_list(A) ::= attrib_list(B) attrib(C). { g_ptr_array_add(B,C); A = B; }
attrib_list(A) ::= attrib(B).  { A = g_ptr_array_new(); g_ptr_array_add(A,B);  }

%type   attrib				{ gchar* }
attrib(A) ::= NAME(B) att_type att_default. {
	A = g_ascii_strdown(B->text, -1);
    g_free(B->text);
    g_free(B->location);
    g_free(B);
}

att_type ::= ATT_TYPE.
att_type ::= enumeration.

att_default ::= ATT_DEF.
att_default ::= ATT_DEF_WITH_VALUE QUOTED. 
att_default ::= QUOTED.
att_default ::= IMPLIED_KW.
att_default ::= REQUIRED_KW.

enumeration ::= OPEN_PARENS enum_list CLOSE_PARENS.

enum_list ::= enum_list PIPE enum_item.
enum_list ::= enum_item.
enum_list ::= enumeration.
enum_list ::= enum_list PIPE enumeration.

enum_item ::= NAME.
enum_item ::= QUOTED.


%type   sub_elements		{ GPtrArray* }
sub_elements(A) ::= sub_elements(B) STAR. {A=B;}
sub_elements(A) ::= sub_elements(B) PLUS. {A=B;}
sub_elements(A) ::= sub_elements(B) QUESTION. {A=B;}
sub_elements(A) ::= OPEN_PARENS ELEM_DATA CLOSE_PARENS. { A = g_ptr_array_new(); }
sub_elements(A) ::= OPEN_PARENS element_list(B) COMMA ELEM_DATA CLOSE_PARENS.	{ A = B; }
sub_elements(A) ::= OPEN_PARENS element_list(B) PIPE ELEM_DATA CLOSE_PARENS.	{ A = B; }
sub_elements(A) ::= OPEN_PARENS element_list(B) CLOSE_PARENS. { A = B; }
sub_elements(A) ::= EMPTY_KW. { A = g_ptr_array_new(); }

%type   element_list	{ GPtrArray* }
element_list(A)	::= element_list(B) COMMA element_child(C).	{ g_ptr_array_add(B,C); A = B; }
element_list(A)	::= element_list(B) PIPE element_child(C).	{ g_ptr_array_add(B,C); A = B; }
element_list(A)	::= element_child(B).						{ A = g_ptr_array_new(); g_ptr_array_add(A,B); }
element_list(A) ::= sub_elements(B).						{ A = B; }
element_list(A) ::= element_list(B) COMMA sub_elements(C).   { A = g_ptr_array_join(B,C); }
element_list(A) ::= element_list(B) PIPE sub_elements(C).   { A = g_ptr_array_join(B,C); }

%type   element_child		{ gchar* }
element_child(A) ::= NAME(B).			{
	A = g_ascii_strdown(B->text, -1);
	g_free(B->text);
    g_free(B->location);
    g_free(B);
}

element_child(A) ::= NAME(B) STAR.		{
	A = g_ascii_strdown(B->text, -1);
	g_free(B->text);
    g_free(B->location);
    g_free(B);
}

element_child(A) ::= NAME(B) QUESTION.	{
	A = g_ascii_strdown(B->text, -1);
	g_free(B->text);
    g_free(B->location);
    g_free(B);
}

element_child(A) ::= NAME(B) PLUS.		{
	A = g_ascii_strdown(B->text, -1);
	g_free(B->text);
    g_free(B->location);
    g_free(B);
}