1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #ifndef INCLUDED_IDL_INC_LEX_HXX
21 #define INCLUDED_IDL_INC_LEX_HXX
22
23 #include <sal/types.h>
24 #include "hash.hxx"
25 #include <tools/stream.hxx>
26 #include <vector>
27 #include <memory>
28
29 enum class SVTOKENTYPE { Empty, Comment,
30 Integer, String,
31 Bool, Identifier,
32 Char,
33 EndOfFile, HashId };
34
35 class SvToken
36 {
37 friend class SvTokenStream;
38 sal_uInt64 nLine, nColumn;
39 SVTOKENTYPE nType;
40 OString aString;
41 union
42 {
43 sal_uInt64 nLong;
44 bool bBool;
45 char cChar;
46 SvStringHashEntry * pHash;
47 };
48 public:
49 SvToken();
50 SvToken( const SvToken & rObj ) = delete;
51
52 SvToken & operator = ( const SvToken & rObj );
53
54 OString GetTokenAsString() const;
55
SetLine(sal_uInt64 nLineP)56 void SetLine( sal_uInt64 nLineP ) { nLine = nLineP; }
GetLine() const57 sal_uInt64 GetLine() const { return nLine; }
58
SetColumn(sal_uInt64 nColumnP)59 void SetColumn( sal_uInt64 nColumnP ) { nColumn = nColumnP; }
GetColumn() const60 sal_uInt64 GetColumn() const { return nColumn; }
61
IsComment() const62 bool IsComment() const { return nType == SVTOKENTYPE::Comment; }
IsInteger() const63 bool IsInteger() const { return nType == SVTOKENTYPE::Integer; }
IsString() const64 bool IsString() const { return nType == SVTOKENTYPE::String; }
IsBool() const65 bool IsBool() const { return nType == SVTOKENTYPE::Bool; }
IsIdentifierHash() const66 bool IsIdentifierHash() const
67 { return nType == SVTOKENTYPE::HashId; }
IsIdentifier() const68 bool IsIdentifier() const
69 {
70 return nType == SVTOKENTYPE::Identifier
71 || nType == SVTOKENTYPE::HashId;
72 }
IsChar() const73 bool IsChar() const { return nType == SVTOKENTYPE::Char; }
IsEof() const74 bool IsEof() const { return nType == SVTOKENTYPE::EndOfFile; }
75
GetString() const76 const OString& GetString() const
77 {
78 return IsIdentifierHash()
79 ? pHash->GetName()
80 : aString;
81 }
GetNumber() const82 sal_uInt64 GetNumber() const { return nLong; }
GetBool() const83 bool GetBool() const { return bBool; }
GetChar() const84 char GetChar() const { return cChar; }
85
SetHash(SvStringHashEntry * pHashP)86 void SetHash( SvStringHashEntry * pHashP )
87 { pHash = pHashP; nType = SVTOKENTYPE::HashId; }
Is(SvStringHashEntry const * pEntry) const88 bool Is( SvStringHashEntry const * pEntry ) const
89 { return IsIdentifierHash() && pHash == pEntry; }
90 };
91
SvToken()92 inline SvToken::SvToken()
93 : nLine(0)
94 , nColumn(0)
95 , nType( SVTOKENTYPE::Empty )
96 {
97 }
98
99 class SvTokenStream
100 {
101 sal_uInt64 nLine, nColumn;
102 sal_Int32 nBufPos;
103 char c; // next character
104 static const sal_uInt16 nTabSize = 4; // length of tabulator
105 OString aStrTrue;
106 OString aStrFalse;
107 sal_uInt32 nMaxPos;
108
109 std::unique_ptr<SvFileStream> pInStream;
110 OUString aFileName;
111 std::vector<std::unique_ptr<SvToken> > aTokList;
112 std::vector<std::unique_ptr<SvToken> >::iterator pCurToken;
113
114 OString aBufStr;
115
116 void InitCtor();
117
118 char GetNextChar();
GetFastNextChar()119 char GetFastNextChar()
120 {
121 return (nBufPos < aBufStr.getLength())
122 ? aBufStr[nBufPos++]
123 : '\0';
124 }
125
126 void FillTokenList();
127 sal_uInt64 GetNumber();
128 bool MakeToken( SvToken & );
IsEof() const129 bool IsEof() const { return pInStream->eof(); }
SetMax()130 void SetMax()
131 {
132 sal_uInt32 n = Tell();
133 if( n > nMaxPos )
134 nMaxPos = n;
135 }
CalcColumn()136 void CalcColumn()
137 {
138 // if end of line spare calculation
139 if( 0 != c )
140 {
141 sal_Int32 n = 0;
142 nColumn = 0;
143 while( n < nBufPos )
144 nColumn += aBufStr[n++] == '\t' ? nTabSize : 1;
145 }
146 }
147 public:
148 SvTokenStream( const OUString & rFileName );
149 ~SvTokenStream();
150
GetFileName() const151 const OUString & GetFileName() const { return aFileName; }
GetStream()152 SvStream & GetStream() { return *pInStream; }
153
GetToken_PrevAll()154 SvToken& GetToken_PrevAll()
155 {
156 std::vector<std::unique_ptr<SvToken> >::iterator pRetToken = pCurToken;
157
158 // current iterator always valid
159 if(pCurToken != aTokList.begin())
160 --pCurToken;
161
162 return *(*pRetToken);
163 }
164
GetToken_Next()165 SvToken& GetToken_Next()
166 {
167 std::vector<std::unique_ptr<SvToken> >::iterator pRetToken = pCurToken++;
168
169 if (pCurToken == aTokList.end())
170 pCurToken = pRetToken;
171
172 SetMax();
173
174 return *(*pRetToken);
175 }
176
GetToken() const177 SvToken& GetToken() const { return *(*pCurToken); }
178
ReadIf(char cChar)179 bool ReadIf( char cChar )
180 {
181 if( GetToken().IsChar() && cChar == GetToken().GetChar() )
182 {
183 GetToken_Next();
184 return true;
185 }
186 else
187 return false;
188 }
189
ReadIfDelimiter()190 void ReadIfDelimiter()
191 {
192 if( GetToken().IsChar()
193 && (';' == GetToken().GetChar()
194 || ',' == GetToken().GetChar()) )
195 {
196 GetToken_Next();
197 }
198 }
199
Tell() const200 sal_uInt32 Tell() const { return pCurToken-aTokList.begin(); }
201
Seek(sal_uInt32 nPos)202 void Seek( sal_uInt32 nPos )
203 {
204 pCurToken = aTokList.begin() + nPos;
205 SetMax();
206 }
207
SeekToMax()208 void SeekToMax()
209 {
210 pCurToken = aTokList.begin()+nMaxPos;
211 }
212 };
213
214
215 #endif // INCLUDED_IDL_INC_LEX_HXX
216
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
218