1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The MITRE Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1999
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Keith Visco <kvisco@ziplink.net> (Original Author)
24 : * Axel Hecht <axel@pike.org>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 :
41 : #ifndef MITREXSL_EXPRLEXER_H
42 : #define MITREXSL_EXPRLEXER_H
43 :
44 : #include "txCore.h"
45 : #include "nsString.h"
46 :
47 : /**
48 : * A Token class for the ExprLexer.
49 : *
50 : * This class was ported from XSL:P, an open source Java based
51 : * XSLT processor, written by yours truly.
52 : */
53 : class Token
54 : {
55 : public:
56 :
57 : /**
58 : * Token types
59 : */
60 : enum Type {
61 : //-- Trivial Tokens
62 : NULL_TOKEN = 1,
63 : LITERAL,
64 : NUMBER,
65 : CNAME,
66 : VAR_REFERENCE,
67 : PARENT_NODE,
68 : SELF_NODE,
69 : R_PAREN,
70 : R_BRACKET, // 9
71 : /**
72 : * start of tokens for 3.7, bullet 1
73 : * ExprLexer::nextIsOperatorToken bails if the tokens aren't
74 : * consecutive.
75 : */
76 : COMMA,
77 : AT_SIGN,
78 : L_PAREN,
79 : L_BRACKET,
80 : AXIS_IDENTIFIER,
81 :
82 : // These tokens include their following left parenthesis
83 : FUNCTION_NAME_AND_PAREN, // 15
84 : COMMENT_AND_PAREN,
85 : NODE_AND_PAREN,
86 : PROC_INST_AND_PAREN,
87 : TEXT_AND_PAREN,
88 :
89 : /**
90 : * operators
91 : */
92 : //-- boolean ops
93 : AND_OP, // 20
94 : OR_OP,
95 :
96 : //-- relational
97 : EQUAL_OP, // 22
98 : NOT_EQUAL_OP,
99 : LESS_THAN_OP,
100 : GREATER_THAN_OP,
101 : LESS_OR_EQUAL_OP,
102 : GREATER_OR_EQUAL_OP,
103 : //-- additive operators
104 : ADDITION_OP, // 28
105 : SUBTRACTION_OP,
106 : //-- multiplicative
107 : DIVIDE_OP, // 30
108 : MULTIPLY_OP,
109 : MODULUS_OP,
110 : //-- path operators
111 : PARENT_OP, // 33
112 : ANCESTOR_OP,
113 : UNION_OP,
114 : /**
115 : * end of tokens for 3.7, bullet 1 -/
116 : */
117 : //-- Special endtoken
118 : END // 36
119 : };
120 :
121 :
122 : /**
123 : * Constructors
124 : */
125 : typedef nsASingleFragmentString::const_char_iterator iterator;
126 :
127 84 : Token(iterator aStart, iterator aEnd, Type aType)
128 : : mStart(aStart),
129 : mEnd(aEnd),
130 : mType(aType),
131 : mNext(nsnull),
132 84 : mPrevious(nsnull)
133 : {
134 84 : }
135 42 : Token(iterator aChar, Type aType)
136 : : mStart(aChar),
137 42 : mEnd(aChar + 1),
138 : mType(aType),
139 : mNext(nsnull),
140 84 : mPrevious(nsnull)
141 : {
142 42 : }
143 :
144 0 : const nsDependentSubstring Value()
145 : {
146 0 : return Substring(mStart, mEnd);
147 : }
148 :
149 : iterator mStart, mEnd;
150 : Type mType;
151 : Token* mNext;
152 : // XXX mPrevious needed for pushBack(), do we pushBack more than once?
153 : Token* mPrevious;
154 : };
155 :
156 : /**
157 : * A class for splitting an "Expr" String into tokens and
158 : * performing basic Lexical Analysis.
159 : *
160 : * This class was ported from XSL:P, an open source Java based XSL processor
161 : */
162 :
163 : class txExprLexer
164 : {
165 : public:
166 :
167 : txExprLexer();
168 : ~txExprLexer();
169 :
170 : /**
171 : * Parse the given string.
172 : * returns an error result if lexing failed.
173 : * The given string must outlive the use of the lexer, as the
174 : * generated Tokens point to Substrings of it.
175 : * mPosition points to the offending location in case of an error.
176 : */
177 : nsresult parse(const nsASingleFragmentString& aPattern);
178 :
179 : typedef nsASingleFragmentString::const_char_iterator iterator;
180 : iterator mPosition;
181 :
182 : /**
183 : * Functions for iterating over the TokenList
184 : */
185 :
186 : Token* nextToken();
187 294 : Token* peek()
188 : {
189 294 : return mCurrentItem;
190 : }
191 : void pushBack();
192 : bool hasMoreTokens()
193 : {
194 : return (mCurrentItem->mType != Token::END);
195 : }
196 :
197 : /**
198 : * Trivial Tokens
199 : */
200 : //-- LF, changed to enum
201 : enum _TrivialTokens {
202 : D_QUOTE = '\"',
203 : S_QUOTE = '\'',
204 : L_PAREN = '(',
205 : R_PAREN = ')',
206 : L_BRACKET = '[',
207 : R_BRACKET = ']',
208 : L_ANGLE = '<',
209 : R_ANGLE = '>',
210 : COMMA = ',',
211 : PERIOD = '.',
212 : ASTERIX = '*',
213 : FORWARD_SLASH = '/',
214 : EQUAL = '=',
215 : BANG = '!',
216 : VERT_BAR = '|',
217 : AT_SIGN = '@',
218 : DOLLAR_SIGN = '$',
219 : PLUS = '+',
220 : HYPHEN = '-',
221 : COLON = ':',
222 : //-- whitespace tokens
223 : SPACE = ' ',
224 : TX_TAB = '\t',
225 : TX_CR = '\n',
226 : TX_LF = '\r'
227 : };
228 :
229 : private:
230 :
231 : Token* mCurrentItem;
232 : Token* mFirstItem;
233 : Token* mLastItem;
234 :
235 : int mTokenCount;
236 :
237 : void addToken(Token* aToken);
238 :
239 : /**
240 : * Returns true if the following Token should be an operator.
241 : * This is a helper for the first bullet of [XPath 3.7]
242 : * Lexical Structure
243 : */
244 : bool nextIsOperatorToken(Token* aToken);
245 :
246 : /**
247 : * Returns true if the given character represents a numeric letter (digit)
248 : * Implemented in ExprLexerChars.cpp
249 : */
250 42 : static bool isXPathDigit(PRUnichar ch)
251 : {
252 42 : return (ch >= '0' && ch <= '9');
253 : }
254 : };
255 :
256 : #endif
257 :
|