Discussion Different Commander Parser

Nestharus

o-o
Reaction score
84
released:
parser http://www.thehelper.net/forums/showthread.php?t=152352
Cmd http://www.thehelper.net/forums/showthread.php?t=152356

So this is the current one-
http://www.thehelper.net/forums/showthread.php?t=140936

I don't like it... for example, it's many isType and getType methods irritate me and the fact that it couples up the parser and the command manager just seems like an extremely poor design to me >.<..

So, I've been working on a 2 part project-
parser (highly customizable) (done)
command framework (support for different shells and etc)

The parser includes a string stack object that will automatically split a string up into a stack given a delimhttp://www.thehelper.net/forums/forumdisplay.php?f=109iter, a typeof to automatically infer the data type of a given string ('' is treated as ascii and rest are inferred based upon the values). For the most part, types fit into other types. For example, an integer can be treated as a real or a string (follows this hierarchy on data types).

JASS:
struct StringType extends array
        public static constant integer NULL = 0
        public static constant integer BOOLEAN = 1
        public static constant integer ASCII = 2
        public static constant integer INTEGER = 3
        public static constant integer REAL = 4
        public static constant integer STRING = 5


names can also be printed given a type id
JASS:
////
        public static method operator [] takes integer t returns string
            return typeNames[t]
        endmethod


The typeof method automatically infers types (as I said above)
JASS:
/////
        public static method typeof takes string val returns integer
            local integer length
            local string char
            local integer curType = NULL
            local string boolChecker
            local boolean foundDecimal
            
            //make sure not null
            if (val != null) then
                set curType = BOOLEAN
                //check to see if boolean
                set boolChecker = StringCase(val, false)
                if (boolChecker != &quot;true&quot; and boolChecker != &quot;false&quot;) then
                    set length = StringLength(val)
                    set curType = ASCII
                    
                    //check to see if ascii integer
                    if ((length != 3 and length != 6) or (SubString(val, 0, 1) != &quot;&#039;&quot; or SubString(val, length-1, length) != &quot;&#039;&quot;)) then
                        set curType = INTEGER
                        
                        //if curType can&#039;t be determined at this point, have to parse it
                        set foundDecimal = false
                        loop
                            exitwhen length == 0
                            set char = SubString(val, length-1, length)
                            if (char != &quot;0&quot; and char != &quot;1&quot; and char != &quot;2&quot; and char != &quot;3&quot; and char != &quot;4&quot; and char != &quot;5&quot; and char != &quot;6&quot; and char != &quot;7&quot; and char != &quot;8&quot; and char != &quot;9&quot;) then
                                if (char == &quot;.&quot; and not foundDecimal) then
                                    set curType = REAL
                                    set foundDecimal = true
                                else
                                    return STRING //no more parsing necessary
                                endif
                            endif
                            set length = length - 1
                        endloop
                    endif
                endif
            endif
            return curType
        endmethod


Obviously I'm still going to be doing some more stuff, but this is how I believe a Parser should be designed ; |. CommandParser is designed in such a way that I refuse to use it, lol.

also, I'm going to do different build strings for the StringStack struct (single argument builds for ripping specific types out of the string and etc).


example of use (with just the parser) (I know I have a ton of extra locals, lol) (uses default delimiter on the StringStack creation)
JASS:
struct tester extends array
    private static trigger t = CreateTrigger()
    private static string array typeName
    
    private static method test takes nothing returns boolean
        //call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, SubString(GetEventPlayerChatString(), 0, 1))
        //call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, SubString(GetEventPlayerChatString(), StringLength(GetEventPlayerChatString())-1, StringLength(GetEventPlayerChatString())))
        local StringStack stringStack = StringStack.create(GetEventPlayerChatString())
        local StringStack node = stringStack
        local integer stringType
        
        local boolean b
        local integer a
        local integer i
        local real r
        local string s
        
        local string bs
        local string as
        local string is
        local string rs
        local string ss
        
        local string value
        loop
            exitwhen node == 0
            set value = node.value
            set stringType = StringType.typeof(value)
            
            if (stringType == StringType.NULL) then
                set s = value
                call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, StringType[stringType] + &quot;: &quot; + s)
            elseif (stringType == StringType.BOOLEAN) then
                set b = StringType.S2B(value)
                set bs = StringType.B2S(b)
                call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, StringType[stringType] + &quot;: &quot; + bs)
            elseif (stringType == StringType.ASCII) then
                set a = StringType.S2A(value)
                set as = StringType.A2S(a)
                call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, StringType[stringType] + &quot;: &quot; + as)
            elseif (stringType == StringType.INTEGER) then
                set i = S2I(value)
                set is = I2S(i)
                call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, StringType[stringType] + &quot;: &quot; + is)
            elseif (stringType == StringType.REAL) then
                set r = S2R(value)
                set rs = R2S(r)
                call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, StringType[stringType] + &quot;: &quot; + rs)
            elseif (stringType == StringType.STRING) then
                call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, StringType[stringType] + &quot;: &quot; + value)
            endif
            
            //set node = node.next
            set node = node.pop()
        endloop
        
        //call stringStack.destroy()
        return false
    endmethod
    
    private static method onInit takes nothing returns nothing
        call TriggerRegisterPlayerChatEvent(t, Player(0), &quot;&quot;, false)
        call TriggerAddCondition(t, Condition(function thistype.test))
    endmethod
endstruct


ripper code
for single argument commands (provides more flexibility)
JASS:
////
        //rips out a single argument given a type
        //slow but extremely flexible
        public static method rip takes string val, integer typeToRip returns string
            local string arg = &quot;&quot;
            local integer length
            local integer count
            local string char
            local integer argLength
            local integer boolCount
            local boolean foundDecimal
            
            if (val != null) then
                set length = StringLength(val)
                set count = 0
                
                //rip null, which is easy, lol
                if (typeToRip == StringType.NULL) then
                    set arg = null
                //rip boolean, which tries to piece together straight** chars that could build boolean
                elseif (typeToRip == StringType.BOOLEAN) then
                    set boolCount = 0
                    loop
                        exitwhen count == length
                        set char = SubString(val, count, count+1)
                        if (boolCount == 0) then
                            if (char == boolChars[trueChar]) then
                                set boolCount = trueChar+1
                                set arg = char
                            elseif (char == boolChars[falseChar]) then
                                set boolCount = falseChar+1
                                set arg = char
                            endif
                        elseif (char == boolChars[boolCount]) then
                            set arg = arg + char
                            set boolCount = boolCount + 1
                            exitwhen arg == &quot;true&quot; or arg == &quot;false&quot;
                        else
                            set boolCount = 0
                            set arg = &quot;&quot;
                        endif
                        set count = count + 1
                    endloop
                    if (arg != &quot;true&quot; and arg != &quot;false&quot;) then
                        set arg = null
                    endif
                //rip out an ascii value, which rips out the first 3 to 6 possible chars it comes across
                elseif (typeToRip == StringType.ASCII) then
                    set foundDecimal = false //found &#039; ?
                    set argLength = 0
                    loop
                        exitwhen count == length or argLength == 6
                        set char = SubString(val, count, count+1)
                        set argLength = StringLength(arg)
                        //find start
                        if (argLength == 0 or argLength == 5 and char == &quot;&#039;&quot;) then
                            set arg = arg + &quot;&#039;&quot;
                        elseif (argLength &gt; 0 and argLength &lt; 5) then
                            if (argLength == 2 and char == &quot;&#039;&quot;) then
                                set foundDecimal = true
                            endif
                            set arg = arg + char
                        endif
                        set count = count + 1
                    endloop
                    set argLength = StringLength(arg)
                    //if maxed ascii and no final &quot;&#039;&quot;, check for previous one
                    if (argLength == 5 and foundDecimal) then
                        set arg = SubString(arg, 0, 2) + &quot;&#039;&quot;
                    elseif (argLength != 6 or (argLength != 3 and SubString(arg, argLength-1, argLength) != &quot;&#039;&quot;)) then
                        set arg = null
                    endif
                //rips out a plain old integer
                elseif (typeToRip == StringType.INTEGER) then
                    loop
                        exitwhen count == length
                        set char = SubString(val, count, count+1)
                        if (char == &quot;0&quot; or char == &quot;1&quot; or char == &quot;2&quot; or char == &quot;3&quot; or char == &quot;4&quot; or char == &quot;5&quot; or char == &quot;6&quot; or char == &quot;7&quot; or char == &quot;8&quot; or char == &quot;9&quot;) then
                            set arg = arg + char
                        endif
                        set count = count + 1
                    endloop
                    if (arg == &quot;&quot;) then
                        set arg = null
                    endif
                //rips out a real
                elseif (typeToRip == StringType.REAL) then
                    set foundDecimal = false
                    loop
                        exitwhen count == length
                        set char = SubString(val, count, count+1)
                        if (char == &quot;.&quot; and not foundDecimal) then
                            set foundDecimal = true
                            set arg = arg + char
                        elseif (char == &quot;0&quot; or char == &quot;1&quot; or char == &quot;2&quot; or char == &quot;3&quot; or char == &quot;4&quot; or char == &quot;5&quot; or char == &quot;6&quot; or char == &quot;7&quot; or char == &quot;8&quot; or char == &quot;9&quot;) then
                            set arg = arg + char
                        endif
                        set count = count + 1
                    endloop
                    if (arg == &quot;&quot;) then
                        set arg = null
                    endif
                else
                    set arg = val
                endif
            endif
            
            return arg
        endmethod


and how you might use the above
[ljass]local string arg = StringType.rip(GetEventPlayerChatString(), StringType.ASCII)[/ljass]
 

Jesus4Lyf

Good Idea™
Reaction score
397
Well done? Another failure interface? :(
I don't like it...
...
So, I've been working on a 2 part project
If you're looking to submit something and get approved, that's really not how things are done around here... you post in the thread when you don't like it, instead of jumping to remake it. :)
 

Nestharus

o-o
Reaction score
84
If you're looking to submit something and get approved, that's really not how things are done around here... you post in the thread when you don't like it, instead of jumping to remake it.

I'm very impatient. I'd rather rewrite it than wait for an unknown period of time for a response =P. Even 1 day on a response is too long for me = ).

oh, and how does the API fail? : o.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top