tools/icheck/parser/src/shared/cplusplus/Lexer.h
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     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 Qt Mobility Components.
       
     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 // Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
       
    42 //
       
    43 // Permission is hereby granted, free of charge, to any person obtaining a copy
       
    44 // of this software and associated documentation files (the "Software"), to deal
       
    45 // in the Software without restriction, including without limitation the rights
       
    46 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
    47 // copies of the Software, and to permit persons to whom the Software is
       
    48 // furnished to do so, subject to the following conditions:
       
    49 //
       
    50 // The above copyright notice and this permission notice shall be included in
       
    51 // all copies or substantial portions of the Software.
       
    52 //
       
    53 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    54 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    55 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
       
    56 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    57 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    58 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
       
    59 // THE SOFTWARE.
       
    60 
       
    61 #ifndef CPLUSPLUS_LEXER_H
       
    62 #define CPLUSPLUS_LEXER_H
       
    63 
       
    64 #include "CPlusPlusForwardDeclarations.h"
       
    65 #include "Token.h"
       
    66 
       
    67 
       
    68 namespace CPlusPlus {
       
    69 
       
    70 class CPLUSPLUS_EXPORT Lexer
       
    71 {
       
    72     Lexer(const Lexer &other);
       
    73     void operator =(const Lexer &other);
       
    74 
       
    75 public:
       
    76     enum State {
       
    77         State_Default,
       
    78         State_MultiLineComment,
       
    79         State_MultiLineDoxyComment
       
    80     };
       
    81 
       
    82     Lexer(TranslationUnit *unit);
       
    83     Lexer(const char *firstChar, const char *lastChar);
       
    84     ~Lexer();
       
    85 
       
    86     Control *control() const;
       
    87     TranslationUnit *translationUnit() const;
       
    88 
       
    89     bool qtMocRunEnabled() const;
       
    90     void setQtMocRunEnabled(bool onoff);
       
    91 
       
    92     bool objCEnabled() const;
       
    93     void setObjCEnabled(bool onoff);
       
    94 
       
    95     void scan(Token *tok);
       
    96 
       
    97     inline void operator()(Token *tok)
       
    98     { scan(tok); }
       
    99 
       
   100     unsigned tokenOffset() const;
       
   101     unsigned tokenLength() const;
       
   102     const char *tokenBegin() const;
       
   103     const char *tokenEnd() const;
       
   104     unsigned currentLine() const;
       
   105 
       
   106     bool scanCommentTokens() const;
       
   107     void setScanCommentTokens(bool onoff);
       
   108 
       
   109     bool scanKeywords() const;
       
   110     void setScanKeywords(bool onoff);
       
   111 
       
   112     bool scanAngleStringLiteralTokens() const;
       
   113     void setScanAngleStringLiteralTokens(bool onoff);
       
   114 
       
   115     void setStartWithNewline(bool enabled);
       
   116 
       
   117     int state() const;
       
   118     void setState(int state);
       
   119 
       
   120     bool isIncremental() const;
       
   121     void setIncremental(bool isIncremental);
       
   122 
       
   123 private:
       
   124     void scan_helper(Token *tok);
       
   125     void setSource(const char *firstChar, const char *lastChar);
       
   126     static int classify(const char *string, int length, bool q);
       
   127     static int classifyObjCAtKeyword(const char *s, int n);
       
   128     static int classifyOperator(const char *string, int length);
       
   129 
       
   130     inline void yyinp()
       
   131     {
       
   132         if (++_currentChar == _lastChar)
       
   133             _yychar = 0;
       
   134         else {
       
   135             _yychar = *_currentChar;
       
   136             if (_yychar == '\n')
       
   137                 pushLineStartOffset();
       
   138         }
       
   139     }
       
   140 
       
   141     void pushLineStartOffset();
       
   142 
       
   143 private:
       
   144     struct Flags {
       
   145         unsigned _isIncremental: 1;
       
   146         unsigned _scanCommentTokens: 1;
       
   147         unsigned _scanKeywords: 1;
       
   148         unsigned _scanAngleStringLiteralTokens: 1;
       
   149         unsigned _qtMocRunEnabled: 1;
       
   150         unsigned _objCEnabled: 1;
       
   151     };
       
   152 
       
   153     TranslationUnit *_translationUnit;
       
   154     const char *_firstChar;
       
   155     const char *_currentChar;
       
   156     const char *_lastChar;
       
   157     const char *_tokenStart;
       
   158     unsigned char _yychar;
       
   159     int _state;
       
   160     union {
       
   161         unsigned _flags;
       
   162         Flags f;
       
   163     };
       
   164     unsigned _currentLine;
       
   165 };
       
   166 
       
   167 } // end of namespace CPlusPlus
       
   168 
       
   169 
       
   170 #endif // CPLUSPLUS_LEXER_H