comparison trunk/src/dil/Parser.d @ 529:135e9e6933a7

Tidied up version/debug parser functions.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Mon, 17 Dec 2007 01:52:46 +0100
parents d0ac6faeaf66
children 4b783fa06277
comparison
equal deleted inserted replaced
528:d0ac6faeaf66 529:135e9e6933a7
1241 nT(); // Skip unittest keyword. 1241 nT(); // Skip unittest keyword.
1242 auto funcBody = parseFunctionBody(); 1242 auto funcBody = parseFunctionBody();
1243 return new UnittestDeclaration(funcBody); 1243 return new UnittestDeclaration(funcBody);
1244 } 1244 }
1245 1245
1246 Token* parseIdentOrInt()
1247 {
1248 if (token.type == T.Int32 ||
1249 token.type == T.Identifier)
1250 {
1251 auto token = this.token;
1252 nT();
1253 return token;
1254 }
1255 else
1256 error(token, MSG.ExpectedIdentOrInt, token.srcText);
1257 return null;
1258 }
1259
1246 Declaration parseDebugDeclaration() 1260 Declaration parseDebugDeclaration()
1247 { 1261 {
1248 assert(token.type == T.Debug); 1262 assert(token.type == T.Debug);
1249
1250 nT(); // Skip debug keyword. 1263 nT(); // Skip debug keyword.
1251 1264
1252 Token* spec; // debug = Integer ; 1265 Token* spec;
1253 // debug = Identifier ; 1266 Token* cond;
1254 Token* cond; // debug ( Integer )
1255 // debug ( Identifier )
1256 Declaration decls, elseDecls; 1267 Declaration decls, elseDecls;
1257 1268
1258 void parseIdentOrInt(ref Token* tok) 1269 if (token.type == T.Assign)
1259 { 1270 { // debug = Integer ;
1260 nT(); 1271 // debug = Identifier ;
1261 if (token.type == T.Int32 || 1272 nT();
1262 token.type == T.Identifier) 1273 spec = parseIdentOrInt();
1263 { 1274 require(T.Semicolon);
1264 tok = token; 1275 }
1276 else
1277 { // ( Condition )
1278 if (token.type == T.LParen)
1279 {
1265 nT(); 1280 nT();
1266 } 1281 cond = parseIdentOrInt();
1267 else
1268 expected(T.Identifier); // TODO: better error msg
1269 }
1270
1271 if (token.type == T.Assign)
1272 {
1273 parseIdentOrInt(spec);
1274 require(T.Semicolon);
1275 }
1276 else
1277 {
1278 // Condition:
1279 // Integer
1280 // Identifier
1281 // ( Condition )
1282 if (token.type == T.LParen)
1283 {
1284 parseIdentOrInt(cond);
1285 require(T.RParen); 1282 require(T.RParen);
1286 } 1283 }
1287
1288 // debug DeclarationsBlock 1284 // debug DeclarationsBlock
1289 // debug ( Condition ) DeclarationsBlock 1285 // debug ( Condition ) DeclarationsBlock
1290 decls = parseDeclarationsBlockNoColon(); 1286 decls = parseDeclarationsBlockNoColon();
1291
1292 // else DeclarationsBlock 1287 // else DeclarationsBlock
1293 if (token.type == T.Else) 1288 if (token.type == T.Else)
1294 { 1289 nT(), (elseDecls = parseDeclarationsBlockNoColon());
1295 nT();
1296 elseDecls = parseDeclarationsBlockNoColon();
1297 }
1298 } 1290 }
1299 1291
1300 return new DebugDeclaration(spec, cond, decls, elseDecls); 1292 return new DebugDeclaration(spec, cond, decls, elseDecls);
1301 } 1293 }
1302 1294
1303 Declaration parseVersionDeclaration() 1295 Declaration parseVersionDeclaration()
1304 { 1296 {
1305 assert(token.type == T.Version); 1297 assert(token.type == T.Version);
1306
1307 nT(); // Skip version keyword. 1298 nT(); // Skip version keyword.
1308 1299
1309 Token* spec; // version = Integer ; 1300 Token* spec;
1310 // version = Identifier ; 1301 Token* cond;
1311 Token* cond; // version ( Integer )
1312 // version ( Identifier )
1313 Declaration decls, elseDecls; 1302 Declaration decls, elseDecls;
1314 1303
1315 void parseIdentOrInt(ref Token* tok)
1316 {
1317 if (token.type == T.Int32 ||
1318 token.type == T.Identifier)
1319 {
1320 tok = token;
1321 nT();
1322 }
1323 else
1324 expected(T.Identifier); // TODO: better error msg
1325 }
1326
1327 if (token.type == T.Assign) 1304 if (token.type == T.Assign)
1328 { 1305 { // version = Integer ;
1329 nT(); 1306 // version = Identifier ;
1330 parseIdentOrInt(spec); 1307 nT();
1308 spec = parseIdentOrInt();
1331 require(T.Semicolon); 1309 require(T.Semicolon);
1332 } 1310 }
1333 else 1311 else
1334 { 1312 { // ( Condition )
1335 // Condition:
1336 // Integer
1337 // Identifier
1338
1339 // ( Condition )
1340 require(T.LParen); 1313 require(T.LParen);
1341 parseIdentOrInt(cond); 1314 cond = parseIdentOrInt();
1342 require(T.RParen); 1315 require(T.RParen);
1343
1344 // version ( Condition ) DeclarationsBlock 1316 // version ( Condition ) DeclarationsBlock
1345 decls = parseDeclarationsBlockNoColon(); 1317 decls = parseDeclarationsBlockNoColon();
1346
1347 // else DeclarationsBlock 1318 // else DeclarationsBlock
1348 if (token.type == T.Else) 1319 if (token.type == T.Else)
1349 { 1320 nT(), (elseDecls = parseDeclarationsBlockNoColon());
1350 nT();
1351 elseDecls = parseDeclarationsBlockNoColon();
1352 }
1353 } 1321 }
1354 1322
1355 return new VersionDeclaration(spec, cond, decls, elseDecls); 1323 return new VersionDeclaration(spec, cond, decls, elseDecls);
1356 } 1324 }
1357 1325
2480 Statement parseDebugStatement() 2448 Statement parseDebugStatement()
2481 { 2449 {
2482 assert(token.type == T.Debug); 2450 assert(token.type == T.Debug);
2483 nT(); // Skip debug keyword. 2451 nT(); // Skip debug keyword.
2484 2452
2485 Token* cond; // debug ( Integer ) 2453 Token* cond;
2486 // debug ( Identifier )
2487 Statement debugBody, elseBody; 2454 Statement debugBody, elseBody;
2488 2455
2489 void parseIdentOrInt(ref Token* tok) 2456 // ( Condition )
2490 { 2457 if (token.type == T.LParen)
2491 nT(); 2458 {
2492 if (token.type == T.Int32 || 2459 nT();
2493 token.type == T.Identifier) 2460 cond = parseIdentOrInt();
2494 { 2461 require(T.RParen);
2495 tok = token; 2462 }
2496 nT(); 2463 // debug Statement
2497 } 2464 // debug ( Condition ) Statement
2498 else 2465 debugBody = parseNoScopeStatement();
2499 expected(T.Identifier); // TODO: better error msg 2466 // else Statement
2500 } 2467 if (token.type == T.Else)
2501 2468 nT(), (elseBody = parseNoScopeStatement());
2502 // if (token.type == T.Assign)
2503 // {
2504 // parseIdentOrInt(identSpec, levelSpec);
2505 // require(T.Semicolon);
2506 // }
2507 // else
2508 {
2509 // Condition:
2510 // Integer
2511 // Identifier
2512
2513 // ( Condition )
2514 if (token.type == T.LParen)
2515 {
2516 parseIdentOrInt(cond);
2517 require(T.RParen);
2518 }
2519
2520 // debug Statement
2521 // debug ( Condition ) Statement
2522 debugBody = parseNoScopeStatement();
2523
2524 // else Statement
2525 if (token.type == T.Else)
2526 {
2527 // debug without condition and else body makes no sense
2528 //if (levelCond == -1 && identCond.length == 0)
2529 // TODO: issue error msg
2530 nT();
2531 elseBody = parseNoScopeStatement();
2532 }
2533 }
2534 2469
2535 return new DebugStatement(cond, debugBody, elseBody); 2470 return new DebugStatement(cond, debugBody, elseBody);
2536 } 2471 }
2537 2472
2538 Statement parseVersionStatement() 2473 Statement parseVersionStatement()
2539 { 2474 {
2540 assert(token.type == T.Version); 2475 assert(token.type == T.Version);
2541
2542 nT(); // Skip version keyword. 2476 nT(); // Skip version keyword.
2543 2477
2544 Token* cond; // version ( Integer ) 2478 Token* cond;
2545 // version ( Identifier )
2546 Statement versionBody, elseBody; 2479 Statement versionBody, elseBody;
2547 2480
2548 void parseIdentOrInt(ref Token* tok) 2481 // ( Condition )
2549 { 2482 require(T.LParen);
2550 if (token.type == T.Int32 || 2483 cond = parseIdentOrInt();
2551 token.type == T.Identifier) 2484 require(T.RParen);
2552 { 2485 // version ( Condition ) Statement
2553 tok = token; 2486 versionBody = parseNoScopeStatement();
2554 nT(); 2487 // else Statement
2555 } 2488 if (token.type == T.Else)
2556 else 2489 nT(), (elseBody = parseNoScopeStatement());
2557 expected(T.Identifier); // TODO: better error msg
2558 }
2559
2560 // if (token.type == T.Assign)
2561 // {
2562 // parseIdentOrInt(identSpec, levelSpec);
2563 // require(T.Semicolon);
2564 // }
2565 // else
2566 {
2567 // Condition:
2568 // Integer
2569 // Identifier
2570
2571 // ( Condition )
2572 require(T.LParen);
2573 parseIdentOrInt(cond);
2574 require(T.RParen);
2575
2576 // version ( Condition ) Statement
2577 versionBody = parseNoScopeStatement();
2578
2579 // else Statement
2580 if (token.type == T.Else)
2581 {
2582 nT();
2583 elseBody = parseNoScopeStatement();
2584 }
2585 }
2586 2490
2587 return new VersionStatement(cond, versionBody, elseBody); 2491 return new VersionStatement(cond, versionBody, elseBody);
2588 } 2492 }
2589 2493
2590 /+++++++++++++++++++++++++++++ 2494 /+++++++++++++++++++++++++++++