Files
libreoffice/idl/source/cmptools/lex.cxx

370 lines
9.2 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 15:33:13 +00:00
#include <ctype.h>
#include <stdio.h>
#include <hash.hxx>
#include <lex.hxx>
#include <globals.hxx>
2011-09-20 23:30:51 +01:00
#include <rtl/strbuf.hxx>
2000-09-18 15:33:13 +00:00
2011-09-19 23:44:54 +01:00
rtl::OString SvToken::GetTokenAsString() const
2000-09-18 15:33:13 +00:00
{
2011-09-19 23:44:54 +01:00
rtl::OString aStr;
2000-09-18 15:33:13 +00:00
switch( nType )
{
case SVTOKEN_EMPTY:
break;
case SVTOKEN_COMMENT:
aStr = aString;
break;
case SVTOKEN_INTEGER:
aStr = OString::number(nLong);
2000-09-18 15:33:13 +00:00
break;
case SVTOKEN_STRING:
aStr = aString;
break;
case SVTOKEN_BOOL:
aStr = bBool ? "TRUE" : "FALSE";
break;
case SVTOKEN_IDENTIFIER:
aStr = aString;
break;
case SVTOKEN_CHAR:
2011-09-19 23:44:54 +01:00
aStr = rtl::OString(cChar);
2000-09-18 15:33:13 +00:00
break;
case SVTOKEN_RTTIBASE:
aStr = "RTTIBASE";
2000-09-18 15:33:13 +00:00
break;
case SVTOKEN_EOF:
case SVTOKEN_HASHID:
2000-09-18 15:33:13 +00:00
break;
}
return aStr;
}
SvToken::SvToken( const SvToken & rObj )
{
nLine = rObj.nLine;
nColumn = rObj.nColumn;
nType = rObj.nType;
aString = rObj.aString;
nLong = rObj.nLong;
2000-09-18 15:33:13 +00:00
}
SvToken & SvToken::operator = ( const SvToken & rObj )
{
if( this != &rObj )
{
nLine = rObj.nLine;
nColumn = rObj.nColumn;
nType = rObj.nType;
aString = rObj.aString;
nLong = rObj.nLong;
2000-09-18 15:33:13 +00:00
}
return *this;
}
void SvTokenStream::InitCtor()
{
2011-11-19 21:02:35 +00:00
aStrTrue = rtl::OString(RTL_CONSTASCII_STRINGPARAM("TRUE"));
aStrFalse = rtl::OString(RTL_CONSTASCII_STRINGPARAM("FALSE"));
2000-09-18 15:33:13 +00:00
nLine = nColumn = 0;
nBufPos = 0;
nTabSize = 4;
nMaxPos = 0;
c = GetNextChar();
FillTokenList();
}
SvTokenStream::SvTokenStream( const String & rFileName )
: pInStream( new SvFileStream( rFileName, STREAM_STD_READ | STREAM_NOCREATE ) )
2000-09-18 15:33:13 +00:00
, rInStream( *pInStream )
, aFileName( rFileName )
2000-09-18 15:33:13 +00:00
{
InitCtor();
}
SvTokenStream::SvTokenStream( SvStream & rStream, const String & rFileName )
: pInStream( NULL )
2000-09-18 15:33:13 +00:00
, rInStream( rStream )
, aFileName( rFileName )
2000-09-18 15:33:13 +00:00
{
InitCtor();
}
SvTokenStream::~SvTokenStream()
{
delete pInStream;
}
void SvTokenStream::FillTokenList()
{
SvToken * pToken = new SvToken();
aTokList.push_back(pToken);
2000-09-18 15:33:13 +00:00
do
{
if( !MakeToken( *pToken ) )
{
if (!aTokList.empty())
2000-09-18 15:33:13 +00:00
{
*pToken = SvToken();
boost::ptr_vector<SvToken>::const_iterator it = aTokList.begin();
pToken->SetLine(it->GetLine());
pToken->SetColumn(it->GetColumn());
2000-09-18 15:33:13 +00:00
}
break;
}
else if( pToken->IsComment() )
*pToken = SvToken();
else if( pToken->IsEof() )
break;
else
{
pToken = new SvToken();
aTokList.push_back(pToken);
2000-09-18 15:33:13 +00:00
}
}
while( !pToken->IsEof() );
pCurToken = aTokList.begin();
2000-09-18 15:33:13 +00:00
}
int SvTokenStream::GetNextChar()
{
int nChar;
2011-11-13 23:17:06 +00:00
if( aBufStr.getLength() < nBufPos )
2000-09-18 15:33:13 +00:00
{
if( rInStream.ReadLine( aBufStr ) )
{
nLine++;
nColumn = 0;
nBufPos = 0;
}
else
{
2011-11-13 23:17:06 +00:00
aBufStr = rtl::OString();
2000-09-18 15:33:13 +00:00
nColumn = 0;
nBufPos = 0;
return '\0';
}
}
2011-11-13 23:17:06 +00:00
nChar = aBufStr[nBufPos++];
2000-09-18 15:33:13 +00:00
nColumn += nChar == '\t' ? nTabSize : 1;
return nChar;
}
sal_uLong SvTokenStream::GetNumber()
2000-09-18 15:33:13 +00:00
{
sal_uLong l = 0;
2000-09-18 15:33:13 +00:00
short nLog = 10;
if( '0' == c )
{
c = GetFastNextChar();
if( 'x' == c )
{
nLog = 16;
c = GetFastNextChar();
}
};
if( nLog == 16 )
{
while( isxdigit( c ) )
{
if( isdigit( c ) )
l = l * nLog + (c - '0');
else
l = l * nLog + (toupper( c ) - 'A' + 10 );
c = GetFastNextChar();
}
}
else
{
while( isdigit( c ) || 'x' == c )
{
l = l * nLog + (c - '0');
c = GetFastNextChar();
}
}
return( l );
}
sal_Bool SvTokenStream::MakeToken( SvToken & rToken )
2000-09-18 15:33:13 +00:00
{
do
{
if( 0 == c )
c = GetNextChar();
// skip whitespace
2000-09-18 15:33:13 +00:00
while( isspace( c ) || 26 == c )
{
c = GetFastNextChar();
nColumn += c == '\t' ? nTabSize : 1;
}
}
while( 0 == c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
sal_uLong nLastLine = nLine;
sal_uLong nLastColumn = nColumn;
// comment
2000-09-18 15:33:13 +00:00
if( '/' == c )
{
// time optimization, no comments
int c1 = c;
2000-09-18 15:33:13 +00:00
c = GetFastNextChar();
if( '/' == c )
{
while( '\0' != c )
{
c = GetFastNextChar();
}
c = GetNextChar();
rToken.nType = SVTOKEN_COMMENT;
}
else if( '*' == c )
{
c = GetFastNextChar();
do
{
while( '*' != c )
{
if( '\0' == c )
{
c = GetNextChar();
if( IsEof() )
return sal_False;
2000-09-18 15:33:13 +00:00
}
else
c = GetFastNextChar();
}
c = GetFastNextChar();
}
while( '/' != c && !IsEof() && ( SVSTREAM_OK == rInStream.GetError() ) );
if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
return sal_False;
2000-09-18 15:33:13 +00:00
c = GetNextChar();
rToken.nType = SVTOKEN_COMMENT;
CalcColumn();
}
else
{
rToken.nType = SVTOKEN_CHAR;
rToken.cChar = (char)c1;
}
}
else if( c == '"' )
{
2011-09-20 23:30:51 +01:00
rtl::OStringBuffer aStr;
sal_Bool bDone = sal_False;
2000-09-18 15:33:13 +00:00
while( !bDone && !IsEof() && c )
{
c = GetFastNextChar();
if( '\0' == c )
{
// read strings beyond end of line
2011-09-20 23:30:51 +01:00
aStr.append('\n');
2000-09-18 15:33:13 +00:00
c = GetNextChar();
if( IsEof() )
return sal_False;
2000-09-18 15:33:13 +00:00
}
if( c == '"' )
{
c = GetFastNextChar();
if( c == '"' )
{
2011-09-20 23:30:51 +01:00
aStr.append('"');
aStr.append('"');
2000-09-18 15:33:13 +00:00
}
else
bDone = sal_True;
2000-09-18 15:33:13 +00:00
}
else if( c == '\\' )
{
2011-09-20 23:30:51 +01:00
aStr.append('\\');
2000-09-18 15:33:13 +00:00
c = GetFastNextChar();
if( c )
2011-09-20 23:30:51 +01:00
aStr.append(static_cast<char>(c));
2000-09-18 15:33:13 +00:00
}
else
2011-09-20 23:30:51 +01:00
aStr.append(static_cast<char>(c));
2000-09-18 15:33:13 +00:00
}
if( IsEof() || ( SVSTREAM_OK != rInStream.GetError() ) )
return sal_False;
2000-09-18 15:33:13 +00:00
rToken.nType = SVTOKEN_STRING;
2011-09-20 23:30:51 +01:00
rToken.aString = aStr.makeStringAndClear();
2000-09-18 15:33:13 +00:00
}
else if( isdigit( c ) )
{
rToken.nType = SVTOKEN_INTEGER;
rToken.nLong = GetNumber();
}
else if( isalpha (c) || (c == '_') )
{
2011-09-20 23:30:51 +01:00
rtl::OStringBuffer aBuf;
2000-09-18 15:33:13 +00:00
while( isalnum( c ) || c == '_' )
{
2011-09-20 23:30:51 +01:00
aBuf.append(static_cast<char>(c));
2000-09-18 15:33:13 +00:00
c = GetFastNextChar();
}
2012-01-22 07:29:33 +00:00
rtl::OString aStr = aBuf.makeStringAndClear();
if( aStr.equalsIgnoreAsciiCase( aStrTrue ) )
2000-09-18 15:33:13 +00:00
{
rToken.nType = SVTOKEN_BOOL;
rToken.bBool = sal_True;
2000-09-18 15:33:13 +00:00
}
2012-01-22 07:29:33 +00:00
else if( aStr.equalsIgnoreAsciiCase( aStrFalse ) )
2000-09-18 15:33:13 +00:00
{
rToken.nType = SVTOKEN_BOOL;
rToken.bBool = sal_False;
2000-09-18 15:33:13 +00:00
}
else
{
sal_uInt32 nHashId;
2000-09-18 15:33:13 +00:00
if( IDLAPP->pHashTable->Test( aStr, &nHashId ) )
rToken.SetHash( IDLAPP->pHashTable->Get( nHashId ) );
else
{
rToken.nType = SVTOKEN_IDENTIFIER;
rToken.aString = aStr;
}
}
}
else if( IsEof() )
{
rToken.nType = SVTOKEN_EOF;
}
else
{
rToken.nType = SVTOKEN_CHAR;
rToken.cChar = (char)c;
c = GetFastNextChar();
}
rToken.SetLine( nLastLine );
rToken.SetColumn( nLastColumn );
return rInStream.GetError() == SVSTREAM_OK;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */