Mercurial > projects > dang
comparison parser/Parser.d @ 92:771ac63898e2 new_gen
A few better parser errors plus renaming most of the sema classes to match that they do now. Some have changes a lot.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Mon, 05 May 2008 18:44:20 +0200 |
parents | a49bb982a7b0 |
children | 48bb2287c035 |
comparison
equal
deleted
inserted
replaced
91:1a24e61eb104 | 92:771ac63898e2 |
---|---|
46 { | 46 { |
47 Token t = lexer.peek; | 47 Token t = lexer.peek; |
48 | 48 |
49 if (t.isBasicType || t.isIdentifier) | 49 if (t.isBasicType || t.isIdentifier) |
50 { | 50 { |
51 Id type = Id(lexer.next); | 51 Id type; |
52 Id iden = Id(require(Tok.Identifier)); | 52 Id iden; |
53 Token next = lexer.peek(); | 53 int len = peekParseType; |
54 if (next.type == Tok.Seperator) | 54 if(lexer.peek(len).type == Tok.Identifier && len != 0) |
55 { | 55 { |
56 Token sep = lexer.next(); | 56 type = parseType; |
57 return action.actOnDeclarator(type, iden, null); | 57 parseDeclAfterInvalidType: |
58 } | 58 iden = Id(require(Tok.Identifier)); |
59 else if (next.type == Tok.Assign) | 59 Token next = lexer.peek(); |
60 { | 60 if (next.type == Tok.Seperator) |
61 Token assign = lexer.next(); | 61 { |
62 Exp exp = parseExpression(); | 62 Token sep = lexer.next(); |
63 require(Tok.Seperator); | 63 return action.actOnDeclarator(type, iden, null); |
64 return action.actOnDeclarator(type, iden, exp); | 64 } |
65 } | 65 else if (next.type == Tok.Assign) |
66 else if (next.type == Tok.OpenParentheses) | 66 { |
67 return parseFunc(type, iden); | 67 Token assign = lexer.next(); |
68 else | 68 Exp exp = parseExpression(); |
69 messages.report(UnexpectedTok, next.location).arg(next.getType); | 69 require(Tok.Seperator); |
70 return action.actOnDeclarator(type, iden, exp); | |
71 } | |
72 else if (next.type == Tok.OpenParentheses) | |
73 return parseFunc(type, iden); | |
74 else | |
75 messages.report(UnexpectedTok, next.location).arg(next.getType); | |
76 } | |
77 t = lexer.peek(len); | |
78 messages.report(InvalidDeclType, t.location) | |
79 .arg(sm.getText(t.asRange)); | |
80 while(len--) | |
81 lexer.next; | |
82 while(lexer.peek.type != Tok.Identifier) | |
83 lexer.next; | |
84 type = Id(lexer.peek); | |
85 goto parseDeclAfterInvalidType; | |
70 } | 86 } |
71 else if (t.type == Tok.Struct) | 87 else if (t.type == Tok.Struct) |
72 { | 88 { |
73 Id type = Id(lexer.next); | 89 Id type = Id(lexer.next); |
74 Id iden = Id(require(Tok.Identifier)); | 90 Id iden = Id(require(Tok.Identifier)); |
75 | 91 |
76 return parseStruct(type, iden); | 92 return parseStruct(type, iden); |
77 } | 93 } |
78 messages.report(UnexpectedTok, t.location).arg(t.getType); | 94 messages.report(UnexpectedTok, t.location) |
95 .arg(t.getType) | |
96 .arg(Tok.Identifier) | |
97 .fatal(ExitLevel.Parser); | |
79 } | 98 } |
80 | 99 |
81 /** | 100 /** |
82 Parse struct | 101 Parse struct |
83 */ | 102 */ |
328 Token type = lexer.next; | 347 Token type = lexer.next; |
329 | 348 |
330 Id currentType; | 349 Id currentType; |
331 | 350 |
332 if ( !(type.isBasicType || type.type == Tok.Identifier) ) | 351 if ( !(type.isBasicType || type.type == Tok.Identifier) ) |
333 messages.report(UnexpectedTokSingle, type.location) | 352 messages.report(InvalidType, type.location); |
334 .arg(type.getType); | |
335 | 353 |
336 currentType = Id(type); | 354 currentType = Id(type); |
337 type = lexer.peek; | 355 type = lexer.peek; |
338 | 356 |
339 while(type.type == Tok.Star || type.type == Tok.OpenBracket) | 357 while(type.type == Tok.Star || type.type == Tok.OpenBracket) |
344 lexer.next; | 362 lexer.next; |
345 } | 363 } |
346 else | 364 else |
347 { | 365 { |
348 lexer.next; | 366 lexer.next; |
349 currentType = ArrayId(currentType, action.actOnNumericConstant(require(Tok.Integer))); | 367 if(lexer.peek.type == Tok.Integer) |
368 currentType = ArrayId(currentType, action.actOnNumericConstant(require(Tok.Integer))); | |
350 require(Tok.CloseBracket); | 369 require(Tok.CloseBracket); |
351 | 370 |
352 } | 371 } |
353 type = lexer.peek; | 372 type = lexer.peek; |
354 } | 373 } |
377 } | 396 } |
378 else | 397 else |
379 { | 398 { |
380 if(lexer.peek(i++).type != Tok.OpenBracket) | 399 if(lexer.peek(i++).type != Tok.OpenBracket) |
381 return 0; | 400 return 0; |
382 if(lexer.peek(i++).type != Tok.Integer) | 401 if(lexer.peek(i).type == Tok.Integer) |
383 return 0; | 402 { |
384 if(lexer.peek(i++).type != Tok.CloseBracket) | 403 i++; |
385 return 0; | 404 if(lexer.peek(i++).type != Tok.CloseBracket) |
405 return 0; | |
406 } | |
407 else | |
408 if(lexer.peek(i++).type != Tok.CloseBracket) | |
409 return 0; | |
386 | 410 |
387 } | 411 } |
388 type = lexer.peek(i); | 412 type = lexer.peek(i); |
389 } | 413 } |
390 | 414 |
473 else if (next.type == Tok.Cast) | 497 else if (next.type == Tok.Cast) |
474 return parseCast(next); | 498 return parseCast(next); |
475 else if (next.type == Tok.Integer) | 499 else if (next.type == Tok.Integer) |
476 return action.actOnNumericConstant(next); | 500 return action.actOnNumericConstant(next); |
477 | 501 |
478 messages.report(ExpectedExp, next.location, true); | 502 messages.report(ExpectedExp, next.location) |
479 // assert(0, "Should not happen"); | 503 .fatal(ExitLevel.Parser); |
480 return null; | 504 return null; |
481 } | 505 } |
482 | 506 |
483 Exp parseCast(ref Token _cast) | 507 Exp parseCast(ref Token _cast) |
484 { | 508 { |