Debian Codegraph @

src/tinyxpath/xpath_stream.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 www.sourceforge.net/projects/tinyxpath
3 Copyright (c) 2002-2004 Yves Berquin ([email protected])
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any
7 damages arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any
10 purpose, including commercial applications, and to alter it and
11 redistribute it freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must
14 not claim that you wrote the original software. If you use this
15 software in a product, an acknowledgment in the product documentation
16 would be appreciated but is not required.
17
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
20
21 3. This notice may not be removed or altered from any source
22 distribution.
23 */
24 /**
25 \file xpath_stream.h
26 \author Yves Berquin
27 Specialized byte stream for the TinyXPath project
28 */
29
30 #ifndef __TINYXPSTREAM_H
31 #define __TINYXPSTREAM_H
32
33 #include "lex_util.h"
34 #include "byte_stream.h"
35 #include "xpath_syntax.h"
36 #include "tinyxml.h"
37
38 namespace TinyXPath
39 {
40
41 /**
42 A specialized version of byte_stream for XPath
43 */
44 class xpath_stream : public byte_stream
45 {
46 protected :
47 /// List of tokens
48 token_syntax_decoder * tlp_list;
49
50 public :
51 /// constructor
52 xpath_stream (const char * cp_in);
53 /// destructor
54 virtual ~ xpath_stream ()
55 {
56 delete tlp_list;
57 }
58 /// Decode the byte stream, and construct the lexical list
59 void v_lexico_decode ()
60 {
61 enum {s_init, s_ncname, s_number, s_literal_1, s_literal_2, s_end} state;
62 lexico lex_new, lex_next;
63 unsigned u_size;
64 bool o_dot_in_number;
65
66 u_size = 0;
67 o_dot_in_number = false;
68 state = s_init;
69 while (state != s_end)
70 {
71 lex_next = lex_get_class (b_top ());
72 switch (state)
73 {
74 case s_init :
75 switch (lex_next)
76 {
77 case lex_bchar :
78 case lex_under :
79 // [XML:4] NCName ::= (Letter | '_') (NCNameChar)*
80
81 u_size = 1;
82 state = s_ncname;
83 b_pop ();
84 break;
85 case lex_null :
86 state = s_end;
87 break;
88 case lex_digit :
89 u_size = 1;
90 state = s_number;
91 o_dot_in_number = false;
92 b_pop ();
93 break;
94 case lex_dot :
95 if (lex_get_class (b_forward (1)) == lex_digit)
96 {
97 // [30] Number ::= Digits ('.' Digits?)? | '.' Digits
98 // [31] Digits ::= [0-9]+
99 u_size = 1;
100 state = s_number;
101 o_dot_in_number = true;
102 b_pop ();
103 }
104 else
105 {
106 tlp_list -> v_add_token (lex_next, bp_get_backward (1), 1);
107 b_pop ();
108 }
109 break;
110
111 case lex_1_quote :
112 // [29] Literal ::= '"' [^"]* '"' | "'" [^']* "'"
113 u_size = 0;
114 b_pop ();
115 state = s_literal_1;
116 break;
117
118 case lex_2_quote :
119 // [29] Literal ::= '"' [^"]* '"' | "'" [^']* "'"
120 u_size = 0;
121 b_pop ();
122 state = s_literal_2;
123 break;
124
125 default :
126 tlp_list -> v_add_token (lex_next, bp_get_backward (1), 1);
127 b_pop ();
128 break;
129 }
130 break;
131 case s_literal_1 :
132 // [29] Literal ::= '"' [^"]* '"' | "'" [^']* "'"
133 switch (lex_next)
134 {
135 case lex_1_quote :
136 tlp_list -> v_add_token (lex_literal, bp_get_backward (u_size + 1), u_size);
137 b_pop ();
138 state = s_init;
139 break;
140 default :
141 u_size++;
142 b_pop ();
143 break;
144 }
145 break;
146 case s_literal_2 :
147 // [29] Literal ::= '"' [^"]* '"' | "'" [^']* "'"
148 switch (lex_next)
149 {
150 case lex_2_quote :
151 tlp_list -> v_add_token (lex_literal, bp_get_backward (u_size + 1), u_size);
152 b_pop ();
153 state = s_init;
154 break;
155 default :
156 u_size++;
157 b_pop ();
158 break;
159 }
160 break;
161 case s_ncname :
162 switch (lex_next)
163 {
164 // [XML:5] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender
165 case lex_bchar :
166 case lex_digit :
167 case lex_dot :
168 case lex_minus :
169 case lex_under :
170 case lex_extend :
171 u_size++;
172 b_pop ();
173 break;
174 default :
175 lex_new = lex_test_id (bp_get_backward (u_size + 1), u_size, lex_next);
176 tlp_list -> v_add_token (lex_new, bp_get_backward (u_size + 1), u_size);
177 state = s_init;
178 break;
179 }
180 break;
181 case s_number :
182 switch (lex_next)
183 {
184 // [30] Number ::= Digits ('.' Digits?)? | '.' Digits
185 // [31] Digits ::= [0-9]+
186 case lex_dot :
187 if (o_dot_in_number)
188 {
189 tlp_list -> v_add_token (lex_number, bp_get_backward (u_size + 1), u_size);
190 state = s_init;
191 }
192 else
193 {
194 o_dot_in_number = true;
195 u_size++;
196 b_pop ();
197 }
198 break;
199 case lex_digit :
200 u_size++;
201 b_pop ();
202 break;
203 default :
204 tlp_list -> v_add_token (lex_number, bp_get_backward (u_size + 1), u_size);
205 state = s_init;
206 break;
207 }
208 break;
209
210 // jan 21th, 07 - neoneurone: silently ignore the other cases
211 default:
212 break;
213 }
214 if (lex_next == lex_null)
215 state = s_end;
216 }
217 }
218
219 /// Evaluate a XPath expression \n
220 /// Decodes the lexical and syntax contents.
221 void v_evaluate ()
222 {
223 v_lexico_decode ();
224 tlp_list -> v_syntax_decode ();
225 }
226
227 /// Callback used by token_syntax_decoder::v_syntax_decode to notify of an action to be made. Pure virtual
228 virtual void v_action (xpath_construct , unsigned , unsigned , const char * ) = 0;
229
230 /// Callback used by token_syntax_decoder::v_syntax_decode to know the action counter position. Pure virtual
231 /// \n This can be any kind of nomenclature, provided that the redefinition is coherent
232 virtual int i_get_action_counter () = 0;
233 } ; // class xpath_stream
234
235 }
236
237 #endif