How to get these lines and blocks?

Asked

Viewed 38 times

0

I’m creating a parser of a language I’m developing, I’m having trouble separating lines and blocks from code, follow the code I use to return 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 on each item in the object Itens() 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 index 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 don’t understand the fact of returning only the first element, it suits the question problem. In other words, the member Itens() is only with the object tudo bem com você? isso aqui deve ser um elemento, and without the others, that’s what I don’t understand.

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

1 answer

1


You’re going through the lines through the while. When you’re not on a block, you walk out of it... so you’re just taking 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.