Lexer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Lexer.h"
  27. #include <AK/Debug.h>
  28. #include <ctype.h>
  29. namespace SQL {
  30. HashMap<String, TokenType> Lexer::s_keywords;
  31. HashMap<char, TokenType> Lexer::s_one_char_tokens;
  32. HashMap<String, TokenType> Lexer::s_two_char_tokens;
  33. Lexer::Lexer(StringView source)
  34. : m_source(source)
  35. {
  36. if (s_keywords.is_empty()) {
  37. #define __ENUMERATE_SQL_TOKEN(value, type, category) \
  38. if (TokenCategory::category == TokenCategory::Keyword) \
  39. s_keywords.set(value, TokenType::type);
  40. ENUMERATE_SQL_TOKENS
  41. #undef __ENUMERATE_SQL_TOKEN
  42. }
  43. if (s_one_char_tokens.is_empty()) {
  44. #define __ENUMERATE_SQL_TOKEN(value, type, category) \
  45. if (TokenCategory::category != TokenCategory::Keyword && StringView(value).length() == 1) \
  46. s_one_char_tokens.set(value[0], TokenType::type);
  47. ENUMERATE_SQL_TOKENS
  48. #undef __ENUMERATE_SQL_TOKEN
  49. }
  50. if (s_two_char_tokens.is_empty()) {
  51. #define __ENUMERATE_SQL_TOKEN(value, type, category) \
  52. if (TokenCategory::category != TokenCategory::Keyword && StringView(value).length() == 2) \
  53. s_two_char_tokens.set(value, TokenType::type);
  54. ENUMERATE_SQL_TOKENS
  55. #undef __ENUMERATE_SQL_TOKEN
  56. }
  57. consume();
  58. }
  59. Token Lexer::next()
  60. {
  61. bool found_invalid_comment = consume_whitespace_and_comments();
  62. size_t value_start = m_position;
  63. size_t value_start_line_number = m_line_number;
  64. size_t value_start_column_number = m_line_column;
  65. auto token_type = TokenType::Invalid;
  66. if (is_eof()) {
  67. token_type = found_invalid_comment ? TokenType::Invalid : TokenType::Eof;
  68. } else if (is_numeric_literal_start()) {
  69. token_type = TokenType::NumericLiteral;
  70. if (!consume_numeric_literal())
  71. token_type = TokenType::Invalid;
  72. } else if (is_string_literal_start()) {
  73. token_type = TokenType::StringLiteral;
  74. if (!consume_string_literal())
  75. token_type = TokenType::Invalid;
  76. } else if (is_blob_literal_start()) {
  77. token_type = TokenType::BlobLiteral;
  78. if (!consume_blob_literal())
  79. token_type = TokenType::Invalid;
  80. } else if (is_identifier_start()) {
  81. do {
  82. consume();
  83. } while (is_identifier_middle());
  84. if (auto it = s_keywords.find(m_source.substring_view(value_start - 1, m_position - value_start)); it != s_keywords.end()) {
  85. token_type = it->value;
  86. } else {
  87. token_type = TokenType::Identifier;
  88. }
  89. } else {
  90. bool found_two_char_token = false;
  91. if (m_position < m_source.length()) {
  92. if (auto it = s_two_char_tokens.find(m_source.substring_view(m_position - 1, 2)); it != s_two_char_tokens.end()) {
  93. found_two_char_token = true;
  94. token_type = it->value;
  95. consume();
  96. consume();
  97. }
  98. }
  99. bool found_one_char_token = false;
  100. if (!found_two_char_token) {
  101. if (auto it = s_one_char_tokens.find(m_current_char); it != s_one_char_tokens.end()) {
  102. found_one_char_token = true;
  103. token_type = it->value;
  104. consume();
  105. }
  106. }
  107. if (!found_two_char_token && !found_one_char_token) {
  108. token_type = TokenType::Invalid;
  109. consume();
  110. }
  111. }
  112. Token token(token_type, m_source.substring_view(value_start - 1, m_position - value_start), value_start_line_number, value_start_column_number);
  113. if constexpr (SQL_DEBUG) {
  114. dbgln("------------------------------");
  115. dbgln("Token: {}", token.name());
  116. dbgln("Value: {}", token.value());
  117. dbgln("Line: {}, Column: {}", token.line_number(), token.line_column());
  118. dbgln("------------------------------");
  119. }
  120. return token;
  121. }
  122. void Lexer::consume()
  123. {
  124. auto did_reach_eof = [this] {
  125. if (m_position != m_source.length())
  126. return false;
  127. m_current_char = EOF;
  128. ++m_line_column;
  129. ++m_position;
  130. return true;
  131. };
  132. if (m_position > m_source.length())
  133. return;
  134. if (did_reach_eof())
  135. return;
  136. if (is_line_break()) {
  137. ++m_line_number;
  138. m_line_column = 1;
  139. } else {
  140. ++m_line_column;
  141. }
  142. m_current_char = m_source[m_position++];
  143. }
  144. bool Lexer::consume_whitespace_and_comments()
  145. {
  146. bool found_invalid_comment = false;
  147. while (true) {
  148. if (isspace(m_current_char)) {
  149. do {
  150. consume();
  151. } while (isspace(m_current_char));
  152. } else if (is_line_comment_start()) {
  153. consume();
  154. do {
  155. consume();
  156. } while (!is_eof() && !is_line_break());
  157. } else if (is_block_comment_start()) {
  158. consume();
  159. do {
  160. consume();
  161. } while (!is_eof() && !is_block_comment_end());
  162. if (is_eof())
  163. found_invalid_comment = true;
  164. consume(); // consume *
  165. if (is_eof())
  166. found_invalid_comment = true;
  167. consume(); // consume /
  168. } else {
  169. break;
  170. }
  171. }
  172. return found_invalid_comment;
  173. }
  174. bool Lexer::consume_numeric_literal()
  175. {
  176. // https://sqlite.org/syntax/numeric-literal.html
  177. bool is_valid_numeric_literal = true;
  178. if (m_current_char == '0') {
  179. consume();
  180. if (m_current_char == '.') {
  181. consume();
  182. while (isdigit(m_current_char))
  183. consume();
  184. if (m_current_char == 'e' || m_current_char == 'E')
  185. is_valid_numeric_literal = consume_exponent();
  186. } else if (m_current_char == 'e' || m_current_char == 'E') {
  187. is_valid_numeric_literal = consume_exponent();
  188. } else if (m_current_char == 'x' || m_current_char == 'X') {
  189. is_valid_numeric_literal = consume_hexadecimal_number();
  190. } else if (isdigit(m_current_char)) {
  191. do {
  192. consume();
  193. } while (isdigit(m_current_char));
  194. }
  195. } else {
  196. do {
  197. consume();
  198. } while (isdigit(m_current_char));
  199. if (m_current_char == '.') {
  200. consume();
  201. while (isdigit(m_current_char))
  202. consume();
  203. }
  204. if (m_current_char == 'e' || m_current_char == 'E')
  205. is_valid_numeric_literal = consume_exponent();
  206. }
  207. return is_valid_numeric_literal;
  208. }
  209. bool Lexer::consume_string_literal()
  210. {
  211. // https://sqlite.org/lang_expr.html - See "3. Literal Values (Constants)"
  212. bool is_valid_string_literal = true;
  213. consume();
  214. while (!is_eof() && !is_string_literal_end())
  215. consume();
  216. if (is_eof())
  217. is_valid_string_literal = false;
  218. consume();
  219. return is_valid_string_literal;
  220. }
  221. bool Lexer::consume_blob_literal()
  222. {
  223. // https://sqlite.org/lang_expr.html - See "3. Literal Values (Constants)"
  224. consume();
  225. return consume_string_literal();
  226. }
  227. bool Lexer::consume_exponent()
  228. {
  229. consume();
  230. if (m_current_char == '-' || m_current_char == '+')
  231. consume();
  232. if (!isdigit(m_current_char))
  233. return false;
  234. while (isdigit(m_current_char)) {
  235. consume();
  236. }
  237. return true;
  238. }
  239. bool Lexer::consume_hexadecimal_number()
  240. {
  241. consume();
  242. if (!isxdigit(m_current_char))
  243. return false;
  244. while (isxdigit(m_current_char))
  245. consume();
  246. return true;
  247. }
  248. bool Lexer::match(char a, char b) const
  249. {
  250. if (m_position >= m_source.length())
  251. return false;
  252. return m_current_char == a && m_source[m_position] == b;
  253. }
  254. bool Lexer::is_identifier_start() const
  255. {
  256. return isalpha(m_current_char) || m_current_char == '_';
  257. }
  258. bool Lexer::is_identifier_middle() const
  259. {
  260. return is_identifier_start() || isdigit(m_current_char);
  261. }
  262. bool Lexer::is_numeric_literal_start() const
  263. {
  264. return isdigit(m_current_char) || (m_current_char == '.' && m_position < m_source.length() && isdigit(m_source[m_position]));
  265. }
  266. bool Lexer::is_string_literal_start() const
  267. {
  268. return m_current_char == '\'';
  269. }
  270. bool Lexer::is_string_literal_end() const
  271. {
  272. return m_current_char == '\'' && !(m_position < m_source.length() && m_source[m_position] == '\'');
  273. }
  274. bool Lexer::is_blob_literal_start() const
  275. {
  276. return match('x', '\'') || match('X', '\'');
  277. }
  278. bool Lexer::is_line_comment_start() const
  279. {
  280. return match('-', '-');
  281. }
  282. bool Lexer::is_block_comment_start() const
  283. {
  284. return match('/', '*');
  285. }
  286. bool Lexer::is_block_comment_end() const
  287. {
  288. return match('*', '/');
  289. }
  290. bool Lexer::is_line_break() const
  291. {
  292. return m_current_char == '\n';
  293. }
  294. bool Lexer::is_eof() const
  295. {
  296. return m_current_char == EOF;
  297. }
  298. }