/// <summary> /// /// </summary> /// <syntax> /// Loop : while BooleanExpression /// loop /// [ invariant PredicateList ] /// [ StatementList ] /// [ variant PredicateList ] /// end [ loop ] /// /// | loop /// [ invariant PredicateList ] /// [ StatementList ] /// [ variant PredicateList ] /// [ while BooleanExpression ] /// end [ loop ] /// </syntax> /// <returns></returns> public static void parse(Token id, iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering LOOP.parse"); // If id != null, then 'id' is a loop label. // ':' after label is already parsed. LOOP loop = new LOOP(); Context.enter(loop); Token token = get(); Token begin = token; if (token.code == TokenCode.While) { forget(); loop.prefix = true; EXPRESSION whileExpr = EXPRESSION.parse(null, context); token = get(); if (token.code == TokenCode.Loop) { forget(); loop.addw(whileExpr); whileExpr.parent = loop; } else // Syntax error { error(token, "no-loop"); } token = get(); if (token.code == TokenCode.Invariant) { forget(); parseInvariant(loop); } BODY body = new BODY(); body.parent = loop; loop.body = body; Context.enter(body); token = get(); BODY.parse(TokenCode.End, TokenCode.Variant, TokenCode.ERROR, body); body.setSpan(token, get()); Context.exit(); token = get(); if (token.code == TokenCode.Variant) { forget(); parseVariant(loop); } } else if (token.code == TokenCode.Loop) { loop.prefix = false; forget(); token = get(); if (token.code == TokenCode.Invariant) { forget(); parseInvariant(loop); } BODY body = new BODY(); body.parent = loop; loop.body = body; Context.enter(body); token = get(); BODY.parse(TokenCode.End, TokenCode.Variant, TokenCode.While, body); body.setSpan(token, get()); Context.exit(); token = get(); if (token.code == TokenCode.Variant) { forget(); parseVariant(loop); } token = get(); if (token.code == TokenCode.While) { forget(); EXPRESSION whileExpr = EXPRESSION.parse(null, context); loop.addw(whileExpr); whileExpr.parent = loop; } } else // Compiler error { error(token, "system-bug"); } token = get(); if (token.code != TokenCode.End) // Syntax error { error(token, "no-end", "loop"); } else { forget(); token = get(); if (token.code == TokenCode.Loop) { forget(); } } if (loop != null) { loop.setSpan(begin, token); } context.add(loop); Context.exit(); Debug.WriteLine("Exiting LOOP.parse"); Debug.Unindent(); }
public static void parse(Token routineName, bool hidden, bool final, bool isOverride, int pure_safe, iSCOPE context) { ROUTINE routine = null; if (routineName.code == TokenCode.Init) { Debug.Indent(); Debug.WriteLine("Entering INITIALIZER.parse"); routine = new INITIALIZER(); goto Init; } Debug.Indent(); Debug.WriteLine("Entering ROUTINE.parse (" + routineName.image + ")"); string image = routineName.image; Token token = get(); // What's after the routine name? switch (routineName.image) { case "and": if (token.code == TokenCode.Then) { forget(); image += " then"; Span span = new Span(routineName.span, token.span); routineName = new Token(span, TokenCode.Identifier, image, new Category(CategoryCode.identifier)); } break; case "or": token = get(); if (token.code == TokenCode.Else) { forget(); image += " else"; Span span = new Span(routineName.span, token.span); routineName = new Token(span, TokenCode.Identifier, image, new Category(CategoryCode.identifier)); } break; } routine = new ROUTINE(pure_safe, isOverride, routineName); Init: routine.setSpecs(hidden, final); Context.enter(routine); if (routineName.code == TokenCode.Init) { goto Init2; } token = get(); if (token.code == TokenCode.Alias) { forget(); token = expect(TokenCode.Identifier); routine.alias = new IDENTIFIER(token.image); } if (token.code == TokenCode.LBracket) { // Generic routine forget(); while (true) { var generic = FORMAL_GENERIC.parse(context); routine.add(generic); token = get(); switch (token.code) { case TokenCode.Comma: case TokenCode.Semicolon: forget(); continue; case TokenCode.RBracket: forget(); goto Finish; default: { /* Syntax error */ break; } } } Finish: ; } Init2: token = get(); if (token.code == TokenCode.LParen) { forget(); token = get(); if (token.code == TokenCode.RParen) { // Empty parameter list forget(); goto Weiter; } while (true) { VARIABLE.parse(false, false, false, false, null, null, routine); token = get(); if (token.code == TokenCode.Comma) { forget(); continue; } if (token.code == TokenCode.Semicolon) { forget(); continue; } break; } expect(TokenCode.RParen); } Weiter: token = get(); if (token.code == TokenCode.Colon) { forget(); bool ref_val, conc; // TEMP SOLUTION Span span2; routine.type = VARIABLE.parseTypeSpecifier(routine, out ref_val, out conc, out span2); if (routine.type != null) { routine.type.parent = routine; routine.type.setSpan(span2); } } token = get(); if (token.code == TokenCode.Require) { forget(); token = get(); if (token.code == TokenCode.Else) { forget(); routine.requireElse = true; } while (true) { EXPRESSION precondition = EXPRESSION.parse(null, routine); routine.addPre(precondition); precondition.parent = routine; token = get(); if (token.code == TokenCode.Is || token.code == TokenCode.Arrow) { break; } } } if (token.code == TokenCode.Arrow) { forget(); BODY body = new BODY(); routine.routineBody = body; body.parent = routine; Context.enter(body); EXPRESSION expression = EXPRESSION.parse(null, body); RETURN ret = new RETURN(expression); expression.parent = ret; ret.setSpan(expression.span); ret.parent = body; body.add(ret); body.setSpan(ret.span); Context.exit(); } else if (token.code == TokenCode.Is) { forget(); token = get(); if (token.code == TokenCode.Abstract) { forget(); routine.isAbstract = true; } else if (token.code == TokenCode.Foreign) { forget(); routine.isForeign = true; } else { BODY body = new BODY(); body.parent = routine; routine.routineBody = body; Context.enter(body); BODY.parse(TokenCode.End, TokenCode.Ensure, TokenCode.ERROR, body); Context.exit(); } token = get(); if (token.code == TokenCode.Ensure) { forget(); token = get(); if (token.code == TokenCode.Then) { forget(); routine.ensureThen = true; } ENTITY.weAreWithinEnsure = true; while (true) { EXPRESSION postcondition = EXPRESSION.parse(null, routine); routine.addPre(postcondition); postcondition.parent = routine; token = get(); if (token.code == TokenCode.End) { forget(); break; } } ENTITY.weAreWithinEnsure = false; } else if (!routine.isAbstract && !routine.isForeign) { expect(TokenCode.End); } } token = get(); if (token.code == TokenCode.Semicolon) { forget(); } Context.exit(); context.add(routine); routine.parent = context.self; routine.setSpan(routineName, token); if (routineName.code == TokenCode.Init) { Debug.WriteLine("Exiting INITIALIZER.parse"); } else { Debug.WriteLine("Exiting ROUTINE.parse"); } Debug.Unindent(); }