AstSwitchBranch InstrumentSwitchCase(AstSwitchBranch branch) { if (branch.Source == null) { return(branch); } var idx = _owner.LastIndex++; var call = new AstCall(new AstSymbolRef(_owner.FncNameStatement)); call.Args.Add(new AstNumber(idx)); branch.Body.Insert(0) = new AstSimpleStatement(call); _owner.GetForFile(branch.Source) .AddInfo(new InstrumentedInfo(InstrumentedInfoType.SwitchBranch, idx, branch.Start, branch.End)); return(branch); }
AstSwitch ParseSwitchStatement(Position nodeStart) { Next(); var discriminant = ParseParenExpression(); var cases = new StructList <AstNode>(); Expect(TokenType.BraceL); EnterLexicalScope(); var startLoc = Start; AstSwitchBranch consequent = null; var backupAllowBreak = _allowBreak; for (var sawDefault = false; Type != TokenType.BraceR;) { if (Type == TokenType.Case || Type == TokenType.Default) { var isCase = Type == TokenType.Case; if (consequent != null) { consequent.End = _lastTokEnd; } startLoc = Start; Next(); _allowBreak = true; if (isCase) { var test = ParseExpression(); consequent = new AstCase(this, startLoc, startLoc, test); } else { if (sawDefault) { RaiseRecoverable(_lastTokStart, "Multiple default clauses"); } sawDefault = true; consequent = new AstDefault(this, startLoc, startLoc); } cases.Add(consequent); Expect(TokenType.Colon); } else { if (consequent == null) { Raise(Start, "Unexpected token"); } consequent.Body.Add(ParseStatement(true)); } } ExitLexicalScope(); if (consequent != null) { consequent.End = _lastTokEnd; } Next(); // Closing brace _allowBreak = backupAllowBreak; return(new AstSwitch(this, nodeStart, _lastTokEnd, discriminant, ref cases)); }