Split the XML parsing, reduced fsm, and the code generation data structures out
[external/ragel.git] / redfsm / xmlparse.kh
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
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 <iostream>
27
28 using std::ostream;
29
30 struct AttrMarker
31 {
32         char *id;
33         int idLen;
34         char *value;
35         int valueLen;
36 };
37
38 struct Attribute
39 {
40         char *id;
41         char *value;
42 };
43
44 typedef Vector<AttrMarker> AttrMkList;
45 typedef Vector<Attribute> AttrList;
46 struct XMLTagHashPair;
47
48 struct XMLTag
49 {
50         enum TagType { Open, Close };
51
52         XMLTag( XMLTagHashPair *tagId, TagType type ) : 
53                 tagId(tagId), type(type), 
54                 content(0), attrList(0) {}
55         
56         Attribute *findAttr( char *id )
57         {
58                 if ( attrList != 0 ) {
59                         for ( AttrList::Iter attr = *attrList; attr.lte(); attr++ ) {
60                                 if ( strcmp( id, attr->id ) == 0 )
61                                         return attr;
62                         }
63                 }
64                 return 0;
65         }
66
67         XMLTagHashPair *tagId;
68         TagType type;
69
70         /* Content is associtated with closing tags. */
71         char *content;
72
73         /* Attribute lists are associated with opening tags. */
74         AttrList *attrList;
75 };
76
77
78 struct XMLTagHashPair
79 {
80     char *name;
81     int id;
82 };
83
84 struct Token
85 {
86         XMLTag *tag;
87         InputLoc loc;
88 };
89
90 struct InlineItem;
91 struct InlineList;
92
93 struct LmSwitchVect;
94 struct LmSwitchAction;
95
96 extern char *lelNames[];
97
98 struct LangEl;
99
100 struct Parser
101 {
102         %%{
103                 parser Parser;
104
105                 token TAG_unknown, TAG_ragel, TAG_ragel_def, TAG_host, TAG_state_list,
106                         TAG_state, TAG_trans_list, TAG_t, TAG_machine, TAG_start_state,
107                         TAG_action_list, TAG_action_table_list, TAG_action,
108                         TAG_action_table, TAG_alphtype, TAG_element, TAG_getkey,
109                         TAG_state_actions, TAG_entry_points, TAG_sub_action,
110                         TAG_cond_space_list, TAG_cond_space, TAG_cond_list, TAG_c;
111
112                 # Inline block tokens.
113                 token TAG_text, TAG_goto, TAG_call, TAG_next, TAG_goto_expr,
114                         TAG_call_expr, TAG_next_expr, TAG_ret, TAG_pchar, TAG_char,
115                         TAG_hold, TAG_exec, TAG_holdte, TAG_execte, TAG_curs, TAG_targs,
116                         TAG_entry, TAG_data, TAG_lm_switch, TAG_init_act, TAG_set_act,
117                         TAG_set_tokend, TAG_get_tokend, TAG_init_tokstart,
118                         TAG_set_tokstart, TAG_write, TAG_curstate, TAG_access, TAG_break,
119                         TAG_arg;
120         }%%
121
122         %% write instance_data;
123
124         void init();
125         int parseLangEl( int type, const Token *token );
126
127         Parser( char *fileName, bool outputActive, bool wantComplete ) : 
128                 fileName(fileName), sourceFileName(0), outStream(0),
129                 outputActive(outputActive), wantComplete(wantComplete),
130                 cgd(0) { }
131
132         int token( int tokenId, Token &token );
133         int token( int tokenId, int col, int line );
134         int token( XMLTag *tag, int col, int line );
135
136         /* Report an error encountered by the parser. */
137         ostream &warning( const InputLoc &loc );
138         ostream &error();
139         ostream &error( const InputLoc &loc );
140         ostream &parser_error( int tokId, Token &token );
141
142         /* The name of the root section, this does not change during an include. */
143         char *fileName;
144         char *sourceFileName;
145         ostream *outStream;
146         bool outputActive;
147         bool wantComplete;
148
149         /* Collected during parsing. */
150         char *attrKey;
151         char *attrValue;
152         int curAction;
153         int curActionTable;
154         int curTrans;
155         int curState;
156         int curCondSpace;
157         int curStateCond;
158
159         CodeGenData *cgd;
160         CodeGenMap codeGenMap;
161
162         Vector <char*> writeOptions;
163 };
164
165 %% write token_defs;
166
167 int xml_parse( std::istream &input, char *fileName, 
168                 bool outputActive, bool wantComplete );
169
170 #endif /* _XMLPARSE_H */