tmodlex.cpp
来自「在手机操作系统symbina上使用的一个脚本扩展语言的代码实现,可以参考用于自己」· C++ 代码 · 共 689 行 · 第 1/2 页
CPP
689 行
test.Next(_L("Token offset"));
TInt pos=iLex->TokenOffset();
test(iLex->LineNumber()==1);
test.Next(_L("SetLineNumber"));
iLex->SetLineNumber(1234);
test(iLex->LineNumber()==1234);
test.Next(_L("Goto file position"));
iLex->GotoPosition(pos); // Back to where we were
test(MarkLex()==TOplToken::EConstant);
test.Next(_L("SavePos/RestorePos"));
iLex->Mark();
test(MarkLex()==TOplToken::ESimple);
test(MarkLex()==TOplToken::EEos);
iLex->UnGetToMark();
test(MarkLex()==TOplToken::ESimple);
test(MarkLex()==TOplToken::EEos);
test.Next(_L("Token type"));
test(MarkLex()==TOplToken::EConstant);
test(iLex->Type()==TOplToken::EReal);
test(MarkLex()==TOplToken::ESimple);
test(iLex->Type()==TOplToken::EString);
test.Next(_L("Set Target"));
iLex->SetTarget(EOplTargetOpl1993);
test.Next(_L("End of file"));
test(MarkLex()==TOplToken::EEos);
test(MarkLex()==TOplToken::EEof);
test(MarkLex()==TOplToken::EEof);
DestroyLex();
test.End();
__UHEAP_MARKEND;
}
void TTestModLex::Test2()
//
// Alloc heaven in construction
// Fails each alloc in turn.
//
{
test.Start(_L("Constructors"));
TUint failNum=1;
for (;;failNum++)
{
__UHEAP_MARK;
__UHEAP_SETFAIL(RHeap::EDeterministic,failNum);
TRAPD(ret,iLex=COplModuleLexer::NewL());
__UHEAP_RESET;
if (ret==0)
{
test(iLex!=NULL);
break;
}
test(iLex==NULL);
__UHEAP_MARKEND;
}
#if defined(_DEBUG)
// test(failNum!=1); // Check that we allocate at least one cell for the lexer (it is C... after all)
#endif
test.Next(_L("Destructor"));
DestroyLex();
__UHEAP_MARKEND;
test.End();
}
const TInt cInt0=0;
const TInt cInt1=1;
const TInt cInt127=127;
const TInt cInt128=128;
const TInt cInt32767=32767;
const TInt cIntM1=-1;
const TInt cIntM127=-127;
const TInt cIntM128=-128;
const TInt cIntM32767=-32767;
const TInt cIntM32768=-32768;
const TInt32 cLong0=0;
const TInt32 cLong1=1;
const TInt32 cLong127=127;
const TInt32 cLong128=128;
const TInt32 cLong32767=32767;
const TInt32 cLong32768=32768;
const TInt32 cLong2etc7=2147483647;
const TInt32 cLongM1=-1;
const TInt32 cLongM127=-127;
const TInt32 cLongM128=-128;
const TInt32 cLongM32767=-32767;
const TInt32 cLongM32768=-32768;
const TInt32 cLongM2etc7=-2147483647;
const TInt32 cLongM2etc8=0x80000000;
const TReal64 cReal1=1.0;
const TReal64 cReal10=10;
const TReal64 cRealP1=0.1;
const TReal64 cReal32767=32767;
const TReal64 cReal32768=32768;
const TReal64 cReal2etc7=2147483647;
const TReal64 cReal2etc8=0x80000000;
LOCAL_D const TText test3DecSource[]=
//
// Line(s) used for testing basic interface
//
{
_MYS("0 1 127 128 32767\x2029\
32768 2147483647\x2029\
1.0 .1 32767.0 32768.0 2147483647.0 2147483648 1e1 1e+1 1e-1\x2029")
};
LOCAL_D const TOplTokenCheck test3DecTokens[]=
{
{ TOplToken::EConstant,TOplToken::EWord,&cInt0}, // Line 1 - word constants
{ TOplToken::EConstant,TOplToken::EWord,&cInt1},
{ TOplToken::EConstant,TOplToken::EWord,&cInt127},
{ TOplToken::EConstant,TOplToken::EWord,&cInt128},
{ TOplToken::EConstant,TOplToken::EWord,&cInt32767},
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EConstant,TOplToken::ELong,&cLong32768}, // Line 2 - word constants
{ TOplToken::EConstant,TOplToken::ELong,&cLong2etc7},
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EConstant,TOplToken::EReal,&cReal1}, // Line 4 - real constants
{ TOplToken::EConstant,TOplToken::EReal,&cRealP1},
{ TOplToken::EConstant,TOplToken::EReal,&cReal32767},
{ TOplToken::EConstant,TOplToken::EReal,&cReal32768},
{ TOplToken::EConstant,TOplToken::EReal,&cReal2etc7},
{ TOplToken::EConstant,TOplToken::EReal,&cReal2etc8},
{ TOplToken::EConstant,TOplToken::EReal,&cReal10},
{ TOplToken::EConstant,TOplToken::EReal,&cReal10},
{ TOplToken::EConstant,TOplToken::EReal,&cRealP1},
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EEof,TOplToken::EBadType,NULL},
{ TOplToken::EBadToken,TOplToken::EBadType,NULL}
};
LOCAL_D const TText test3HexSource[]=
{
_MYS("$0 $1 $7f $80 $7fff\x2029\
$ffff $ff81 $ff80 $8001 $8000\x2029\
&0 &1 &7f &80 &7fff &8000 &7fffffff\x2029\
&ffffffff &ffffff81 &ffffff80 &ffff8001 &ffff8000 &80000001 &80000000\x2029")
};
LOCAL_D const TOplTokenCheck test3HexTokens[]=
{
{ TOplToken::EConstant,TOplToken::EWord,&cInt0}, // Line 1 - word constants
{ TOplToken::EConstant,TOplToken::EWord,&cInt1},
{ TOplToken::EConstant,TOplToken::EWord,&cInt127},
{ TOplToken::EConstant,TOplToken::EWord,&cInt128},
{ TOplToken::EConstant,TOplToken::EWord,&cInt32767},
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EConstant,TOplToken::EWord,&cIntM1}, // Line 2 - negative words
{ TOplToken::EConstant,TOplToken::EWord,&cIntM127},
{ TOplToken::EConstant,TOplToken::EWord,&cIntM128},
{ TOplToken::EConstant,TOplToken::EWord,&cIntM32767},
{ TOplToken::EConstant,TOplToken::EWord,&cIntM32768},
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EConstant,TOplToken::ELong,&cLong0}, // Line 3 - long constants
{ TOplToken::EConstant,TOplToken::ELong,&cLong1},
{ TOplToken::EConstant,TOplToken::ELong,&cLong127},
{ TOplToken::EConstant,TOplToken::ELong,&cLong128},
{ TOplToken::EConstant,TOplToken::ELong,&cLong32767},
{ TOplToken::EConstant,TOplToken::ELong,&cLong32768},
{ TOplToken::EConstant,TOplToken::ELong,&cLong2etc7},
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EConstant,TOplToken::ELong,&cLongM1}, // Line 4 - negative longs
{ TOplToken::EConstant,TOplToken::ELong,&cLongM127},
{ TOplToken::EConstant,TOplToken::ELong,&cLongM128},
{ TOplToken::EConstant,TOplToken::ELong,&cLongM32767},
{ TOplToken::EConstant,TOplToken::ELong,&cLongM32768},
{ TOplToken::EConstant,TOplToken::ELong,&cLongM2etc7},
{ TOplToken::EConstant,TOplToken::ELong,&cLongM2etc8},
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EEof,TOplToken::EBadType,NULL},
{ TOplToken::EBadToken,TOplToken::EBadType,NULL}
};
LOCAL_D const TText test3StringSource[]=
//
//
//
{
_MYS("\"\" \"aString\" \"a quote\"\"\" \"two\"\"\"\"\"\x2029\
\"012346789abcdef012346789abcdef012346789abcdef012346789abcdef\
012346789abcdef012346789abcdef012346789abcdef012346789abcdef\
012346789abcdef012346789abcdef012346789abcdef012346789abcdef\
012346789abcdef012346789abcdef012346789abcdef012346789abcde\"\x2029")
};
const TPtrC cTextEmpty(_S(""));
const TPtrC cTextString(_S("aString"));
const TPtrC cTextQuote(_S("a quote\""));
const TPtrC cText2Quote(_S("two\"\""));
const TPtrC cTextMaxLen(_S("012346789abcdef012346789abcdef012346789abcdef012346789abcdef\
012346789abcdef012346789abcdef012346789abcdef012346789abcdef\
012346789abcdef012346789abcdef012346789abcdef012346789abcdef\
012346789abcdef012346789abcdef012346789abcdef012346789abcde"));
LOCAL_D const TOplTokenCheck test3StringTokens[]=
{
{ TOplToken::EConstant,TOplToken::EString,&cTextEmpty}, // Line 1 - word constants
{ TOplToken::EConstant,TOplToken::EString,&cTextString},
{ TOplToken::EConstant,TOplToken::EString,&cTextQuote},
{ TOplToken::EConstant,TOplToken::EString,&cText2Quote},
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EConstant,TOplToken::EString,&cTextMaxLen}, // Line 2 - negative words
{ TOplToken::EEos,TOplToken::EBadType,NULL},
{ TOplToken::EEof,TOplToken::EBadType,NULL},
{ TOplToken::EBadToken,TOplToken::EBadType,NULL}
};
void TTestModLex::Test3()
//
// Numeric constants
//
{
__UHEAP_MARK;
GetLex();
test.Start(_L("Decimal"));
SOURCE(test3DecSource);
CHECK_SOURCE(test3DecTokens);
test.Next(_L("Decimal Errors"));
// FOR NOW
test.Next(_L("Hex"));
SET_CHECK_SOURCE(test3HexSource,test3HexTokens);
test.Next(_L("Hex Errors"));
// FOR NOW
test.Next(_L("Strings"));
SET_CHECK_SOURCE(test3StringSource,test3StringTokens);
test.Next(_L("Strings Errors"));
// FOR NOW
test.End();
DestroyLex();
__UHEAP_MARKEND;
}
LOCAL_D const TText test4OperatorSource[]=
{
_MYS("< > = + - * / % ( )\x2029\
<= >= <> **\x2029\
aNd oR NoT\x2029")
};
LOCAL_D const TOplTokenCheck test4OperatorTokens[]=
{
{TOplToken::ELessThan,TOplToken::EBadType,NULL},
{TOplToken::EGreaterThan,TOplToken::EBadType,NULL},
{TOplToken::EEqual,TOplToken::EBadType,NULL},
{TOplToken::EPlus,TOplToken::EBadType,NULL},
{TOplToken::EMinus,TOplToken::EBadType,NULL},
{TOplToken::EMultiply,TOplToken::EBadType,NULL},
{TOplToken::EDivide,TOplToken::EBadType,NULL},
{TOplToken::EPerc,TOplToken::EBadType,NULL},
{TOplToken::EOpenBracket,TOplToken::EBadType,NULL},
{TOplToken::ECloseBracket,TOplToken::EBadType,NULL},
{TOplToken::EEos,TOplToken::EBadType,NULL},
{TOplToken::ELessThanEq,TOplToken::EBadType,NULL},
{TOplToken::EGreaterThanEq,TOplToken::EBadType,NULL},
{TOplToken::ENotEqual,TOplToken::EBadType,NULL},
{TOplToken::EPower,TOplToken::EBadType,NULL},
{TOplToken::EEos,TOplToken::EBadType,NULL},
{TOplToken::EAnd,TOplToken::EBadType,NULL},
{TOplToken::EOr,TOplToken::EBadType,NULL},
{TOplToken::ENot,TOplToken::EBadType,NULL},
{TOplToken::EEos,TOplToken::EBadType,NULL},
{TOplToken::EEof,TOplToken::EBadType,NULL},
{TOplToken::EBadToken,TOplToken::EBadType,NULL}
};
LOCAL_D const TText test4PunctuationSource[]=
{
_MYS("; : , #\x2029")
};
LOCAL_D const TOplTokenCheck test4PunctuationTokens[]=
{
{TOplToken::ESemiColon,TOplToken::EBadType,NULL},
{TOplToken::EColon,TOplToken::EBadType,NULL},
{TOplToken::EComma,TOplToken::EBadType,NULL},
{TOplToken::EHash,TOplToken::EBadType,NULL},
{TOplToken::EEos,TOplToken::EBadType,NULL},
{TOplToken::EEof,TOplToken::EBadType,NULL},
{TOplToken::EBadToken,TOplToken::EBadType,NULL}
};
void TTestModLex::Test4()
//
// Operators
//
{
__UHEAP_MARK;
GetLex();
test.Start(_L("Operators"));
SOURCE(test4OperatorSource);
CHECK_SOURCE(test4OperatorTokens);
test.Next(_L("Punctuation"));
SET_CHECK_SOURCE(test4PunctuationSource,test4PunctuationTokens);
test.End();
DestroyLex();
__UHEAP_MARKEND;
}
LOCAL_D const TText test5VariableSource[]=
{
_MYS("a% a1% abcdefg% a2b4c6h%
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?