0

I am developing a server-side scripting language which I intend to use on my private server. It is similar to PHP, and I know that I could easily use PHP instead but I'm just doing some programming for fun.

The syntax of basic commands in my language is as follows:

command_name "parameter1" : "parameter2" : "parameter3"

But it can also be like this when I want to join values for a parameter:

command_name "parameter1" : "param" & "eter2" : "par" & "amet" & "er3"

How would I go about parsing a string like the ones shown above (it will be perfectly typed, no syntax errors) to an object that has these properties

  • Custom class "Request"
  • Property "Command" as String, should be the "command_name" part
  • Property "Parameters" as String(), should be an array of Parameter objects
  • Shared Function FromString(s As String) as Request, this should accept a string in the language above and parse it to a Request object
  • Custom class "Parameter"
  • Property "Segments" as String(), for example "para", "mete", and "r3"
  • Sub New(ParamArray s as String()), this is how it should be generated from the code

It should be done in VB.NET and I am a moderate level programmer, so even if you just have an idea of how to attack this then please share it with me. I am very new to parsing complex data like this so I need a lot of help. Thanks so much!

2 Answers 2

1

Here is another method that is simpler.

Module Module1

    Sub Main()
        Dim inputs As String() = {"command_name ""parameter1"" : ""parameter2"" : ""parameter3""", "command_name ""parameter1"" : ""param"" & ""eter2"" : ""par"" & ""amet"" & ""er3"""}

        For Each _input As String In inputs
            Dim commandStr As String = _input.Substring(0, _input.IndexOf(" ")).Trim()
            Dim parameters As String = _input.Substring(_input.IndexOf(" ")).Trim()
            Dim parametersA As String() = parameters.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(Function(x) x.Trim()).ToArray()
            Dim parametersB As String()() = parametersA.Select(Function(x) x.Split("&".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(Function(y) y.Trim(" """.ToCharArray())).ToArray()).ToArray()

            Dim newCommand As New Command() With {.name = commandStr, .parameters = parametersB.Select(Function(x) New Parameter(x)).ToArray()}
            Command.commands.Add(newCommand)
        Next (_input)

        Dim z = Command.commands

    End Sub

End Module

Public Class Command
    Public Shared commands As New List(Of Command)

    Public name As String
    Public parameters As Parameter()
End Class
Public Class Parameter
    Sub New()

    End Sub
    Sub New(names As String())
        Me.names = names
    End Sub
    Public names As String()
End Class
Sign up to request clarification or add additional context in comments.

1 Comment

Learnt a lot from this, thanks so much :) I will tweak it to make it work a bit better
0

I figured it out myself

Module Module1

Sub Main()
    Dim r As Request = Request.Parse(Console.ReadLine())
    Console.WriteLine("The type of request is " & r.Name)
    For Each p As Parameter In r.Parameters
        Console.WriteLine("All segments inside of parameter " & r.Parameters.IndexOf(p).ToString)
        For Each s As String In p.Segments
            Console.WriteLine("    Segment " & p.Segments.IndexOf(s).ToString & " is " & s)
        Next
    Next
    Main()
End Sub

Public Class Request
    Public Name As String
    Public Parameters As New List(Of Parameter)
    Public Shared Function Parse(line As String)
        Dim r As New Request
        r.Name = line.Split(" ")(0)
        Dim u As String = line.Substring(line.IndexOf(" "), line.Length - line.IndexOf(" "))
        Dim p As String() = u.Split(":")
        For Each n As String In p
            Dim b As String() = n.Split("&")
            Dim e As New List(Of String)
            For Each m As String In b
                Dim i As Integer = 0
                Do Until i > m.Length - 1
                    If m(i) = ControlChars.Quote Then
                        Dim s As String = ""
                        i += 1
                        Do Until i > m.Length - 1 Or m(i) = ControlChars.Quote
                            s &= m(i)
                            i += 1
                        Loop
                        e.Add(s)
                    End If
                    i += 1
                Loop
            Next
            r.Parameters.Add(New Parameter(e.ToArray))
        Next
        Return r
    End Function
End Class

Public Class Parameter
    Public Segments As New List(Of String)
    Public Sub New(ParamArray s As String())
        Segments = s.ToList
    End Sub
End Class
End Module

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.