ArkScript
A small, fast, functional and scripting language for video games
Lexer.hpp
Go to the documentation of this file.
1/**
2 * @file Lexer.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Tokenize ArkScript code
5 * @version 0.1
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2021
9 *
10 */
11
12#ifndef ARK_COMPILER_LEXER_HPP
13#define ARK_COMPILER_LEXER_HPP
14
15#include <vector>
16
19
20namespace Ark::internal
21{
22 /**
23 * @brief The lexer, in charge of creating a list of tokens
24 *
25 */
26 class Lexer
27 {
28 public:
29 /**
30 * @brief Construct a new Lexer object
31 *
32 * @param debug the debug level
33 */
34 explicit Lexer(unsigned debug) noexcept;
35
36 /**
37 * @brief Give code to tokenize and create the list of tokens
38 *
39 * @param code the ArkScript code
40 */
41 void feed(const std::string& code);
42
43 /**
44 * @brief Return the list of tokens
45 *
46 * @return std::vector<Token>&
47 */
48 std::vector<Token>& tokens() noexcept;
49
50 private:
51 unsigned m_debug;
52 std::vector<Token> m_tokens;
53
54 inline constexpr bool isHexChar(char chr)
55 {
56 return (('a' <= chr && chr <= 'f') || ('A' <= chr && chr <= 'F') || ('0' <= chr && chr <= '9'));
57 }
58
59 /**
60 * @brief Helper function to determine the type of a token
61 *
62 * @param value
63 * @return TokenType
64 */
65 TokenType guessType(const std::string& value) noexcept;
66
67 /**
68 * @brief Check if the value is a keyword in ArkScript
69 *
70 * @param value
71 * @return true
72 * @return false
73 */
74 bool isKeyword(const std::string& value) noexcept;
75 /**
76 * @brief Check if the value can be an identifier in ArkScript
77 *
78 * @param value
79 * @return true
80 * @return false
81 */
82 bool isIdentifier(const std::string& value) noexcept;
83
84 /**
85 * @brief Check if the value is an operator in ArkScript
86 *
87 * @param value
88 * @return true
89 * @return false
90 */
91 bool isOperator(const std::string& value) noexcept;
92
93 /**
94 * @brief Check if a control character / sequence is complete or not
95 *
96 * @param sequence the sequence without the leading \\
97 * @param next the next character to come, maybe, in the sequence
98 * @return true
99 * @return false
100 */
101 bool endOfControlChar(const std::string& sequence, char next) noexcept;
102
103 /**
104 * @brief To throw nice lexer errors
105 *
106 * @param message
107 * @param match
108 * @param line
109 * @param col
110 * @param context
111 */
112 [[noreturn]] void throwTokenizingError(const std::string& message, const std::string& match, std::size_t line, std::size_t col, const std::string& context);
113 };
114}
115
116#endif
Common code for the compiler.
Token definition for ArkScript.
The lexer, in charge of creating a list of tokens.
Definition: Lexer.hpp:27
std::vector< Token > & tokens() noexcept
Return the list of tokens.
Definition: Lexer.cpp:257
bool isIdentifier(const std::string &value) noexcept
Check if the value can be an identifier in ArkScript.
Definition: Lexer.cpp:291
constexpr bool isHexChar(char chr)
Definition: Lexer.hpp:54
bool isKeyword(const std::string &value) noexcept
Check if the value is a keyword in ArkScript.
Definition: Lexer.cpp:286
TokenType guessType(const std::string &value) noexcept
Helper function to determine the type of a token.
Definition: Lexer.cpp:262
void feed(const std::string &code)
Give code to tokenize and create the list of tokens.
Definition: Lexer.cpp:19
bool isOperator(const std::string &value) noexcept
Check if the value is an operator in ArkScript.
Definition: Lexer.cpp:296
std::vector< Token > m_tokens
Definition: Lexer.hpp:52
unsigned m_debug
Definition: Lexer.hpp:51
bool endOfControlChar(const std::string &sequence, char next) noexcept
Check if a control character / sequence is complete or not.
Definition: Lexer.cpp:301
void throwTokenizingError(const std::string &message, const std::string &match, std::size_t line, std::size_t col, const std::string &context)
To throw nice lexer errors.
Definition: Lexer.cpp:330