The Battle for Wesnoth  1.17.0-dev
context_free_grammar_generator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2021
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 <list>
21 #include <vector>
22 #include <cstdint>
23 
25 {
26 private:
27 
28  struct nonterminal {
29  nonterminal() : last_(1) {}
30  std::vector<std::vector<std::string>> possibilities_;
31  mutable unsigned int last_;
32  };
33 
34  void init_seed(uint32_t seed[]) const;
35  std::map<std::string, nonterminal> nonterminals_;
36  std::string print_nonterminal(const std::string& name, uint32_t seed[], short int seed_pos) const;
37  static const short unsigned int seed_size = 20;
38 
39 public:
40  /** Initialisation
41  * @param source the definition of the context-free grammar to use
42  */
43  context_free_grammar_generator(const std::string& source);
44 
45  /** Initialisation
46  * @param source A map of nonterminals to lists of possibilities
47  */
48  context_free_grammar_generator(const std::map<std::string, std::vector<std::string>>& source);
49 
50  /** Generates a possible word in the grammar set before
51  * @return the word
52  */
53  std::string generate() const override;
54 
56 };
std::vector< std::vector< std::string > > possibilities_
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
context_free_grammar_generator(const std::string &source)
Initialisation.
std::map< std::string, nonterminal > nonterminals_
static const short unsigned int seed_size