ArkScript
A small, fast, functional and scripting language for video games
BaseParser.hpp
Go to the documentation of this file.
1#ifndef SRC_BASEPARSER_HPP
2#define SRC_BASEPARSER_HPP
3
4#include <string>
5#include <vector>
6#include <initializer_list>
7
8#include <Ark/Platform.hpp>
11
12namespace Ark::internal
13{
14 /**
15 * @brief Describe a position in a given file ; handled by the BaseParser
16 */
18 {
19 std::size_t row = 0;
20 std::size_t col = 0;
21 };
22
24 {
25 public:
26 BaseParser() = default;
27
28 private:
29 std::string m_str;
30 std::vector<std::pair<std::string::iterator, std::size_t>> m_it_to_row; ///< A crude map of \n position to line number to speed up line number computing
31 std::string::iterator m_it, m_next_it;
32 utf8_char_t m_sym; ///< The current utf8 character we're on
33 FilePosition m_filepos; ///< The position of the cursor in the file
34
35 /**
36 * @brief Register the position of a new line, with an iterator pointing to the new line and the row number
37 *
38 * @param it
39 * @param row
40 */
41 void registerNewLine(std::string::iterator it, std::size_t row);
42
43 /**
44 * @brief getting next character and changing the values of count/row/col/sym
45 */
46 void next();
47
48 protected:
49 std::string m_filename;
50
51 void initParser(const std::string& filename, const std::string& code);
52
53 FilePosition getCursor() const;
54
55 /**
56 *
57 * @param error an error message
58 * @param exp the expression causing the error
59 */
60 void error(const std::string& error, std::string exp);
61
62 /**
63 * @brief Fetch the next token (space and paren delimited) to generate an error
64 *
65 * @param message an error message
66 */
67 void errorWithNextToken(const std::string& message);
68
69 /**
70 * @brief Generate an error for a given node when a suffix is missing
71 *
72 * @param suffix a suffix char, eg " or )
73 * @param node_name can be "string", "node" ; represents a structure
74 */
75 void errorMissingSuffix(char suffix, const std::string& node_name);
76
77 /**
78 *
79 * @return distance in characters from the beginning of the file to the cursor
80 */
81 long getCount() { return std::distance(m_str.begin(), m_it); }
82
83 /**
84 *
85 * @return file size in bytes
86 */
87 std::size_t getSize() const { return m_str.size(); }
88
89 /**
90 *
91 * @return true if the cursor is positioned at the end of the file
92 */
93 [[nodiscard]] bool isEOF() const { return m_it == m_str.end(); }
94
95 void backtrack(long n);
96
97 /**
98 * @brief check if a Character Predicate was able to parse, call next() if matching
99 *
100 * @param t a char predicate to match
101 * @param s optional string to append the matching chars to
102 * @return true if matched
103 */
104 bool accept(const CharPred& t, std::string* s = nullptr);
105
106 /**
107 * @brief heck if a Character Predicate was able to parse, call next() if matching ; throw a CodeError if it doesn't match
108 * @param t a char predicate to match
109 * @param s optional string to append the matching chars to
110 * @return true if matched
111 */
112 bool expect(const CharPred& t, std::string* s = nullptr);
113
114 // basic parsers
115
116 bool space(std::string* s = nullptr);
117 bool inlineSpace(std::string* s = nullptr);
118 bool comment(std::string* s = nullptr);
119 bool spaceComment(std::string* s = nullptr);
120 bool newlineOrComment(std::string* s = nullptr);
121 bool prefix(char c);
122 bool suffix(char c);
123 bool number(std::string* s = nullptr);
124 bool signedNumber(std::string* s = nullptr);
125 bool hexNumber(unsigned length, std::string* s = nullptr);
126 bool name(std::string* s = nullptr);
127 bool sequence(const std::string& s);
128 bool packageName(std::string* s = nullptr);
129
130 /**
131 * @brief Match any char that do not match the predicate
132 *
133 * @param delim delimiter predicate
134 * @param s optional string to append the matching chars to
135 * @return true if matched
136 */
137 bool anyUntil(const CharPred& delim, std::string* s = nullptr);
138
139 /**
140 * @brief Fetch a token and try to match one of the given words
141 *
142 * @param words list of words to match against
143 * @param s optional string to append the matching chars to
144 * @return true if matched
145 */
146 bool oneOf(std::initializer_list<std::string> words, std::string* s = nullptr);
147 };
148}
149
150#endif
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
FilePosition m_filepos
The position of the cursor in the file.
std::size_t getSize() const
std::string::iterator m_it
std::vector< std::pair< std::string::iterator, std::size_t > > m_it_to_row
A crude map of position to line number to speed up line number computing.
utf8_char_t m_sym
The current utf8 character we're on.
Describe a position in a given file ; handled by the BaseParser.