Hi!

----

Attached are two scripts ("jsontest1_fail.sh.txt" and "
jsontest1_working.sh.txt" ; based on the JSON parser demo described in
http://lists.research.att.com/pipermail/ast-users/2013q2/003921.html)
which only differ in the detail that "main" is a function without
seperate scope in the working version and with seperate scope in the
version which no longer works:
-- snip --
  --- jsontest1_working.sh.txt    2013-05-26 04:29:17.691657859 +0200
+++ jsontest1_fail.sh.txt       2013-05-26 04:45:36.411650647 +0200
@@ -149,41 +149,41 @@

                                build_compound_tree js_ar[$jari] xar
$((i+1)) $k 0
                                [[ -v ar[6][$k] ]] && ((k++)) # ',' handling

                                (( i=k, jari++ ))
                                (( i > (j-1) )) && break
                        done

                        (( i=j ))
                fi

                [[ -v ar[6][$i] ]] && ((i++)) # ',' handling

                (( cari++ ))
        done

        return 0
 }


-main()
+function main
 {
        typeset -a xar
        parse_json xar "${jsontext}"
        #print -v xar

        compound -a xcar
        build_compound_tree xcar xar 1 ${#xar[0][@]} 0

        print -v xcar

        return 0
 }


 # program start
 builtin cat
 set -o nounset

 main
 exit $?
-- snip --

It seems there is somehow a problem with function-local variables and
nameref - IMO both scripts should produce identical output... ;-(

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) [email protected]
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
#!/usr/bin/ksh93

########################################################################
#                                                                      #
#               This software is part of the ast package               #
#                    Copyright (c) 2013 Roland Mainz                   #
#                      and is licensed under the                       #
#                 Eclipse Public License, Version 1.0                  #
#                    by AT&T Intellectual Property                     #
#                                                                      #
#                A copy of the License is available at                 #
#          http://www.eclipse.org/org/documents/epl-v10.html           #
#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
#                                                                      #
#                                                                      #
#                 Roland Mainz <[email protected]>              #
#                                                                      #
########################################################################

#
# Copyright (c) 2013, Roland Mainz. All rights reserved.
#

#
# jsonparse1 - a simple JSON parser 
#

typeset -r jsontext="$(
cat <<EOF
{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]


    "done": 666,

}

EOF
)"

function parse_json
{
        typeset jsontext="$2"
        nameref ar="$1"
        typeset dummy

        # fixme:
        # - We want to enforce standard conformance - does ~(Exp) or ~(Ex-p) 
does that ?
        dummy="${jsontext//~(Ex-p)(?:
                (?:\"([^\"]+?)\"):|     # name
                (\{)|                   # object begin
                (\})|                   # object end
                (\[)|                   # array start
                (\])|                   # array end
                (,)|                    #
                ([[:digit:]]+)|         # numerical value
                (?:\"([^\"]*?)\")       # string value
                )/D}"

        # debug output
#       print -v .sh.match
#       printf $"#dummy=%q\n" "${dummy}"

        # copy ".sh.match" to array "ar"
        # fixme: Use typeset -c instead
        integer i j
        for i in "${!.sh.match[@]}" ; do
                for j in "${!.sh.match[i][@]}" ; do
                        [[ -v .sh.match[i][j] ]] && 
ar[i][j]="${.sh.match[i][j]}"
                done
        done

        return 0
}


function build_compound_tree
{
        nameref car=$1
        nameref ar=$2
        integer i=$3
        integer ar_max=$4
        integer cari=$5 # car index

        integer j k nested
        bool foundobj

        for (( ; i < ar_max ; )) ; do
                # end of object ? then return...
                if [[ -v ar[3][$i] ]] ; then
                        return 0
                fi

                car[cari].name="${ar[1][$i]-}"
                ((i++))

                # string value
                if [[ -v ar[8][$i] ]] ; then
                        typeset car[cari].value="${ar[8][i++]}"
                elif [[ -v ar[7][$i] ]] ; then
                        float car[cari].value="${ar[7][i++]}"
                elif [[ -v ar[2][$i] ]] ; then
                        (( nested=0 , foundobj=false ))
                        for (( j=i ; (j < ar_max) && (nested > 0 || !foundobj) 
; j++ )) ; do
                                [[ -v ar[2][$j] ]] && ((nested++, 
foundobj=true))
                                [[ -v ar[3][$j] ]] && ((nested--))
                        done

                        compound -a car[$cari].value
                        build_compound_tree car[$cari].value xar $((i+1)) $j 0
                        (( i=j ))
                elif [[ -v ar[4][$i] ]] ; then
                        (( nested=0 , foundobj=false ))
                        for (( j=i ; (j < ar_max) && (nested > 0 || !foundobj) 
; j++ )) ; do
                                [[ -v ar[4][$j] ]] && ((nested++, 
foundobj=true))
                                [[ -v ar[5][$j] ]] && ((nested--))
                        done

                        compound -a car[$cari].arrayvalue
                        nameref js_ar=car[$cari].arrayvalue
                        integer jari=0 # json array index

                        ((i++))

                        for (( ;; )) ; do
                                (( nested=0 , foundobj=false ))
                                for (( k=i ; (k < j) && (nested > 0 || 
!foundobj) ; k++ )) ; do
                                        [[ -v ar[2][$k] ]] && ((nested++, 
foundobj=true))
                                        [[ -v ar[3][$k] ]] && ((nested--))
                                done

                                build_compound_tree js_ar[$jari] xar $((i+1)) 
$k 0
                                [[ -v ar[6][$k] ]] && ((k++)) # ',' handling

                                (( i=k, jari++ ))
                                (( i > (j-1) )) && break
                        done

                        (( i=j ))
                fi

                [[ -v ar[6][$i] ]] && ((i++)) # ',' handling

                (( cari++ ))
        done

        return 0
}


main()
{
        typeset -a xar
        parse_json xar "${jsontext}"
        #print -v xar

        compound -a xcar
        build_compound_tree xcar xar 1 ${#xar[0][@]} 0

        print -v xcar

        return 0
}


# program start
builtin cat
set -o nounset

main
exit $?
#!/usr/bin/ksh93

########################################################################
#                                                                      #
#               This software is part of the ast package               #
#                    Copyright (c) 2013 Roland Mainz                   #
#                      and is licensed under the                       #
#                 Eclipse Public License, Version 1.0                  #
#                    by AT&T Intellectual Property                     #
#                                                                      #
#                A copy of the License is available at                 #
#          http://www.eclipse.org/org/documents/epl-v10.html           #
#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
#                                                                      #
#                                                                      #
#                 Roland Mainz <[email protected]>              #
#                                                                      #
########################################################################

#
# Copyright (c) 2013, Roland Mainz. All rights reserved.
#

#
# jsonparse1 - a simple JSON parser 
#

typeset -r jsontext="$(
cat <<EOF
{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]


    "done": 666,

}

EOF
)"

function parse_json
{
        typeset jsontext="$2"
        nameref ar="$1"
        typeset dummy

        # fixme:
        # - We want to enforce standard conformance - does ~(Exp) or ~(Ex-p) 
does that ?
        dummy="${jsontext//~(Ex-p)(?:
                (?:\"([^\"]+?)\"):|     # name
                (\{)|                   # object begin
                (\})|                   # object end
                (\[)|                   # array start
                (\])|                   # array end
                (,)|                    #
                ([[:digit:]]+)|         # numerical value
                (?:\"([^\"]*?)\")       # string value
                )/D}"

        # debug output
#       print -v .sh.match
#       printf $"#dummy=%q\n" "${dummy}"

        # copy ".sh.match" to array "ar"
        # fixme: Use typeset -c instead
        integer i j
        for i in "${!.sh.match[@]}" ; do
                for j in "${!.sh.match[i][@]}" ; do
                        [[ -v .sh.match[i][j] ]] && 
ar[i][j]="${.sh.match[i][j]}"
                done
        done

        return 0
}


function build_compound_tree
{
        nameref car=$1
        nameref ar=$2
        integer i=$3
        integer ar_max=$4
        integer cari=$5 # car index

        integer j k nested
        bool foundobj

        for (( ; i < ar_max ; )) ; do
                # end of object ? then return...
                if [[ -v ar[3][$i] ]] ; then
                        return 0
                fi

                car[cari].name="${ar[1][$i]-}"
                ((i++))

                # string value
                if [[ -v ar[8][$i] ]] ; then
                        typeset car[cari].value="${ar[8][i++]}"
                elif [[ -v ar[7][$i] ]] ; then
                        float car[cari].value="${ar[7][i++]}"
                elif [[ -v ar[2][$i] ]] ; then
                        (( nested=0 , foundobj=false ))
                        for (( j=i ; (j < ar_max) && (nested > 0 || !foundobj) 
; j++ )) ; do
                                [[ -v ar[2][$j] ]] && ((nested++, 
foundobj=true))
                                [[ -v ar[3][$j] ]] && ((nested--))
                        done

                        compound -a car[$cari].value
                        build_compound_tree car[$cari].value xar $((i+1)) $j 0
                        (( i=j ))
                elif [[ -v ar[4][$i] ]] ; then
                        (( nested=0 , foundobj=false ))
                        for (( j=i ; (j < ar_max) && (nested > 0 || !foundobj) 
; j++ )) ; do
                                [[ -v ar[4][$j] ]] && ((nested++, 
foundobj=true))
                                [[ -v ar[5][$j] ]] && ((nested--))
                        done

                        compound -a car[$cari].arrayvalue
                        nameref js_ar=car[$cari].arrayvalue
                        integer jari=0 # json array index

                        ((i++))

                        for (( ;; )) ; do
                                (( nested=0 , foundobj=false ))
                                for (( k=i ; (k < j) && (nested > 0 || 
!foundobj) ; k++ )) ; do
                                        [[ -v ar[2][$k] ]] && ((nested++, 
foundobj=true))
                                        [[ -v ar[3][$k] ]] && ((nested--))
                                done

                                build_compound_tree js_ar[$jari] xar $((i+1)) 
$k 0
                                [[ -v ar[6][$k] ]] && ((k++)) # ',' handling

                                (( i=k, jari++ ))
                                (( i > (j-1) )) && break
                        done

                        (( i=j ))
                fi

                [[ -v ar[6][$i] ]] && ((i++)) # ',' handling

                (( cari++ ))
        done

        return 0
}


function main
{
        typeset -a xar
        parse_json xar "${jsontext}"
        #print -v xar

        compound -a xcar
        build_compound_tree xcar xar 1 ${#xar[0][@]} 0

        print -v xcar

        return 0
}


# program start
builtin cat
set -o nounset

main
exit $?
_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers

Reply via email to