1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-FileCopyrightText: Copyright (c) 2012 David Gobbi
// SPDX-License-Identifier: BSD-3-Clause
/**
This file provides string handling routines.
The two important jobs done by these routines are string tokenization
and string caching.
Tokenization is done as per the rules of a C++ preprocessor, and
breaks the strings into ids, literals, and operators. Any string
is a valid input for the tokenizer, and it is up to the parser to
decide if the resulting tokens are valid within the grammar. The
two primary tokenization functions are vtkParse_InitTokenizer()
and vtkParse_NextToken().
Caching refers to how string memory management is done. The
parser uses "const char *" for all strings, and expects all strings
to be persistent and constant. These conditions are automatically
met by static strings, but dynamically-generated strings must be
cached until the parse is complete. The primary caching functions
are vtkParse_CacheString() and vtkParse_FreeStringCache().
*/
#ifndef vtkParseString_h
#define vtkParseString_h
#include "vtkWrappingToolsModule.h"
#include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* Various important char types for tokenization
*/
typedef enum parse_char_type_
{
CPRE_NONDIGIT = 0x01, /* A-Z a-z and _ */
CPRE_DIGIT = 0x02, /* 0-9 */
CPRE_XDIGIT = 0x03, /* 0-9 A-Z a-z and _ */
CPRE_EXTEND = 0x04, /* non-ascii character */
CPRE_ID = 0x05, /* starting char for identifier */
CPRE_XID = 0x07, /* continuing char for identifier */
CPRE_HEX = 0x08, /* 0-9 A-F a-f hexadecimal digits */
CPRE_SIGN = 0x10, /* +- (sign for floats) */
CPRE_QUOTE = 0x20, /* " and ' */
CPRE_HSPACE = 0x40, /* space, tab, carriage return */
CPRE_VSPACE = 0x80, /* newline, vertical tab, form feed */
CPRE_WHITE = 0xC0, /* all whitespace characters */
} parse_char_type;
/**
* check if a char is of a certain type
*/
VTKWRAPPINGTOOLS_EXPORT int vtkParse_CharType(char c, int bits);
/**
* Whitespace types that can be used with the tokenizer.
* - WS_DEFAULT treats newlines and formfeeds as regular whitespace.
* - WS_PREPROC treats newline as end-of-line, not as whitespace.
* - WS_COMMENT treats comments as tokens, not as whitespace.
*/
typedef enum parse_space_t_
{
WS_DEFAULT = CPRE_WHITE, /* skip all whitespace */
WS_PREPROC = CPRE_HSPACE, /* skip horizontal whitespace only */
WS_COMMENT = (CPRE_WHITE | 0x100), /* comments as tokens */
} parse_space_t;
/**
* Preprocessor tokens for C++.
*/
typedef enum preproc_token_t_
{
TOK_OTHER = 257,
TOK_ID, /* any id */
TOK_CHAR, /* char literal */
TOK_STRING, /* string literal */
TOK_NUMBER, /* any numeric literal */
TOK_COMMENT, /* C or C++ comment */
TOK_DBLHASH, /* ## */
TOK_SCOPE, /* :: */
TOK_INCR, /* ++ */
TOK_DECR, /* -- */
TOK_RSHIFT, /* >> */
TOK_LSHIFT, /* << */
TOK_AND, /* && */
TOK_OR, /* || */
TOK_EQ, /* == */
TOK_NE, /* != */
TOK_GE, /* >= */
TOK_LE, /* <= */
TOK_ADD_EQ, /* += */
TOK_SUB_EQ, /* -= */
TOK_MUL_EQ, /* *= */
TOK_DIV_EQ, /* /= */
TOK_MOD_EQ, /* %= */
TOK_AND_EQ, /* &= */
TOK_OR_EQ, /* |= */
TOK_XOR_EQ, /* ^= */
TOK_ARROW, /* -> */
TOK_DOT_STAR, /* .* */
TOK_ARROW_STAR, /* ->* */
TOK_RSHIFT_EQ, /* >>= */
TOK_LSHIFT_EQ, /* <<= */
TOK_ELLIPSIS, /* ... */
} preproc_token_t;
/**
* A struct for going through a string one token at a time.
* If ws is set to WS_PREPROC, then tokenization stops when a
* newline or null is encountered. If ws is set to WS_DEFAULT,
* then tokenization only stops when a null is encountered. If
* ws is set to WS_COMMENT, then tokenization stops only when
* a null is encountered, and comments are returned as tokens
* instead of being skipped as whitespace.
*/
typedef struct StringTokenizer_
{
int tok; /* the current token */
unsigned int hash; /* the hash of the current token, if it is an id */
const char* text; /* the text for the current token, not null-teminated */
size_t len; /* the length of the current token */
parse_space_t ws; /* controls what to consider as whitespace */
} StringTokenizer;
/**
* Initialize the tokenizer and get the first token.
*/
VTKWRAPPINGTOOLS_EXPORT
void vtkParse_InitTokenizer(StringTokenizer* tokens, const char* text, parse_space_t wstype);
/**
* Return the next preprocessor token, or '0' if none left.
*/
VTKWRAPPINGTOOLS_EXPORT
int vtkParse_NextToken(StringTokenizer* tokens);
/**
* Skip over whitespace.
* Return the number of chars until the first non-whitespace token.
* Set spacetype to WS_DEFAULT, WS_PREPROC, or WS_COMMENT.
*/
VTKWRAPPINGTOOLS_EXPORT
size_t vtkParse_SkipWhitespace(const char* cp, parse_space_t spacetype);
/**
* Skip over a comment, C style or C++ style.
* Return the number of chars until the end of the comment.
*/
VTKWRAPPINGTOOLS_EXPORT
size_t vtkParse_SkipComment(const char* cp);
/**
* Skip over a string in double or single quotes.
* Return the number of chars until the end of the quotes.
*/
VTKWRAPPINGTOOLS_EXPORT
size_t vtkParse_SkipQuotes(const char* cp);
/**
* Skip over a number. Uses preprocessor semantics.
* Return the number of chars until the end of the number.
*/
VTKWRAPPINGTOOLS_EXPORT
size_t vtkParse_SkipNumber(const char* cp);
/**
* Skip over an identifier.
* Return the number of chars until the end of the identifier.
*/
VTKWRAPPINGTOOLS_EXPORT
size_t vtkParse_SkipId(const char* cp);
/**
* Compute the hash for a id, for use in hash table lookups.
* This stops at the first non-Id character, so it is safe to use
* on a string that is not null-terminated as long as there is either
* whitespace or an operator character before the end of the string.
* It can be used on null-terminated strings as well, of course.
*/
VTKWRAPPINGTOOLS_EXPORT
unsigned int vtkParse_HashId(const char* cp);
/**
* Decode a single unicode character from utf8, or set error flag to 1.
* The character pointer will be advanced by one if an error occurred,
* and the return value will be the value of the first octet.
*/
VTKWRAPPINGTOOLS_EXPORT
unsigned int vtkParse_DecodeUtf8(const char** cpp, int* error_flag);
/**
* StringCache provides a simple way of allocating strings centrally.
* It eliminates the need to allocate and free each individual string,
* which makes the code simpler and more efficient.
*/
typedef struct StringCache_
{
unsigned long NumberOfChunks;
char** Chunks;
size_t ChunkSize;
size_t Position;
} StringCache;
/**
* Initialize the string cache.
*/
VTKWRAPPINGTOOLS_EXPORT
void vtkParse_InitStringCache(StringCache* cache);
/**
* Allocate a new string from the cache.
* A total of n+1 bytes will be allocated, to leave room for null.
*/
VTKWRAPPINGTOOLS_EXPORT
char* vtkParse_NewString(StringCache* cache, size_t n);
/**
* Cache a string so that it can then be used in the vtkParse data
* structures. The string will last until the application exits.
* At most 'n' chars will be copied, and the string will be terminated.
* If a null pointer is provided, then a null pointer will be returned.
*/
VTKWRAPPINGTOOLS_EXPORT
const char* vtkParse_CacheString(StringCache* cache, const char* cp, size_t n);
/**
* Merge the second cache into the first cache, leaving the second
* cache empty.
*/
VTKWRAPPINGTOOLS_EXPORT
void vtkParse_MergeStringCache(StringCache* cache, StringCache* other);
/**
* Free all strings that were created with vtkParse_NewString() or
* with vtkParse_CacheString().
*/
VTKWRAPPINGTOOLS_EXPORT
void vtkParse_FreeStringCache(StringCache* cache);
/**
* Compute a hash for a string, using the first n bytes.
*/
VTKWRAPPINGTOOLS_EXPORT
unsigned int vtkParse_HashString(const char* cp, size_t n);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
/* VTK-HeaderTest-Exclude: vtkParseString.h */
|