getQuantifier()

in wasm/wasm-sharding-js/sharding/strgen.js [281:395]


    getQuantifier() { // get the value within quantifier operators, if present
        this.createLogEntry("Processing quantifier at pattern position " + (this.current_index + 1));
        var start_value = this.current_index + 1;
        var quantifier_value;
        var quantifier_first_value;
        var quant_range_state = false;
        var symbol_quantifier = false;

        if (this.symbol_quantifiers.includes(this.current())) {
            this.createLogEntry("Symbol quantifier specified");
            quant_range_state = true;
            symbol_quantifier = true;
            if(this.current() == "?") {
                this.createLogEntry("Quantifier", "?");
                quantifier_value = 1;
                quantifier_first_value = 0;
            } else if (this.current() == "*") {
                this.createLogEntry("Quantifier", "*");
                quantifier_value = this.symbol_quantifier_max;
                quantifier_first_value = 0;
            } else if (this.current() == "+") {
                this.createLogEntry("Quantifier", "+");
                quantifier_value = this.symbol_quantifier_max;
                quantifier_first_value = 1;
            }
        } else {
            do {
                if (this.operators.includes(this.lookahead()) == false && this.quantifier_operators.includes(this.lookahead()) == false) { // if lookahead is not any operator
                    if (quantifier_value == undefined) {
                        quantifier_value = this.next();
                    } else {
                        quantifier_value+= this.next();  
                    }
                } else if (this.quantifier_operators.includes(this.lookahead()) == true && quantifier_first_value == undefined) { // if lookahead is quantifier operator i.e. , : -
                    quant_range_state = true;
                    this.createLogEntry("Quantifier range specified");
                    quantifier_first_value = quantifier_value;
                    quantifier_value = "";
                    this.next();
                } else if (this.lookahead() == "") { // if lookahead is nothing (error case)
                    this.outputError("Quantifier not closed.");
                    break;  
                } else {
                    this.next();
                    quantifier_value = 1;
                    this.outputError("Unexpected character at position " + (this.current_index + 1) + ", character '" + this.pattern.charAt(this.current_index) + "'.");
                    break;          
                }
            } while (this.lookahead() != '}')           
        }
        
        if (quant_range_state == true) {

            if (quantifier_first_value == undefined || quantifier_first_value == "") {
                quantifier_first_value = 0;
            } else if (quantifier_value == undefined || quantifier_value == "") {
                this.outputWarning("Max quantifier value was not set, quantifier at position " + start_value + " set to 0.");
                quantifier_value = 0;
            }

            this.createLogEntry("Generating random quantifier between", quantifier_first_value + " and " + quantifier_value);

            quantifier_first_value = parseInt(quantifier_first_value);
            quantifier_value = parseInt(quantifier_value);

            if (quantifier_first_value > quantifier_value) { // swap values if the quantifier_first_value is the largest value
                var store = quantifier_first_value;
                quantifier_first_value = quantifier_value;
                quantifier_value = store;
                this.createLogEntry("Quantifier values swapped");
            }

            // temporary solution to getting a random quantifier from a range
            var quantifier_array = [];

            for (var count = quantifier_first_value; count <= quantifier_value; count++) { // populate array with every value between quantifier_first_value and quantifier_value
                quantifier_array.push(count);
            }
            this.createLogEntry("Quantifier range values", quantifier_array.toString());

            var random_value = Math.floor(Math.random() * quantifier_array.length);
            var selected_quantifier = quantifier_array[random_value]; // select a value based math.random and array length
            this.createLogEntry("Selected index", random_value + ", selected quantifier value: " + selected_quantifier);

            quantifier_array = [];
            //end of temp code 

            quantifier_value = selected_quantifier;
        }

        if (this.allow_duplicate_characters == false) {
            var value_list_length = this.getValueListLength();
            if (quantifier_value > value_list_length) {
                this.outputWarning("Character quantifier at position " + start_value + " reduced from " + 
                    quantifier_value + " to " + value_list_length + 
                    ". Toggle 'Allow Duplicate Characters' to generate the full amount.")

                    quantifier_value = value_list_length;
            }
        }

        this.createLogEntry("Quantifier value is " + quantifier_value);

        if (quantifier_value == 0 && quant_range_state == false) {
            this.createLogEntry("No value was returned. Character quantifier at position " + start_value + " is 0.", undefined, true);
        } else if (quantifier_value == 0 && quant_range_state == true) {
            this.createLogEntry("No value was returned. Character quantifier range at position " + start_value + " generated the value 0!", undefined, true);
        }

        if (isNaN(quantifier_value)) {
            this.outputError("Quantifier at position " + start_value + " contains invalid characters.");
        }

        return parseInt(quantifier_value);
    };