public object VisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data) { B.TryStatement t = new B.TryStatement(GetLexicalInfo(tryCatchStatement)); t.EndSourceLocation = GetLocation(tryCatchStatement.EndLocation); t.ProtectedBlock = ConvertBlock(tryCatchStatement.StatementBlock); t.EnsureBlock = ConvertBlock(tryCatchStatement.FinallyBlock); foreach (CatchClause clause in tryCatchStatement.CatchClauses) { B.ExceptionHandler handler = new B.ExceptionHandler(GetLexicalInfo(clause)); handler.Block = ConvertBlock(clause.StatementBlock); B.TypeReference typeRef = ConvertTypeReference(clause.TypeReference); string name = clause.VariableName; if (typeRef != null) { if (name == null || name.Length == 0) { name = GenerateName(); } handler.Declaration = new B.Declaration(name, typeRef); } else { if (name != null && name.Length > 0) { handler.Declaration = new B.Declaration(name, null); } } t.ExceptionHandlers.Add(handler); } return(t); }
public override void OnExceptionHandler(ExceptionHandler node) { bool untypedException = (node.Flags & ExceptionHandlerFlags.Untyped) == ExceptionHandlerFlags.Untyped; bool anonymousException = (node.Flags & ExceptionHandlerFlags.Anonymous) == ExceptionHandlerFlags.Anonymous; bool filterHandler = (node.Flags & ExceptionHandlerFlags.Filter) == ExceptionHandlerFlags.Filter; if (untypedException) { // If untyped, set the handler to except System.Exception node.Declaration.Type = CodeBuilder.CreateTypeReference(TypeSystemServices.ExceptionType); } else { Visit(node.Declaration.Type); // Require typed exception handlers to except only // exceptions at least as derived as System.Exception if(!TypeSystemServices.IsValidException(GetType(node.Declaration.Type))) { Errors.Add(CompilerErrorFactory.InvalidExceptArgument(node.Declaration.Type, GetType(node.Declaration.Type))); } } if(!anonymousException) { // If the exception is not anonymous, place it into a // local variable and enter a new namespace DeclareLocal(node.Declaration, true); EnterNamespace(new DeclarationsNamespace(CurrentNamespace, node.Declaration)); } try { // The filter handler has access to the exception if it // is not anonymous, so it is protected to ensure // any exception in the filter condition (a big no-no) // will still clean up the namespace if necessary if (filterHandler) { Visit(node.FilterCondition); } Visit(node.Block); } finally { // Clean up the namespace if necessary if(!anonymousException) { LeaveNamespace(); } } }
protected void exception_handler( TryStatement t ) //throws RecognitionException, TokenStreamException { IToken c = null; IToken x = null; IToken u = null; ExceptionHandler eh = null; TypeReference tr = null; Expression e = null; try { // for error handling c = LT(1); match(EXCEPT); { switch ( LA(1) ) { case ID: { x = LT(1); match(ID); break; } case AS: case IF: case UNLESS: case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { switch ( LA(1) ) { case AS: { match(AS); tr=type_reference(); break; } case IF: case UNLESS: case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { switch ( LA(1) ) { case IF: case UNLESS: { { switch ( LA(1) ) { case IF: { match(IF); break; } case UNLESS: { u = LT(1); match(UNLESS); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } e=boolean_expression(); break; } case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } if (0==inputState.guessing) { eh = new ExceptionHandler(ToLexicalInfo(c)); eh.Declaration = new Declaration(); eh.Declaration.Type = tr; if (x != null) { eh.Declaration.LexicalInfo = ToLexicalInfo(x); eh.Declaration.Name = x.getText(); } else { eh.Declaration.Name = null; eh.Flags |= ExceptionHandlerFlags.Anonymous; } if (tr != null) { eh.Declaration.LexicalInfo = tr.LexicalInfo; } else if (x != null) { eh.Declaration.LexicalInfo = eh.LexicalInfo; } if(tr == null) { eh.Flags |= ExceptionHandlerFlags.Untyped; } if (e != null) { if(u != null) { UnaryExpression not = new UnaryExpression(ToLexicalInfo(u)); not.Operator = UnaryOperatorType.LogicalNot; not.Operand = e; e = not; } eh.FilterCondition = e; eh.Flags |= ExceptionHandlerFlags.Filter; } } compound_stmt(eh.Block); if (0==inputState.guessing) { t.ExceptionHandlers.Add(eh); } } catch (RecognitionException ex) { if (0 == inputState.guessing) { reportError(ex, "exception_handler"); recover(ex,tokenSet_106_); } else { throw ex; } } }
//throws RecognitionException, TokenStreamException protected Block exception_handler( TryStatement t ) { Block lastBlock; IToken c = null; IToken x = null; IToken u = null; ExceptionHandler eh = null; TypeReference tr = null; Expression e = null; lastBlock = null; try { // for error handling c = LT(1); match(EXCEPT); { switch ( LA(1) ) { case ID: { x = LT(1); match(ID); break; } case AS: case IF: case UNLESS: case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { switch ( LA(1) ) { case AS: { match(AS); tr=type_reference(); break; } case IF: case UNLESS: case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { switch ( LA(1) ) { case IF: case UNLESS: { { switch ( LA(1) ) { case IF: { match(IF); break; } case UNLESS: { u = LT(1); match(UNLESS); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } e=expression(); break; } case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } begin(); if (0==inputState.guessing) { eh = new ExceptionHandler(SourceLocationFactory.ToLexicalInfo(c)); eh.Declaration = new Declaration(); eh.Declaration.Type = tr; if (x != null) { eh.Declaration.LexicalInfo = SourceLocationFactory.ToLexicalInfo(x); eh.Declaration.Name = x.getText(); } else { eh.Declaration.Name = null; eh.Flags |= ExceptionHandlerFlags.Anonymous; } if (tr != null) { eh.Declaration.LexicalInfo = tr.LexicalInfo; } else if (x != null) { eh.Declaration.LexicalInfo = eh.LexicalInfo; } if(tr == null) { eh.Flags |= ExceptionHandlerFlags.Untyped; } if (e != null) { if(u != null) { UnaryExpression not = new UnaryExpression(SourceLocationFactory.ToLexicalInfo(u)); not.Operator = UnaryOperatorType.LogicalNot; not.Operand = e; e = not; } eh.FilterCondition = e; eh.Flags |= ExceptionHandlerFlags.Filter; } eh.Block = new Block(SourceLocationFactory.ToLexicalInfo(c)); } block(eh.Block.Statements); if (0==inputState.guessing) { lastBlock = eh.Block; t.ExceptionHandlers.Add(eh); } } catch (RecognitionException ex) { if (0 == inputState.guessing) { reportError(ex); recover(ex,tokenSet_95_); } else { throw ex; } } return lastBlock; }
public override void OnExceptionHandler(ExceptionHandler node) { _state.EnterExceptionHandler(); Visit(node.Block); _state.LeaveExceptionHandler(); }
public static CompilerError ExceptionAlreadyHandled(ExceptionHandler dupe, ExceptionHandler previous) { return Instantiate("BCE0165", dupe.Declaration, dupe.Declaration.Type, previous.Declaration.Type, AstUtil.SafePositionOnlyLexicalInfo(previous.Declaration), LanguageAmbiance.ExceptKeyword); }
private void EmitStoreOrPopException(ExceptionHandler node) { if((node.Flags & ExceptionHandlerFlags.Anonymous) == ExceptionHandlerFlags.None) { _il.Emit(OpCodes.Stloc, GetLocalBuilder(node.Declaration)); } else { _il.Emit(OpCodes.Pop); } }
override public void OnExceptionHandler(ExceptionHandler node) { if((node.Flags & ExceptionHandlerFlags.Filter) == ExceptionHandlerFlags.Filter) { _il.BeginExceptFilterBlock(); Label endLabel = _il.DefineLabel(); // If the filter is not untyped, then test the exception type // before testing the filter condition if((node.Flags & ExceptionHandlerFlags.Untyped) == ExceptionHandlerFlags.None) { Label filterCondition = _il.DefineLabel(); // Test the type of the exception. _il.Emit(OpCodes.Isinst, GetSystemType(node.Declaration.Type)); // Duplicate it. If it is null, then it will be used to // skip the filter. Dup(); // If the exception is of the right type, branch // to test the filter condition. _il.Emit(OpCodes.Brtrue_S, filterCondition); // Otherwise, clean up the stack and prepare the stack // to skip the filter. EmitStoreOrPopException(node); _il.Emit(OpCodes.Ldc_I4_0); _il.Emit(OpCodes.Br, endLabel); _il.MarkLabel(filterCondition); } else if((node.Flags & ExceptionHandlerFlags.Anonymous) == ExceptionHandlerFlags.None) { // Cast the exception to the default except type _il.Emit(OpCodes.Isinst, GetSystemType(node.Declaration.Type)); } EmitStoreOrPopException(node); // Test the condition and convert to boolean if needed. node.FilterCondition.Accept(this); PopType(); EmitToBoolIfNeeded(node.FilterCondition); // If the type is right and the condition is true, // proceed with the handler. _il.MarkLabel(endLabel); _il.Emit(OpCodes.Ldc_I4_0); _il.Emit(OpCodes.Cgt_Un); _il.BeginCatchBlock(null); } else { // Begin a normal catch block of the appropriate type. _il.BeginCatchBlock(GetSystemType(node.Declaration.Type)); // Clean up the stack or store the exception if not anonymous. EmitStoreOrPopException(node); } Visit(node.Block); }
public override void OnExceptionHandler(ExceptionHandler node) { if (node.Declaration != null) { if (node.LexicalInfo.Line <= resolver.CaretLine && GetEndSourceLocation(node).Line >= resolver.CaretLine) { DeclarationFound(node.Declaration.Name, node.Declaration.Type ?? new SimpleTypeReference("System.Exception"), null, node.Declaration.LexicalInfo); } } base.OnExceptionHandler(node); }
public static CompilerWarning AmbiguousExceptionName(ExceptionHandler node) { return new CompilerWarning("BCW0019", AstUtil.SafeLexicalInfo(node), node.Declaration.Name); }
public override void LeaveExceptionHandler(ExceptionHandler node) { if (null != node.Declaration.Type.Entity && ((IType)node.Declaration.Type.Entity).FullName == "System.Exception" && !string.IsNullOrEmpty(node.Declaration.Name)) { if (null != NameResolutionService.ResolveTypeName(new SimpleTypeReference(node.Declaration.Name))) Warnings.Add(CompilerWarningFactory.AmbiguousExceptionName(node)); } }
protected override Statement ExpandImpl(MacroStatement macro){ if (macro.Arguments.Count == 0){ throw new Exception("need at least one IEnumerable parameter"); } var result = new Block(); Expression src = macro.Arguments[0]; var idx = new ReferenceExpression("_idx"); Expression item = new ReferenceExpression("i"); var col = new ReferenceExpression("current_collection"); if (src is BinaryExpression){ item = ((BinaryExpression) src).Left; src = ((BinaryExpression) src).Right; } if (macro.Arguments.Count > 1){ idx = (ReferenceExpression) macro.Arguments[1]; } string prefix = macro.get<string>("_prefix") ?? ""; string suffix = macro.get<string>("_suffix") ?? ""; Func<string, MacroStatement> findmacro = s =>{ var r = macro.get<Node>(s); if (null == r){ return null; } if (!(r is MacroStatement)){ var m = new MacroStatement("stub"); m.Body = new Block().add(r); r = m; } return (MacroStatement) r; }; Func<MacroStatement, Block> extract = m =>{ if (null == m){ return null; } if (m.Body != null && !m.Body.IsEmpty){ if (m.Body.Statements.Count == 1){ if (m.Body.Statements[0] is ExpressionStatement){ Expression _ex = ((ExpressionStatement) m.Body.Statements[0]). Expression; if (_ex is LiteralExpression || !_ex.ToCodeString().ToLower().StartsWith("out")){ return new Block().add(_ex.writeOut()); } } } return m.Body; } if (m.Arguments.Count == 0){ return null; } var r = new Block(); if (!(m.Arguments[0] is MethodInvocationExpression)){ r.Add(BrailBuildingHelper.WriteOut(m.Arguments[0])); } else{ r.Add(m.Arguments[0]); } return r; }; MacroStatement beforeall_macro = findmacro("beforeall"); MacroStatement onitem_macro = findmacro("onitem") ?? macro; MacroStatement onerror_macro = findmacro("onerror"); ; MacroStatement between_macro = findmacro("between"); ; MacroStatement afterall_macro = findmacro("afterall"); ; MacroStatement onempty_macro = findmacro("onempty"); ; MacroStatement beforeeach_macro = findmacro("beforeeach"); ; MacroStatement aftereach_macro = findmacro("aftereach"); MacroStatement prepare_macro = findmacro("prepare"); ; Block beforeall = extract(beforeall_macro); Block onitem = extractMainItemBlock(macro, extract, onitem_macro, item, prefix, suffix); Block onerror = extract(onerror_macro); Block between = extract(between_macro); Block afterall = extract(afterall_macro); Block onempty = null; bool proceed_on_empty = false; if (onempty_macro != null && onempty_macro.Arguments.Count != 0 && onempty_macro.Arguments[0].ToCodeString() == "proceed"){ proceed_on_empty = true; } else{ onempty = extract(onempty_macro); } Block beforeeach = extract(beforeeach_macro); Block aftereach = extract(aftereach_macro); // _idx = 0 Statement betweener = getBetweener(idx, between); result.Add(col.assign(new MethodInvocationExpression(new ReferenceExpression("_wrapcollection"), src))); result.Add(new ReferenceExpression("___proceed").assign(new BoolLiteralExpression(proceed_on_empty))); var mainblock = new Block(); mainblock.Add(idx.assign(0)); mainblock.Add(new IfStatement( new BinaryExpression(BinaryOperatorType.Equality, new NullLiteralExpression(), col), new Block().add(new BinaryExpression(BinaryOperatorType.Assign, col, new MethodInvocationExpression( new ReferenceExpression("_wrapcollection"), new ArrayLiteralExpression()))), null )); if (beforeall != null){ mainblock.Add(beforeall); } var maincycle = new ForStatement(); maincycle.Iterator = col; string declname = item.ToCodeString(); TypeReference decltype = null; if (item is TryCastExpression){ declname = ((TryCastExpression) item).Target.ToCodeString(); decltype = ((TryCastExpression) item).Type; } maincycle.Declarations.Add(new Declaration(maincycle.LexicalInfo, declname, decltype)); maincycle.Block = new Block(); maincycle.ThenBlock = afterall; if (betweener != null){ maincycle.Block.Add(betweener); } if (null != prepare_macro){ maincycle.Block.Add(prepare_macro.Body); } if (null != beforeeach){ maincycle.Block.Add(beforeeach); } if (onerror == null){ maincycle.Block.Add(onitem); } else{ var trycatch = new TryStatement(); var exchandler = new ExceptionHandler(); exchandler.Block = onerror; exchandler.Declaration = new Declaration("_ex", new SimpleTypeReference("System.Exception")); trycatch.ProtectedBlock = onitem; trycatch.ExceptionHandlers.Add(exchandler); maincycle.Block.Add(trycatch); } if (null != aftereach){ maincycle.Block.Add(aftereach); } maincycle.Block.Add(new UnaryExpression(UnaryOperatorType.Increment, idx)); mainblock.Add(maincycle); result.Add( new IfStatement( getMainCondition(col), mainblock, onempty ) ); // if null!=items and (items as IEnumerable).Cast[of System.Object]().Count() != 0: return result; }
public override void OnExceptionHandler(ExceptionHandler node) { WriteIndented(); WriteKeyword("except"); if ((node.Flags & ExceptionHandlerFlags.Untyped) == ExceptionHandlerFlags.None) { if((node.Flags & ExceptionHandlerFlags.Anonymous) == ExceptionHandlerFlags.None) { Write(" "); Visit(node.Declaration); } else { WriteTypeReference(node.Declaration.Type); } } else if((node.Flags & ExceptionHandlerFlags.Anonymous) == ExceptionHandlerFlags.None) { Write(" "); Write(node.Declaration.Name); } if((node.Flags & ExceptionHandlerFlags.Filter) == ExceptionHandlerFlags.Filter) { UnaryExpression unless = node.FilterCondition as UnaryExpression; if(unless != null && unless.Operator == UnaryOperatorType.LogicalNot) { WriteKeyword(" unless "); Visit(unless.Operand); } else { WriteKeyword(" if "); Visit(node.FilterCondition); } } WriteLine(":"); Indent(); WriteBlockStatements(node.Block); Dedent(); }
override public object Clone() { ExceptionHandler clone = new ExceptionHandler(); clone._lexicalInfo = _lexicalInfo; clone._endSourceLocation = _endSourceLocation; clone._documentation = _documentation; clone._isSynthetic = _isSynthetic; clone._entity = _entity; if (_annotations != null) clone._annotations = (Hashtable)_annotations.Clone(); if (null != _declaration) { clone._declaration = _declaration.Clone() as Declaration; clone._declaration.InitializeParent(clone); } if (null != _filterCondition) { clone._filterCondition = _filterCondition.Clone() as Expression; clone._filterCondition.InitializeParent(clone); } clone._flags = _flags; if (null != _block) { clone._block = _block.Clone() as Block; clone._block.InitializeParent(clone); } return clone; }
public object VisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data) { B.TryStatement t = new B.TryStatement(GetLexicalInfo(tryCatchStatement)); t.EndSourceLocation = GetLocation(tryCatchStatement.EndLocation); t.ProtectedBlock = ConvertBlock(tryCatchStatement.StatementBlock); t.EnsureBlock = ConvertBlock(tryCatchStatement.FinallyBlock); foreach (CatchClause clause in tryCatchStatement.CatchClauses) { B.ExceptionHandler handler = new B.ExceptionHandler(GetLexicalInfo(clause)); handler.Block = ConvertBlock(clause.StatementBlock); B.TypeReference typeRef = ConvertTypeReference(clause.TypeReference); string name = clause.VariableName; if (typeRef != null) { if (name == null || name.Length == 0) name = GenerateName(); handler.Declaration = new B.Declaration(name, typeRef); } else { if (name != null && name.Length > 0) handler.Declaration = new B.Declaration(name, null); } t.ExceptionHandlers.Add(handler); } return t; }
public void try_statement(Block container) { IToken token = null; IToken token2 = null; IToken token3 = null; try { TryStatement statement; Block block; token = this.LT(1); this.match(0x2b); if (base.inputState.guessing == 0) { statement = new TryStatement(ToLexicalInfo(token)); block = statement.get_ProtectedBlock(); container.Add(statement); } this.compound_or_single_stmt(block); while (true) { TypeReference reference; if ((this.LA(1) != 7) || (this.LA(2) != 0x3f)) { break; } token2 = this.LT(1); this.match(7); this.match(0x3f); token3 = this.LT(1); this.match(0x3b); switch (this.LA(1)) { case 0x42: this.match(0x42); reference = this.type_reference(); break; case 0x40: break; default: throw new NoViableAltException(this.LT(1), this.getFilename()); } this.match(0x40); if (base.inputState.guessing == 0) { Declaration declaration; ExceptionHandler handler; if (reference == null) { reference = new SimpleTypeReference(ToLexicalInfo(token3), "System.Exception"); } ExceptionHandler handler1 = handler = new ExceptionHandler(ToLexicalInfo(token2)); Declaration declaration1 = declaration = new Declaration(ToLexicalInfo(token3)); declaration.set_Name(token3.getText()); declaration.set_Type(reference); handler.set_Declaration(declaration); ExceptionHandler handler2 = handler; statement.get_ExceptionHandlers().Add(handler2); block = handler2.get_Block(); reference = null; } this.compound_or_single_stmt(block); } if ((this.LA(1) != 0x11) || !tokenSet_2_.member(this.LA(2))) { if (!tokenSet_15_.member(this.LA(1)) || !tokenSet_20_.member(this.LA(2))) { throw new NoViableAltException(this.LT(1), this.getFilename()); } } else { this.finally_block(statement); } } catch (RecognitionException exception) { if (base.inputState.guessing != 0) { throw; } this.reportError(exception); this.recover(exception, tokenSet_15_); } }