2 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "rlcodegen.h"
42 static inline unsigned int hash (const char *str, unsigned int len);
45 static struct XMLTagHashPair *in_word_set (const char *str, unsigned int len);
50 Scanner( istream &input ) :
66 void adjustAttrPointers( int distance );
71 int cs, act, have, curline, curcol;
72 char *tokstart, *tokend;
80 AttrMkList attrMkList;
84 int token_col, token_line;
90 #define TK_NO_TOKEN (-1)
96 #define ret_tok( _tok ) token = (_tok); data = tokstart
98 void Scanner::adjustAttrPointers( int distance )
100 for ( AttrMkList::Iter attr = attrMkList; attr.lte(); attr++ ) {
101 attr->id -= distance;
102 attr->value -= distance;
108 int token = TK_NO_TOKEN;
111 char *attr_value_start;
120 //printf("scanner: need more data\n");
125 /* There is data that needs to be shifted over. */
126 //printf("scanner: buffer broken mid token\n");
127 have = pe - tokstart;
128 memmove( buf, tokstart, have );
130 int distance = tokstart - buf;
132 tag_id_start -= distance;
133 attr_id_start -= distance;
134 attr_value_start -= distance;
135 adjustAttrPointers( distance );
140 space = BUFSIZE - have;
143 /* We filled up the buffer trying to scan a token. */
144 //printf("scanner: out of buffer space, you have a really long tag\n");
149 //printf("scanner: end of file\n");
154 input.read( p, space );
155 readlen = input.gcount();
157 //printf("scanner: setting done flag\n");
165 /* There is no claim that this is a proper XML parser, but it is good
166 * enough for our purposes. */
170 action colup { curcol++; }
171 action start_tok { token_col = curcol; token_line = curline; }
172 NL = '\n' @{ curcol = 0; curline++; };
175 id = [_a-zA-Z][_a-zA-Z0-9]*;
176 literal = '"' ( [^"] | NL )* '"';
178 # Attribute identifiers.
179 action start_attr_id { attr_id_start = p; }
180 action leave_attr_id { attr_id_len = p - attr_id_start; }
182 attr_id = id >start_attr_id %leave_attr_id;
185 action start_attr_value { attr_value_start = p; }
186 action leave_attr_value
188 attr_value_len = p - attr_value_start;
191 newAttr.id = attr_id_start;
192 newAttr.idLen = attr_id_len;
193 newAttr.value = attr_value_start;
194 newAttr.valueLen = attr_value_len;
195 attrMkList.append( newAttr );
198 attr_value = literal >start_attr_value %leave_attr_value;
201 attribute = attr_id WS* '=' WS* attr_value WS*;
204 action tag_id_start { tag_id_start = p; }
205 action leave_tag_id { tag_id_len = p - tag_id_start; }
207 tag_id = id >tag_id_start %leave_tag_id;
211 ( '<' WS* tag_id ( WS+ attribute* )? '>' ) >start_tok $colup
212 => { ret_tok( TK_OpenTag ); fbreak; };
214 ( '<' WS* '/' WS* tag_id WS* '>' ) >start_tok $colup
215 => { ret_tok( TK_CloseTag ); fbreak; };
217 # Data in between tags.
218 ( [^<&\0] | NL ) $colup
219 => { buffer.append( *p ); };
223 => { buffer.append( '&' ); };
225 => { buffer.append( '<' ); };
227 => { buffer.append( '>' ); };
230 0 >start_tok => { ret_tok( TK_EOF ); fbreak; };
237 if ( cs == Scanner_error )
240 if ( token != TK_NO_TOKEN ) {
241 /* fbreak does not advance p, so we do it manually. */
250 int xml_parse( istream &input, char *fileName )
252 Scanner scanner( input );
253 Parser parser( fileName );
258 int token = scanner.scan();
259 if ( token == TK_EOF ) {
260 //cout << "parser_driver: EOF" << endl;
261 parser.token( _eof );
264 else if ( token == TK_ERR ) {
265 //cout << "parser_driver: ERR" << endl;
269 /* All other tokens are either open or close tags. */
270 XMLTagHashPair *tagId = Perfect_Hash::in_word_set(
271 scanner.tag_id_start, scanner.tag_id_len );
273 XMLTag *tag = new XMLTag( tagId, token == TK_OpenTag ?
274 XMLTag::Open : XMLTag::Close );
277 /* Get attributes for open tags. */
278 if ( token == TK_OpenTag && scanner.attrMkList.length() > 0 ) {
279 tag->attrList = new AttrList;
280 for ( AttrMkList::Iter attr = scanner.attrMkList;
284 newAttr.id = new char[attr->idLen+1];
285 memcpy( newAttr.id, attr->id, attr->idLen );
286 newAttr.id[attr->idLen] = 0;
288 /* Exclude the surrounding quotes. */
289 newAttr.value = new char[attr->valueLen-1];
290 memcpy( newAttr.value, attr->value+1, attr->valueLen-2 );
291 newAttr.value[attr->valueLen-2] = 0;
293 tag->attrList->append( newAttr );
297 /* Get content for closing tags. */
298 if ( token == TK_CloseTag ) {
299 switch ( tagId->id ) {
300 case TAG_host: case TAG_option:
301 case TAG_t: case TAG_alphtype:
302 case TAG_text: case TAG_goto:
303 case TAG_call: case TAG_next:
304 case TAG_entry: case TAG_set_tokend:
305 case TAG_set_act: case TAG_start_state:
306 case TAG_state_actions: case TAG_action_table:
307 case TAG_cond_space: case TAG_c:
308 tag->content = new char[scanner.buffer.length+1];
309 memcpy( tag->content, scanner.buffer.data,
310 scanner.buffer.length );
311 tag->content[scanner.buffer.length] = 0;
318 cout << "parser_driver: " << (tag->type == XMLTag::Open ? "open" : "close") <<
319 ": " << tag->tagId->name << endl;
320 if ( tag->attrList != 0 ) {
321 for ( AttrList::Iter attr = *tag->attrList; attr.lte(); attr++ )
322 cout << " " << attr->id << ": " << attr->value << endl;
324 if ( tag->content != 0 )
325 cout << " content: " << tag->content << endl;
328 parser.token( tag, scanner.token_col, scanner.token_line );