The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sourceparser.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Sytyi Nick <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 /**
15  * @file
16  * This file contains sourceparser object, collecting annotations
17  * and building a tag tree.
18  * Also here are declared fuctions that return regex templates.
19  * And a fuction to create a file with list of templates.
20  */
21 
22 #ifndef TOOLS_SCHEMA_SOURCEPARSER_HPP_INCLUDED
23 #define TOOLS_SCHEMA_SOURCEPARSER_HPP_INCLUDED
24 
26 #include "tools/schema/tag.hpp"
27 
28 #include <iostream>
29 #include <fstream>
30 #include <map>
31 #include <queue>
32 #include <string>
33 #include <vector>
34 
35 namespace schema_validation{
36 /** A few regex templates. Please notice, that adding any regex template,
37  * schould be called in test_regexes()
38  */
39 
40 /** Template to check line of beeing valid*/
41 const std::string & get_valid();
42 /** Template to check line is beginnnig of Wiki block*/
43 const std::string & get_wiki();
44 /** Template to check begining of parent block*/
45 const std::string & get_parent_begin() ;
46 /** Template to check closing of parent block*/
47 const std::string & get_parent_end() ;
48 /** Template to check if line contains opening of tag block*/
49 const std::string & get_tag_begin() ;
50 /** Template to check end of tag block*/
51 
52 const std::string & get_tag_end() ;
53 
54 /** Template to check allow{link} block*/
55 const std::string & get_allow_link();
56 /** Template to check allow{global} block*/
58 
59 /** Template to check begining of table{config} storing key values*/
61 /** Template to check if table is closed*/
62 const std::string & get_table_end() ;
63 
64 /** Template to get key value*/
65 const std::string & get_key_value();
66 /** Writes to the file regex templates list.*/
67 void test_regex(std::ostream & f );
68 
70 public:
72  input_ (""),
73  output_(""),
74  f_(),
75  line_(0),
76  current_(),
77  root_(class_tag("root",1,1)),
78  parent_name_(""),
79  orphan_tags_(),
80  errors_(),
81  types_(),
82  forbidden_()
83  {
84  }
85 
87  }
88 
89  void set_input(const std::string &s){
90  input_ = s;
91  }
92  void set_output(const std::string &s){
93  output_ = s;
94  }
95  /**
96  * Parses file line-by-line, checking every line to open WIKI block
97  * Please, notice that main input work is made in check_*** methods.
98  * Methods parse_*** are used to organize alhoritm and set which
99  * regex template can be used in this context.
100  * The only exception is parse_keys, where table of keys is read
101  * and add to top of stack tags.
102  */
103  bool parse_source();
104  /**
105  * Saves tag tree to schema file.
106  */
107  bool save_schema();
108 
109  /**
110  * Expands all tags.
111  * While expanding tag copies list of keys and links from super-tag
112  * And adds links to super-tag children to links list.
113  * Useful when debugging the schema_markup
114  */
115  void expand(){
117  }
118 
119 
120  const std::vector<class_tag> & see_orphans() const{
121  return orphan_tags_;
122  }
123  /** Grants access to error container*/
125  return errors_;
126  }
127 private:
128  /** name of input file to be parsed*/
130  /** name of output file to print schema*/
132  std::fstream f_;
133  //current line number
134  /** number of current read line. Is used when generating errors*/
135  int line_;
136  //vector-based stack. The element on top is current opened tag.
137  /** Stack of opened tags.*/
138  std::vector<class_tag> current_;
139  /** Root of the schema tree*/
141  /** Name of current parent*/
143  /** List of tags without parents.*/
144  std::vector<class_tag> orphan_tags_;
145  /** used to store errors*/
147  /** Allowed types*/
148  std::map<std::string,std::string> types_;
149  /** Types to remove*/
150  std::vector<std::string> forbidden_;
151  /**
152  * Parses WIKI block line-by-line, checking every line
153  * to open annotation block
154  */
155  bool parse_block();
156  /**
157  * Parses lines inside tag block.
158  * Calls checkers that are allowed in tag block
159  */
160  bool parse_tag();
161  /**
162  * Read key table and add keys to tag on the top of the stack.
163  */
164  bool parse_keys();
165 
166  /** check the input line with a template
167  * check if the line is valid (still in block)
168  */
169  bool check_valid(const std::string& s);
170  /**
171  * Gets a line from file and returns it.
172  * Is used to manage exceptions while IO.
173  */
174  bool getline(std::string & s);
175  /**
176  * Сhecks stack of opened tags. If any tags is opened - closes it
177  *and adds to sublist of next tag in stack.
178  * Add last tag in stack to parent
179  * @param i number of tags in stack to close.
180  */
181  void close_opened_tags(int i);
182  /** Generates errors for each opened tag.
183  * @param i number of tags in stack to complain.
184  */
185  void add_open_tag_error(int i);
186 
187 
188  /**
189  * Read tag form the line and add it to stack
190  */
191  bool check_wiki(const std::string& s);
192 
193  /** Checks line for tag annotation. Reads tag and puts in into stack.*/
194  bool check_tag_begin(const std::string& s);
195  /**
196  * Puts closed tag to child list of previous tag.
197  * Also closes all opened child tags, if they are, and generates warnings.
198  */
199  bool check_tag_end(const std::string& s);
200 
201  /** Opens parrent block*/
202  bool check_parent_begin(const std::string& s);
203  /** Closes parent block*/
204  bool check_parent_end(const std::string& s);
205  /** Checks beginning of keys*/
206  bool check_keys_begin(const std::string&s);
207  /** Checks end of keys*/
208  bool check_keys_end(const std::string&s);
209  /** Checks links*/
210  bool check_allow_link(const std::string & s);
211  /** Checks allowed global tags*/
212  bool check_allow_global(const std::string &s);
213  /** Checks allowed types*/
214  bool check_allow_type(const std::string &s);
215  /** Checks removed types*/
216  bool check_remove_type(const std::string &s);
217  /** Checks removed keys*/
218  bool check_remove_key(const std::string &s);
219 };
220 } // namespace schema_validation
221 
222 #endif // TOOLS_SCHEMA_SOURCEPARSER_HPP_INCLUDED
const std::string & get_parent_end()
Template to check closing of parent block.
void test_regex(std::ostream &f)
Writes to the file regex templates list.
const std::string & get_allow_global()
Template to check allow{global} block.
bool parse_source()
Parses file line-by-line, checking every line to open WIKI block Please, notice that main input work ...
bool check_valid(const std::string &s)
check the input line with a template check if the line is valid (still in block)
const std::string & get_table_key_begin()
Template to check begining of table{config} storing key values.
std::vector< class_tag > current_
Stack of opened tags.
bool check_remove_key(const std::string &s)
Checks removed keys.
bool check_wiki(const std::string &s)
Read tag form the line and add it to stack.
bool getline(std::string &s)
Gets a line from file and returns it.
const std::string & get_tag_begin()
Template to check if line contains opening of tag block.
const std::string & get_table_end()
Template to check if table is closed.
bool check_keys_begin(const std::string &s)
Checks beginning of keys.
const std::string & get_valid()
A few regex templates.
This file contains object "error_container", which are used to store error messages while annotation ...
bool parse_tag()
Parses lines inside tag block.
std::map< std::string, std::string > types_
Allowed types.
bool parse_keys()
Read key table and add keys to tag on the top of the stack.
bool check_tag_begin(const std::string &s)
Checks line for tag annotation.
const std::string & get_tag_end()
Template to check end of tag block.
std::string output_
name of output file to print schema
bool check_parent_begin(const std::string &s)
Opens parrent block.
const std::vector< class_tag > & see_orphans() const
bool save_schema()
Saves tag tree to schema file.
This file contains objects "tag" and "key", which are used to store information about tags and keys w...
void set_input(const std::string &s)
void expand_all(class_tag &root)
Calls the expansion on each child.
Definition: tag.cpp:152
void close_opened_tags(int i)
Сhecks stack of opened tags.
const std::string & get_key_value()
Template to get key value.
int line_
number of current read line.
std::vector< class_tag > orphan_tags_
List of tags without parents.
std::vector< std::string > forbidden_
Types to remove.
bool check_tag_end(const std::string &s)
Puts closed tag to child list of previous tag.
const class_error_container & see_errors() const
Grants access to error container.
std::string parent_name_
Name of current parent.
bool check_allow_type(const std::string &s)
Checks allowed types.
bool check_keys_end(const std::string &s)
Checks end of keys.
bool check_allow_link(const std::string &s)
Checks links.
const std::string & get_allow_link()
Template to check allow{link} block.
bool parse_block()
Parses WIKI block line-by-line, checking every line to open annotation block.
class_tag root_
Root of the schema tree.
Container of errors, which are generated while schema generator tool is parsing source files...
size_t i
Definition: function.cpp:1057
void set_output(const std::string &s)
void add_open_tag_error(int i)
Generates errors for each opened tag.
const std::string & get_wiki()
Template to check line is beginnnig of Wiki block.
const std::string & get_parent_begin()
Template to check begining of parent block.
std::string input_
name of input file to be parsed
GLdouble s
Definition: glew.h:1358
bool check_parent_end(const std::string &s)
Closes parent block.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Stores information about tag.
Definition: tag.hpp:116
bool check_remove_type(const std::string &s)
Checks removed types.
GLclampf f
Definition: glew.h:3024
class_error_container errors_
used to store errors
bool check_allow_global(const std::string &s)
Checks allowed global tags.