Prompt Reader Enhanced

After discovering that My Little TextFile Parser and HexScope couldn't handle statements with an ' in a sentence ultimately causing it to implode and corrupt the output. The cost of implementing such fixes weighed more than creating from anew.

Redesigning the idea of the file format being an JSON like CSV, I want to implement a feature-set that can parse two lines and count them as one data block sequence. This would enable such powerful features as execution and immutability.

{"executable"},{"immutable"}
{"%[set version "9.0"]%"},{"%_tcl_%"},{"My version is $version"}

Execution is the idea that the command set version "9.0" would be safe guarded from execution if the non-execution flag was given. With the immutable flag the variable "version" would be transformed to an read-only state stopping any further modifications.

I plan to implement such features when I get some time to improve however the so-far result is an robust; rudimentary hex parser. It can perform successful syntax checking on an single one data block in a file a the moment.

While it can technically complete a single row of data blocks, the checking sequence on the ending condition is not checked upon the individual data blocks. It's only performed on the ending of the whole line so I need to jiggle the logic to incorporate checking of a comma (which it does) but determine if there is another data block ahead of itself.

More commenting is required but the debug outputs should guide you enough. It needs work.

On a note of voodoo that you may notice within the reference code:

switch statements normally follow a pattern of:

switch condition
case
default

I have started utilizing default as the starting case. This defeats the whole purpose of the switch however implementing the same switch a second time within grants you fun. So you will see unconventional switch statements such as:

  switch condition
  default
    same switch condition
    case
    default

Examples from the script below are executed on a single data block: {"%_tcl_%"}.

>? cool, we matched a open parenthesis :: at 0 | 1
>? scoping x_pos 2 & y_pos 3 :: for a quotation mark
>? 22 :: Scoped result
>? we do :: have quotation mark
>? 7b :: Is our previous character an open parenthesis
>? open parenthesis discovered :: starting sequence success!
>? >> {"%_tcl_%"} << :: lets see
>? 7d :: I found closed parenthesis at the end of my hex string
>? syntax check :: is my previous character a quotation mark?
>? it is! :: a quotation mark
>? end of line :: Correct sequence!
>? 00 : 7b :: 01 :  {
>? 02 : 22 :: 03 :  "
>? 04 : 25 :: 05 :  %
>? 06 : 5f :: 07 :  _
>? 08 : 74 :: 09 :  t
>? 10 : 63 :: 11 :  c
>? 12 : 6c :: 13 :  l
>? 14 : 5f :: 15 :  _
>? 16 : 25 :: 17 :  %
>? 18 : 22 :: 19 :  "
>? 20 : 7d :: 21 :  }

Without an ending quotation mark

>? 7b22255f74636c5f257d :: data line in base16
>? cool, we matched a open parenthesis :: at 0 | 1
>? scoping x_pos 2 & y_pos 3 :: for a quotation mark
>? 22 :: Scoped result
>? we do :: have quotation mark
>? 7b :: Is our previous character an open parenthesis
>? open parenthesis discovered :: starting sequence success!
>? >> {"%_tcl_%} << :: lets see
>? 7d :: I found closed parenthesis at the end of my hex string
>? syntax check :: is my previous character a quotation mark?
>? syntax error :: missing a ending quotation mark
>? syntax state :: halted

Without a front parenthesis

>? 22255f74636c5f25227d :: data line in base16
>? syntax :: error
>? >> 22 @ 0 1 << :: lets see
>? syntax error :: missing a front open parenthesis
>? syntax state :: halted

The code.

namespace eval crystal {}
proc crystal::2Hex         {input}               { binary encode hex [encoding convertto utf-8 "$input"] }             ;#end procedure
proc crystal::2Base        {input}               { encoding convertfrom utf-8 [binary decode hex "$input"] }           ;#end procedure
proc crystal::debug        {debug input display} { switch $debug { 1 { puts ">? $input :: $display" } } }              ;#end procedure

variable data "7b22255f74636c5f25227d"
variable hexString $data
crystal::debug 1 "$data" "my data line"
crystal::debug 0 "[crystal::2Hex $data]" "my line in base16"
set xy_pos(x) {0}
set xy_pos(y) {1}

#   switch range [crystal::2Hex $data] 0 1]
#       This switch checks the first two characters
#       each cahracter is represented by two digits A-F to 0-9
#       7b = open parenthesis
#                7d = closed parenthesis
#       xy_pos is an array carrying the x and y position of our scope
#       If the scope matches than action
#   Case
#       23 - hexidemical representation of the character hash
#       7b - hexidemical representattion of the character open parenthesis

# As we want to compare the previous character we need to start ahead of the first character
# x y pos                x y pos
# 0 1 = first characer | 2 3 = second character

crystal::debug 1 "$data" "data line in base16"
switch [llength $hexString] {
        0   {
                # do nothing as our string is empty
        }
        default {
                switch [string range $hexString 2 3]  {
                0   {
                        # do nothing as our string is empty
                }
                22 {
                        # Our first scope found an open parenthesis
                crystal::debug 1 "cool, we matched a open parenthesis" "at $xy_pos(x) | $xy_pos(y)" ;# Matched an open parenthesis (7b) \{
                crystal::debug 1 "scoping x_pos [expr $xy_pos(x)+2] & y_pos [expr $xy_pos(y)+2]" "for a quotation mark"

                switch [string range $hexString $xy_pos(x)+2 $xy_pos(y)+2] {
                        default {
                                crystal::debug 1 "[string range $hexString $xy_pos(x)+2 $xy_pos(y)+2]" "Scoped result"
                                switch [string range $hexString $xy_pos(x)+2 $xy_pos(y)+2]  {
                                        22 {
                                                crystal::debug 1 "we do" "have quotation mark"
                                                # This means we now have the open parenthesis
                                                # And that we now have our quotation
                                                switch [string range $hexString $xy_pos(x) $xy_pos(y)] {
                                                        default {
                                                                crystal::debug 1 "[string range $hexString $xy_pos(x) $xy_pos(y)]" "Is our previous character an open parenthesis"
                                                                switch [string range $hexString $xy_pos(x) $xy_pos(y)] {
                                                                        7b {
                                                                                crystal::debug 1 "open parenthesis discovered" "starting sequence success!"
                                                                                crystal::debug 1 ">> [crystal::2Base [string range $hexString $xy_pos(x) end]] <<" "lets see"
                                                                                switch [string range $hexString end-1 end] {
                                                                                        7d {
                                                                                                crystal::debug 1 "[string range $hexString end-1 end]" "I found closed parenthesis at the end of my hex string"
                                                                                                crystal::debug 1 "syntax check" "is my previous character a quotation mark?"
                                                                                                        switch [string range $hexString end-3 end-2] {
                                                                                                                22 {
                                                                                                                        crystal::debug 1 "it is!" "a quotation mark"
                                                                                                                        crystal::debug 1 "end of line" "Correct sequence!"
                                                                                                                }
                                                                                                                default {
                                                                                                                        crystal::debug 1 "syntax error" "missing a ending quotation mark" ; crystal::debug 1 "syntax state" "halted" ; return
                                                                                                                }
                                                                                                        } ;# end switch
                                                                                        }
                                                                                        default {
                                                                                                crystal::debug 1 "is my" "next character a comma?"
                                                                                                switch [string range $hexString end-1 end] {
                                                                                                        2c {
                                                                                                                crystal::debug 1 "cool" "and a comma."
                                                                                                                switch [string range $hexString end-3 end-2] {
                                                                                                                        7d {
                                                                                                                                crystal::debug 1 "[string range $hexString end-5 end-4]" "scope"
                                                                                                                                switch [string range $hexString end-5 end-4] {
                                                                                                                                        default {
                                                                                                                                                switch [string range $hexString end-5 end-4] {
                                                                                                                                                        22 {
                                                                                                                                                                crystal::debug 1 "syntax check" "correct sequence!" ; crystal::debug 1 "syntax state" "correct" ; return
                                                                                                                                                        }
                                                                                                                                                        default {
                                                                                                                                                                crystal::debug 1 "syntax error" "missing ending quotation mark" ; crystal::debug 1 "syntax state" "halted" ; return
                                                                                                                                                        }
                                                                                                                                                } ;# end switch
                                                                                                                                        }
                                                                                                                                } ;# end switch
                                                                                                                        }
                                                                                                                        default {
                                                                                                                                crystal::debug 1 "syntax error" "missing a closed parenthesis" ; crystal::debug 1 "syntax state" "halted" ; return
                                                                                                                        }
                                                                                                                } ;# end switch
                                                                                                        }
                                                                                                        default {
                                                                                                                crystal::debug 1 "syntax error" "missing a closing parenthesis" ; crystal::debug 1 "syntax state" "halted" ; return
                                                                                                        }
                                                                                                } ;# end switch
                                                                                        }
                                                                                } ;# end switch
                                                                        }
                                                                } ;# end switch
                                                        }
                                                } ;# end switch
                                        }
                                } ;# end switch
                        }
                } ;# end switch

                set string_length [string length $data] ;# Discover the length of the hex string
                set hexString $data ;# define the hexstring to scope upon
                for {set z 0} {$z < $string_length} {incr z} {
                        # keep loop until z is the length of our hex string
                        # set our string range to the current range plus 2 counts
                        set string_range  [string range $hexString $xy_pos(x)+2 $xy_pos(y)+2] ;# we skip ahead by two as we already set 0 1
                        crystal::debug 0 "[crystal::2Base $string_range]" "letterbet 32" ;# Display our letterbet in base 32
                        crystal::debug 0 "$string_range" "letterbet 16"                  ;# Display our letterbet in base 16
                        crystal::debug 0 "[expr 1 + $xy_pos(x)]" "Is it nine?"

                        for { set val [expr $xy_pos(x)+1] } { $val >= 0 && $val <= 9 } {} {
                                crystal::debug 0 "[format "0%s" $xy_pos(x)]" "[format "0%s" $xy_pos(y)]"
                                crystal::debug 1 "[format "0%s" $xy_pos(x)] : [string range $hexString $xy_pos(x) $xy_pos(y)]" "[format "0%s" $xy_pos(y)] :  [crystal::2Base [string range $hexString $xy_pos(x) $xy_pos(y)]]"
                                break
                        } ;# end for

                        for { set val [expr $xy_pos(x)+1] } { $val >= 10 } {} {
                                crystal::debug 0 "$xy_pos(x)" "$xy_pos(y)"
                                crystal::debug 1 "$xy_pos(x) : [string range $hexString $xy_pos(x) $xy_pos(y)]" "$xy_pos(y) :  [crystal::2Base [string range $hexString $xy_pos(x) $xy_pos(y)]]"
                                break
                        } ;# end for

                                incr xy_pos(x) ; incr xy_pos(y)
                                incr xy_pos(x) ; incr xy_pos(y)
                } ;# end for
        }
        default {
                crystal::debug 1 "syntax" "error"
                switch [string range $hexString $xy_pos(x) $xy_pos(y)] {
                        default {
                                crystal::debug 1 ">> [string range $hexString $xy_pos(x) $xy_pos(y)] @ $xy_pos(x) $xy_pos(y) <<" "lets see"
                                switch [string range $hexString $xy_pos(x) $xy_pos(y)] {
                                        22 { crystal::debug 1 "syntax error" "missing open parenthesis" ; crystal::debug 1 "syntax state" "halted" ; return }
                                        default { crystal::debug 1 "syntax error" "missing quotation marks" ; crystal::debug 1 "syntax state" "halted" ; return }
                                }
                        }
                }
        }
} ;# end switch
        }
} ;#end switch