|
< index < 7. File parser < 7.4 Standard data types |
===================================== | > 7.6 Using the default parser listener |
typedef TCOD_value_t (*TCOD_parser_custom_t)(TCOD_lex_t *lex, TCOD_parser_listener_t *listener, TCOD_parser_struct_t str, char *name); C++ : TCOD_value_type_t TCODParser::newCustomType(TCOD_parser_custom_t custom_type_parser) C : TCOD_value_type_t TCOD_parser_new_custom_type(TCOD_parser_t parser,TCOD_parser_custom_t custom_type_parser)This function associate a datatype parser with a new TCOD type. It returns the corresponding TCOD type between TCOD_TYPE_CUSTOM00 and TCOD_TYPE_CUSTOM15 or TCOD_TYPE_NONE if there are no more custom types available.
| Parameter | Description |
|---|---|
| parser | In the C version, the parser handler, returned by TCOD_parser_new. |
| custom_type_parser | A function pointer to the new datatype parser. |
| Parameter | Description |
|---|---|
| lex | The libtcod generic lexical parser. This part is not yet documented. |
| listener | The parser listener, mainly to call the error callback. |
| str | The structure type currently being parsed. |
| name | The name of the property to parse. |
TCOD_value_t TCOD_parse_bool_value(); TCOD_value_t TCOD_parse_char_value(); TCOD_value_t TCOD_parse_integer_value(); TCOD_value_t TCOD_parse_float_value(); TCOD_value_t TCOD_parse_string_value(); TCOD_value_t TCOD_parse_color_value(); TCOD_value_t TCOD_parse_dice_value();Example of a (very simple) custom parser :
// custom color parser. handle "sun" value
// World::sunColor is a special color that is replaced at runtime by the actual sun color
TCOD_value_t customColorParser(TCOD_lex_t *lex, TCOD_parser_listener_t *listener, TCOD_parser_struct_t def, char *propname) {
if ( strcmp(lex->tok,"sun") == 0 ) {
TCOD_value_t ret;
ret.col.r = World::sunColor.r;
ret.col.g = World::sunColor.g;
ret.col.b = World::sunColor.b;
return ret;
}
return TCOD_parse_color_value();
}
TCOD_value_type_t customColor = parser.newCustomType(customColorParser);
TCODParserStruct *feat = parser.newStructure("feature");
feat->addProperty("color",customColor,false);