Class ITCODParserListener

Class Documentation

class ITCODParserListener

For basic config files, you don’t have to write a listener.

Instead, use the default listener. The parser uses a SAX-like approach during the parsing of the file. This means that the whole file is not stored in memory in a tree structure. Instead, it works like a stream parser and raises events. Each event has an associated callback that is provided by a listener :

class ITCODParserListener { public : virtual bool parserNewStruct(TCODParser *parser,const TCODParserStruct *str,const char *name)=0; virtual bool parserFlag(TCODParser *parser,const char *name)=0; virtual bool parserProperty(TCODParser *parser,const char *name, TCOD_value_type_t type, TCOD_value_t value)=0; virtual bool parserEndStruct(TCODParser *parser,const TCODParserStruct *str, const char *name)=0; virtual void error(const char *msg) = 0; };

typedef struct { bool (*new_struct)(TCOD_parser_struct_t str,const char *name); bool (*new_flag)(const char *name); bool (*new_property)(const char *name, TCOD_value_type_t type, TCOD_value_t value); bool (*end_struct)(TCOD_parser_struct_t str, const char *name); void (*error)(const char *msg); } TCOD_parser_listener_t;

class ParserListener : def new_struct(str,name) : … def new_flag(name) : … def new_property(name,type,value) : … def end_struct(self, struct, name) : … def error(msg) : …

Before running the parser, you have to build a listener :

class MyListener : public ITCODParserListener { bool parserNewStruct(TCODParser *parser,const TCODParserStruct *str,const char *name) { printf (“new structure type ‘%s’ with name ‘%s’\n”,str->getname(),name ? name : “NULL”); return true; } bool parserFlag(TCODParser *parser,const char *name) { printf (“found new flag ‘%s’\n”,name); return true; } bool parserProperty(TCODParser *parser,const char *name, TCOD_value_type_t type, TCOD_value_t value) { printf (“found new property ‘%s’\n”,name); return true; } bool parserEndStruct(TCODParser *parser,const TCODParserStruct *str,const char *name) { printf (“end of structure type ‘%s’\n”,name); return true; } void error(char *msg) { fprintf(stderr,msg); exit(1); } };

bool my_parser_new_struct(TCOD_parser_struct_t str, const char *name) { printf (“new structure type ‘%s’ with name ‘%s’\n”,TCOD_struct_get_name(str),name ? name : “NULL”); return true; } bool my_parser_flag(const char *name) { printf (“found new flag ‘%s’\n”,name); return true; } bool my_parser_property(const char *name, TCOD_value_type_t type, TCOD_value_t value) { printf (“found new property ‘%s’\n”,name); return true; } bool my_parser_end_struct(TCOD_parser_struct_t str, const char *name) { printf (“end of structure type ‘%s’\n”,name); return true; } void my_parser_error(const char *msg) { fprintf(stderr,msg); exit(1); } TCOD_parser_listener_t my_listener = { my_parser_new_struct, my_parser_flag, my_parser_property, my_parser_end_struct, my_parser_error };

class MyListener: def new_struct(self, struct, name): print ‘new structure type’, libtcod.struct_get_name(struct), ‘ named ‘, name return True def new_flag(self, name): print ‘new flag named ‘, name return True def new_property(self,name, typ, value): type_names = [‘NONE’, ‘BOOL’, ‘CHAR’, ‘INT’, ‘FLOAT’, ‘STRING’, ‘COLOR’, ‘DICE’] if typ == libtcod.TYPE_COLOR : print ‘new property named ‘, name,’ type ‘,type_names[typ], ‘ value ‘, value.r, value.g, value.b elif typ == libtcod.TYPE_DICE : print ‘new property named ‘, name,’ type ‘,type_names[typ], ‘ value ‘, value.nb_rolls, value.nb_faces, value.multiplier, value.addsub else: print ‘new property named ‘, name,’ type ‘,type_names[typ], ‘ value ‘, value return True def end_struct(self, struct, name): print ‘end structure type’, libtcod.struct_get_name(struct), ‘ named ‘, name return True def error(self,msg): print ‘error : ‘, msg return True

Public Functions

inline virtual ~ITCODParserListener()
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.

Example : It must return true if everything is right, false if there is an error and the parser must exit.

Parameters
  • parser – In the C++ version, the parser object, returned by TCODParser constructor. It’s used for error handling.

  • str – The structure type. Can be used to retrieve the type’s name with getName. In the example above, this would be “item_type”.

  • name – The name of the structure or NULL if no name is present in the file. In the example above, this would be “blade”.

virtual bool parserFlag(TCODParser *parser, const char *name) = 0

This callback is called each time the parser find a new flag in the file.

Example : It must return true if everything is right, false if there is an error and the parser must exit.

Parameters
  • parser – In the C++ version, the parser object, returned by TCODParser constructor. It’s used for error handling.

  • name – The name of the flag. In the example, this would be “abstract”.

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.

Example : It must return true if everything is right, false if there is an error and the parser must exit.

Parameters
  • parser – In the C++ version, the parser object, returned by TCODParser constructor. It’s used for error handling.

  • name – The name of the property. In the example, this would be “cost”.

  • type – The type of the property as defined when you called addProperty or addValueList. In the example, this would be TCOD_TYPE_INT.

  • value – The value of the property, stored in a generic value structure. In the example, we would have value.i == 300. In the case of a value-list property, the type would reflect the list id (between TCOD_TYPE_VALUELIST00 and TCOD_TYPE_VALUELIST15) and value.s would contain the actual string.

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.

Example : It must return true if everything is right, false if there is an error and the parser must exit.

Parameters
  • parser – In the C++ version, the parser object, returned by TCODParser constructor. It’s used for error handling.

  • str – The structure type. Can be used to retrieve the type’s name with getName. In the example above, this would be “item_type”.

  • name – The name of the structure or NULL if no name is present in the file. In the example above, this would be “blade”.

virtual void error(const char *msg) = 0

There are two kind of errors : Errors that are detected by the parser itself (malformed file, bad value syntax for a property, missing mandatory property in a structure, …).

Errors that you detect in your callbacks. When the parser finds an error in the file, it will call the error callback and stop :

If you find an error in your callback, you have to call the parser error function. It will add the file name and line number to your error message, and then call your error callback : The code in the example below will result in your error callback called with the following string : “error in <filename> line <line_number> : Bad cost value %d. Cost must be between 0 and 1000”

Parameters
  • msg – The error message from the parser with the file name and the line number.

  • msg – printf-like format string for your error message.