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