public/ITextParsers.h
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 729 110c14c99545
child 1226 478550a4d5ec
permissions -rw-r--r--
Removed strange debugging code
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * All rights reserved.
     6  * ===============================================================
     7  *
     8  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be 
     9  * used or modified under the Terms and Conditions of its License Agreement, 
    10  * which is found in public/licenses/LICENSE.txt.  As of this notice, derivative 
    11  * works must be licensed under the GNU General Public License (version 2 or 
    12  * greater).  A copy of the GPL is included under public/licenses/GPL.txt.
    13  * 
    14  * To view the latest information, see: http://www.sourcemod.net/license.php
    15  *
    16  * Version: $Id$
    17  */
    18 
    19 #ifndef _INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
    20 #define _INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_
    21 
    22 /**
    23  * @file ITextParsers.h
    24  * @brief Defines various text/file parsing functions, as well as UTF-8 support code.
    25  */
    26 
    27 #include <IShareSys.h>
    28 
    29 namespace SourceMod
    30 {
    31 	/**
    32 	 * The INI file format is defined as:
    33 	 * WHITESPACE: 0x20, \n, \t, \r
    34 	 * IDENTIFIER: A-Z a-z 0-9 _ - , + . $ ? / 
    35 	 * STRING: Any set of symbols
    36 	 * 
    37 	 * Basic syntax is comprised of SECTIONs.
    38 	 * A SECTION is defined as:
    39 	 * [SECTIONNAME]
    40 	 * OPTION
    41 	 * OPTION
    42 	 * OPTION...
    43 	 *
    44 	 * SECTIONNAME is an IDENTIFIER.
    45 	 * OPTION can be repeated any number of times, once per line.
    46 	 * OPTION is defined as one of:
    47 	 *  KEY = "VALUE"
    48 	 *  KEY = VALUE
    49 	 *  KEY
    50 	 * Where KEY is an IDENTIFIER and VALUE is a STRING.
    51 	 * 
    52 	 * WHITESPACE should always be omitted.
    53 	 * COMMENTS should be stripped, and are defined as text occurring in:
    54 	 * ;<TEXT>
    55 	 * 
    56 	 * Example file below.  Note that
    57 	 * The second line is technically invalid.  The event handler
    58 	 * must decide whether this should be allowed.
    59 	 * --FILE BELOW--
    60 	 * [gaben]
    61 	 * hi = clams
    62 	 * bye = "NO CLAMS"
    63 	 *
    64 	 * [valve]
    65 	 * cannot
    66 	 * maintain
    67 	 * products
    68 	 */
    69 
    70 	/**
    71 	 * @brief Contains parse events for INI files.
    72 	 */
    73 	class ITextListener_INI
    74 	{
    75 	public:
    76 		/**
    77 		 * @brief Called when a new section is encountered in an INI file.
    78 		 * 
    79 		 * @param section		Name of section in between the [ and ] characters.
    80 		 * @param invalid_tokens True if invalid tokens were detected in the name.
    81 		 * @param close_bracket	True if a closing bracket was detected, false otherwise.
    82 		 * @param extra_tokens	True if extra tokens were detected on the line.
    83 		 * @param curtok		Contains current token in the line where the section name starts.
    84 		 *						You can add to this offset when failing to point to a token.
    85 		 * @return				True to keep parsing, false otherwise.
    86 		 */
    87 		virtual bool ReadINI_NewSection(const char *section,
    88 										bool invalid_tokens,
    89 										bool close_bracket,
    90 										bool extra_tokens,
    91 										unsigned int *curtok)
    92 		{
    93 			return true;
    94 		}
    95 
    96 		/**
    97 		 * @brief Called when encountering a key/value pair in an INI file.
    98 		 * 
    99 		 * @param key			Name of key.
   100 		 * @param value			String containing value (with quotes stripped, if any).
   101 		 * @param invalid_tokens Whether or not the key contained invalid tokens.
   102 		 * @param equal_token	There was an '=' sign present (in case the value is missing).
   103 		 * @param quotes		Whether value was enclosed in quotes.
   104 		 * @param curtok		Contains the token index of the start of the value string.  
   105 		 *						This can be changed when returning false.
   106 		 * @return				True to keep parsing, false otherwise.
   107 		 */
   108 		virtual bool ReadINI_KeyValue(const char *key, 
   109 									  const char *value, 
   110 									  bool invalid_tokens,
   111 									  bool equal_token,
   112 									  bool quotes,
   113 									  unsigned int *curtok)
   114 		{
   115 			return true;
   116 		}
   117 
   118 		/**
   119 		 * @brief Called after a line has been preprocessed, if it has text.
   120 		 *
   121 		 * @param line			Contents of line.
   122 		 * @param curtok		Pointer to optionally store failed position in string.
   123 		 * @return				True to keep parsing, false otherwise.
   124 		 */
   125 		virtual bool ReadINI_RawLine(const char *line, unsigned int *curtok)
   126 		{
   127 			return true;
   128 		}
   129 	};
   130 
   131 	/**
   132 	 * :TODO: write this in CFG (context free grammar) format so it makes sense
   133 	 * 
   134 	 * The SMC file format is defined as:
   135 	 * WHITESPACE: 0x20, \n, \t, \r
   136 	 * IDENTIFIER: Any ASCII character EXCLUDING ", {, }, ;, //, / *, or WHITESPACE.
   137 	 * STRING: Any set of symbols enclosed in quotes.
   138 	 * Note: if a STRING does not have quotes, it is parsed as an IDENTIFIER.
   139 	 *
   140 	 * Basic syntax is comprised of SECTIONBLOCKs.
   141 	 * A SECTIONBLOCK defined as:
   142 	 *
   143 	 * SECTIONNAME
   144 	 * {
   145 	 *    OPTION
   146 	 * }
   147 	 * 
   148 	 * OPTION can be repeated any number of times inside a SECTIONBLOCK.
   149 	 * A new line will terminate an OPTION, but there can be more than one OPTION per line.
   150 	 * OPTION is defined any of:
   151 	 * 	  "KEY"  "VALUE"
   152 	 *    SECTIONBLOCK
   153 	 *
   154 	 * SECTIONNAME, KEY, VALUE, and SINGLEKEY are strings
   155 	 * SECTIONNAME cannot have trailing characters if quoted, but the quotes can be optionally removed.
   156 	 * If SECTIONNAME is not enclosed in quotes, the entire sectionname string is used (minus surrounding whitespace).
   157 	 * If KEY is not enclosed in quotes, the key is terminated at first whitespace.
   158 	 * If VALUE is not properly enclosed in quotes, the entire value string is used (minus surrounding whitespace).
   159 	 * The VALUE may have inner quotes, but the key string may not.
   160 	 *
   161 	 * For an example, see configs/permissions.cfg
   162 	 *
   163 	 * WHITESPACE should be ignored.
   164 	 * Comments are text occurring inside the following tokens, and should be stripped
   165 	 * unless they are inside literal strings:
   166 	 *  ;<TEXT>
   167 	 *  //<TEXT>
   168 	 *  / *<TEXT> */
   169 
   170 	/**
   171 	 * @brief Lists actions to take when an SMC parse hook is done.
   172 	 */
   173 	enum SMCParseResult
   174 	{
   175 		SMCParse_Continue,		/**< Continue parsing */
   176 		SMCParse_Halt,			/**< Stop parsing here */
   177 		SMCParse_HaltFail		/**< Stop parsing and return SMCParseError_Custom */
   178 	};
   179 
   180 	/**
   181 	 * @brief Lists error codes possible from parsing an SMC file.
   182 	 */
   183 	enum SMCParseError
   184 	{
   185 		SMCParse_Okay = 0,			/**< No error */
   186 		SMCParse_StreamOpen,		/**< Stream failed to open */
   187 		SMCParse_StreamError,		/**< The stream died... somehow */
   188 		SMCParse_Custom,			/**< A custom handler threw an error */
   189 		SMCParse_InvalidSection1,	/**< A section was declared without quotes, and had extra tokens */
   190 		SMCParse_InvalidSection2,	/**< A section was declared without any header */
   191 		SMCParse_InvalidSection3,	/**< A section ending was declared with too many unknown tokens */
   192 		SMCParse_InvalidSection4,	/**< A section ending has no matching beginning */
   193 		SMCParse_InvalidSection5,	/**< A section beginning has no matching ending */
   194 		SMCParse_InvalidTokens,		/**< There were too many unidentifiable strings on one line */
   195 		SMCParse_TokenOverflow,		/**< The token buffer overflowed */
   196 		SMCParse_InvalidProperty1,	/**< A property was declared outside of any section */
   197 	};
   198 
   199 	/**
   200 	 * @brief Describes the events available for reading an SMC stream.
   201 	 */
   202 	class ITextListener_SMC
   203 	{
   204 	public:
   205 		/**
   206 		 * @brief Called when starting parsing.
   207 		 */
   208 		virtual void ReadSMC_ParseStart()
   209 		{
   210 		};
   211 
   212 		/**
   213 		 * @brief Called when ending parsing.
   214 		 *
   215 		 * @param halted			True if abnormally halted, false otherwise.
   216 		 * @param failed			True if parsing failed, false otherwise.
   217 		 */
   218 		virtual void ReadSMC_ParseEnd(bool halted, bool failed)
   219 		{
   220 		}
   221 
   222 		/**
   223 		 * @brief Called when a warning occurs.
   224 		 * @param error				By-reference variable containing the error message of the warning.
   225 		 * @param tokens			Pointer to the token stream causing the error.
   226 		 * @return					SMCParseResult directive.
   227 		 */
   228 		virtual SMCParseResult ReadSMC_OnWarning(SMCParseError &error, const char *tokens)
   229 		{
   230 			return SMCParse_HaltFail;
   231 		}
   232 
   233 		/**
   234 		 * @brief Called when entering a new section
   235 		 *
   236 		 * @param name			Name of section, with the colon omitted.
   237 		 * @param opt_quotes	Whether or not the option string was enclosed in quotes.
   238 		 * @return				SMCParseResult directive.
   239 		 */
   240 		virtual SMCParseResult ReadSMC_NewSection(const char *name, bool opt_quotes)
   241 		{
   242 			return SMCParse_Continue;
   243 		}
   244 
   245 		/**
   246 		 * @brief Called when encountering a key/value pair in a section.
   247 		 * 
   248 		 * @param key			Key string.
   249 		 * @param value			Value string.  If no quotes were specified, this will be NULL, 
   250 								and key will contain the entire string.
   251 		 * @param key_quotes	Whether or not the key was in quotation marks.
   252 		 * @param value_quotes	Whether or not the value was in quotation marks.
   253 		 * @return				SMCParseResult directive.
   254 		 */
   255 		virtual SMCParseResult ReadSMC_KeyValue(const char *key, 
   256 												const char *value, 
   257 												bool key_quotes, 
   258 												bool value_quotes)
   259 		{
   260 			return SMCParse_Continue;
   261 		}
   262 
   263 		/**
   264 		 * @brief Called when leaving the current section.
   265 		 *
   266 		 * @return				SMCParseResult directive.
   267 		 */
   268 		virtual SMCParseResult ReadSMC_LeavingSection()
   269 		{
   270 			return SMCParse_Continue;
   271 		}
   272 
   273 		/**
   274 		 * @brief Called after an input line has been preprocessed.
   275 		 *
   276 		 * @param line			String containing line input.
   277 		 * @param curline		Number of line in file.
   278 		 * @return				SMCParseResult directive.
   279 		 */
   280 		virtual SMCParseResult ReadSMC_RawLine(const char *line, unsigned int curline)
   281 		{
   282 			return SMCParse_Continue;
   283 		}
   284 	};	
   285 
   286 	#define SMINTERFACE_TEXTPARSERS_NAME		"ITextParsers"
   287 	#define SMINTERFACE_TEXTPARSERS_VERSION		1
   288 
   289 	/**
   290 	 * @brief Contains various text stream parsing functions.
   291 	 */
   292 	class ITextParsers : public SMInterface
   293 	{
   294 	public:
   295 		virtual const char *GetInterfaceName()
   296 		{
   297 			return SMINTERFACE_TEXTPARSERS_NAME;
   298 		}
   299 		virtual unsigned int GetInterfaceVersion()
   300 		{
   301 			return SMINTERFACE_TEXTPARSERS_VERSION;
   302 		}
   303 	public:
   304 		/**
   305 		 * @brief Parses an INI-format file.
   306 		 *
   307 		 * @param file			Path to file.
   308 		 * @param ini_listener	Event handler for reading file.
   309 		 * @param line			If non-NULL, will contain last line parsed (0 if file could not be opened).
   310 		 * @param col			If non-NULL, will contain last column parsed (undefined if file could not be opened).
   311 		 * @return				True if parsing succeeded, false if file couldn't be opened or there was a syntax error.
   312 		 */
   313 		virtual bool ParseFile_INI(const char *file, 
   314 									ITextListener_INI *ini_listener,
   315 									unsigned int *line,
   316 									unsigned int *col) =0;
   317 
   318 		/**
   319 		 * @brief Parses an SMC-format text file.
   320 		 * Note that the parser makes every effort to obey broken syntax.
   321 		 * For example, if an open brace is missing, but the section name has a colon,
   322 		 * it will let you know.  It is up to the event handlers to decide whether to be strict or not.
   323 		 *
   324 		 * @param file			Path to file.
   325 		 * @param smc_listener	Event handler for reading file.
   326 		 * @param line			If non-NULL, will contain last line parsed (0 if file could not be opened).
   327 		 * @param col			If non-NULL, will contain last column parsed (undefined if file could not be opened).
   328 		 * @return				An SMCParseError result code.
   329 		 */
   330 		virtual SMCParseError ParseFile_SMC(const char *file, 
   331 									ITextListener_SMC *smc_listener, 
   332 									unsigned int *line, 
   333 									unsigned int *col) =0;
   334 
   335 		/**
   336 		 * @brief Converts an SMCParseError to a string.
   337 		 *
   338 		 * @param err			SMCParseError.
   339 		 * @return				String error message, or NULL if none.
   340 		 */
   341 		virtual const char *GetSMCErrorString(SMCParseError err) =0;
   342 
   343 	public:
   344 		/**
   345 		 * @brief Returns the number of bytes that a multi-byte character contains in a UTF-8 stream.
   346 		 * If the current character is not multi-byte, the function returns 1.
   347 		 *
   348 		 * @param stream		Pointer to multi-byte ANSI character string.
   349 		 * @return				Number of bytes in current character.
   350 		 */
   351 		virtual unsigned int GetUTF8CharBytes(const char *stream) =0;
   352 	};
   353 }
   354 
   355 #endif //_INCLUDE_SOURCEMOD_TEXTPARSERS_INTERFACE_H_