00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/Debug.h>
00018 #include <spl/Log.h>
00019 #include <spl/interp/JsLex.h>
00020
00021 #ifdef DEBUG
00022
00023 static void _TestJsLexSingles()
00024 {
00025 JsLex lex("; ! % ^ & * ( ) - + = < > , . / : ~ | { } [ ]");
00026 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_SEMI );
00027 UNIT_ASSERT( "Lex !", lex.Next() == JsLex::T_BANG );
00028 UNIT_ASSERT( "Lex %", lex.Next() == JsLex::T_MOD );
00029 UNIT_ASSERT( "Lex ^", lex.Next() == JsLex::T_XOR );
00030 UNIT_ASSERT( "Lex &", lex.Next() == JsLex::T_AMPR );
00031 UNIT_ASSERT( "Lex *", lex.Next() == JsLex::T_STAR );
00032 UNIT_ASSERT( "Lex (", lex.Next() == JsLex::T_LPAR );
00033 UNIT_ASSERT( "Lex )", lex.Next() == JsLex::T_RPAR );
00034 UNIT_ASSERT( "Lex -", lex.Next() == JsLex::T_MINUS );
00035 UNIT_ASSERT( "Lex +", lex.Next() == JsLex::T_PLUS );
00036 UNIT_ASSERT( "Lex ==", lex.Next() == JsLex::T_ASSIGN );
00037 UNIT_ASSERT( "Lex <", lex.Next() == JsLex::T_LT );
00038 UNIT_ASSERT( "Lex >", lex.Next() == JsLex::T_GT );
00039 UNIT_ASSERT( "Lex ,", lex.Next() == JsLex::T_COMMA );
00040 UNIT_ASSERT( "Lex .", lex.Next() == JsLex::T_DOT );
00041 UNIT_ASSERT( "Lex /", lex.Next() == JsLex::T_SLASH );
00042 UNIT_ASSERT( "Lex :", lex.Next() == JsLex::T_COLON );
00043 UNIT_ASSERT( "Lex ~", lex.Next() == JsLex::T_COMP );
00044 UNIT_ASSERT( "Lex |", lex.Next() == JsLex::T_PIPE );
00045 UNIT_ASSERT( "Lex [", lex.Next() == JsLex::T_LBRACE );
00046 UNIT_ASSERT( "Lex ]", lex.Next() == JsLex::T_RBRACE );
00047 UNIT_ASSERT( "Lex {", lex.Next() == JsLex::T_LBRAC );
00048 UNIT_ASSERT( "Lex }", lex.Next() == JsLex::T_RBRAC );
00049 UNIT_ASSERT( "Lex EOF", lex.Next() == JsLex::T_JEOF );
00050
00051 lex.Init(";!%^&*()-+<>,./:~|{}[]");
00052 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_SEMI );
00053 UNIT_ASSERT( "Lex !", lex.Next() == JsLex::T_BANG );
00054 UNIT_ASSERT( "Lex %", lex.Next() == JsLex::T_MOD );
00055 UNIT_ASSERT( "Lex ^", lex.Next() == JsLex::T_XOR );
00056 UNIT_ASSERT( "Lex &", lex.Next() == JsLex::T_AMPR );
00057 UNIT_ASSERT( "Lex *", lex.Next() == JsLex::T_STAR );
00058 UNIT_ASSERT( "Lex (", lex.Next() == JsLex::T_LPAR );
00059 UNIT_ASSERT( "Lex )", lex.Next() == JsLex::T_RPAR );
00060 UNIT_ASSERT( "Lex -", lex.Next() == JsLex::T_MINUS );
00061 UNIT_ASSERT( "Lex +", lex.Next() == JsLex::T_PLUS );
00062 UNIT_ASSERT( "Lex <", lex.Next() == JsLex::T_LT );
00063 UNIT_ASSERT( "Lex >", lex.Next() == JsLex::T_GT );
00064 UNIT_ASSERT( "Lex ,", lex.Next() == JsLex::T_COMMA );
00065 UNIT_ASSERT( "Lex .", lex.Next() == JsLex::T_DOT );
00066 UNIT_ASSERT( "Lex /", lex.Next() == JsLex::T_SLASH );
00067 UNIT_ASSERT( "Lex :", lex.Next() == JsLex::T_COLON );
00068 UNIT_ASSERT( "Lex ~", lex.Next() == JsLex::T_COMP );
00069 UNIT_ASSERT( "Lex |", lex.Next() == JsLex::T_PIPE );
00070 UNIT_ASSERT( "Lex [", lex.Next() == JsLex::T_LBRACE );
00071 UNIT_ASSERT( "Lex ]", lex.Next() == JsLex::T_RBRACE );
00072 UNIT_ASSERT( "Lex {", lex.Next() == JsLex::T_LBRAC );
00073 UNIT_ASSERT( "Lex }", lex.Next() == JsLex::T_RBRAC );
00074 UNIT_ASSERT( "Lex EOF", lex.Next() == JsLex::T_JEOF );
00075
00076 Log::SWriteOkFail( "JS lex singles" );
00077 }
00078
00079 static void _TestJsLexSemi()
00080 {
00081 JsLex lex(";");
00082 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_SEMI );
00083 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00084
00085 DEBUG_CLEAR_MEM_CHECK_POINTS();
00086 lex.CheckMem();
00087 UNIT_ASSERT_MEM_NOTED("_TestJsLexSemi 1");
00088
00089 Log::SWriteOkFail( "JS lex ;" );
00090 }
00091
00092 static void _TestJsLexKeywords( )
00093 {
00094 JsLex lex("finally try catch null const new delete switch case if else while for var return class public private protected break do continue");
00095 UNIT_ASSERT( "Lex finally", lex.Next() == JsLex::T_FINALLY );
00096 UNIT_ASSERT( "Lex try", lex.Next() == JsLex::T_TRY );
00097 UNIT_ASSERT( "Lex catch", lex.Next() == JsLex::T_CATCH );
00098 UNIT_ASSERT( "Lex null", lex.Next() == JsLex::T_JNULL );
00099 UNIT_ASSERT( "Lex const", lex.Next() == JsLex::T_CONST );
00100 UNIT_ASSERT( "Lex new", lex.Next() == JsLex::T_NEW );
00101 UNIT_ASSERT( "Lex delete", lex.Next() == JsLex::T_DELETE );
00102 UNIT_ASSERT( "Lex switch", lex.Next() == JsLex::T_SWITCH );
00103 UNIT_ASSERT( "Lex case", lex.Next() == JsLex::T_CASE );
00104 UNIT_ASSERT( "Lex if", lex.Next() == JsLex::T_IF );
00105 UNIT_ASSERT( "Lex else", lex.Next() == JsLex::T_ELSE );
00106 UNIT_ASSERT( "Lex while", lex.Next() == JsLex::T_WHILE );
00107 UNIT_ASSERT( "Lex for", lex.Next() == JsLex::T_FOR );
00108 UNIT_ASSERT( "Lex var", lex.Next() == JsLex::T_VAR );
00109 UNIT_ASSERT( "Lex return", lex.Next() == JsLex::T_RETURN );
00110 UNIT_ASSERT( "Lex class", lex.Next() == JsLex::T_CLASS );
00111 UNIT_ASSERT( "Lex public", lex.Next() == JsLex::T_PUBLIC );
00112 UNIT_ASSERT( "Lex private", lex.Next() == JsLex::T_PRIVATE );
00113 UNIT_ASSERT( "Lex protected", lex.Next() == JsLex::T_PROTECTED );
00114 UNIT_ASSERT( "Lex break", lex.Next() == JsLex::T_BREAK );
00115 UNIT_ASSERT( "Lex do", lex.Next() == JsLex::T_DO );
00116 UNIT_ASSERT( "Lex continue", lex.Next() == JsLex::T_CONTINUE );
00117 UNIT_ASSERT( "Lex EOF", lex.Next() == JsLex::T_JEOF );
00118
00119 Log::SWriteOkFail( "JS lex keywords" );
00120 }
00121
00122 static void _TestJsLexTwoChar( )
00123 {
00124 JsLex lex ( "~= != %= ^= &= *= /= -- ++ <= >=" );
00125 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_COMPEQ );
00126 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_ISNEQ );
00127 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_MODEQ );
00128 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_XOREQ );
00129 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_ANDEQ );
00130 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_TIMESEQ );
00131 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_DIVEQ );
00132 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_DEC );
00133 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_INC );
00134 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_LTEQ );
00135 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_GTEQ );
00136 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00137
00138 Log::SWriteOkFail( "JS lex two char op test ... " );
00139 }
00140
00141 static void _TestJsLexStringNullTest( )
00142 {
00143 JsLex lex ("\"\"");
00144 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_STRING );
00145 UNIT_ASSERT( "Lex ;", lex.Lexum().Length() == 0 );
00146 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00147
00148 Log::SWriteOkFail( "JS lex string null test ... " );
00149 }
00150
00151 static void _TestJsLexStringTest()
00152 {
00153 JsLex lex ("\"hi mom\"");
00154 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_STRING );
00155 UNIT_ASSERT( "Lex ;", lex.Lexum().Equals("hi mom") );
00156 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00157
00158 Log::SWriteOkFail( "JS lex string test ... " );
00159 }
00160
00161 static void _TestJsLexCharTest1()
00162 {
00163 JsLex lex ("\n'\\n'");
00164 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_CHAR );
00165 UNIT_ASSERT( "lex", lex.Lexum().CharAt(0) == '\n' );
00166 UNIT_ASSERT( "lex line", lex.LineNumber() == 2 );
00167 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00168
00169 Log::SWriteOkFail( "JS lex char test ... " );
00170 }
00171
00172 static void _TestJsLexCharTest2( )
00173 {
00174 JsLex lex ("\n'\\\\'");
00175 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_CHAR );
00176 UNIT_ASSERT( "lex", lex.Lexum().CharAt(0) == '\\' );
00177 UNIT_ASSERT( "lex line", lex.LineNumber() == 2 );
00178 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00179
00180 Log::SWriteOkFail( "JS lex char 2 test ... " );
00181 }
00182
00183 static void _TestJsLexIdTest( )
00184 {
00185 JsLex lex ("_mybob my_bob mybob_ my1bob _123");
00186 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_ID );
00187 UNIT_ASSERT( "lex id", lex.Lexum().Equals("_mybob") );
00188 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_ID );
00189 UNIT_ASSERT( "lex id", lex.Lexum().Equals("my_bob") );
00190 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_ID );
00191 UNIT_ASSERT( "lex id", lex.Lexum().Equals("mybob_") );
00192 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_ID );
00193 UNIT_ASSERT( "lex id", lex.Lexum().Equals("my1bob") );
00194 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_ID );
00195 UNIT_ASSERT( "lex id", lex.Lexum().Equals("_123") );
00196 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00197
00198 Log::SWriteOkFail( "JS lex ID test ... " );
00199 }
00200
00201 static void _TestJsLexNumTest( )
00202 {
00203 JsLex lex( "1 1.0 .1 0x10 1.2e5 1.2e-5" );
00204 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_INT );
00205 UNIT_ASSERT( "lex id", lex.Lexum().Equals("1") );
00206 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_REAL );
00207 UNIT_ASSERT( "lex id", lex.Lexum().Equals("1.0") );
00208 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_REAL );
00209 UNIT_ASSERT( "lex id", lex.Lexum().Equals(".1") );
00210 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_HEX );
00211 UNIT_ASSERT( "lex id", lex.Lexum().Equals("0x10") );
00212 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_REAL );
00213 UNIT_ASSERT( "lex id", lex.Lexum().Equals("1.2e5") );
00214 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_REAL );
00215 UNIT_ASSERT( "lex id", lex.Lexum().Equals("1.2e-5") );
00216 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00217
00218 Log::SWriteOkFail( "JS lex num test ... " );
00219 }
00220
00221 static void _TestJsLexSmallProgTest( )
00222 {
00223 JsLex lex ("class test {\n\tpublic method()\n{var x = 1; return x;}\n \t}\n");
00224 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_CLASS );
00225 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_ID );
00226 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_LBRACE );
00227 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_PUBLIC );
00228 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_ID );
00229 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_LPAR );
00230 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_RPAR );
00231 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_LBRACE );
00232 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_VAR );
00233 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_ID );
00234 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_ASSIGN );
00235 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_INT );
00236 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_SEMI );
00237 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_RETURN );
00238 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_ID );
00239 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_SEMI );
00240 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_RBRACE );
00241 UNIT_ASSERT( "lex prog 1", lex.Next() == JsLex::T_RBRACE );
00242 UNIT_ASSERT( "Lex ;", lex.Next() == JsLex::T_JEOF );
00243
00244 Log::SWriteOkFail( "JS lex small prog test ... " );
00245 }
00246
00247 void _TestJsLex()
00248 {
00249 _TestJsLexSemi();
00250 DEBUG_CLEAR_MEM_CHECK_POINTS();
00251 DEBUG_DUMP_MEM_LEAKS();
00252
00253 _TestJsLexSingles();
00254 DEBUG_CLEAR_MEM_CHECK_POINTS();
00255 DEBUG_DUMP_MEM_LEAKS();
00256
00257 _TestJsLexKeywords();
00258 DEBUG_CLEAR_MEM_CHECK_POINTS();
00259 DEBUG_DUMP_MEM_LEAKS();
00260
00261 _TestJsLexTwoChar();
00262 DEBUG_CLEAR_MEM_CHECK_POINTS();
00263 DEBUG_DUMP_MEM_LEAKS();
00264
00265 _TestJsLexStringNullTest();
00266 DEBUG_CLEAR_MEM_CHECK_POINTS();
00267 DEBUG_DUMP_MEM_LEAKS();
00268
00269 _TestJsLexStringTest();
00270 DEBUG_CLEAR_MEM_CHECK_POINTS();
00271 DEBUG_DUMP_MEM_LEAKS();
00272
00273 _TestJsLexCharTest1();
00274 DEBUG_CLEAR_MEM_CHECK_POINTS();
00275 DEBUG_DUMP_MEM_LEAKS();
00276
00277 _TestJsLexCharTest2();
00278 DEBUG_CLEAR_MEM_CHECK_POINTS();
00279 DEBUG_DUMP_MEM_LEAKS();
00280
00281 _TestJsLexIdTest();
00282 DEBUG_CLEAR_MEM_CHECK_POINTS();
00283 DEBUG_DUMP_MEM_LEAKS();
00284
00285 _TestJsLexNumTest();
00286 DEBUG_CLEAR_MEM_CHECK_POINTS();
00287 DEBUG_DUMP_MEM_LEAKS();
00288
00289 _TestJsLexSmallProgTest();
00290 DEBUG_CLEAR_MEM_CHECK_POINTS();
00291 DEBUG_DUMP_MEM_LEAKS();
00292 }
00293
00294 #endif