Simplify a bit
[LibreOffice.git] / idl / inc / lex.hxx
bloba45964f5235342cfb04597e30cd1e05b257473f6
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 #pragma once
22 #include <sal/types.h>
23 #include "hash.hxx"
24 #include <tools/stream.hxx>
25 #include <vector>
26 #include <memory>
28 enum class SVTOKENTYPE { Empty, Comment,
29 Integer, String,
30 Bool, Identifier,
31 Char,
32 EndOfFile, HashId };
34 class SvToken
36 friend class SvTokenStream;
37 sal_uInt64 nLine, nColumn;
38 SVTOKENTYPE nType;
39 OString aString;
40 union
42 sal_uInt64 nLong;
43 bool bBool;
44 char cChar;
45 SvStringHashEntry * pHash;
47 public:
48 SvToken();
49 SvToken( const SvToken & rObj ) = delete;
51 SvToken & operator = ( const SvToken & rObj );
53 OString GetTokenAsString() const;
55 void SetLine( sal_uInt64 nLineP ) { nLine = nLineP; }
56 sal_uInt64 GetLine() const { return nLine; }
58 void SetColumn( sal_uInt64 nColumnP ) { nColumn = nColumnP; }
59 sal_uInt64 GetColumn() const { return nColumn; }
61 bool IsComment() const { return nType == SVTOKENTYPE::Comment; }
62 bool IsInteger() const { return nType == SVTOKENTYPE::Integer; }
63 bool IsString() const { return nType == SVTOKENTYPE::String; }
64 bool IsBool() const { return nType == SVTOKENTYPE::Bool; }
65 bool IsIdentifierHash() const
66 { return nType == SVTOKENTYPE::HashId; }
67 bool IsIdentifier() const
69 return nType == SVTOKENTYPE::Identifier
70 || nType == SVTOKENTYPE::HashId;
72 bool IsChar() const { return nType == SVTOKENTYPE::Char; }
73 bool IsEof() const { return nType == SVTOKENTYPE::EndOfFile; }
75 const OString& GetString() const
77 return IsIdentifierHash()
78 ? pHash->GetName()
79 : aString;
81 sal_uInt64 GetNumber() const { return nLong; }
82 bool GetBool() const { return bBool; }
83 char GetChar() const { return cChar; }
85 void SetHash( SvStringHashEntry * pHashP )
86 { pHash = pHashP; nType = SVTOKENTYPE::HashId; }
87 bool Is( SvStringHashEntry const * pEntry ) const
88 { return IsIdentifierHash() && pHash == pEntry; }
91 inline SvToken::SvToken()
92 : nLine(0)
93 , nColumn(0)
94 , nType( SVTOKENTYPE::Empty )
98 class SvTokenStream
100 sal_uInt64 nLine, nColumn;
101 sal_Int32 nBufPos;
102 char c; // next character
103 static const sal_uInt16 nTabSize = 4; // length of tabulator
104 OString aStrTrue;
105 OString aStrFalse;
106 sal_uInt32 nMaxPos;
108 std::unique_ptr<SvFileStream> pInStream;
109 OUString aFileName;
110 std::vector<std::unique_ptr<SvToken> > aTokList;
111 std::vector<std::unique_ptr<SvToken> >::iterator pCurToken;
113 OString aBufStr;
115 void InitCtor();
117 char GetNextChar();
118 char GetFastNextChar()
120 return (nBufPos < aBufStr.getLength())
121 ? aBufStr[nBufPos++]
122 : '\0';
125 void FillTokenList();
126 sal_uInt64 GetNumber();
127 bool MakeToken( SvToken & );
128 bool IsEof() const { return pInStream->eof(); }
129 void SetMax()
131 sal_uInt32 n = Tell();
132 if( n > nMaxPos )
133 nMaxPos = n;
135 void CalcColumn()
137 // if end of line spare calculation
138 if( 0 != c )
140 sal_Int32 n = 0;
141 nColumn = 0;
142 while( n < nBufPos )
143 nColumn += aBufStr[n++] == '\t' ? nTabSize : 1;
146 public:
147 SvTokenStream( const OUString & rFileName );
148 ~SvTokenStream();
150 const OUString & GetFileName() const { return aFileName; }
151 SvStream & GetStream() { return *pInStream; }
153 SvToken& GetToken_PrevAll()
155 std::vector<std::unique_ptr<SvToken> >::iterator pRetToken = pCurToken;
157 // current iterator always valid
158 if(pCurToken != aTokList.begin())
159 --pCurToken;
161 return *(*pRetToken);
164 SvToken& GetToken_Next()
166 std::vector<std::unique_ptr<SvToken> >::iterator pRetToken = pCurToken++;
168 if (pCurToken == aTokList.end())
169 pCurToken = pRetToken;
171 SetMax();
173 return *(*pRetToken);
176 SvToken& GetToken() const { return *(*pCurToken); }
178 bool ReadIf( char cChar )
180 if( GetToken().IsChar() && cChar == GetToken().GetChar() )
182 GetToken_Next();
183 return true;
185 else
186 return false;
189 void ReadIfDelimiter()
191 if( GetToken().IsChar()
192 && (';' == GetToken().GetChar()
193 || ',' == GetToken().GetChar()) )
195 GetToken_Next();
199 sal_uInt32 Tell() const { return pCurToken-aTokList.begin(); }
201 void Seek( sal_uInt32 nPos )
203 pCurToken = aTokList.begin() + nPos;
204 SetMax();
207 void SeekToMax()
209 pCurToken = aTokList.begin()+nMaxPos;
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */