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,
220 XmlScanner &scanner, XmlParser &parser )
223 int token = scanner.scan();
224 if ( token == TK_NO_TOKEN ) {
225 cerr << "xmlscan: interal error: scanner returned NO_TOKEN" << endl;
228 else if ( token == TK_EOF ) {
229 parser.token( XmlParser_tk_eof, scanner.token_col, scanner.token_line );
232 else if ( token == TK_ERR ) {
233 scanner.error() << "scanner error" << endl;
236 else if ( token == TK_SPACE ) {
237 scanner.error() << "scanner is out of buffer space" << endl;
241 /* All other tokens are either open or close tags. */
242 XMLTagHashPair *tagId = Perfect_Hash::in_word_set(
243 scanner.tag_id_start, scanner.tag_id_len );
245 XMLTag *tag = new XMLTag( tagId, token == TK_OpenTag ?
246 XMLTag::Open : XMLTag::Close );
249 /* Get attributes for open tags. */
250 if ( token == TK_OpenTag && scanner.attrMkList.length() > 0 ) {
251 tag->attrList = new AttrList;
252 for ( AttrMkList::Iter attr = scanner.attrMkList;
256 newAttr.id = new char[attr->idLen+1];
257 memcpy( newAttr.id, attr->id, attr->idLen );
258 newAttr.id[attr->idLen] = 0;
260 /* Exclude the surrounding quotes. */
261 newAttr.value = new char[attr->valueLen-1];
262 memcpy( newAttr.value, attr->value+1, attr->valueLen-2 );
263 newAttr.value[attr->valueLen-2] = 0;
265 tag->attrList->append( newAttr );
269 /* Get content for closing tags. */
270 if ( token == TK_CloseTag ) {
271 switch ( tagId->id ) {
272 case TAG_host: case TAG_arg:
273 case TAG_t: case TAG_alphtype:
274 case TAG_text: case TAG_goto:
275 case TAG_call: case TAG_next:
276 case TAG_entry: case TAG_set_tokend:
277 case TAG_set_act: case TAG_start_state:
278 case TAG_error_state: case TAG_state_actions:
279 case TAG_action_table: case TAG_cond_space:
280 case TAG_c: case TAG_ex: case TAG_eof_t:
281 tag->content = new char[scanner.buffer.length+1];
282 memcpy( tag->content, scanner.buffer.data,
283 scanner.buffer.length );
284 tag->content[scanner.buffer.length] = 0;
291 cerr << "parser_driver: " << (tag->type == XMLTag::Open ? "open" : "close") <<
292 ": " << (tag->tagId != 0 ? tag->tagId->name : "<unknown>") << endl;
293 if ( tag->attrList != 0 ) {
294 for ( AttrList::Iter attr = *tag->attrList; attr.lte(); attr++ )
295 cerr << " " << attr->id << ": " << attr->value << endl;
297 if ( tag->content != 0 )
298 cerr << " content: " << tag->content << endl;
301 parser.token( tag, scanner.token_col, scanner.token_line );
308 std::ostream &XmlScanner::error()
311 cerr << fileName << ":" << curline << ":" << curcol << ": ";