libtcod
Loading...
Searching...
No Matches
parser.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34#pragma once
35#ifndef TCOD_PARSER_HPP_
36#define TCOD_PARSER_HPP_
37
38#include <memory>
39#include <vector>
40
41#include "color.hpp"
42#include "list.hpp"
43#include "parser.h"
44// clang-format off
53
136
137class TCODLIB_API TCODParser;
138class TCODLIB_API TCODParserStruct;
139class TCODLIB_API ITCODParserListener;
140
141class TCODLIB_API TCODParser {
142public :
154
155 // Disable copy operators.
156 TCODParser(const TCODParser&) = delete;
157 TCODParser& operator=(const TCODParser&) = delete;
158 TCODParser(TCODParser&& rhs) noexcept { std::swap(data, rhs.data); };
159 TCODParser& operator=(TCODParser&& rhs) noexcept {
160 std::swap(data, rhs.data);
161 return *this;
162 };
163
182 TCODParserStruct *newStructure(const char *name);
183
184 // register a new custom type
185 TCOD_value_type_t newCustomType(TCOD_parser_custom_t custom_type_parser);
186
203 void run(const char *filename, ITCODParserListener *listener = NULL);
204
215
216 // error during parsing. can be called by the parser listener
217 void error(const char *msg, ...);
218
219 bool hasProperty(const char *name) const;
220 bool getBoolProperty(const char *name) const;
221 int getIntProperty(const char *name) const;
222 int getCharProperty(const char *name) const;
223 float getFloatProperty(const char *name) const;
224 TCODColor getColorProperty(const char *name) const;
225 TCOD_dice_t getDiceProperty(const char *name) const;
226 const char * getStringProperty(const char *name) const;
227 void * getCustomProperty(const char *name) const;
228 TCOD_list_t getListProperty(const char *name, TCOD_value_type_t type) const;
229private :
230 bool parseEntity(TCODParserStruct *def, ITCODParserListener *listener);
231 TCOD_parser_t data{};
232#ifdef _MSC_VER
233 // Disable dll-interface warning. This value should only used internally.
234#pragma warning(push)
235#pragma warning(disable: 4251)
236#endif // _MSC_VER
237 std::vector<std::unique_ptr<TCODParserStruct>> defs;
238#ifdef _MSC_VER
239#pragma warning(pop)
240#endif // _MSC_VER
241 friend bool new_struct(TCOD_parser_struct_t def, const char* name) noexcept;
242 friend bool end_struct(TCOD_parser_struct_t def, const char* name) noexcept;
243};
244
245// a parser structure
246class TCODLIB_API TCODParserStruct {
247public :
261 TCODParserStruct* addFlag(const char *propname);
262
290 TCODParserStruct* addProperty(const char *propname, TCOD_value_type_t type, bool mandatory);
291
315 TCODParserStruct* addValueList(const char *propname, const char **value_list, bool mandatory);
316
341 TCODParserStruct* addListProperty(const char *propname, TCOD_value_type_t type, bool mandatory);
342
359
374 const char *getName() const;
375
389 bool isPropertyMandatory(const char *propname) const;
390
407 TCOD_value_type_t getPropertyType(const char *propname) const;
408
409// private stuff
410 TCOD_parser_struct_t data;
411};
412
527
528// sax event listener
529class TCODLIB_API ITCODParserListener {
530public :
531 virtual ~ITCODParserListener(){}
548 virtual bool parserNewStruct(TCODParser *parser,const TCODParserStruct *str,const char *name)=0;
549
565 virtual bool parserFlag(TCODParser *parser,const char *name)=0;
566
586 virtual bool parserProperty(TCODParser *parser,const char *propname, TCOD_value_type_t type, TCOD_value_t value)=0;
587
604 virtual bool parserEndStruct(TCODParser *parser,const TCODParserStruct *str, const char *name)=0;
605
618
632 virtual void error(const char *msg) = 0;
633};
634
696
697
698#endif // TCOD_PARSER_HPP_
For basic config files, you don't have to write a listener.
Definition parser.hpp:529
virtual bool parserEndStruct(TCODParser *parser, const TCODParserStruct *str, const char *name)=0
This callback is called each time the parser find the end of a structure declaration in the file.
virtual bool parserProperty(TCODParser *parser, const char *propname, TCOD_value_type_t type, TCOD_value_t value)=0
This callback is called each time the parser find a new property in the file.
virtual bool parserNewStruct(TCODParser *parser, const TCODParserStruct *str, const char *name)=0
This callback is called each time the parser find a new structure declaration in the file.
virtual void error(const char *msg)=0
There are two kind of errors : Errors that are detected by the parser itself (malformed file,...
virtual bool parserFlag(TCODParser *parser, const char *name)=0
This callback is called each time the parser find a new flag in the file.
Definition color.hpp:221
Definition parser.hpp:141
TCODParserStruct * newStructure(const char *name)
TCODParser()
Use this function to create a generic parser.
~TCODParser()
Once you've done with the file parsing, you can release the resources used by the parser :
void run(const char *filename, ITCODParserListener *listener=NULL)
Once you defined all the structure types and created your listener, you can start the actual parsing ...
Definition parser.hpp:246
TCODParserStruct * addValueList(const char *propname, const char **value_list, bool mandatory)
A value-list property is a string property for which we define the list of allowed values.
bool isPropertyMandatory(const char *propname) const
You can know if a property is mandatory :
TCOD_value_type_t getPropertyType(const char *propname) const
You get the type of a property : In the case of a list property, the value returned is a bitwise or o...
TCODParserStruct * addStructure(TCODParserStruct *sub_entity)
A structure can contain others structures.
TCODParserStruct * addProperty(const char *propname, TCOD_value_type_t type, bool mandatory)
Use this function to add a standard property to a structure type.
const char * getName() const
You can retrieve the name of the structure type with these functions.
TCODParserStruct * addListProperty(const char *propname, TCOD_value_type_t type, bool mandatory)
Use this function to add a list property to a structure type.
TCODParserStruct * addFlag(const char *propname)
Use this function to add a flag property to a structure type.
Color handling module.
Deprecated TCODList class.
Libtcod config parser.
Definition mersenne_types.h:45
Definition parser.h:93