tools/schema/sourceparser.hpp

Go to the documentation of this file.
00001 /* $Id: sourceparser.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2011 - 2012 by Sytyi Nick <nsytyi@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 /**
00016  * @file
00017  * This file contains sourceparser object, collecting annotations
00018  * and building a tag tree.
00019  * Also here are declared fuctions that return regex templates.
00020  * And a fuction to create a file with list of templates.
00021  */
00022 
00023 #ifndef TOOLS_SCHEMA_SOURCEPARSER_HPP_INCLUDED
00024 #define TOOLS_SCHEMA_SOURCEPARSER_HPP_INCLUDED
00025 
00026 #include "tools/schema/error_container.hpp"
00027 #include "tools/schema/tag.hpp"
00028 
00029 #include <iostream>
00030 #include <fstream>
00031 #include <map>
00032 #include <queue>
00033 #include <string>
00034 #include <vector>
00035 
00036 namespace schema_validation{
00037 /** A few regex templates. Please notice, that adding any regex template,
00038  * schould be called in test_regexes()
00039  */
00040 
00041 /** Template to check line of beeing valid*/
00042 const std::string & get_valid();
00043 /** Template to check line is beginnnig of Wiki block*/
00044 const std::string & get_wiki();
00045 /** Template to check begining of parent block*/
00046 const std::string & get_parent_begin() ;
00047 /** Template to check closing of parent block*/
00048 const std::string & get_parent_end() ;
00049 /** Template to check if line contains opening of tag block*/
00050 const std::string & get_tag_begin() ;
00051 /** Template to check end of tag block*/
00052 
00053 const std::string & get_tag_end() ;
00054 
00055 /** Template to check allow{link} block*/
00056 const std::string & get_allow_link();
00057 /** Template to check allow{global} block*/
00058 const std::string & get_allow_global();
00059 
00060 /** Template to check begining of table{config} storing key values*/
00061 const std::string & get_table_key_begin() ;
00062 /** Template to check if table is closed*/
00063 const std::string & get_table_end() ;
00064 
00065 /** Template to get key value*/
00066 const std::string & get_key_value();
00067 /** Writes to the file regex templates list.*/
00068 void test_regex(std::ostream & f );
00069 
00070 class class_source_parser{
00071 public:
00072     class_source_parser():
00073             input_ (""),
00074             output_(""),
00075             f_(),
00076             line_(0),
00077             current_(),
00078             root_(class_tag("root",1,1)),
00079             parent_name_(""),
00080             orphan_tags_(),
00081             errors_(),
00082             types_(),
00083             forbidden_()
00084     {
00085     }
00086 
00087     ~class_source_parser(){
00088     }
00089 
00090     void set_input(const std::string &s){
00091         input_ = s;
00092     }
00093     void set_output(const std::string &s){
00094         output_ = s;
00095     }
00096     /**
00097      * Parses file line-by-line, checking every line to open WIKI block
00098      * Please, notice that main input work is made in check_*** methods.
00099      * Methods parse_*** are used to organize alhoritm and set which
00100      * regex template can be used in this context.
00101      * The only exception is parse_keys, where table of keys is read
00102      * and add to top of stack tags.
00103      */
00104     bool parse_source();
00105     /**
00106      * Saves tag tree to schema file.
00107      */
00108     bool save_schema();
00109 
00110     /**
00111      * Expands all tags.
00112      * While expanding tag copies list of keys and links from super-tag
00113      * And adds links to super-tag children to links list.
00114      * Useful when debugging the schema_markup
00115      */
00116     void expand(){
00117         root_.expand_all(root_);
00118     }
00119 
00120 
00121     const std::vector<class_tag> & see_orphans() const{
00122         return orphan_tags_;
00123     }
00124     /** Grants access to error container*/
00125     const class_error_container & see_errors() const{
00126         return errors_;
00127     }
00128 private:
00129     /** name of input file to be parsed*/
00130     std::string input_;
00131     /** name of output file to print schema*/
00132     std::string output_;
00133     std::fstream f_;
00134     //current line number
00135     /** number of current read line. Is used when generating errors*/
00136     int line_;
00137     //vector-based stack. The element on top is current opened tag.
00138     /** Stack of opened tags.*/
00139     std::vector<class_tag> current_;
00140     /** Root of the schema tree*/
00141     class_tag root_;
00142     /** Name of current parent*/
00143     std::string parent_name_;
00144     /** List of tags without parents.*/
00145     std::vector<class_tag> orphan_tags_;
00146     /** used to store errors*/
00147     class_error_container  errors_;
00148     /** Allowed types*/
00149     std::map<std::string,std::string> types_;
00150     /** Types to remove*/
00151     std::vector<std::string> forbidden_;
00152     /**
00153      * Parses WIKI block line-by-line, checking every line
00154      * to open annotation block
00155      */
00156     bool parse_block();
00157     /**
00158      * Parses lines inside tag block.
00159      * Calls checkers that are allowed in tag block
00160      */
00161     bool parse_tag();
00162     /**
00163      * Read key table and add keys to tag on the top of the stack.
00164      */
00165     bool parse_keys();
00166 
00167     /** check the input line with a template
00168      * check if the line is valid (still in block)
00169      */
00170     bool check_valid(const std::string& s);
00171     /**
00172      * Gets a line from file and returns it.
00173      * Is used to manage exceptions while IO.
00174      */
00175     bool getline(std::string & s);
00176     /**
00177      * Сhecks stack of opened tags. If any tags is opened - closes it
00178      *and adds to sublist of next tag in stack.
00179      * Add last tag in stack to parent
00180      * @param i number of tags in stack to close.
00181      */
00182     void close_opened_tags(int i);
00183     /** Generates errors for each opened tag.
00184      * @param i number of tags in stack to complain.
00185      */
00186     void add_open_tag_error(int i);
00187 
00188 
00189     /**
00190      * Read tag form the line and add it to stack
00191      */
00192     bool check_wiki(const std::string& s);
00193 
00194     /** Checks line for tag annotation. Reads tag and puts in into stack.*/
00195     bool check_tag_begin(const std::string& s);
00196     /**
00197      * Puts closed tag to child list of previous tag.
00198      * Also closes all opened child tags, if they are, and generates warnings.
00199      */
00200     bool check_tag_end(const std::string& s);
00201 
00202     /** Opens parrent block*/
00203     bool check_parent_begin(const std::string& s);
00204     /** Closes parent block*/
00205     bool check_parent_end(const std::string& s);
00206     /** Checks beginning of keys*/
00207     bool check_keys_begin(const std::string&s);
00208     /** Checks end of keys*/
00209     bool check_keys_end(const std::string&s);
00210     /** Checks links*/
00211     bool check_allow_link(const std::string & s);
00212     /** Checks allowed global tags*/
00213     bool check_allow_global(const std::string &s);
00214     /** Checks allowed types*/
00215     bool check_allow_type(const std::string &s);
00216     /** Checks removed types*/
00217     bool check_remove_type(const std::string &s);
00218     /** Checks removed keys*/
00219     bool check_remove_key(const std::string &s);
00220 };
00221 } // namespace schema_validation
00222 
00223 #endif // TOOLS_SCHEMA_SOURCEPARSER_HPP_INCLUDED
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:13 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs