The Battle for Wesnoth  1.19.7+dev
context_free_grammar_generator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
3  by Ján Dugáček
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "utils/name_generator.hpp"
19 
20 #include <vector>
21 #include <cstdint>
22 
24 {
25 private:
26 
27  struct nonterminal {
28  nonterminal() : last_(1) {}
29  std::vector<std::vector<std::string>> possibilities_;
30  mutable unsigned int last_;
31  };
32 
33  void init_seed(uint32_t seed[]) const;
34  std::map<std::string, nonterminal> nonterminals_;
35  std::string print_nonterminal(const std::string& name, uint32_t seed[], short int seed_pos) const;
36  static const short unsigned int seed_size = 20;
37 
38 public:
39  /** Initialisation
40  * @param source the definition of the context-free grammar to use
41  */
42  context_free_grammar_generator(const std::string& source);
43 
44  /** Initialisation
45  * @param source A map of nonterminals to lists of possibilities
46  */
47  context_free_grammar_generator(const std::map<std::string, std::vector<std::string>>& source);
48 
49  /** Generates a possible word in the grammar set before
50  * @return the word
51  */
52  std::string generate() const override;
53 
55 };
std::string generate() const override
Generates a possible word in the grammar set before.
std::string print_nonterminal(const std::string &name, uint32_t seed[], short int seed_pos) const
std::map< std::string, nonterminal > nonterminals_
context_free_grammar_generator(const std::string &source)
Initialisation.
static const short unsigned int seed_size
std::vector< std::vector< std::string > > possibilities_