summaryrefslogtreecommitdiff
path: root/textwolf/include/textwolf/xmlpathautomatonparse.hpp
blob: 33856ded1d20ce4594e95ab4c7420899fc2ec6ce (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
---------------------------------------------------------------------
    The template library textwolf implements an input iterator on
    a set of XML path expressions without backward references on an
    STL conforming input iterator as source. It does no buffering
    or read ahead and is dedicated for stream processing of XML
    for a small set of XML queries.
    Stream processing in this context refers to processing the
    document without buffering anything but the current result token
    processed with its tag hierarchy information.

    Copyright (C) 2010,2011,2012,2013,2014 Patrick Frey

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3.0 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

--------------------------------------------------------------------

	The latest version of textwolf can be found at 'http://github.com/patrickfrey/textwolf'
	For documentation see 'http://patrickfrey.github.com/textwolf'

--------------------------------------------------------------------
*/
/// \file textwolf/xmlpathautomatonparse.hpp
/// \brief Parser to create a path expression selector automaton from a source (list of path expression in abbreviated syntax of xpath)

#ifndef __TEXTWOLF_XML_PATH_AUTOMATON_PARSE_HPP__
#define __TEXTWOLF_XML_PATH_AUTOMATON_PARSE_HPP__
#include "textwolf/xmlpathautomaton.hpp"
#include "textwolf/charset.hpp"
#include "textwolf/cstringiterator.hpp"
#include <limits>
#include <string>
#include <vector>
#include <cstring>
#include <cstddef>
#include <stdexcept>

namespace textwolf {

///\class XMLPathSelectAutomatonParser
///\tparam SrcCharSet character set of the automaton definition source
///\tparam AtmCharSet character set of the token defintions of the automaton
///\brief Automaton to define XML path expressions and assign types (int values) to them
template <class SrcCharSet=charset::UTF8, class AtmCharSet=charset::UTF8>
class XMLPathSelectAutomatonParser :public XMLPathSelectAutomaton<AtmCharSet>
{
public:
	typedef XMLPathSelectAutomaton<AtmCharSet> ThisAutomaton;
	typedef typename ThisAutomaton::PathElement PathElement;
	typedef XMLPathSelectAutomatonParser This;
	typedef TextScanner<CStringIterator,SrcCharSet> SrcScanner;

public:
	///\brief Constructor
	XMLPathSelectAutomatonParser(){}
	virtual ~XMLPathSelectAutomatonParser(){}

	int addExpression( int typeidx, const char* esrc, std::size_t esrcsize)
	{
		std::string idstrings;
		CStringIterator pitr( esrc, esrcsize);
		SrcScanner pp( m_srccharset, pitr);
		std::vector<std::size_t> idref;

		for (; *pp; skipSpaces( pp))
		{
			switch (*pp)
			{
				case '/':
				case '@':
					++pp;
					continue;
				case '[':
					while (*pp != 0 && *pp != ']') pp++;
					if (*pp == 0) return pp.getPosition()+1;
					++pp;
					continue;
				default:
					if (pp.control() == Undef || pp.control() == Any)
					{
						idref.push_back( parseIdentifier( pp, idstrings));
					}
					else
					{
						return pp.getPosition()+1;
					}
			}
		}
		typename std::vector<std::size_t>::const_iterator di = idref.begin(), de = idref.end();

		CStringIterator itr( esrc, esrcsize);
		SrcScanner src( m_srccharset, itr);
		PathElement expr( this);

		for (; *src; skipSpaces( src))
		{
			switch (*src)
			{
				case '@':
				{
					if (di == de) return src.getPosition()+1;
					++src;
					skipIdentifier( src);
					expr( getIdentifier( *di, idstrings) );
				}
				case '/':
				{
					++src;
					if (*src == '/')
					{
						++src;
						if (*src == '@')
						{
							if (di == de) return src.getPosition()+1;
							++src;
							skipIdentifier( src);
							expr -- ( getIdentifier( *di, idstrings) );
						}
						else
						{
							if (di == de) return src.getPosition()+1;
							skipIdentifier( src);
							expr -- [ getIdentifier( *di, idstrings) ];
						}
					}
					else
					{
						if (*src == '@')
						{
							if (di == de) return src.getPosition()+1;
							++src;
							skipIdentifier( src);
							expr ( getIdentifier( *di, idstrings) );
						}
						else
						{
							if (di == de) return src.getPosition()+1;
							skipIdentifier( src);
							expr [ getIdentifier( *di, idstrings) ];
						}
					}
					continue;
				}
				case '[':
				{
					// Range
					int range_start = -1;
					int range_end = -1;
					++src; skipSpaces( src);
					range_start = parseNum( src);
					if (range_start < 0) return src.getPosition()+1;
					skipSpaces( src);

					if (*src == ',')
					{
						++src; skipSpaces( src);
						if (*src == ']')
						{
							expr.FROM( range_start);
							++src;
						}
						else
						{
							range_end = parseNum( src);
							if (range_end < 0) return src.getPosition()+1;
							++src; skipSpaces( src);
							if (*src != ']') return src.getPosition()+1;
							expr.RANGE( range_start, range_end);
							++src;
						}
					}
					else if (*src == ']')
					{
						range_start = range_end;
						expr.INDEX( range_start);
						++src;
					}
					else
					{
						return src.getPosition()+1;
					}
					continue;
				}
				default:
					return src.getPosition()+1;
			}
		}
		expr.assignType( typeidx);
		return 0;
	}

private:
	static void skipSpaces( SrcScanner& src)
	{
		for (; src.control() == Space; ++src);
	}

	static int parseNum( SrcScanner& src)
	{
		std::string num;
		for (; *src>='0' && *src<='9';++src) num.push_back( *src);
		if (num.size() == 0 || num.size() > 8) return -1;
		return std::atoi( num.c_str());
	}

	std::size_t parseIdentifier( SrcScanner& src, std::string& idstrings)
	{
		std::size_t rt = idstrings.size();
		for (; src.control() == Undef || src.control() == Any; ++src)
		{
			m_atmcharset.print( *src, idstrings);
		}
		m_atmcharset.print( 0, idstrings);
		return rt;
	}

	static void skipIdentifier( SrcScanner& src)
	{
		for (; src.control() == Undef || src.control() == Any; ++src);
	}

	const char* getIdentifier( std::size_t idx, const std::string& idstrings) const
	{
		return idstrings.c_str() + idx;
	}

private:
	AtmCharSet m_atmcharset;
	SrcCharSet m_srccharset;
};

} //namespace
#endif