summaryrefslogtreecommitdiff
path: root/libfsm/xmlparse.kh
blob: 1b0b30ad5af47a335dab4bffafb1eb351570b59b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright 2001-2007 Adrian Thurston <thurston@colm.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef _XMLPARSE_H
#define _XMLPARSE_H

#include "vector.h"
#include "gendata.h"
#include "buffer.h"
#include <iostream>

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<AttrMarker> AttrMkList;
typedef Vector<Attribute> 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 <char*> writeOptions;
};

%% write token_defs;

int xml_parse( std::istream &input, const char *fileName, 
		bool outputActive, bool wantComplete, 
		XmlScanner &scanner, XmlParser &parser );

#endif