<commit_before>#!/bin/bash

babel_help() {
    echo "Usage $0 [OPTION]"
    echo
    echo "  -g var         Generate. Converts piped bash variable declarations into babel."
    echo "  -p var <file>  Parse. Converts piped babel into bash variable declarations."
    echo "  -h             Help. This text."
    echo
}

NAME_RE="[[:alnum:]_]+"

VAR_RE="^( *)($NAME_RE(/$NAME_RE)*)( *)=(.*)$"

IGNORE_RE="(^[[:space:]]*$|^[[:space:]]*#)"

babel_parse() {
    if [ -z "$1" ]; then
        babel_help
        exit 1
    fi

    container=$1
    last_var=
    cont_re=
    output=
    file=${2:--}

    IFS=$'\n'

    local $container

    declare -A $container

    for line in $(cat $file); do
        if [[ "$line" =~ $VAR_RE ]]; then
            last_var=${BASH_REMATCH[2]}
            ((indent_len=${#BASH_REMATCH[1]}+${#last_var}+${#BASH_REMATCH[4]}))
            value=${BASH_REMATCH[5]}

            eval "$container[$last_var]=\"$value\""

            cont_re="^ {$indent_len}[ =](.*)$"
        elif [[ -n "$last_var" && "$line" =~ $cont_re ]]; then
            eval "$container[$last_var]+=\"\n${BASH_REMATCH[1]}\""
        elif [[ ! "$line" =~ $IGNORE_RE ]]; then
            echo "Invalid line: '$line'"<commit_msg>Support repeated keys in bash<commit_after>#!/bin/bash

babel_help() {
    echo "Usage $0 [OPTION]"
    echo
    echo "  -g var         Generate. Converts piped bash variable declarations into babel."
    echo "  -p var <file>  Parse. Converts piped babel into bash variable declarations."
    echo "  -h             Help. This text."
    echo
}

NAME_RE="[[:alnum:]_]+"

VAR_RE="^( *)($NAME_RE(/$NAME_RE)*)( *)=(.*)$"

IGNORE_RE="(^[[:space:]]*$|^[[:space:]]*#)"

babel_parse() {
    if [ -z "$1" ]; then
        babel_help
        exit 1
    fi

    container=$1
    last_var=
    cont_re=
    output=
    file=${2:--}

    IFS=$'\n'

    local $container

    declare -A $container

    for line in $(cat $file); do
        if [[ "$line" =~ $VAR_RE ]]; then
            last_var=${BASH_REMATCH[2]}
            ((indent_len=${#BASH_REMATCH[1]}+${#last_var}+${#BASH_REMATCH[4]}))
            value=${BASH_REMATCH[5]}

            key_exists=$(eval "echo \${$container[$last_var]+isset}")

            if [ "$key_exists" ]; then
                eval "$container[$last_var]+=\"\n$value\""
            else
                eval "$container[$last_var]=\"$value\""
            fi

            cont_re="^ {$indent_len}[ =](.*)$"
        elif [[ -n "$last_var" && "$line" =~ $cont_re ]]; then
            eval "$container[$last_var]+=\"\n${BASH_REMATCH[1]}\""
        elif [[ ! "$line" =~ $IGNORE_RE ]]; then
            echo "Invalid line: '$line'"
{'fraction_non_alphanumeric': 0.2022514071294559, 'fraction_numerical': 0.0075046904315197, 'mean_word_length': 2.24330900243309, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4173445', 'n_tokens_mistral': 989, 'n_tokens_neox': 929, 'n_words': 199}