| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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 tools applications of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | /* | 
|---|
| 43 | codechunk.cpp | 
|---|
| 44 | */ | 
|---|
| 45 |  | 
|---|
| 46 | #include <qregexp.h> | 
|---|
| 47 | #include <qstringlist.h> | 
|---|
| 48 |  | 
|---|
| 49 | #include "codechunk.h" | 
|---|
| 50 |  | 
|---|
| 51 | QT_BEGIN_NAMESPACE | 
|---|
| 52 |  | 
|---|
| 53 | enum { Other, Alnum, Gizmo, Comma, LParen, RParen, RAngle, Colon }; | 
|---|
| 54 |  | 
|---|
| 55 | // entries 128 and above are Other | 
|---|
| 56 | static const int charCategory[256] = { | 
|---|
| 57 | Other,  Other,  Other,  Other,  Other,  Other,  Other,  Other, | 
|---|
| 58 | Other,  Other,  Other,  Other,  Other,  Other,  Other,  Other, | 
|---|
| 59 | Other,  Other,  Other,  Other,  Other,  Other,  Other,  Other, | 
|---|
| 60 | Other,  Other,  Other,  Other,  Other,  Other,  Other,  Other, | 
|---|
| 61 | //          !       "       #       $       %       &       ' | 
|---|
| 62 | Other,  Other,  Other,  Other,  Other,  Gizmo,  Gizmo,  Other, | 
|---|
| 63 | //  (       )       *       +       ,       -       .       / | 
|---|
| 64 | LParen, RParen, Gizmo,  Gizmo,  Comma,  Other,  Other,  Gizmo, | 
|---|
| 65 | //  0       1       2       3       4       5       6       7 | 
|---|
| 66 | Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum, | 
|---|
| 67 | //  8       9       :       ;       <       =       >       ? | 
|---|
| 68 | Alnum,  Alnum,  Colon,  Other,  Other,  Gizmo,  RAngle, Gizmo, | 
|---|
| 69 | //  @       A       B       C       D       E       F       G | 
|---|
| 70 | Other,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum, | 
|---|
| 71 | //  H       I       J       K       L       M       N       O | 
|---|
| 72 | Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum, | 
|---|
| 73 | //  P       Q       R       S       T       U       V       W | 
|---|
| 74 | Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum, | 
|---|
| 75 | //  X       Y       Z       [       \       ]       ^       _ | 
|---|
| 76 | Alnum,  Alnum,  Alnum,  Other,  Other,  Other,  Gizmo,  Alnum, | 
|---|
| 77 | //  `       a       b       c       d       e       f       g | 
|---|
| 78 | Other,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum, | 
|---|
| 79 | //  h       i       j       k       l       m       n       o | 
|---|
| 80 | Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum, | 
|---|
| 81 | //  p       q       r       s       t       u       v       w | 
|---|
| 82 | Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum,  Alnum, | 
|---|
| 83 | //  x       y       z       {       |       }       ~ | 
|---|
| 84 | Alnum,  Alnum,  Alnum,  LParen, Gizmo,  RParen, Other,  Other | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | static const bool needSpace[8][8] = { | 
|---|
| 88 | /*        [      a      +      ,      (       )     >      :  */ | 
|---|
| 89 | /* [ */ { false, false, false, false, false, true,  false, false }, | 
|---|
| 90 | /* a */ { false, true,  true,  false, false, true,  false, false }, | 
|---|
| 91 | /* + */ { false, true,  false, false, false, true,  false, true }, | 
|---|
| 92 | /* , */ { true,  true,  true,  true,  true,  true,  true,  true }, | 
|---|
| 93 | /* ( */ { true,  true,  true,  false, true,  false, true,  true }, | 
|---|
| 94 | /* ) */ { true,  true,  true,  false, true,  true,  true,  true }, | 
|---|
| 95 | /* > */ { true,  true,  true,  false, true,  true,  true,  false }, | 
|---|
| 96 | /* : */ { false, false, true,  true,  true,  true,  true,  false } | 
|---|
| 97 | }; | 
|---|
| 98 |  | 
|---|
| 99 | static int category( QChar ch ) | 
|---|
| 100 | { | 
|---|
| 101 | return charCategory[(int)ch.toLatin1()]; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | CodeChunk::CodeChunk() | 
|---|
| 105 | : hotspot( -1 ) | 
|---|
| 106 | { | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | CodeChunk::CodeChunk( const QString& str ) | 
|---|
| 110 | : s( str ), hotspot( -1 ) | 
|---|
| 111 | { | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void CodeChunk::append( const QString& lexeme ) | 
|---|
| 115 | { | 
|---|
| 116 | if ( !s.isEmpty() && !lexeme.isEmpty() ) { | 
|---|
| 117 | /* | 
|---|
| 118 | Should there be a space or not between the code chunk so far and the | 
|---|
| 119 | new lexeme? | 
|---|
| 120 | */ | 
|---|
| 121 | int cat1 = category(s.at(s.size() - 1)); | 
|---|
| 122 | int cat2 = category(lexeme[0]); | 
|---|
| 123 | if ( needSpace[cat1][cat2] ) | 
|---|
| 124 | s += QLatin1Char( ' ' ); | 
|---|
| 125 | } | 
|---|
| 126 | s += lexeme; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | void CodeChunk::appendHotspot() | 
|---|
| 130 | { | 
|---|
| 131 | /* | 
|---|
| 132 | The first hotspot is the right one. | 
|---|
| 133 | */ | 
|---|
| 134 | if ( hotspot == -1 ) | 
|---|
| 135 | hotspot = s.length(); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | QString CodeChunk::toString() const | 
|---|
| 139 | { | 
|---|
| 140 | return s; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | QStringList CodeChunk::toPath() const | 
|---|
| 144 | { | 
|---|
| 145 | QString t = s; | 
|---|
| 146 | t.remove(QRegExp(QLatin1String("<([^<>]|<([^<>]|<[^<>]*>)*>)*>"))); | 
|---|
| 147 | return t.split(QLatin1String("::")); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | QT_END_NAMESPACE | 
|---|