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"
43 static inline unsigned int hash (const char *str, unsigned int len);
46 static struct XMLTagHashPair *in_word_set (const char *str, unsigned int len);
51 Scanner( char *fileName, istream &input ) :
68 void adjustAttrPointers( int distance );
69 std::ostream &error();
75 int cs, act, have, curline, curcol;
76 char *tokstart, *tokend;
84 AttrMkList attrMkList;
88 int token_col, token_line;
94 #define TK_NO_TOKEN (-1)
101 #define ret_tok( _tok ) token = (_tok); data = tokstart
103 void Scanner::adjustAttrPointers( int distance )
105 for ( AttrMkList::Iter attr = attrMkList; attr.lte(); attr++ ) {
106 attr->id -= distance;
107 attr->value -= distance;
111 /* There is no claim that this is a proper XML parser, but it is good
112 * enough for our purposes. */
116 action colup { curcol++; }
117 action start_tok { token_col = curcol; token_line = curline; }
118 NL = '\n' @{ curcol = 0; curline++; };
121 id = [_a-zA-Z][_a-zA-Z0-9]*;
122 literal = '"' ( [^"] | NL )* '"';
124 # Attribute identifiers.
125 action start_attr_id { attr_id_start = p; }
126 action leave_attr_id { attr_id_len = p - attr_id_start; }
128 attr_id = id >start_attr_id %leave_attr_id;
131 action start_attr_value { attr_value_start = p; }
132 action leave_attr_value
134 attr_value_len = p - attr_value_start;
137 newAttr.id = attr_id_start;
138 newAttr.idLen = attr_id_len;
139 newAttr.value = attr_value_start;
140 newAttr.valueLen = attr_value_len;
141 attrMkList.append( newAttr );
144 attr_value = literal >start_attr_value %leave_attr_value;
147 attribute = attr_id WS* '=' WS* attr_value WS*;
150 action tag_id_start { tag_id_start = p; }
151 action leave_tag_id { tag_id_len = p - tag_id_start; }
153 tag_id = id >tag_id_start %leave_tag_id;
157 ( '<' WS* tag_id ( WS+ attribute* )? '>' ) >start_tok $colup
158 => { ret_tok( TK_OpenTag ); fbreak; };
160 ( '<' WS* '/' WS* tag_id WS* '>' ) >start_tok $colup
161 => { ret_tok( TK_CloseTag ); fbreak; };
163 # Data in between tags.
164 ( [^<&\0] | NL ) $colup
165 => { buffer.append( *p ); };
169 => { buffer.append( '&' ); };
171 => { buffer.append( '<' ); };
173 => { buffer.append( '>' ); };
176 0 >start_tok => { ret_tok( TK_EOF ); fbreak; };
183 int token = TK_NO_TOKEN;
186 char *attr_value_start;
195 //printf("scanner: need more data\n");
200 /* There is data that needs to be shifted over. */
201 //printf("scanner: buffer broken mid token\n");
202 have = pe - tokstart;
203 memmove( buf, tokstart, have );
205 int distance = tokstart - buf;
207 tag_id_start -= distance;
208 attr_id_start -= distance;
209 attr_value_start -= distance;
210 adjustAttrPointers( distance );
215 space = BUFSIZE - have;
218 /* We filled up the buffer trying to scan a token. */
223 //printf("scanner: end of file\n");
228 input.read( p, space );
229 readlen = input.gcount();
231 //printf("scanner: setting done flag\n");
241 if ( cs == Scanner_error )
244 if ( token != TK_NO_TOKEN ) {
245 /* fbreak does not advance p, so we do it manually. */
254 int xml_parse( istream &input, char *fileName )
256 Scanner scanner( fileName, input );
257 Parser parser( fileName );
262 int token = scanner.scan();
263 if ( token == TK_NO_TOKEN ) {
264 cerr << PROGNAME << ": interal error: scanner returned NO_TOKEN" << endl;
267 else if ( token == TK_EOF ) {
268 parser.token( _eof, scanner.token_col, scanner.token_line );
271 else if ( token == TK_ERR ) {
272 scanner.error() << "scanner error" << endl;
275 else if ( token == TK_SPACE ) {
276 scanner.error() << "scanner is out of buffer space" << endl;
280 /* All other tokens are either open or close tags. */
281 XMLTagHashPair *tagId = Perfect_Hash::in_word_set(
282 scanner.tag_id_start, scanner.tag_id_len );
284 XMLTag *tag = new XMLTag( tagId, token == TK_OpenTag ?
285 XMLTag::Open : XMLTag::Close );
288 /* Get attributes for open tags. */
289 if ( token == TK_OpenTag && scanner.attrMkList.length() > 0 ) {
290 tag->attrList = new AttrList;
291 for ( AttrMkList::Iter attr = scanner.attrMkList;
295 newAttr.id = new char[attr->idLen+1];
296 memcpy( newAttr.id, attr->id, attr->idLen );
297 newAttr.id[attr->idLen] = 0;
299 /* Exclude the surrounding quotes. */
300 newAttr.value = new char[attr->valueLen-1];
301 memcpy( newAttr.value, attr->value+1, attr->valueLen-2 );
302 newAttr.value[attr->valueLen-2] = 0;
304 tag->attrList->append( newAttr );
308 /* Get content for closing tags. */
309 if ( token == TK_CloseTag ) {
310 switch ( tagId->id ) {
311 case TAG_host: case TAG_option:
312 case TAG_t: case TAG_alphtype:
313 case TAG_text: case TAG_goto:
314 case TAG_call: case TAG_next:
315 case TAG_entry: case TAG_set_tokend:
316 case TAG_set_act: case TAG_start_state:
317 case TAG_state_actions: case TAG_action_table:
318 case TAG_cond_space: case TAG_c:
319 tag->content = new char[scanner.buffer.length+1];
320 memcpy( tag->content, scanner.buffer.data,
321 scanner.buffer.length );
322 tag->content[scanner.buffer.length] = 0;
329 cerr << "parser_driver: " << (tag->type == XMLTag::Open ? "open" : "close") <<
330 ": " << (tag->tagId != 0 ? tag->tagId->name : "<unknown>") << endl;
331 if ( tag->attrList != 0 ) {
332 for ( AttrList::Iter attr = *tag->attrList; attr.lte(); attr++ )
333 cerr << " " << attr->id << ": " << attr->value << endl;
335 if ( tag->content != 0 )
336 cerr << " content: " << tag->content << endl;
339 parser.token( tag, scanner.token_col, scanner.token_line );
346 std::ostream &Scanner::error()
348 cerr << fileName << ":" << curline << ":" << curcol << ": ";