IDEA: IsAnyOf and IsNoneOf for TypeOf expression.

Topics: VB Language Design
May 17, 2014 at 1:10 PM
During my review and modification of the Visual Basic compiler source code.
I notice that there some section that could be shorted by the inclusion of two addition contextual operators for the TypeOf expression.

 TypeOf obj IsAnyOf  { t_0, t_1, t_2, t_3 }
 TypeOf obj IsNoneOf { t_0, t_1, t_2, t_3 } 

Current
    Public Function GetContainingMemberDeclaration(      root As SyntaxNode,
                                                     position As Integer
                                                   ) As SyntaxNode Implements ISyntaxFactsService.GetContainingMemberDeclaration
      Contract.ThrowIfNull(root, "root")
      Contract.ThrowIfTrue(position < 0 OrElse position > root.FullSpan.End, "position")
      Dim [end] = root.FullSpan.End
      If [end] = 0 Then Return Nothing ' empty file
      ' make sure position doesn't touch end of root
      position = Math.Min(position, [end] - 1)

      Dim node = root.FindToken(position).Parent
      While node IsNot Nothing
        Select Case True
          Case (TypeOf node Is MethodBlockBaseSyntax  ) AndAlso (TypeOf node.Parent IsNot PropertyBlockSyntax) : Return node
          Case (TypeOf node Is PropertyStatementSyntax) AndAlso (TypeOf node.Parent IsNot PropertyBlockSyntax) : Return node
          Case (TypeOf node Is EventStatementSyntax   ) AndAlso (TypeOf node.Parent IsNot EventBlockSyntax   ) : Return node
        End Select

        If TypeOf node Is PropertyBlockSyntax OrElse
           TypeOf node Is TypeBlockSyntax OrElse
           TypeOf node Is EnumBlockSyntax OrElse
           TypeOf node Is NamespaceBlockSyntax OrElse
           TypeOf node Is EventBlockSyntax OrElse
           TypeOf node Is FieldDeclarationSyntax Then
          Return node
        End If

        node = node.Parent
      End While

      Return Nothing
    End Function
With the IsAnyOf operator
    Public Function GetContainingMemberDeclaration(      root As SyntaxNode,
                                                     position As Integer
                                                   ) As SyntaxNode Implements ISyntaxFactsService.GetContainingMemberDeclaration
      Contract.ThrowIfNull(root, "root")
      Contract.ThrowIfTrue(position < 0 OrElse position > root.FullSpan.End, "position")
      Dim [end] = root.FullSpan.End
      If [end] = 0 Then Return Nothing ' empty file
      ' make sure position doesn't touch end of root
      position = Math.Min(position, [end] - 1)

      Dim node = root.FindToken(position).Parent
      While node IsNot Nothing
        Select Case True
          Case (TypeOf node Is MethodBlockBaseSyntax  ) AndAlso (TypeOf node.Parent IsNot PropertyBlockSyntax) : Return node
          Case (TypeOf node Is PropertyStatementSyntax) AndAlso (TypeOf node.Parent IsNot PropertyBlockSyntax) : Return node
          Case (TypeOf node Is EventStatementSyntax   ) AndAlso (TypeOf node.Parent IsNot EventBlockSyntax   ) : Return node
        End Select

        If TypeOf node IsAnyOf { PropertyBlockSyntax,
                                 TypeBlockSyntax,
                                 EnumBlockSyntax,
                                 NamespaceBlockSyntax,
                                 EventBlockSyntax,
                                 FieldDeclarationSyntax } Then
          Return node
        End If

        node = node.Parent
      End While

      Return Nothing
    End Function