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
39 static inline unsigned int hash (const char *str, unsigned int len);
42 static struct XMLTagHashPair *in_word_set (const char *str, unsigned int len);
45 XmlScanner::XmlScanner( const char *fileName, istream &input ) :
61 #define TK_NO_TOKEN (-1)
68 #define ret_tok( _tok ) token = (_tok); data = ts
70 void XmlScanner::adjustAttrPointers( int distance )
72 for ( AttrMkList::Iter attr = attrMkList; attr.lte(); attr++ ) {
74 attr->value -= distance;
78 /* There is no claim that this is a proper XML parser, but it is good
79 * enough for our purposes. */
83 action colup { curcol++; }
84 action start_tok { token_col = curcol; token_line = curline; }
85 NL = '\n' @{ curcol = 0; curline++; };
88 id = [_a-zA-Z][_a-zA-Z0-9]*;
89 literal = '"' ( [^"] | NL )* '"';
91 # Attribute identifiers.
92 action start_attr_id { attr_id_start = p; }
93 action leave_attr_id { attr_id_len = p - attr_id_start; }
95 attr_id = id >start_attr_id %leave_attr_id;
98 action start_attr_value { attr_value_start = p; }
99 action leave_attr_value
101 attr_value_len = p - attr_value_start;
104 newAttr.id = attr_id_start;
105 newAttr.idLen = attr_id_len;
106 newAttr.value = attr_value_start;
107 newAttr.valueLen = attr_value_len;
108 attrMkList.append( newAttr );
111 attr_value = literal >start_attr_value %leave_attr_value;
114 attribute = attr_id WS* '=' WS* attr_value WS*;
117 action tag_id_start { tag_id_start = p; }
118 action leave_tag_id { tag_id_len = p - tag_id_start; }
120 tag_id = id >tag_id_start %leave_tag_id;
124 ( '<' WS* tag_id ( WS+ attribute* )? '>' ) >start_tok $colup
125 => { ret_tok( TK_OpenTag ); fbreak; };
127 ( '<' WS* '/' WS* tag_id WS* '>' ) >start_tok $colup
128 => { ret_tok( TK_CloseTag ); fbreak; };
130 # Data in between tags.
131 ( [^<&\0] | NL ) $colup
132 => { buffer.append( *p ); };
136 => { buffer.append( '&' ); };
138 => { buffer.append( '<' ); };
140 => { buffer.append( '>' ); };
143 0 >start_tok => { ret_tok( TK_EOF ); fbreak; };
148 int XmlScanner::scan( )
150 int token = TK_NO_TOKEN;
151 int space = 0, readlen = 0;
152 char *attr_id_start = 0;
153 char *attr_value_start = 0;
155 int attr_value_len = 0;
162 //printf("scanner: need more data\n");
167 /* There is data that needs to be shifted over. */
168 //printf("scanner: buffer broken mid token\n");
170 memmove( buf, ts, have );
172 int distance = ts - buf;
174 tag_id_start -= distance;
175 attr_id_start -= distance;
176 attr_value_start -= distance;
177 adjustAttrPointers( distance );
182 space = XML_BUFSIZE - have;
185 /* We filled up the buffer trying to scan a token. */
190 //printf("scanner: end of file\n");
195 input.read( p, space );
196 readlen = input.gcount();
198 //printf("scanner: setting done flag\n");
208 if ( cs == XmlScanner_error )
211 if ( token != TK_NO_TOKEN ) {
218 int xml_parse( std::istream &input, const char *fileName,
219 bool outputActive, bool wantComplete )
221 XmlScanner scanner( fileName, input );
222 XmlParser parser( fileName, outputActive, wantComplete );
227 int token = scanner.scan();
228 if ( token == TK_NO_TOKEN ) {
229 cerr << "xmlscan: interal error: scanner returned NO_TOKEN" << endl;
232 else if ( token == TK_EOF ) {
233 parser.token( _eof, scanner.token_col, scanner.token_line );
236 else if ( token == TK_ERR ) {
237 scanner.error() << "scanner error" << endl;
240 else if ( token == TK_SPACE ) {
241 scanner.error() << "scanner is out of buffer space" << endl;
245 /* All other tokens are either open or close tags. */
246 XMLTagHashPair *tagId = Perfect_Hash::in_word_set(
247 scanner.tag_id_start, scanner.tag_id_len );
249 XMLTag *tag = new XMLTag( tagId, token == TK_OpenTag ?
250 XMLTag::Open : XMLTag::Close );
253 /* Get attributes for open tags. */
254 if ( token == TK_OpenTag && scanner.attrMkList.length() > 0 ) {
255 tag->attrList = new AttrList;
256 for ( AttrMkList::Iter attr = scanner.attrMkList;
260 newAttr.id = new char[attr->idLen+1];
261 memcpy( newAttr.id, attr->id, attr->idLen );
262 newAttr.id[attr->idLen] = 0;
264 /* Exclude the surrounding quotes. */
265 newAttr.value = new char[attr->valueLen-1];
266 memcpy( newAttr.value, attr->value+1, attr->valueLen-2 );
267 newAttr.value[attr->valueLen-2] = 0;
269 tag->attrList->append( newAttr );
273 /* Get content for closing tags. */
274 if ( token == TK_CloseTag ) {
275 switch ( tagId->id ) {
276 case TAG_host: case TAG_arg:
277 case TAG_t: case TAG_alphtype:
278 case TAG_text: case TAG_goto:
279 case TAG_call: case TAG_next:
280 case TAG_entry: case TAG_set_tokend:
281 case TAG_set_act: case TAG_start_state:
282 case TAG_error_state: case TAG_state_actions:
283 case TAG_action_table: case TAG_cond_space:
284 case TAG_c: case TAG_ex: case TAG_eof_t:
285 tag->content = new char[scanner.buffer.length+1];
286 memcpy( tag->content, scanner.buffer.data,
287 scanner.buffer.length );
288 tag->content[scanner.buffer.length] = 0;
295 cerr << "parser_driver: " << (tag->type == XMLTag::Open ? "open" : "close") <<
296 ": " << (tag->tagId != 0 ? tag->tagId->name : "<unknown>") << endl;
297 if ( tag->attrList != 0 ) {
298 for ( AttrList::Iter attr = *tag->attrList; attr.lte(); attr++ )
299 cerr << " " << attr->id << ": " << attr->value << endl;
301 if ( tag->content != 0 )
302 cerr << " content: " << tag->content << endl;
305 parser.token( tag, scanner.token_col, scanner.token_line );
312 std::ostream &XmlScanner::error()
315 cerr << fileName << ":" << curline << ":" << curcol << ": ";