// Lista las cadenas más cortas del lenguaje de una expresión: regex_lang EXPRESIÓN [LONGITUD] [ALFABETO]
// Sin alfabeto, usa los símbolos que aparecen en la expresión. La longitud máxima por defecto es 4.
#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

#include "nfa.h"
#include "regex.h"
#include "thompson.h"
#include "words.h"

int main(int argc, char** argv) {
    if (argc < 2 || argc > 4) {
        std::cerr << "uso: regex_lang EXPRESIÓN [LONGITUD] [ALFABETO]\n";
        return 2;
    }
    try {
        RegexPtr r = parse_regex(argv[1]);
        std::size_t max_len = (argc >= 3) ? std::stoul(argv[2]) : 4;
        std::vector<char> alphabet = symbols_of(r);
        if (argc == 4) {
            std::string given = argv[3];
            alphabet.assign(given.begin(), given.end());
        }
        if (alphabet.empty()) {
            throw std::runtime_error("la expresión no tiene símbolos: indiquen el alfabeto");
        }
        Nfa n = thompson(r, alphabet);
        std::vector<std::string> words = all_strings(alphabet, max_len);
        std::size_t inside = 0;
        std::cout << "L(" << to_string(r) << "), hasta longitud " << max_len << ":\n ";
        for (const std::string& w : words) {
            if (accepts(n, w)) {
                std::cout << ' ' << show(w);
                inside++;
            }
        }
        std::cout << (inside == 0 ? " (ninguna)" : "") << '\n'
                  << inside << " de " << words.size() << " cadenas\n";
    } catch (const std::exception& error) {
        std::cerr << "error: " << error.what() << '\n';
        return 1;
    }
}
