sql.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <AK/Format.h>
  27. #include <AK/String.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/StandardPaths.h>
  30. #include <LibLine/Editor.h>
  31. #include <LibSQL/Lexer.h>
  32. #include <LibSQL/Parser.h>
  33. #include <LibSQL/Token.h>
  34. namespace {
  35. String s_history_path = String::formatted("{}/.sql-history", Core::StandardPaths::home_directory());
  36. RefPtr<Line::Editor> s_editor;
  37. int s_repl_line_level = 0;
  38. bool s_keep_running = true;
  39. String prompt_for_level(int level)
  40. {
  41. static StringBuilder prompt_builder;
  42. prompt_builder.clear();
  43. prompt_builder.append("> ");
  44. for (auto i = 0; i < level; ++i)
  45. prompt_builder.append(" ");
  46. return prompt_builder.build();
  47. }
  48. String read_next_piece()
  49. {
  50. StringBuilder piece;
  51. do {
  52. if (!piece.is_empty())
  53. piece.append('\n');
  54. auto line_result = s_editor->get_line(prompt_for_level(s_repl_line_level));
  55. if (line_result.is_error()) {
  56. s_keep_running = false;
  57. return {};
  58. }
  59. auto& line = line_result.value();
  60. auto lexer = SQL::Lexer(line);
  61. s_editor->add_to_history(line);
  62. piece.append(line);
  63. bool is_first_token = true;
  64. bool is_command = false;
  65. bool last_token_ended_statement = false;
  66. for (SQL::Token token = lexer.next(); token.type() != SQL::TokenType::Eof; token = lexer.next()) {
  67. switch (token.type()) {
  68. case SQL::TokenType::ParenOpen:
  69. ++s_repl_line_level;
  70. break;
  71. case SQL::TokenType::ParenClose:
  72. --s_repl_line_level;
  73. break;
  74. case SQL::TokenType::SemiColon:
  75. last_token_ended_statement = true;
  76. break;
  77. case SQL::TokenType::Period:
  78. if (is_first_token)
  79. is_command = true;
  80. break;
  81. default:
  82. last_token_ended_statement = is_command;
  83. break;
  84. }
  85. is_first_token = false;
  86. }
  87. s_repl_line_level = last_token_ended_statement ? 0 : (s_repl_line_level > 0 ? s_repl_line_level : 1);
  88. } while (s_repl_line_level > 0);
  89. return piece.to_string();
  90. }
  91. void handle_command(StringView command)
  92. {
  93. if (command == ".exit")
  94. s_keep_running = false;
  95. else
  96. outln("\033[33;1mUnrecognized command:\033[0m {}", command);
  97. }
  98. void handle_statement(StringView statement_string)
  99. {
  100. auto parser = SQL::Parser(SQL::Lexer(statement_string));
  101. [[maybe_unused]] auto statement = parser.next_statement();
  102. if (parser.has_errors()) {
  103. auto error = parser.errors()[0];
  104. outln("\033[33;1mInvalid statement:\033[0m {}", error.to_string());
  105. }
  106. }
  107. void repl()
  108. {
  109. while (s_keep_running) {
  110. String piece = read_next_piece();
  111. if (piece.is_empty())
  112. continue;
  113. if (piece.starts_with('.'))
  114. handle_command(piece);
  115. else
  116. handle_statement(piece);
  117. }
  118. }
  119. }
  120. int main()
  121. {
  122. s_editor = Line::Editor::construct();
  123. s_editor->load_history(s_history_path);
  124. s_editor->on_display_refresh = [](Line::Editor& editor) {
  125. editor.strip_styles();
  126. size_t open_indents = s_repl_line_level;
  127. auto line = editor.line();
  128. SQL::Lexer lexer(line);
  129. bool indenters_starting_line = true;
  130. for (SQL::Token token = lexer.next(); token.type() != SQL::TokenType::Eof; token = lexer.next()) {
  131. auto length = token.value().length();
  132. auto start = token.line_column() - 1;
  133. auto end = start + length;
  134. if (indenters_starting_line) {
  135. if (token.type() != SQL::TokenType::ParenClose)
  136. indenters_starting_line = false;
  137. else
  138. --open_indents;
  139. }
  140. switch (token.category()) {
  141. case SQL::TokenCategory::Invalid:
  142. editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Red), Line::Style::Underline });
  143. break;
  144. case SQL::TokenCategory::Number:
  145. editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Magenta) });
  146. break;
  147. case SQL::TokenCategory::String:
  148. editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Green), Line::Style::Bold });
  149. break;
  150. case SQL::TokenCategory::Blob:
  151. editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Magenta), Line::Style::Bold });
  152. break;
  153. case SQL::TokenCategory::Keyword:
  154. editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Blue), Line::Style::Bold });
  155. break;
  156. case SQL::TokenCategory::Identifier:
  157. editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::White), Line::Style::Bold });
  158. default:
  159. break;
  160. }
  161. }
  162. editor.set_prompt(prompt_for_level(open_indents));
  163. };
  164. repl();
  165. s_editor->save_history(s_history_path);
  166. return 0;
  167. }