Back to index

lightning-sunbird  0.9+nobinonly
ExprLexer.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * The MITRE Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Keith Visco <kvisco@ziplink.net> (Original Author)
00024  *   Axel Hecht <axel@pike.org>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 
00041 #ifndef MITREXSL_EXPRLEXER_H
00042 #define MITREXSL_EXPRLEXER_H
00043 
00044 #include "txCore.h"
00045 #include "nsString.h"
00046 
00053 class Token
00054 {
00055 public:
00056 
00060     enum Type {
00061         //-- Trivial Tokens
00062         NULL_TOKEN = 1,
00063         LITERAL,
00064         NUMBER,
00065         CNAME,
00066         FUNCTION_NAME,
00067         VAR_REFERENCE,
00068         PARENT_NODE,
00069         SELF_NODE,
00070         R_PAREN,
00071         R_BRACKET, // 10
00077         COMMA,
00078         AT_SIGN,
00079         L_PAREN,
00080         L_BRACKET,
00081         AXIS_IDENTIFIER,
00082 
00086         //-- boolean ops
00087         AND_OP, // 16
00088         OR_OP,
00089 
00090         //-- relational
00091         EQUAL_OP, // 18
00092         NOT_EQUAL_OP,
00093         LESS_THAN_OP,
00094         GREATER_THAN_OP,
00095         LESS_OR_EQUAL_OP,
00096         GREATER_OR_EQUAL_OP,
00097         //-- additive operators
00098         ADDITION_OP, // 24
00099         SUBTRACTION_OP,
00100         //-- multiplicative
00101         DIVIDE_OP , // 26
00102         MULTIPLY_OP,
00103         MODULUS_OP,
00104         //-- path operators
00105         PARENT_OP, // 29
00106         ANCESTOR_OP,
00107         UNION_OP,
00111         //-- node type tokens
00112         COMMENT, // 32
00113         NODE,
00114         PROC_INST,
00115         TEXT,
00116         
00117         //-- Special endtoken
00118         END // 36
00119     };
00120 
00121 
00125     typedef nsASingleFragmentString::const_char_iterator iterator;
00126 
00127     Token(iterator aStart, iterator aEnd, Type aType)
00128         : mStart(aStart),
00129           mEnd(aEnd),
00130           mType(aType),
00131           mNext(nsnull),
00132           mPrevious(nsnull)
00133     {
00134     }
00135     Token(iterator aChar, Type aType)
00136         : mStart(aChar),
00137           mEnd(aChar + 1),
00138           mType(aType),
00139           mNext(nsnull),
00140           mPrevious(nsnull)
00141     {
00142     }
00143 
00144     const nsDependentSubstring Value()
00145     {
00146         return Substring(mStart, mEnd);
00147     }
00148 
00149     iterator mStart, mEnd;
00150     Type mType;
00151     Token* mNext;
00152     // XXX mPrevious needed for pushBack(), do we pushBack more than once?
00153     Token* mPrevious;
00154 };
00155 
00163 class txExprLexer
00164 {
00165 public:
00166 
00167     txExprLexer();
00168     ~txExprLexer();
00169 
00177     nsresult parse(const nsASingleFragmentString& aPattern);
00178 
00179     typedef nsASingleFragmentString::const_char_iterator iterator;
00180     iterator mPosition;
00181 
00186     Token* nextToken();
00187     Token* peek()
00188     {
00189         return mCurrentItem;
00190     }
00191     void pushBack();
00192     PRBool hasMoreTokens()
00193     {
00194         return (mCurrentItem->mType != Token::END);
00195     }
00196 
00200     //-- LF, changed to enum
00201     enum _TrivialTokens {
00202         D_QUOTE        = '\"',
00203         S_QUOTE        = '\'',
00204         L_PAREN        = '(',
00205         R_PAREN        = ')',
00206         L_BRACKET      = '[',
00207         R_BRACKET      = ']',
00208         L_ANGLE        = '<',
00209         R_ANGLE        = '>',
00210         COMMA          = ',',
00211         PERIOD         = '.',
00212         ASTERIX        = '*',
00213         FORWARD_SLASH  = '/',
00214         EQUAL          = '=',
00215         BANG           = '!',
00216         VERT_BAR       = '|',
00217         AT_SIGN        = '@',
00218         DOLLAR_SIGN    = '$',
00219         PLUS           = '+',
00220         HYPHEN         = '-',
00221         COLON          = ':',
00222         //-- whitespace tokens
00223         SPACE          = ' ',
00224         TX_TAB            = '\t',
00225         TX_CR             = '\n',
00226         TX_LF             = '\r'
00227     };
00228 
00229 private:
00230 
00231     Token* mCurrentItem;
00232     Token* mFirstItem;
00233     Token* mLastItem;
00234 
00235     int mTokenCount;
00236 
00237     void addToken(Token* aToken);
00238 
00244     PRBool nextIsOperatorToken(Token* aToken);
00245 
00250     static PRBool isXPathDigit(PRUnichar ch)
00251     {
00252         return (ch >= '0' && ch <= '9');
00253     }
00254 };
00255 
00256 #endif
00257