The Battle for Wesnoth  1.17.0-dev
schema_validator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
3  by Sytyi Nick <nsytyi@gmail.com>
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 "config_cache.hpp"
19 #include "serialization/parser.hpp"
24 
25 #include <iostream>
26 #include <queue>
27 #include <stack>
28 #include <string>
29 
30 class config;
31 
32 /** @file
33  * One of the realizations of serialization/validator.hpp abstract validator.
34  */
35 namespace schema_validation
36 {
37 /**
38  * Realization of serialization/validator.hpp abstract validator.
39  * Based on stack. Uses some stacks to store different info.
40  */
42 {
43 public:
44  virtual ~schema_validator();
45 
46  /**
47  * Initializes validator from file.
48  * Throws abstract_validator::error if any error.
49  */
50  schema_validator(const std::string& filename, bool validate_schema = false);
51 
52  void set_create_exceptions(bool value)
53  {
54  create_exceptions_ = value;
55  }
56 
57  virtual void open_tag(const std::string& name, const config& parent, int start_line = 0, const std::string& file = "", bool addition = false) override;
58  virtual void close_tag() override;
59  virtual void validate(const config& cfg, const std::string& name, int start_line, const std::string& file) override;
60  virtual void validate_key(const config& cfg, const std::string& name, const config_attribute_value& value, int start_line, const std::string& file) override;
61 
62 private:
63  // types section
64  // Just some magic to ensure zero initialization.
65  struct counter
66  {
67  int cnt;
69  : cnt(0)
70  {
71  }
72  };
73 
74  /** Counters are mapped by tag name. */
75  typedef std::map<std::string, counter> cnt_map;
76 
77  /** And counter maps are organize in stack. */
78  typedef std::stack<cnt_map> cnt_stack;
79 
80 protected:
81  using message_type = int;
83 
84  /**
85  * Messages are cached.
86  * The reason is next: in file where [tag]...[/tag][+tag]...[/tag]
87  * config object will be validated each [/tag]. So message can be as wrong
88  * (when [+tag] section contains missed elements) as duplicated.
89  *
90  * Messages are mapped by config*. That ensures uniqueness.
91  * Also message-maps are organized in stack to avoid memory leaks.
92  */
93  struct message_info
94  {
96  std::string file;
97  int line;
98  int n;
99  std::string tag;
100  std::string key;
101  std::string value;
102  std::string expected;
103 
104  message_info(message_type t, const std::string& file, int line = 0, int n = 0, const std::string& tag = "", const std::string& key = "", const std::string& value = "", const std::string& expected = "")
105  : type(t)
106  , file(file)
107  , line(line)
108  , n(n)
109  , tag(tag)
110  , key(key)
111  , value(value)
112  , expected(expected)
113  {
114  }
115  };
116 
117  /** Controls the way to print errors. */
119 
120  virtual void print(message_info&);
121 private:
122 
123  typedef std::deque<message_info> message_list;
124  typedef std::map<const config*, message_list> message_map;
125 
126  /** Reads config from input. */
127  bool read_config_file(const std::string& filename);
128 
129  /** Shows, if validator is initialized with schema file. */
131 
132  /** Root of schema information. */
134 
135  std::stack<const wml_tag*> stack_;
136 
137  /** Contains number of children. */
138  cnt_stack counter_;
139 
140  /** Caches error messages. */
141  std::stack<message_map> cache_;
142 
143  /** Type validators. */
145 
147 
148 protected:
149  template<typename... T>
150  void queue_message(const config& cfg, T&&... args)
151  {
152  cache_.top()[&cfg].emplace_back(std::forward<T>(args)...);
153  }
154 
155  const wml_tag& active_tag() const;
156  std::string active_tag_path() const;
157  bool have_active_tag() const;
158  bool is_valid() const {return config_read_;}
159  wml_type::ptr find_type(const std::string& type) const;
160 };
161 
162 // A validator specifically designed for validating a schema
163 // In addition to the usual, it verifies that references within the schema are valid, for example via the [link] tag
165 {
166 public:
168  virtual void open_tag(const std::string& name, const config& parent, int start_line = 0, const std::string& file = "", bool addition = false) override;
169  virtual void close_tag() override;
170  virtual void validate(const config& cfg, const std::string& name, int start_line, const std::string& file) override;
171  virtual void validate_key(const config& cfg, const std::string& name, const config_attribute_value& value, int start_line, const std::string& file) override;
172 private:
173  struct reference {
174  reference(const std::string& value, const std::string& file, int line, const std::string& tag)
175  : value_(value)
176  , file_(file)
177  , tag_(tag)
178  , line_(line)
179  {}
180  std::string value_, file_, tag_;
181  int line_;
182  bool match(const std::set<std::string>& with);
183  bool can_find(const wml_tag& root, const config& cfg);
184  bool operator<(const reference& other) const;
185  };
186  std::string current_path() const;
187  std::set<std::string> defined_types_, defined_tag_paths_;
188  std::vector<reference> referenced_types_, referenced_tag_paths_;
189  std::stack<std::string> tag_stack_;
190  std::map<std::string, std::string> links_;
191  std::multimap<std::string, std::string> derivations_;
192  int type_nesting_, condition_nesting_;
193  bool tag_path_exists(const config& cfg, const reference& ref);
194  void check_for_duplicates(const std::string& name, std::vector<std::string>& seen, const config& cfg, message_type type, const std::string& file, int line, const std::string& tag);
195  static bool name_matches(const std::string& pattern, const std::string& name);
196 
197  void print(message_info& message) override;
198  enum { WRONG_TYPE = NEXT_ERROR, WRONG_PATH, DUPLICATE_TAG, DUPLICATE_KEY, NEXT_ERROR };
199 };
200 
201 } // namespace schema_validation{
std::stack< cnt_map > cnt_stack
And counter maps are organize in stack.
std::stack< const wml_tag * > stack_
bool create_exceptions_
Controls the way to print errors.
cnt_stack counter_
Contains number of children.
This file contains object "type", which is used to store information about types while annotation par...
std::multimap< std::string, std::string > derivations_
Variant for storing WML attributes.
Stores information about tag.
Definition: tag.hpp:47
std::deque< message_info > message_list
This file contains information about validation abstract level interface.
std::map< const config *, message_list > message_map
wml_tag root_
Root of schema information.
std::map< std::string, ptr > map
Definition: type.hpp:43
bool config_read_
Shows, if validator is initialized with schema file.
This file contains object "tag", which is used to store information about tags while annotation parsi...
Used in parsing config file.
Definition: validator.hpp:37
std::stack< message_map > cache_
Caches error messages.
virtual void validate_key(const config &cfg, const std::string &name, const config_attribute_value &value, int start_line, const std::string &file) override
Checks if key is allowed and if its value is valid What exactly is validated depends on validator rea...
wml_type::map types_
Type validators.
message_info(message_type t, const std::string &file, int line=0, int n=0, const std::string &tag="", const std::string &key="", const std::string &value="", const std::string &expected="")
schema_validator(const std::string &filename, bool validate_schema=false)
Initializes validator from file.
bool read_config_file(const std::string &filename)
Reads config from input.
static bool operator<(const placing_info &a, const placing_info &b)
Definition: game_state.cpp:140
virtual void close_tag() override
As far as parser is built on stack, some realizations can store stack too.
std::shared_ptr< wml_type > ptr
Definition: type.hpp:42
void queue_message(const config &cfg, T &&... args)
This file contains object "key", which is used to store information about keys while annotation parsi...
double t
Definition: astarsearch.cpp:65
virtual void open_tag(const std::string &name, const config &parent, int start_line=0, const std::string &file="", bool addition=false) override
Is called when parser opens tag.
static const char * match(MatchState *ms, const char *s, const char *p)
Definition: lstrlib.cpp:567
Realization of serialization/validator.hpp abstract validator.
wml_type::ptr find_type(const std::string &type) const
std::map< std::string, counter > cnt_map
Counters are mapped by tag name.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
reference(const std::string &value, const std::string &file, int line, const std::string &tag)
virtual void print(message_info &)
virtual void validate(const config &cfg, const std::string &name, int start_line, const std::string &file) override
Validates config.
std::map< std::string, std::string > links_