PDF export of PDF images: preserve hyperlinks
[LibreOffice.git] / idl / inc / lex.hxx
blob977cc2b50a36ce08da8db6389d132a7b3d0a9347
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_IDL_INC_LEX_HXX
21 #define INCLUDED_IDL_INC_LEX_HXX
23 #include <sal/types.h>
24 #include "hash.hxx"
25 #include <tools/stream.hxx>
26 #include <vector>
27 #include <memory>
29 enum class SVTOKENTYPE { Empty, Comment,
30 Integer, String,
31 Bool, Identifier,
32 Char,
33 EndOfFile, HashId };
35 class SvToken
37 friend class SvTokenStream;
38 sal_uLong nLine, nColumn;
39 SVTOKENTYPE nType;
40 OString aString;
41 union
43 sal_uInt64 nLong;
44 bool bBool;
45 char cChar;
46 SvStringHashEntry * pHash;
48 public:
49 SvToken();
50 SvToken( const SvToken & rObj ) = delete;
52 SvToken & operator = ( const SvToken & rObj );
54 OString GetTokenAsString() const;
56 void SetLine( sal_uLong nLineP ) { nLine = nLineP; }
57 sal_uLong GetLine() const { return nLine; }
59 void SetColumn( sal_uLong nColumnP ) { nColumn = nColumnP; }
60 sal_uLong GetColumn() const { return nColumn; }
62 bool IsComment() const { return nType == SVTOKENTYPE::Comment; }
63 bool IsInteger() const { return nType == SVTOKENTYPE::Integer; }
64 bool IsString() const { return nType == SVTOKENTYPE::String; }
65 bool IsBool() const { return nType == SVTOKENTYPE::Bool; }
66 bool IsIdentifierHash() const
67 { return nType == SVTOKENTYPE::HashId; }
68 bool IsIdentifier() const
70 return nType == SVTOKENTYPE::Identifier
71 || nType == SVTOKENTYPE::HashId;
73 bool IsChar() const { return nType == SVTOKENTYPE::Char; }
74 bool IsEof() const { return nType == SVTOKENTYPE::EndOfFile; }
76 const OString& GetString() const
78 return IsIdentifierHash()
79 ? pHash->GetName()
80 : aString;
82 sal_uInt64 GetNumber() const { return nLong; }
83 bool GetBool() const { return bBool; }
84 char GetChar() const { return cChar; }
86 void SetHash( SvStringHashEntry * pHashP )
87 { pHash = pHashP; nType = SVTOKENTYPE::HashId; }
88 bool Is( SvStringHashEntry const * pEntry ) const
89 { return IsIdentifierHash() && pHash == pEntry; }
92 inline SvToken::SvToken()
93 : nLine(0)
94 , nColumn(0)
95 , nType( SVTOKENTYPE::Empty )
99 class SvTokenStream
101 sal_uLong 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_uLong nMaxPos;
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;
114 OString aBufStr;
116 void InitCtor();
118 char GetNextChar();
119 char GetFastNextChar()
121 return (nBufPos < aBufStr.getLength())
122 ? aBufStr[nBufPos++]
123 : '\0';
126 void FillTokenList();
127 sal_uInt64 GetNumber();
128 bool MakeToken( SvToken & );
129 bool IsEof() const { return pInStream->eof(); }
130 void SetMax()
132 sal_uLong n = Tell();
133 if( n > nMaxPos )
134 nMaxPos = n;
136 void CalcColumn()
138 // if end of line spare calculation
139 if( 0 != c )
141 sal_Int32 n = 0;
142 nColumn = 0;
143 while( n < nBufPos )
144 nColumn += aBufStr[n++] == '\t' ? nTabSize : 1;
147 public:
148 SvTokenStream( const OUString & rFileName );
149 ~SvTokenStream();
151 const OUString & GetFileName() const { return aFileName; }
152 SvStream & GetStream() { return *pInStream; }
154 SvToken& GetToken_PrevAll()
156 std::vector<std::unique_ptr<SvToken> >::iterator pRetToken = pCurToken;
158 // current iterator always valid
159 if(pCurToken != aTokList.begin())
160 --pCurToken;
162 return *(*pRetToken);
165 SvToken& GetToken_Next()
167 std::vector<std::unique_ptr<SvToken> >::iterator pRetToken = pCurToken++;
169 if (pCurToken == aTokList.end())
170 pCurToken = pRetToken;
172 SetMax();
174 return *(*pRetToken);
177 SvToken& GetToken() const { return *(*pCurToken); }
179 bool ReadIf( char cChar )
181 if( GetToken().IsChar() && cChar == GetToken().GetChar() )
183 GetToken_Next();
184 return true;
186 else
187 return false;
190 void ReadIfDelimiter()
192 if( GetToken().IsChar()
193 && (';' == GetToken().GetChar()
194 || ',' == GetToken().GetChar()) )
196 GetToken_Next();
200 sal_uInt32 Tell() const { return pCurToken-aTokList.begin(); }
202 void Seek( sal_uInt32 nPos )
204 pCurToken = aTokList.begin() + nPos;
205 SetMax();
208 void SeekToMax()
210 pCurToken = aTokList.begin()+nMaxPos;
215 #endif // INCLUDED_IDL_INC_LEX_HXX
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */