/* * Copyright 2001-2007 Adrian Thurston */ /* This file is part of Ragel. * * Ragel 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. * * Ragel 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 Ragel; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _XMLPARSE_H #define _XMLPARSE_H #include "vector.h" #include "gendata.h" #include "buffer.h" #include using std::istream; using std::ostream; #define XML_BUFSIZE 4096 struct AttrMarker { char *id; int idLen; char *value; int valueLen; }; struct Attribute { char *id; char *value; }; typedef Vector AttrMkList; typedef Vector AttrList; struct XMLTagHashPair; struct XMLTag { enum TagType { Open, Close }; XMLTag( XMLTagHashPair *tagId, TagType type ) : tagId(tagId), type(type), content(0), attrList(0) {} Attribute *findAttr( const char *id ) { if ( attrList != 0 ) { for ( AttrList::Iter attr = *attrList; attr.lte(); attr++ ) { if ( strcmp( id, attr->id ) == 0 ) return attr; } } return 0; } XMLTagHashPair *tagId; TagType type; /* Content is associtated with closing tags. */ char *content; /* Attribute lists are associated with opening tags. */ AttrList *attrList; }; struct XMLTagHashPair { const char *name; int id; }; struct Token; struct GenInlineItem; struct GenInlineList; struct LmSwitchVect; struct LmSwitchAction; struct XmlScanner { XmlScanner( const char *fileName, istream &input ); int scan(); void adjustAttrPointers( int distance ); std::ostream &error(); const char *fileName; istream &input; /* Scanner State. */ int cs, act, have, curline, curcol; char *ts, *te; char *p, *pe; int done; /* Token data */ char *data; int data_len; int value; AttrMkList attrMkList; Buffer buffer; char *tag_id_start; int tag_id_len; int token_col, token_line; char buf[XML_BUFSIZE]; }; struct XmlParser { %%{ parser XmlParser; token TAG_unknown, TAG_ragel, TAG_ragel_def, TAG_host, TAG_state_list, TAG_state, TAG_trans_list, TAG_t, TAG_machine, TAG_start_state, TAG_error_state, TAG_action_list, TAG_action_table_list, TAG_action, TAG_action_table, TAG_alphtype, TAG_element, TAG_getkey, TAG_state_actions, TAG_entry_points, TAG_sub_action, TAG_cond_space_list, TAG_cond_space, TAG_cond_list, TAG_c, TAG_exports, TAG_ex; # Inline block tokens. token TAG_text, TAG_goto, TAG_call, TAG_next, TAG_goto_expr, TAG_call_expr, TAG_next_expr, TAG_ret, TAG_pchar, TAG_char, TAG_hold, TAG_exec, TAG_curs, TAG_targs, TAG_entry, TAG_data, TAG_lm_switch, TAG_init_act, TAG_set_act, TAG_set_tokend, TAG_get_tokend, TAG_init_tokstart, TAG_set_tokstart; token TAG_write, TAG_access, TAG_break, TAG_arg, TAG_cs_expr; token TAG_p_expr, TAG_pe_expr, TAG_eof_expr, TAG_cs_expr, TAG_top_expr, TAG_stack_expr, TAG_act_expr, TAG_tokstart_expr, TAG_tokend_expr, TAG_data_expr, TAG_prepush, TAG_postpop, TAG_eof_t; }%% %% write instance_data; void init(); int parseLangEl( int type, const Token *token ); XmlParser( const char *sourceFileName, const char *xmlFileName, bool outputActive, bool wantComplete ) : sourceFileName(sourceFileName), fileName(xmlFileName), outStream(0), outputActive(outputActive), wantComplete(wantComplete), cgd(0) { } int token( int tokenId, Token &token ); int token( int tokenId, int col, int line ); int token( XMLTag *tag, int col, int line ); void openOutput(); /* Report an error encountered by the parser. */ ostream &warning( const InputLoc &loc ); ostream &error(); ostream &error( const InputLoc &loc ); ostream &parser_error( int tokId, Token &token ); ostream &source_error( const InputLoc &loc ); /* The name of the root section, this does not change during an include. */ const char *sourceFileName; const char *fileName; ostream *outStream; bool outputActive; bool wantComplete; /* Collected during parsing. */ char *attrKey; char *attrValue; int curAction; int curActionTable; int curTrans; int curState; int curCondSpace; int curStateCond; CodeGenData *cgd; CodeGenMap codeGenMap; Vector writeOptions; }; %% write token_defs; int xml_parse( std::istream &input, const char *fileName, bool outputActive, bool wantComplete, XmlScanner &scanner, XmlParser &parser ); #endif