libtcod
Loading...
Searching...
No Matches
lex.hpp
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 */
32// clang-format off
37#pragma once
38#ifndef TCOD_LEX_HPP_
39#define TCOD_LEX_HPP_
40
41#include <utility>
42
43#include "lex.h"
44
45class TCODLIB_API TCODLex {
46 public:
47 TCODLex();
48 TCODLex(
49 const char** symbols,
50 const char** keywords,
51 const char* simpleComment="//",
52 const char* commentStart="/*",
53 const char* commentStop="*/",
54 const char* javadocCommentStart="/**",
55 const char* stringDelim="\"",
56 int flags=TCOD_LEX_FLAG_NESTING_COMMENT);
57
58 TCODLex(TCODLex&& rhs) noexcept { std::swap(data, rhs.data); };
59 TCODLex& operator=(TCODLex&& rhs) noexcept {
60 std::swap(data, rhs.data);
61 return *this;
62 };
63
64 ~TCODLex();
65
66 void setDataBuffer(char* dat);
67 bool setDataFile(const char* filename);
68
69 int parse();
70 int parseUntil(int tokenType);
71 int parseUntil(const char* tokenValue);
72
73 bool expect(int tokenType);
74 bool expect(int tokenType, const char* tokenValue);
75
76 void savepoint(TCODLex* savepoint);
77 void restore(TCODLex* savepoint);
78 char* getLastJavadoc();
79
80 int getFileLine()
81 {
82 return data->file_line;
83 }
84 int getTokenType()
85 {
86 return data->token_type;
87 }
88 int getTokenIntVal()
89 {
90 return data->token_int_val;
91 }
92 int getTokenIdx()
93 {
94 return data->token_idx;
95 }
96 float getTokenFloatVal()
97 {
98 return data->token_float_val;
99 }
100 char* getToken()
101 {
102 return data->tok;
103 }
104 char getStringLastDelimiter()
105 {
106 return data->lastStringDelim;
107 }
108 char* getPos()
109 { return data->pos; }
110 char* getBuf()
111 {
112 return data->buf;
113 }
114 char* getFilename()
115 {
116 return data->filename;
117 }
118 char* getLastJavadocComment()
119 {
120 return data->last_javadoc_comment;
121 }
122 static const char* getTokenName(int tokenType)
123 {
124 return TCOD_lex_get_token_name(tokenType);
125 }
126 protected:
127 TCOD_lex_t* data{};
128};
129#endif // TCOD_LEX_HPP_
Internal tokenizer module.