libtcod
Loading...
Searching...
No Matches
lex.h
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
36#pragma once
37#ifndef TCOD_LEX_H_
38#define TCOD_LEX_H_
39
40#include "portability.h"
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46#define TCOD_LEX_FLAG_NOCASE 1
47#define TCOD_LEX_FLAG_NESTING_COMMENT 2
48#define TCOD_LEX_FLAG_TOKENIZE_COMMENTS 4
49
50#define TCOD_LEX_ERROR -1
51#define TCOD_LEX_UNKNOWN 0
52#define TCOD_LEX_SYMBOL 1
53#define TCOD_LEX_KEYWORD 2
54#define TCOD_LEX_IDEN 3
55#define TCOD_LEX_STRING 4
56#define TCOD_LEX_INTEGER 5
57#define TCOD_LEX_FLOAT 6
58#define TCOD_LEX_CHAR 7
59#define TCOD_LEX_EOF 8
60#define TCOD_LEX_COMMENT 9
61
62#define TCOD_LEX_MAX_SYMBOLS 100
63#define TCOD_LEX_SYMBOL_SIZE 5
64#define TCOD_LEX_MAX_KEYWORDS 100
65#define TCOD_LEX_KEYWORD_SIZE 20
66
67typedef struct TCOD_lex_t {
68 int file_line; // Current line number.
69 int token_type; // One of the TCOD_LEX_* values.
70 int token_int_val;
71 int token_idx;
72 float token_float_val;
73 char* tok;
74 int toklen;
75 char lastStringDelim;
76 char* pos;
77 char* buf;
78 char* filename;
79 char* last_javadoc_comment;
80 /* private stuff */
81 int nb_symbols; // Current number of symbols in `symbols` array.
82 int nb_keywords; // Current number of keywords in `keywords` array.
83 int flags;
84 char symbols[TCOD_LEX_MAX_SYMBOLS][TCOD_LEX_SYMBOL_SIZE];
85 char keywords[TCOD_LEX_MAX_KEYWORDS][TCOD_LEX_KEYWORD_SIZE];
86 const char* simple_comment;
87 const char* comment_start;
88 const char* comment_stop;
89 const char* javadoc_comment_start;
90 const char* stringDelim;
91 bool javadoc_read;
92 bool allocBuf; // True if `buf` is owned by this object.
93 bool is_savepoint; // is this object a savepoint (no free in destructor)
94} TCOD_lex_t;
95
96TCODLIB_API TCOD_lex_t* TCOD_lex_new_intern(void);
97TCODLIB_API TCOD_lex_t* TCOD_lex_new(
98 const char* const* symbols,
99 const char* const* keywords,
100 const char* simpleComment,
101 const char* commentStart,
102 const char* commentStop,
103 const char* javadocCommentStart,
104 const char* stringDelim,
105 int flags);
106TCODLIB_API void TCOD_lex_delete(TCOD_lex_t* lex);
107
108TCODLIB_API void TCOD_lex_set_data_buffer(TCOD_lex_t* lex, char* dat);
109TCODLIB_API bool TCOD_lex_set_data_file(TCOD_lex_t* lex, const char* filename);
110
111TCODLIB_API int TCOD_lex_parse(TCOD_lex_t* lex);
112TCODLIB_API int TCOD_lex_parse_until_token_type(TCOD_lex_t* lex, int token_type);
113TCODLIB_API int TCOD_lex_parse_until_token_value(TCOD_lex_t* lex, const char* token_value);
114
115TCODLIB_API bool TCOD_lex_expect_token_type(TCOD_lex_t* lex, int token_type);
116TCODLIB_API bool TCOD_lex_expect_token_value(TCOD_lex_t* lex, int token_type, const char* token_value);
117
118TCODLIB_API void TCOD_lex_savepoint(TCOD_lex_t* lex, TCOD_lex_t* savepoint);
119TCODLIB_API void TCOD_lex_restore(TCOD_lex_t* lex, TCOD_lex_t* savepoint);
120TCODLIB_API char* TCOD_lex_get_last_javadoc(TCOD_lex_t* lex);
121TCODLIB_API const char* TCOD_lex_get_token_name(int token_type);
122
123TCODLIB_API int TCOD_lex_hextoint(char c);
124#ifdef __cplusplus
125}
126#endif
127#endif // TCOD_LEX_H_
Miscellaneous tools needed across platforms.