ot_lex.cpp
来自「在手机操作系统symbina上使用的一个脚本扩展语言的代码实现,可以参考用于自己」· C++ 代码 · 共 861 行 · 第 1/2 页
CPP
861 行
// Just seen what purports to be a logical device i.e A.
if (logicalDevice!=TOplField::EBadDevice) // For the second time e.g. A.B.
User::Leave(EErrBadFieldName);
// Logical devices must be a single letter (A-D on OPL1993, A-Z on Opler1)
if (iText.Length()!=2)
User::Leave(EErrBadLogicalDevice);
CheckDeviceL(iText[0]-'A');
logicalDevice=TOplField::TDevice(iText[0]-'A');
// ..it must be followed by an identifier
if (!iLex.Peek().IsAlpha())
User::Leave(EErrBadFieldName);
}
}
void COplLexerBase::ProcByNameL()
//
// Checks out the proc by name @ construct
//
{
iLex.Get(); // absorb that @
if (iLex.Eos())
SyntaxErrorL();
switch (iLex.Get())
{
case '%':
iType=TOplToken::EWord;
break;
case '$':
iType=TOplToken::EString;
break;
case '&':
iType=TOplToken::ELong;
break;
default:
iType=TOplToken::EReal;
iLex.UnGet();
break;
}
if (iLex.Eos() || iLex.Get()!='(')
SyntaxErrorL();
iToken=TOplToken::EProcByName;
}
//
// Couple of structures used for character checking
//
struct TOplCharMap
{
TUint iChar;
TOplToken::TValue iToken;
};
struct TOplDoubleMap
{
TOplToken::TValue iOldToken;
TUint iChar;
TOplToken::TValue iNewToken;
};
LOCAL_D const TOplCharMap characterMaps[]=
{
{'<',TOplToken::ELessThan},
{'>',TOplToken::EGreaterThan},
{'=',TOplToken::EEqual},
{'+',TOplToken::EPlus},
{'-',TOplToken::EMinus},
{'*',TOplToken::EMultiply},
{'/',TOplToken::EDivide},
{'(',TOplToken::EOpenBracket},
{')',TOplToken::ECloseBracket},
{'%',TOplToken::EPerc},
{',',TOplToken::EComma},
{';',TOplToken::ESemiColon},
{':',TOplToken::EColon},
{'#',TOplToken::EHash},
{0,TOplToken::EStartOfExpression} // Null list terminator
};
LOCAL_D const TOplDoubleMap doubleMaps[]=
{
{TOplToken::ELessThan,'=',TOplToken::ELessThanEq},
{TOplToken::ELessThan,'>',TOplToken::ENotEqual},
{TOplToken::EGreaterThan,'=',TOplToken::EGreaterThanEq},
{TOplToken::EMultiply,'*',TOplToken::EPower},
{TOplToken::EStartOfExpression,0,TOplToken::EStartOfExpression} // null list terminator
};
void COplLexerBase::CharacterL()
//
// The operators && punctuation - but generally all the left over stuff.
// These are just checked in lookup tables.
//
{
TChar next=iLex.Get();
// Look up list of acceptable characters
const TOplCharMap *charMap=characterMaps;
while (charMap->iChar!=0)
{
if (charMap->iChar==next)
break;
charMap++;
}
// If it's not in that list, it's no good.
if (charMap->iChar==0)
User::Leave(EErrBadCharacter);
// OK so far
iToken=charMap->iToken;
// Now check for double character pairs e.g. '<='
next=iLex.Peek();
const TOplDoubleMap *dblMap=doubleMaps;
while (dblMap->iChar!=0)
{
if (iToken==dblMap->iOldToken && dblMap->iChar==next)
{
iLex.Get();
iToken=dblMap->iNewToken;
break;
}
dblMap++;
}
}
EXPORT_C TInt COplLexerBase::TokenOffset() const
{
return iLex.MarkedOffset();
}
void COplLexerBase::CheckDeviceL(TUint aDevice)
{
TUint maxDevice=KOplMaxLogicalDevice;
if (iTarget==EOplTargetOpl1993)
maxDevice=KOplMaxLogicalDeviceOpl1993;
if (aDevice>maxDevice)
User::Leave(EErrBadLogicalDevice);
}
/////////////////////////////////////////////////////////////////////////
//
// COplCalcLexer - Lexical scanner for EVAL
//
/////////////////////////////////////////////////////////////////////////
EXPORT_C COplLineLexer* COplLineLexer::NewL()
//
// Forces the lexer to be allocated from the heap.
//
{
return new(ELeave) COplLineLexer();
}
COplLineLexer::COplLineLexer()
{
TLocale iLocale;
iDecimalSeparator=iLocale.DecimalSeparator();
}
void COplLineLexer::DecNumberL(TChar)
//
// Lexes out a number - i.e. something starting with either
// a digit or a decimal separator.
// All calculator decimal numbers are TReal's
// and use the locale's decimal separator.
//
{
GetRealL();
}
void COplLineLexer::AlphaL()
//
// Filters out all the identifiers
//
{
COplLexerBase::AlphaL(); // Something to work with
// Only interested in filtering identifiers and keywords - e.g. AND, OR & NOT & the functions go straight through
/* if ((iToken.Class()==TOplToken::EOperator) && (iToken.Class()!=TOplToken::ECall))
User::Leave(EErrBadIdentifier);
*/ }
/////////////////////////////////////////////////////////////////////////
//
// COplModuleLexer - Lexical scanner for OPL modules
//
/////////////////////////////////////////////////////////////////////////
EXPORT_C COplModuleLexer *COplModuleLexer::NewL()
{
COplModuleLexer *pL=new(ELeave) COplModuleLexer();
CleanupStack::PushL(pL);
pL->ConstructL();
CleanupStack::Pop();
return pL;
}
EXPORT_C COplModuleLexer::~COplModuleLexer()
//
// Destroy any include list
//
{
Reset();
delete iSources;
}
EXPORT_C void COplModuleLexer::OpenL(MTextSourceSystem& aSourceSystem,
const TDesC& aFileName,
TSourceTranslateError& anError)
//
// Resets things and opens up the root file.
//
{
__ASSERT_ALWAYS(iTextSystem==NULL,Panic(EOpltLexerReentrantOpen));
iTextSystem=&aSourceSystem;
iError=&anError;
IncludeL(aFileName);
}
void LexCleanupInclude(TAny *lexer)
{
((COplModuleLexer *)lexer)->CloseCurrentSource(ETrue);
}
EXPORT_C void COplModuleLexer::IncludeL(const TDesC& aFileName)
//
// Opens a new file as the source
//
{
// Fix OPL HA27: Parse filename to be opened with the current file name
// TParse includeName;
// User::LeaveIfError(includeName.Set(aFileName,iCurrentSource.iName,NULL));
TFileName includeName=aFileName;
iCurrentSource.iPosition=TokenOffset(); // Record where we need to resume
iSources->PushL(&iCurrentSource);
Mem::FillZ(&iCurrentSource,sizeof(iCurrentSource));
CleanupStack::PushL(TCleanupItem(LexCleanupInclude,this));
User::LeaveIfError(iTextSystem->OpenSource(includeName,iCurrentSource.iSource));
// Copy the file name
iCurrentSource.iName=includeName/*.FullName()*/.AllocL();
// Open the new source
//hp:was
//User::LeaveIfError(iTextSystem->OpenSource(aFileName,iCurrentSource.iSource));
// User::LeaveIfError(iTextSystem->OpenSource(includeName.FullName(),iCurrentSource.iSource));
// Seed the read buffer
User::LeaveIfError(iCurrentSource.iSource->Read(iReadBuf));
iNewLineNeeded=ETrue; // So we start by finding where the next line is
iCurrentSource.iLineNumber=-1; // And that will be line 0 in the file.
iNextLineOffset=0; // And it starts at the beginning of the buffer
((TSourceTranslateError *)iError)->SetSource(*iCurrentSource.iName); // Keep track of where any error occurs
CleanupStack::Pop();
}
EXPORT_C void COplModuleLexer::Reset()
//
// Closes all the open file(s)
//
{
if (iSources)
{
if (iSources->Depth()) // Got as far as opening
while (!CloseCurrentSource(EFalse))
; // Close all the open files
iSources->Reset();
}
iTextSystem=NULL;
}
EXPORT_C TOplToken COplModuleLexer::LexL()
//
// Puts a preamble on Lex(), to deal with the new line stuff
//
{
if (iNewLineNeeded) // finished with the last line
{
// Find out where the next line runs to in the buffer
TPtrC remainder=iReadBuf.Mid(iNextLineOffset);
TInt lineEnd=remainder.Locate(TChar(KTextTranLineDelimiter));
// If the end of the next line isn't in the buffer - read in the whole of the line
if (lineEnd==KErrNotFound)
{
// Cannot disturb iLex or iFilePos or iLine pos until we're safe
// so that any errors here get reported in the right place
TInt bufStart;
TInt ret;
FOREVER // Grab the next chunk from the file
{
bufStart=iCurrentSource.iPosition+iNextLineOffset; // Where the next line starts in the file
ret=iCurrentSource.iSource->Read(iReadBuf,bufStart);
if (ret!=KErrEof || InRootSource())
break;
CloseCurrentSource(ETrue);
}
if (ret==KErrEof)
return iToken=TOplToken::EEof;
User::LeaveIfError(ret);
lineEnd=iReadBuf.Locate(TChar(KTextTranLineDelimiter));
if (lineEnd==KErrNotFound)
User::Leave(EErrLineTooLong);
iCurrentSource.iPosition=bufStart;
iNextLineOffset=0;
}
iLex=iReadBuf.Mid(iNextLineOffset,lineEnd);
iLineOffset=iNextLineOffset;
iNextLineOffset+=lineEnd+1; // skip over the zero terminator
iNewLineNeeded=EFalse;
iCurrentSource.iLineNumber++;
}
COplLexerBase::LexL();
if (iToken==TOplToken::EEos)
iNewLineNeeded=ETrue;
return iToken;
}
EXPORT_C void COplModuleLexer::UnLex()
//
// Adds the necessary to UnLex() to deal with multiple lines
//
{
if (iToken!=TOplToken::EEof)
{
COplLexerBase::UnLex();
iNewLineNeeded=EFalse; // In case last was EOS
}
}
EXPORT_C void COplModuleLexer::UnGetToMark()
//
// Restores where we were in the line
//
{
COplLexerBase::UnGetToMark();
iNewLineNeeded=EFalse;
}
EXPORT_C void COplModuleLexer::GotoPosition(TInt aPos)
//
// Sets up the lexer to resume continue from the passed position
//
{
iNewLineNeeded=ETrue; // So we will force a read
iReadBuf.SetLength(0);
iCurrentSource.iPosition=aPos; // from aPos
iNextLineOffset=0;
}
EXPORT_C TInt COplModuleLexer::TokenOffset() const
//
// Returns the position relative to the file of the current lex position
//
{
// TInt pos=iCurrentSource.iPosition+iLineOffset+COplLexerBase::TokenOffset();
// if (iNewLineNeeded && pos!=0)
// pos++;
// return pos;
return iCurrentSource.iPosition+iLineOffset+COplLexerBase::TokenOffset();
}
COplModuleLexer::COplModuleLexer()
//
// Safe first stage construction
//
{
}
TBool COplModuleLexer::InRootSource() const
//
// Can tell we're in the root source if the top of the source stack is NULL
//
{
return iSources->Top().iSource==NULL;
}
void COplModuleLexer::ConstructL()
//
// Dangerous second stage construction
//
{
iSources=new(ELeave) CTranStackFlat<TOpenSource>(KOplModLexSourceGran);
}
TBool COplModuleLexer::CloseCurrentSource(TBool aRestoreName)
//
// Closes down the current source file and pops the old one off the stack
//
{
TBool wasLast=ETrue;
delete iCurrentSource.iName;
if (iCurrentSource.iSource)
iCurrentSource.iSource->Close();
// And recover the previous (possibly NULL) source
iSources->Pop(&iCurrentSource);
// If it wasn't the last we need to resume where we were
if (iCurrentSource.iSource)
{
iCurrentSource.iLineNumber--; // So the reasd doesn't increment the line count.
GotoPosition(iCurrentSource.iPosition); // Get back to where we left off
if (aRestoreName)
((TSourceTranslateError *)iError)->SetSource(*iCurrentSource.iName); // Keep track of where any error occurs
wasLast=EFalse;
}
return wasLast;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?