/// <summary> /// The function processes all kinds of statements including /// assignments, calls, and variable/constant declarations. /// </summary> /// <param name="context"></param> /// <returns>true, if any construct (a statement, or a simple declaration) /// was processed, and false otherwise.</returns> public static bool parse(iSCOPE context, TokenCode stop1, TokenCode stop2, TokenCode stop3) { Debug.Indent(); Debug.WriteLine("Entering STATEMENT.parse"); bool result = true; Token token = get(); Token begin = token; if (token.code == stop1 && stop1 != TokenCode.ERROR) { goto Finish; } if (token.code == stop2 && stop2 != TokenCode.ERROR) { goto Finish; } if (token.code == stop3 && stop3 != TokenCode.ERROR) { goto Finish; } switch (token.code) { // case TokenCode.Pure: -- doesn't make any sense case TokenCode.Safe: case TokenCode.Routine: ROUTINE.parse(token, false, false, false, 0, context); break; case TokenCode.If: IF.parse(context); break; case TokenCode.While: case TokenCode.Loop: LOOP.parse(null, context); break; // Break // : break [ Label ] // // Label // : Identifier case TokenCode.Break: BREAK.parse(context); break; // Statement // : ... // | raise [ Expression ] case TokenCode.Raise: forget(); RAISE.parse(token.span, context); break; // Statement // : ... // | check PredicatesList end // | ... case TokenCode.Check: CHECK.parse(context); break; // Statement // : ... // | return [ Expression ] // | ... case TokenCode.Return: forget(); EXPRESSION expr = EXPRESSION.parse(null, context); // can be null RETURN ret = new RETURN(expr); if (expr != null) { expr.parent = ret; ret.setSpan(begin.span, expr.span); } else { ret.setSpan(begin); } context.add(ret); break; // Statement // : ... // | Try // | ... // // Try // : try { Statement } Catches [ Else ] end // // Catches // : catch [ "(" [ Identifier ":" ] Type ")" ] { Statement } // // Else // : else { Statement } case TokenCode.Try: TRY.parse(context); break; // Statement // : ... // | ? Identifier // | ... // case TokenCode.Question: break; case TokenCode.Init: // Initializer call forget(); Token start = token; DECLARATION init = Context.find(INITIALIZER.initName); EXPRESSION initRef; if (init != null) { initRef = new REFERENCE(init); } else { initRef = new UNRESOLVED(context, new IDENTIFIER(INITIALIZER.initName)); } CALL call = new CALL(initRef); token = expect(TokenCode.LParen); while (true) { EXPRESSION argument = EXPRESSION.parse(null, context); call.add(argument); argument.parent = call; token = get(); if (token.code == TokenCode.Comma) { forget(); continue; } break; } token = expect(TokenCode.RParen); call.setSpan(start.span, token.span); context.add(call); break; case TokenCode.Identifier: { // Several different cases: // - a label in front of while/loop // - a declaration // - a statement forget(); Token next = get(); if (next.code == TokenCode.LParen) { forget(); TokenCode codeAfter = saveTokensUntilRightParenth(next); switch (codeAfter) { case TokenCode.Colon: case TokenCode.Is: // case TokenCode.Do: case TokenCode.Arrow: //This as a routine declaration!! ROUTINE.parse(token, false, false, false, 0, context); goto Weiter; } } EXPRESSION attempt = EXPRESSION.parse(token, context); if (attempt is UNRESOLVED) { // Might be a label or a declaration... Token idd = new Token(attempt.span, TokenCode.Identifier, (attempt as UNRESOLVED).name.identifier, new Category(CategoryCode.identifier)); token = get(); switch (token.code) { case TokenCode.Is: case TokenCode.Comma: // This is definitely a declaration forget(); VARIABLE.parse(false, false, false, false, idd, token, context); goto Weiter; case TokenCode.Colon: forget(); Token token2 = get(); if (token2.code == TokenCode.While || token2.code == TokenCode.Loop) { // This is a real label! Don't 'forget()'. LOOP.parse(idd, context); } else { // This is definitely a variable declaration. // Don't forget() VARIABLE.parse(false, false, false, false, idd, token, context); } goto Weiter; default: // Nothing to do; just going further break; } } // 'attempt' is something else: a call or the left part of an assignment token = get(); if (token.code == TokenCode.Assign) { forget(); EXPRESSION right = EXPRESSION.parse(null, context); ASSIGNMENT res = new ASSIGNMENT(attempt, right); res.setSpan(attempt.span, right.span); context.add(res); } else { if (!(attempt is CALL)) // something's wrong { result = false; } context.add(attempt); } Weiter: break; } case TokenCode.Const: // Something like // const a is 5... // OR // const is a, b, ... end forget(); token = get(); if (token.code == TokenCode.Is) { forget(); CONSTANT.parse(context); } else { VARIABLE.parse(false, false, true, false, null, null, context); } break; default: // Something else, e.g., (a... or this... // Either a function call or an assignment. // // this := ... // (if cond then a else b).f ... // ^ EXPRESSION e = EXPRESSION.parse(null, context); if (e == null) { // Either an error or just the end of statement sequence result = false; break; } token = get(); if (token.code == TokenCode.Assign) { forget(); EXPRESSION right = EXPRESSION.parse(null, context); ASSIGNMENT assignment = new ASSIGNMENT(e, right); assignment.setSpan(e.span, right.span); context.add(assignment); } else { context.add(e); } break; } Finish: Debug.WriteLine("Exiting STATEMENT.parse"); Debug.Unindent(); return(result); }
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(); }
public static bool parse(iSCOPE context) { Debug.Indent(); Debug.WriteLine("Entering DECLARATION.parse"); bool result = true; bool isHidden = false; bool isFinal = false; int pure_safe = 0; bool isOverride = false; bool isRoutine = false; bool isAbstract = false; Token token = get(); Token begin = token; // Collecting specifiers while (true) { switch (token.code) { case TokenCode.Abstract: isAbstract = true; forget(); break; case TokenCode.Override: isOverride = true; forget(); break; case TokenCode.Hidden: isHidden = true; forget(); break; case TokenCode.Final: isFinal = true; isRoutine = true; forget(); break; case TokenCode.Routine: isRoutine = true; forget(); break; case TokenCode.Pure: pure_safe = 1; isRoutine = true; forget(); break; case TokenCode.Safe: pure_safe = 2; isRoutine = true; forget(); break; default: goto OutLoop; } token = get(); } OutLoop: // Checking for a leading keyword switch (token.code) { case TokenCode.Unit: if (isOverride || isRoutine || pure_safe > 0) { // Illegal specifier for unit declaration error(token, "illegal-spec", begin, "unit declaration"); } UNIT.parse(isHidden, isFinal, isAbstract, context); break; case TokenCode.Const: if (isRoutine || pure_safe > 0) { // Illegal specifier for constant declaration error(begin, "illegal-spec", begin, "object declaration"); } Token start = token; forget(); token = get(); if (token.code == TokenCode.Is) { forget(); if (isOverride) { // Illegal specifier for constant declaration error(begin, "illegal-spec", begin, "constant declaration"); } CONSTANT.parse(isHidden, isFinal, start, context); } else { if (pure_safe > 0) { // Illegal specifier for variable declaration error(begin, "illegal-spec", begin, "variable declaration"); } VARIABLE.parse(isHidden, isFinal, true, isOverride, null, null, context); } break; case TokenCode.Init: forget(); if (isOverride || isFinal || isRoutine || pure_safe > 0) { // Illegal specifier for initializer error(begin, "illegal-spec", begin, "initializer declaration"); } ROUTINE.parse(token, isHidden, isFinal, false, 0, context); break; case TokenCode.Identifier: // An identifier just following (optional) specifier(s) if (isRoutine || pure_safe > 0) { // This is definitely a routine declaration // (Some time after, 'isRoutine' might be removed...) forget(); ROUTINE.parse(token, isHidden, isFinal, isOverride, pure_safe, context); } else { // Decide whether this is variable or routine declaration if (!isOverride || !isHidden || !isFinal) { if (!(context is UNIT)) { // Tricky point: // We are out of a unit context, i.e., within a routine // or in a global scope, AND there is NO ONE specifier given. // So, we conclude that identifier starts a statement // but not a declaration. // We do nothing and silently exit with result == false. break; } } Token id = token; forget(); token = get(); // forget(); switch (token.code) { case TokenCode.LParen: case TokenCode.LBracket: case TokenCode.Then: case TokenCode.Else: ROUTINE.parse(id, isHidden, isFinal, isOverride, pure_safe, context); break; default: forget(); VARIABLE.parse(isHidden, isFinal, false, isOverride, id, token, context); break; } break; } break; default: // Some other token after optional specifier(s). // Perhaps this is an operator sign? Token name = ROUTINE.detectOperatorSign(token); if (name.code != TokenCode.ERROR) { ROUTINE.parse(name, isHidden, isFinal, isOverride, pure_safe, context); } else if (isRoutine || isOverride || isHidden) { // Syntax error in declaration error(token, "syntax-error", "wrong declaration"); result = false; } else { // What's this? -- something that is not a declaration. // Don't issue a message // error(token,"syntax-error"); result = false; } break; } Debug.WriteLine("Exiting DECLARATION.parse"); Debug.Unindent(); return(result); }