tizen 2.3.1 release
[external/ragel.git] / ragel / xmlparse.kh
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
3  */
4
5 /*  This file is part of Ragel.
6  *
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.
11  * 
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.
16  * 
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 
20  */
21 #ifndef _XMLPARSE_H
22 #define _XMLPARSE_H
23
24 #include "vector.h"
25 #include "gendata.h"
26 #include "buffer.h"
27 #include <iostream>
28
29 using std::istream;
30 using std::ostream;
31
32 #define XML_BUFSIZE 4096
33
34 struct AttrMarker
35 {
36         char *id;
37         int idLen;
38         char *value;
39         int valueLen;
40 };
41
42 struct Attribute
43 {
44         char *id;
45         char *value;
46 };
47
48 typedef Vector<AttrMarker> AttrMkList;
49 typedef Vector<Attribute> AttrList;
50 struct XMLTagHashPair;
51
52 struct XMLTag
53 {
54         enum TagType { Open, Close };
55
56         XMLTag( XMLTagHashPair *tagId, TagType type ) : 
57                 tagId(tagId), type(type), 
58                 content(0), attrList(0) {}
59         
60         Attribute *findAttr( const char *id )
61         {
62                 if ( attrList != 0 ) {
63                         for ( AttrList::Iter attr = *attrList; attr.lte(); attr++ ) {
64                                 if ( strcmp( id, attr->id ) == 0 )
65                                         return attr;
66                         }
67                 }
68                 return 0;
69         }
70
71         XMLTagHashPair *tagId;
72         TagType type;
73
74         /* Content is associtated with closing tags. */
75         char *content;
76
77         /* Attribute lists are associated with opening tags. */
78         AttrList *attrList;
79 };
80
81
82 struct XMLTagHashPair
83 {
84     const char *name;
85     int id;
86 };
87
88 struct Token;
89
90 struct GenInlineItem;
91 struct GenInlineList;
92
93 struct LmSwitchVect;
94 struct LmSwitchAction;
95
96 struct XmlScanner
97 {
98         XmlScanner( const char *fileName, istream &input );
99
100         int scan();
101         void adjustAttrPointers( int distance );
102         std::ostream &error();
103
104         const char *fileName;
105         istream &input;
106
107         /* Scanner State. */
108         int cs, act, have, curline, curcol;
109         char *ts, *te;
110         char *p, *pe;
111         int done;
112
113         /* Token data */
114         char *data;
115         int data_len;
116         int value;
117         AttrMkList attrMkList;
118         Buffer buffer;
119         char *tag_id_start;
120         int tag_id_len;
121         int token_col, token_line;
122
123         char buf[XML_BUFSIZE];
124 };
125
126
127 struct XmlParser
128 {
129         %%{
130                 parser XmlParser;
131
132                 token TAG_unknown, TAG_ragel, TAG_ragel_def, TAG_host, TAG_state_list,
133                         TAG_state, TAG_trans_list, TAG_t, TAG_machine, TAG_start_state,
134                         TAG_error_state, TAG_action_list, TAG_action_table_list,
135                         TAG_action, TAG_action_table, TAG_alphtype, TAG_element,
136                         TAG_getkey, TAG_state_actions, TAG_entry_points, TAG_sub_action,
137                         TAG_cond_space_list, TAG_cond_space, TAG_cond_list, TAG_c,
138                         TAG_exports, TAG_ex;
139
140                 # Inline block tokens.
141                 token TAG_text, TAG_goto, TAG_call, TAG_next, TAG_goto_expr,
142                         TAG_call_expr, TAG_next_expr, TAG_ret, TAG_pchar, TAG_char,
143                         TAG_hold, TAG_exec, TAG_curs, TAG_targs, TAG_entry, TAG_data, 
144                         TAG_lm_switch, TAG_init_act, TAG_set_act, TAG_set_tokend, 
145                         TAG_get_tokend, TAG_init_tokstart, TAG_set_tokstart;
146
147                 token TAG_write, TAG_access, TAG_break, TAG_arg, TAG_cs_expr;
148
149                 token TAG_p_expr, TAG_pe_expr, TAG_eof_expr, TAG_cs_expr, TAG_top_expr,
150                         TAG_stack_expr, TAG_act_expr, TAG_tokstart_expr, TAG_tokend_expr,
151                         TAG_data_expr, TAG_prepush, TAG_postpop, TAG_eof_t;
152         }%%
153
154         %% write instance_data;
155
156         void init();
157         int parseLangEl( int type, const Token *token );
158
159         XmlParser( const char *sourceFileName, const char *xmlFileName, bool outputActive, bool wantComplete ) : 
160                 sourceFileName(sourceFileName),
161                 fileName(xmlFileName),
162                 outStream(0),
163                 outputActive(outputActive),
164                 wantComplete(wantComplete),
165                 cgd(0) { }
166
167         int token( int tokenId, Token &token );
168         int token( int tokenId, int col, int line );
169         int token( XMLTag *tag, int col, int line );
170
171         void openOutput();
172
173         /* Report an error encountered by the parser. */
174         ostream &warning( const InputLoc &loc );
175         ostream &error();
176         ostream &error( const InputLoc &loc );
177         ostream &parser_error( int tokId, Token &token );
178         ostream &source_error( const InputLoc &loc );
179
180         /* The name of the root section, this does not change during an include. */
181         const char *sourceFileName;
182         const char *fileName;
183         ostream *outStream;
184         bool outputActive;
185         bool wantComplete;
186
187         /* Collected during parsing. */
188         char *attrKey;
189         char *attrValue;
190         int curAction;
191         int curActionTable;
192         int curTrans;
193         int curState;
194         int curCondSpace;
195         int curStateCond;
196
197         CodeGenData *cgd;
198         CodeGenMap codeGenMap;
199
200         Vector <char*> writeOptions;
201 };
202
203 %% write token_defs;
204
205 int xml_parse( std::istream &input, const char *fileName, 
206                 bool outputActive, bool wantComplete, 
207                 XmlScanner &scanner, XmlParser &parser );
208
209 #endif