PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2015 Delegate with Multiple Signature Options-VBForums
Results 1 to 2 of 2

Thread: Delegate with Multiple Signature Options

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Delegate with Multiple Signature Options

    I am trying to build a parsing expression grammar in using Visual Studio 2015 targeting the .NET framework 4.5.

    Currently I have a couple of methods that are designed to build a parse tree:
    Code:
    Public Property source As String
    Public Function Literal(ByVal name As String, ByVal value As String) As XElement
        Dim element As XElement = Nothing
    
        If source.StartsWith(value, StringComparison.Ordinal) Then
            element = New XElement(name, source.Substring(0, value.Length))
        End If
    
        Return element
    End Function
    
    Public Function Literal(ByVal name As String, ByVal value As String, ByVal comparisonType As StringComparison) As XElement
        Dim element As XElement = Nothing
    
        If source.StartsWith(value, comparisonType) Then
            element = New XElement(name, source.Substring(0, value.Length))
        End If
    
        Return element
    End Function
    
    Public Function RegEx(ByVal name As String, ByVal pattern As String) As XElement
        Dim element As XElement = Nothing
        Dim match As RegularExpressions.Match = RegularExpressions.Regex.Match(source, pattern)
    
        If match.Success AndAlso match.Index = 0 Then
            element = New XElement(name, match.Value)
        End If
    
        Return element
    End Function
    
    Public Function RegEx(ByVal name As String, ByVal pattern As String, ByVal options As RegularExpressions.RegexOptions) As XElement
        Dim element As XElement = Nothing
        Dim match As RegularExpressions.Match = RegularExpressions.Regex.Match(source, pattern, options)
    
        If match.Success AndAlso match.Index = 0 Then
            element = New XElement(name, match.Value)
        End If
    
        Return element
    End Function
    What I want to do is create two more Functions that will match an alternate match and a sequential match, '|' and ',' respectively in extended backus-naur form(EBNF). My idea is to create a delegate Function that allows users to declare instances of the various Functions so that you can code like this:
    Code:
    Dim unaryOperator As Match = Alternate("unary", {Literal("positive", "+"), Literal("negative", "-", StringComparison.OrdinalIgnoreCase)})
    But as far as I know, you cannot do that with delegates. Do you know of any way to achieve what I'm wanting to do?
    Last edited by dday9; Dec 13th, 2016 at 05:28 PM.

  2. #2
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Delegate with Multiple Signature Options

    Quote Originally Posted by dday9 View Post
    ...My idea is to create a delegate Function that allows users to declare instances of the various Functions ...
    If I'm understanding you correctly, you have a default function definition, but want to allow the user to define a custom function definition. Is that correct? If so, then you could do something similar to this:
    vb.net Code:
    1. Friend Class Demo
    2.     Public Shared TwoParam As Func(Of String, String, XElement)
    3.     Public Shared ThreeParam As Func(Of String, String, StringComparison, XElement)
    4.  
    5.     Public Shared Function Literal(s1 As String, s2 As String) As XElement
    6.         Dim ret As XElement
    7.         If TwoParam Is Nothing Then
    8.             ret = LiteralDefault(s1, s2)
    9.         Else
    10.             ret = TwoParam(s1, s2)
    11.         End If
    12.         Return ret
    13.     End Function
    14.  
    15.     Public Shared Function Literal(s1 As String, s2 As String, comparisonType As StringComparison) As XElement
    16.         Dim ret As XElement
    17.         If ThreeParam Is Nothing Then
    18.             ret = LiteralDefault(s1, s2, comparisonType)
    19.         Else
    20.             ret = ThreeParam(s1, s2, comparisonType)
    21.         End If
    22.         Return ret
    23.     End Function
    24.  
    25.     Public Shared Function LiteralDefault(s1 As String, s2 As String) As XElement
    26.         Return New XElement("2param")
    27.     End Function
    28.  
    29.     Public Shared Function LiteralDefault(s1 As String, s2 As String, comparisonType As StringComparison) As XElement
    30.         Return New XElement("3param")
    31.     End Function
    32.  
    33. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width