How to get these lines and blocks?

0

I'm creating a parser of a language I'm developing, I'm having trouble separating lines and blocks from code, here's the code I use to return the lines:

Friend Class Splitter
    Public Itens As New List(Of String)
    Private CurrentIndex As Integer = 0
    Private IsOnBlock As Boolean
    Public Sub New(ByVal code As String)
        Dim Returned As String = ""
        Dim Lines As String() = code.Split(vbNewLine)
        Dim I As Int32 = CurrentIndex
        While (Not I >= Lines.Count - 1)
            Dim Text As String = Lines(I).TrimStart
            I += 1
            CurrentIndex = I

            If Text.StartsWith("if ") Then
                IsOnBlock = True
                Returned &= Text & vbNewLine
            ElseIf Text.StartsWith("end ") Then
                IsOnBlock = False
                Returned &= Text
            Else
                Returned &= Text
            End If

            If IsOnBlock Then
                Continue While
            Else
                Exit While
            End If

        End While
        Itens.Add(Returned)
    End Sub
End Class

I need to return in each item in the object Itens() the line code and its due block, for example:

tudo bem com você? isso aqui deve ser um elemento
if iniciou um bloco, vamos capturar tudo nele
     ainda faço parte do segundo elemento
     eu também faço parte do segundo 
end acabou o elemento
aqui já é outra coisa

Following in their respective positions and indexes of the elements:

0    tudo bem com você? isso aqui deve ser um elemento
1    if iniciou um bloco, vamos capturar tudo nele
          ainda faço parte do segundo elemento
          eu também faço parte do segundo 
2    end acabou o elemento
3    aqui já é outra coisa

But I do not understand returning only the first element, so the problem of the question is appropriate. In other words, member Itens() is only with object tudo bem com você? isso aqui deve ser um elemento , and without others, this is what I do not understand.

  

Note: I also accept responses in regular expressions such as to help get those lines ...

    
asked by anonymous 13.02.2016 / 01:58

1 answer

1

You are passing through the rows through while . When you're not in a block, you get out of it ... so you're just getting the first line.

Take this test:

Friend Class Splitter
    Public Itens As New List(Of String)
    Private CurrentIndex As Integer = 0
    Private IsOnBlock As Boolean
    Public Sub New(ByVal code As String)
        Dim Returned As String = ""
        Dim Lines As String() = code.Split(vbNewLine)
        Dim I As Int32 = CurrentIndex
        While (Not I >= Lines.Count - 1)
            Dim Text As String = Lines(I).TrimStart
            I += 1
            CurrentIndex = I

            If Text.StartsWith("if ") Then
                IsOnBlock = True
                Returned &= Text & vbNewLine
            ElseIf Text.StartsWith("end ") Then
                IsOnBlock = False
                Returned &= Text
            Else
                Returned &= Text
            End If

            If IsOnBlock Then
                Continue While
            Else
                Itens.Add(Returned)
                Returned = ""
            End If

        End While
    End Sub
End Class
    
13.02.2016 / 13:20