src/script/parser/qscriptlexer_p.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtScript module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QSCRIPTLEXER_P_H
       
    43 #define QSCRIPTLEXER_P_H
       
    44 
       
    45 //
       
    46 //  W A R N I N G
       
    47 //  -------------
       
    48 //
       
    49 // This file is not part of the Qt API.  It exists purely as an
       
    50 // implementation detail.  This header file may change from version to
       
    51 // version without notice, or even be removed.
       
    52 //
       
    53 // We mean it.
       
    54 //
       
    55 
       
    56 #include <QtCore/QString>
       
    57 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 class QScriptEnginePrivate;
       
    61 class QScriptNameIdImpl;
       
    62 
       
    63 namespace QScript {
       
    64 
       
    65 class Lexer
       
    66 {
       
    67 public:
       
    68     Lexer(QScriptEnginePrivate *eng);
       
    69     ~Lexer();
       
    70 
       
    71     void setCode(const QString &c, int lineno);
       
    72     int lex();
       
    73 
       
    74     int currentLineNo() const { return yylineno; }
       
    75     int currentColumnNo() const { return yycolumn; }
       
    76 
       
    77     int startLineNo() const { return startlineno; }
       
    78     int startColumnNo() const { return startcolumn; }
       
    79 
       
    80     int endLineNo() const { return currentLineNo(); }
       
    81     int endColumnNo() const
       
    82     { int col = currentColumnNo(); return (col > 0) ? col - 1 : col; }
       
    83 
       
    84     bool prevTerminator() const { return terminator; }
       
    85 
       
    86     enum State { Start,
       
    87                  Identifier,
       
    88                  InIdentifier,
       
    89                  InSingleLineComment,
       
    90                  InMultiLineComment,
       
    91                  InNum,
       
    92                  InNum0,
       
    93                  InHex,
       
    94                  InOctal,
       
    95                  InDecimal,
       
    96                  InExponentIndicator,
       
    97                  InExponent,
       
    98                  Hex,
       
    99                  Octal,
       
   100                  Number,
       
   101                  String,
       
   102                  Eof,
       
   103                  InString,
       
   104                  InEscapeSequence,
       
   105                  InHexEscape,
       
   106                  InUnicodeEscape,
       
   107                  Other,
       
   108                  Bad };
       
   109 
       
   110     enum Error {
       
   111         NoError,
       
   112         IllegalCharacter,
       
   113         UnclosedStringLiteral,
       
   114         IllegalEscapeSequence,
       
   115         IllegalUnicodeEscapeSequence,
       
   116         UnclosedComment,
       
   117         IllegalExponentIndicator,
       
   118         IllegalIdentifier
       
   119     };
       
   120 
       
   121     enum ParenthesesState {
       
   122         IgnoreParentheses,
       
   123         CountParentheses,
       
   124         BalancedParentheses
       
   125     };
       
   126 
       
   127     enum RegExpBodyPrefix {
       
   128         NoPrefix,
       
   129         EqualPrefix
       
   130     };
       
   131 
       
   132     bool scanRegExp(RegExpBodyPrefix prefix = NoPrefix);
       
   133 
       
   134     QScriptNameIdImpl *pattern;
       
   135     int flags;
       
   136 
       
   137     State lexerState() const
       
   138         { return state; }
       
   139 
       
   140     QString errorMessage() const
       
   141         { return errmsg; }
       
   142     void setErrorMessage(const QString &err)
       
   143         { errmsg = err; }
       
   144     void setErrorMessage(const char *err)
       
   145         { setErrorMessage(QString::fromLatin1(err)); }
       
   146 
       
   147     Error error() const
       
   148         { return err; }
       
   149     void clearError()
       
   150         { err = NoError; }
       
   151 
       
   152 private:
       
   153     QScriptEnginePrivate *driver;
       
   154     int yylineno;
       
   155     bool done;
       
   156     char *buffer8;
       
   157     QChar *buffer16;
       
   158     uint size8, size16;
       
   159     uint pos8, pos16;
       
   160     bool terminator;
       
   161     bool restrKeyword;
       
   162     // encountered delimiter like "'" and "}" on last run
       
   163     bool delimited;
       
   164     int stackToken;
       
   165 
       
   166     State state;
       
   167     void setDone(State s);
       
   168     uint pos;
       
   169     void shift(uint p);
       
   170     int lookupKeyword(const char *);
       
   171 
       
   172     bool isWhiteSpace() const;
       
   173     bool isLineTerminator() const;
       
   174     bool isHexDigit(ushort c) const;
       
   175     bool isOctalDigit(ushort c) const;
       
   176 
       
   177     int matchPunctuator(ushort c1, ushort c2,
       
   178                          ushort c3, ushort c4);
       
   179     ushort singleEscape(ushort c) const;
       
   180     ushort convertOctal(ushort c1, ushort c2,
       
   181                          ushort c3) const;
       
   182 public:
       
   183     static unsigned char convertHex(ushort c1);
       
   184     static unsigned char convertHex(ushort c1, ushort c2);
       
   185     static QChar convertUnicode(ushort c1, ushort c2,
       
   186                                  ushort c3, ushort c4);
       
   187     static bool isIdentLetter(ushort c);
       
   188     static bool isDecimalDigit(ushort c);
       
   189 
       
   190     inline int ival() const { return qsyylval.ival; }
       
   191     inline double dval() const { return qsyylval.dval; }
       
   192     inline QScriptNameIdImpl *ustr() const { return qsyylval.ustr; }
       
   193 
       
   194     const QChar *characterBuffer() const { return buffer16; }
       
   195     int characterCount() const { return pos16; }
       
   196 
       
   197 private:
       
   198     void record8(ushort c);
       
   199     void record16(QChar c);
       
   200     void recordStartPos();
       
   201 
       
   202     int findReservedWord(const QChar *buffer, int size) const;
       
   203 
       
   204     void syncProhibitAutomaticSemicolon();
       
   205 
       
   206     const QChar *code;
       
   207     uint length;
       
   208     int yycolumn;
       
   209     int startlineno;
       
   210     int startcolumn;
       
   211     int bol;     // begin of line
       
   212 
       
   213     union {
       
   214         int ival;
       
   215         double dval;
       
   216         QScriptNameIdImpl *ustr;
       
   217     } qsyylval;
       
   218 
       
   219     // current and following unicode characters
       
   220     ushort current, next1, next2, next3;
       
   221 
       
   222     struct keyword {
       
   223         const char *name;
       
   224         int token;
       
   225     };
       
   226 
       
   227     QString errmsg;
       
   228     Error err;
       
   229 
       
   230     bool wantRx;
       
   231     bool check_reserved;
       
   232 
       
   233     ParenthesesState parenthesesState;
       
   234     int parenthesesCount;
       
   235     bool prohibitAutomaticSemicolon;
       
   236 };
       
   237 
       
   238 } // namespace QScript
       
   239 
       
   240 QT_END_NAMESPACE
       
   241 
       
   242 #endif