Program Listing for File parser.hpp

Return to documentation for file (libtcod/parser.hpp)

/* BSD 3-Clause License
 *
 * Copyright © 2008-2026, Jice and the libtcod contributors.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#pragma once
#ifndef TCOD_PARSER_HPP_
#define TCOD_PARSER_HPP_

#include <memory>
#include <vector>

#include "color.hpp"
#include "list.hpp"
#include "parser.h"
// clang-format off
class TCODLIB_API TCODParser;
class TCODLIB_API TCODParserStruct;
class TCODLIB_API ITCODParserListener;

class TCODLIB_API TCODParser {
public :
  TCODParser();

  // Disable copy operators.
  TCODParser(const TCODParser&) = delete;
  TCODParser& operator=(const TCODParser&) = delete;
  TCODParser(TCODParser&& rhs) noexcept { std::swap(data, rhs.data); };
  TCODParser& operator=(TCODParser&& rhs) noexcept {
    std::swap(data, rhs.data);
    return *this;
  };

  TCODParserStruct *newStructure(const char *name);

  // register a new custom type
  TCOD_value_type_t newCustomType(TCOD_parser_custom_t custom_type_parser);

  void run(const char *filename, ITCODParserListener *listener = NULL);

  ~TCODParser();

  // error during parsing. can be called by the parser listener
  void error(const char *msg, ...);

  bool hasProperty(const char *name) const;
  bool getBoolProperty(const char *name) const;
  int getIntProperty(const char *name) const;
  int getCharProperty(const char *name) const;
  float getFloatProperty(const char *name) const;
  TCODColor getColorProperty(const char *name) const;
  TCOD_dice_t getDiceProperty(const char *name) const;
  const char * getStringProperty(const char *name) const;
  void * getCustomProperty(const char *name) const;
  TCOD_list_t getListProperty(const char *name, TCOD_value_type_t type) const;
private :
  bool parseEntity(TCODParserStruct *def, ITCODParserListener *listener);
  TCOD_parser_t data{};
#ifdef _MSC_VER
  // Disable dll-interface warning.  This value should only used internally.
#pragma warning(push)
#pragma warning(disable: 4251)
#endif // _MSC_VER
  std::vector<std::unique_ptr<TCODParserStruct>> defs;
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
  friend bool new_struct(TCOD_parser_struct_t def, const char* name) noexcept;
  friend bool end_struct(TCOD_parser_struct_t def, const char* name) noexcept;
};

// a parser structure
class TCODLIB_API TCODParserStruct {
public :
  TCODParserStruct* addFlag(const char *propname);

  TCODParserStruct* addProperty(const char *propname, TCOD_value_type_t type, bool mandatory);

  TCODParserStruct* addValueList(const char *propname, const char **value_list, bool mandatory);

  TCODParserStruct* addListProperty(const char *propname, TCOD_value_type_t type, bool mandatory);

  TCODParserStruct* addStructure(TCODParserStruct *sub_entity);

  const char *getName() const;

  bool isPropertyMandatory(const char *propname) const;

  TCOD_value_type_t getPropertyType(const char *propname) const;

// private stuff
  TCOD_parser_struct_t data;
};

// sax event listener
class TCODLIB_API ITCODParserListener {
public :
  virtual ~ITCODParserListener(){}
  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 *propname, 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;
};

#endif // TCOD_PARSER_HPP_