Implementation for ParseInterpolatedString

Coordinator
Aug 31, 2014 at 11:20 PM
Edited Aug 31, 2014 at 11:27 PM
ParseInterpolatedString
  Function ParseInterpolatedString(sourcetext As String,
                                      paramlist As InterpolatedArgs,
                                   _outputting_ As Boolean,
                                  _exitonerror_ As Boolean
                                 ) As OutputResult(Of String)

    Dim cc = PChar.Create(sourcetext)
    Dim result As New OutputResult(Of String)
    '
    ' -- Parse Header --
    '
    If TypeOf cc Is EOT           Then result.AddError(UnexpectedlyReachedEndOfText(cc)) : GoTo _exit_
    If cc <> _InterpolatedString_ Then result.AddError(Errors.UnexpectedChar(cc)) : GoTo _exit_
    cc = cc.Next
    If TypeOf cc Is EOT           Then result.AddError(UnexpectedlyReachedEndOfText(cc)) : GoTo _exit_
    If cc <> _OpeningParenthesis_ Then result.AddError(Errors.UnexpectedChar(cc)) : GoTo _exit_
    cc = cc.Next
    Dim InsideParenthesis = True
    If TypeOf cc Is EOT           Then result.AddError(UnexpectedlyReachedEndOfText(cc)) : GoTo _exit_
    If cc <> _DoubleQuote_        Then result.AddError(Errors.UnexpectedChar(cc)) : GoTo _exit_
    '
    ' -- Parse internals --
    '
    Dim output As New Text.StringBuilder
    Dim Quoted = False
    Dim InsideHole = False
    Dim InsideBrace = True
    While TypeOf cc IsNot EOT
      ' Skip over a qouted character
      If Quoted Then
        cc = cc.Next.Next
        Quoted = False
      End If
      '
      '
      '
      Select Case cc.Value
        Case _DoubleQuote_  When cc.Next = _Comma_          :  GoTo _EndOfFormatString_
        Case _ClosingBrace_ When cc.Next = _ClosingBrace_   :  Quoted = True
        Case _ClosingBrace_ When Not InsideHole             :  result.AddError(Errors.MismatchedClosingBrace(cc)) ' Add continue on error functionality
        Case _ClosingBrace_                                 :  InsideHole = False
        Case _OpeningBrace_ When cc.Next = _OpeningBrace_   :  Quoted = True
        Case _OpeningBrace_ When InsideHole                 :  result.AddError(Errors.RecurisiveOpeningBrace(cc)) ' Add continue on error functionality
        Case _OpeningBrace_                                 :  InsideHole = True
        Case Else
          If _outputting_ Then output.Append(cc.Value)
      End Select

      If Not Quoted Then
        cc = ConsumeSpaces(cc)
        If TypeOf cc Is EOT Then result.AddError(UnexpectedlyReachedEndOfText(cc))
        If InsideHole Then
          Dim _Arg_ As OutputResult(Of Object) = Nothing
          Select Case cc.Value
            Case "$"c          :  _Arg_ = Parse_Identifer(cc, paramlist)
            Case "0"c To "9"c  :  _Arg_ = Parse_Index(cc, paramlist)
            Case "A"c To "Z"c  ' TODO: Needs more valid chars for an named parameter ***
              _Arg_ = Parse_Named(cc, paramlist)
            Case Else
              result.AddError(Errors.UnexpectedChar(cc)) ' Add continue on error functionality
          End Select
          If _Arg_ Is Nothing Then
            result.AddError(Errors.FatalError())
            GoTo _exit_
          End If
          result.IncludeErrorsFrom(_Arg_)
          Dim _Alignment_ = Parse_Alignment(cc)
          If _Alignment_.IsValid Then
            ' TODO: 
          Else
            ' TODO:
          End If
          Dim _Formatting_ = Parse_Format(cc)
          ' Find the closing brace.
          If _Formatting_.LastParse = _ClosingBrace_ Then
            ' TODO: 
          Else
            ' TODO:
          End If
          ' Add rest of parsing mostly validation and outputting.  
        Else
          If _outputting_ Then output.Append(cc.Value)
        End If
      Else
        If _outputting_ Then output.Append(cc.Value)
      End If
    End While
_EndOfFormatString_:
    If InsideHole Then
      result.AddError(Errors.UnexpectedlyReachedEndOfText(cc)) ' Add continue on error functionality
      result.AddError(Errors.MismatchedClosingBrace(cc)) ' Add continue on error functionality
    Else

    End If
    If _outputting_ Then result.Output = output.ToString()
_exit_:
    result.LastParse = cc
    Return result
  End Function

  Private Function Parse_Identifer(cc As PChar, paramlist As InterpolatedArgs) As OutputResult(Of Object)
    ' TODO:
    Throw New NotImplementedException()
  End Function

  Private Function Parse_Named(cc As PChar, paramlist As InterpolatedArgs) As OutputResult(Of Object)
    ' TODO:
    Throw New NotImplementedException()
  End Function
Constants
Module Constants
  Public Const _MINUS_         As Char    = "-"c
  Public Const _DoubleQuote_   As Char    = """"c
  Public Const _MAXWIDTH_      As Integer = 1000000
  Public Const ParamCountLimit As Integer = 1000000
  Public Const _ClosingBrace_  As Char    = "}"c
  Public Const _OpeningBrace_  As Char    = "{"c
  Public Const _Colon_ As Char = ":"c
  Public Const _Comma_ As Char = ","c
  Public Const _SPACE_ As Char = " "c
  Public Const _InterpolatedString_ As Char = "$"c
  Public Const _OpeningParenthesis_ As Char = "("c
  Public Const _ClosingParenthesis_ As Char = ")"c
End Module

PChar

Public Class PChar
  Private _Index_ As Integer
  Private _Source_ As String

  Private Sub New(Source As String,Index As Integer)
    _Source_ = Source
    _Index_ = Index 
  End Sub

  Public Function [Next] As PChar
    If _Index_ < (_Source_.Length - 2) Then
      Return New PChar(_Source_, _Index_ + 1)
    Else
      Return New EOT
    End If

  End Function
  Public ReadOnly Property Value As Char
    Get
      Return _Source_(_Index_)
    End Get
  End Property

  Shared Operator <>(pc As PChar, c As Char) As Boolean
    Return pc.Value <> c
  End Operator
  Shared Operator =(pc As PChar, c As Char) As Boolean
    Return pc.Value = c
  End Operator

 Shared Public Function Create( text As String) As PChar
    Return New PChar(text,0) 
  End Function

End Class



Public Class EOT
   Inherits PChar

End Class