summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorJan de Visser <jan@de-visser.net>2021-06-21 10:57:44 -0400
committerAndreas Kling <kling@serenityos.org>2021-06-24 00:36:53 +0200
commit4198f7e1af7997e0615891289194752f1375f5a1 (patch)
treeecea66688f05bb6304ddb6b7c5fe27ec2f57cfe3 /Userland/Libraries
parente0f1c237d21be3e2831092319f50bb429ad1f7a9 (diff)
downloadserenity-4198f7e1af7997e0615891289194752f1375f5a1.zip
LibSQL: Move Lexer and Parser machinery to AST directory
The SQL engine is expected to be a fairly sizeable piece of software. Therefore we're starting to restructure the codebase for growth.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibSQL/AST/AST.h (renamed from Userland/Libraries/LibSQL/AST.h)4
-rw-r--r--Userland/Libraries/LibSQL/AST/Lexer.cpp (renamed from Userland/Libraries/LibSQL/Lexer.cpp)2
-rw-r--r--Userland/Libraries/LibSQL/AST/Lexer.h (renamed from Userland/Libraries/LibSQL/Lexer.h)2
-rw-r--r--Userland/Libraries/LibSQL/AST/Parser.cpp (renamed from Userland/Libraries/LibSQL/Parser.cpp)2
-rw-r--r--Userland/Libraries/LibSQL/AST/Parser.h (renamed from Userland/Libraries/LibSQL/Parser.h)8
-rw-r--r--Userland/Libraries/LibSQL/AST/SyntaxHighlighter.cpp (renamed from Userland/Libraries/LibSQL/SyntaxHighlighter.cpp)16
-rw-r--r--Userland/Libraries/LibSQL/AST/SyntaxHighlighter.h (renamed from Userland/Libraries/LibSQL/SyntaxHighlighter.h)2
-rw-r--r--Userland/Libraries/LibSQL/AST/Token.cpp (renamed from Userland/Libraries/LibSQL/Token.cpp)2
-rw-r--r--Userland/Libraries/LibSQL/AST/Token.h (renamed from Userland/Libraries/LibSQL/Token.h)2
-rw-r--r--Userland/Libraries/LibSQL/CMakeLists.txt8
-rw-r--r--Userland/Libraries/LibSQL/Forward.h45
-rw-r--r--Userland/Libraries/LibSQL/Meta.cpp26
-rw-r--r--Userland/Libraries/LibSQL/Meta.h10
-rw-r--r--Userland/Libraries/LibSQL/Tuple.cpp4
-rw-r--r--Userland/Libraries/LibSQL/TupleDescriptor.h4
15 files changed, 70 insertions, 67 deletions
diff --git a/Userland/Libraries/LibSQL/AST.h b/Userland/Libraries/LibSQL/AST/AST.h
index 8f71d26e17..8f22bbab65 100644
--- a/Userland/Libraries/LibSQL/AST.h
+++ b/Userland/Libraries/LibSQL/AST/AST.h
@@ -11,10 +11,10 @@
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
+#include <LibSQL/AST/Token.h>
#include <LibSQL/Forward.h>
-#include <LibSQL/Token.h>
-namespace SQL {
+namespace SQL::AST {
template<class T, class... Args>
static inline NonnullRefPtr<T>
diff --git a/Userland/Libraries/LibSQL/Lexer.cpp b/Userland/Libraries/LibSQL/AST/Lexer.cpp
index 50598b3159..49c1602243 100644
--- a/Userland/Libraries/LibSQL/Lexer.cpp
+++ b/Userland/Libraries/LibSQL/AST/Lexer.cpp
@@ -8,7 +8,7 @@
#include <AK/Debug.h>
#include <ctype.h>
-namespace SQL {
+namespace SQL::AST {
HashMap<String, TokenType> Lexer::s_keywords;
HashMap<char, TokenType> Lexer::s_one_char_tokens;
diff --git a/Userland/Libraries/LibSQL/Lexer.h b/Userland/Libraries/LibSQL/AST/Lexer.h
index 2ec93a2e80..77abcdec45 100644
--- a/Userland/Libraries/LibSQL/Lexer.h
+++ b/Userland/Libraries/LibSQL/AST/Lexer.h
@@ -11,7 +11,7 @@
#include <AK/String.h>
#include <AK/StringView.h>
-namespace SQL {
+namespace SQL::AST {
class Lexer {
public:
diff --git a/Userland/Libraries/LibSQL/Parser.cpp b/Userland/Libraries/LibSQL/AST/Parser.cpp
index 5ee5d2538d..ae771c9ae0 100644
--- a/Userland/Libraries/LibSQL/Parser.cpp
+++ b/Userland/Libraries/LibSQL/AST/Parser.cpp
@@ -8,7 +8,7 @@
#include <AK/ScopeGuard.h>
#include <AK/TypeCasts.h>
-namespace SQL {
+namespace SQL::AST {
Parser::Parser(Lexer lexer)
: m_parser_state(move(lexer))
diff --git a/Userland/Libraries/LibSQL/Parser.h b/Userland/Libraries/LibSQL/AST/Parser.h
index 94fb0e6a04..466c2f4150 100644
--- a/Userland/Libraries/LibSQL/Parser.h
+++ b/Userland/Libraries/LibSQL/AST/Parser.h
@@ -8,11 +8,11 @@
#include <AK/String.h>
#include <AK/StringView.h>
-#include <LibSQL/AST.h>
-#include <LibSQL/Lexer.h>
-#include <LibSQL/Token.h>
+#include <LibSQL/AST/AST.h>
+#include <LibSQL/AST/Lexer.h>
+#include <LibSQL/AST/Token.h>
-namespace SQL {
+namespace SQL::AST {
namespace Limits {
// https://www.sqlite.org/limits.html
diff --git a/Userland/Libraries/LibSQL/SyntaxHighlighter.cpp b/Userland/Libraries/LibSQL/AST/SyntaxHighlighter.cpp
index db0b308763..da5de16a67 100644
--- a/Userland/Libraries/LibSQL/SyntaxHighlighter.cpp
+++ b/Userland/Libraries/LibSQL/AST/SyntaxHighlighter.cpp
@@ -6,10 +6,10 @@
#include <AK/Debug.h>
#include <LibGfx/Palette.h>
-#include <LibSQL/Lexer.h>
-#include <LibSQL/SyntaxHighlighter.h>
+#include <LibSQL/AST/Lexer.h>
+#include <LibSQL/AST/SyntaxHighlighter.h>
-namespace SQL {
+namespace SQL::AST {
static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, TokenType type)
{
@@ -35,19 +35,19 @@ static Syntax::TextStyle style_for_token_type(Gfx::Palette const& palette, Token
bool SyntaxHighlighter::is_identifier(u64 token) const
{
- auto sql_token = static_cast<SQL::TokenType>(static_cast<size_t>(token));
- return sql_token == SQL::TokenType::Identifier;
+ auto sql_token = static_cast<TokenType>(static_cast<size_t>(token));
+ return sql_token == TokenType::Identifier;
}
void SyntaxHighlighter::rehighlight(Palette const& palette)
{
auto text = m_client->get_text();
- SQL::Lexer lexer(text);
+ Lexer lexer(text);
Vector<GUI::TextDocumentSpan> spans;
- auto append_token = [&](StringView str, SQL::Token const& token) {
+ auto append_token = [&](StringView str, Token const& token) {
if (str.is_empty())
return;
@@ -79,7 +79,7 @@ void SyntaxHighlighter::rehighlight(Palette const& palette)
for (;;) {
auto token = lexer.next();
append_token(token.value(), token);
- if (token.type() == SQL::TokenType::Eof)
+ if (token.type() == TokenType::Eof)
break;
}
diff --git a/Userland/Libraries/LibSQL/SyntaxHighlighter.h b/Userland/Libraries/LibSQL/AST/SyntaxHighlighter.h
index b8f17b8527..981b5b8091 100644
--- a/Userland/Libraries/LibSQL/SyntaxHighlighter.h
+++ b/Userland/Libraries/LibSQL/AST/SyntaxHighlighter.h
@@ -8,7 +8,7 @@
#include <LibSyntax/Highlighter.h>
-namespace SQL {
+namespace SQL::AST {
class SyntaxHighlighter final : public Syntax::Highlighter {
public:
diff --git a/Userland/Libraries/LibSQL/Token.cpp b/Userland/Libraries/LibSQL/AST/Token.cpp
index 7ec61f0574..2780b39a1d 100644
--- a/Userland/Libraries/LibSQL/Token.cpp
+++ b/Userland/Libraries/LibSQL/AST/Token.cpp
@@ -9,7 +9,7 @@
#include <AK/String.h>
#include <stdlib.h>
-namespace SQL {
+namespace SQL::AST {
StringView Token::name(TokenType type)
{
diff --git a/Userland/Libraries/LibSQL/Token.h b/Userland/Libraries/LibSQL/AST/Token.h
index 66ac5dea5a..e8dc8ff5cb 100644
--- a/Userland/Libraries/LibSQL/Token.h
+++ b/Userland/Libraries/LibSQL/AST/Token.h
@@ -9,7 +9,7 @@
#include <AK/HashMap.h>
#include <AK/StringView.h>
-namespace SQL {
+namespace SQL::AST {
// https://sqlite.org/lang_keywords.html
#define ENUMERATE_SQL_TOKENS \
diff --git a/Userland/Libraries/LibSQL/CMakeLists.txt b/Userland/Libraries/LibSQL/CMakeLists.txt
index 0e979d69f8..23bbd35b5e 100644
--- a/Userland/Libraries/LibSQL/CMakeLists.txt
+++ b/Userland/Libraries/LibSQL/CMakeLists.txt
@@ -1,4 +1,8 @@
set(SOURCES
+ AST/Lexer.cpp
+ AST/Parser.cpp
+ AST/SyntaxHighlighter.cpp
+ AST/Token.cpp
BTree.cpp
BTreeIterator.cpp
Database.cpp
@@ -6,12 +10,8 @@ set(SOURCES
Heap.cpp
Index.cpp
Key.cpp
- Lexer.cpp
Meta.cpp
- Parser.cpp
Row.cpp
- SyntaxHighlighter.cpp
- Token.cpp
TreeNode.cpp
Tuple.cpp
Value.cpp
diff --git a/Userland/Libraries/LibSQL/Forward.h b/Userland/Libraries/LibSQL/Forward.h
index 224404d189..e844a1e782 100644
--- a/Userland/Libraries/LibSQL/Forward.h
+++ b/Userland/Libraries/LibSQL/Forward.h
@@ -7,27 +7,45 @@
#pragma once
namespace SQL {
+class BTree;
+class BTreeIterator;
+class ColumnDef;
+class Database;
+class HashBucket;
+class HashDirectoryNode;
+class HashIndex;
+class HashIndexIterator;
+class Heap;
+class Index;
+class IndexNode;
+class IndexDef;
+class Key;
+class KeyPartDef;
+class Row;
+class TableDef;
+class TreeNode;
+class Tuple;
+class TupleDescriptor;
+struct TupleElement;
+class Value;
+}
+
+namespace SQL::AST {
class AddColumn;
class AlterTable;
class ASTNode;
class BetweenExpression;
class BinaryOperatorExpression;
class BlobLiteral;
-class BTree;
-class BTreeIterator;
class CaseExpression;
class CastExpression;
class ChainedExpression;
class CollateExpression;
-class ColumnDef;
class ColumnDefinition;
class ColumnNameExpression;
class CommonTableExpression;
class CommonTableExpressionList;
class CreateTable;
-class Database;
-class TupleDescriptor;
-struct TupleElement;
class Delete;
class DropColumn;
class DropTable;
@@ -36,23 +54,13 @@ class ErrorStatement;
class ExistsExpression;
class Expression;
class GroupByClause;
-class HashBucket;
-class HashDirectoryNode;
-class HashIndex;
-class HashIndexIterator;
-class Heap;
class InChainedExpression;
-class Index;
-class IndexNode;
-class IndexDef;
class InSelectionExpression;
class Insert;
class InTableExpression;
class InvertibleNestedDoubleExpression;
class InvertibleNestedExpression;
class IsExpression;
-class Key;
-class KeyPartDef;
class Lexer;
class LimitClause;
class MatchExpression;
@@ -68,18 +76,13 @@ class RenameColumn;
class RenameTable;
class ResultColumn;
class ReturningClause;
-class Row;
class Select;
class SignedNumber;
class Statement;
class StringLiteral;
-class TableDef;
class TableOrSubquery;
class Token;
-class TreeNode;
-class Tuple;
class TypeName;
class UnaryOperatorExpression;
class Update;
-class Value;
}
diff --git a/Userland/Libraries/LibSQL/Meta.cpp b/Userland/Libraries/LibSQL/Meta.cpp
index 43a240f9af..ce8dc12bd2 100644
--- a/Userland/Libraries/LibSQL/Meta.cpp
+++ b/Userland/Libraries/LibSQL/Meta.cpp
@@ -37,7 +37,7 @@ NonnullRefPtr<IndexDef> SchemaDef::index_def()
{
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$schema", true, 0);
if (!s_index_def->size()) {
- s_index_def->append_column("schema_name", SQLType::Text, Order::Ascending);
+ s_index_def->append_column("schema_name", SQLType::Text, AST::Order::Ascending);
}
return s_index_def;
}
@@ -70,15 +70,15 @@ NonnullRefPtr<IndexDef> ColumnDef::index_def()
{
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$column", true, 0);
if (!s_index_def->size()) {
- s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending);
- s_index_def->append_column("column_number", SQLType::Integer, Order::Ascending);
- s_index_def->append_column("column_name", SQLType::Text, Order::Ascending);
- s_index_def->append_column("column_type", SQLType::Integer, Order::Ascending);
+ s_index_def->append_column("table_hash", SQLType::Integer, AST::Order::Ascending);
+ s_index_def->append_column("column_number", SQLType::Integer, AST::Order::Ascending);
+ s_index_def->append_column("column_name", SQLType::Text, AST::Order::Ascending);
+ s_index_def->append_column("column_type", SQLType::Integer, AST::Order::Ascending);
}
return s_index_def;
}
-KeyPartDef::KeyPartDef(IndexDef* index, String name, SQLType sql_type, Order sort_order)
+KeyPartDef::KeyPartDef(IndexDef* index, String name, SQLType sql_type, AST::Order sort_order)
: ColumnDef(index, index->size(), move(name), sql_type)
, m_sort_order(sort_order)
{
@@ -96,7 +96,7 @@ IndexDef::IndexDef(String name, bool unique, u32 pointer)
{
}
-void IndexDef::append_column(String name, SQLType sql_type, Order sort_order)
+void IndexDef::append_column(String name, SQLType sql_type, AST::Order sort_order)
{
auto part = KeyPartDef::construct(this, move(name), sql_type, sort_order);
m_key_definition.append(part);
@@ -131,9 +131,9 @@ NonnullRefPtr<IndexDef> IndexDef::index_def()
{
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$index", true, 0);
if (!s_index_def->size()) {
- s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending);
- s_index_def->append_column("index_name", SQLType::Text, Order::Ascending);
- s_index_def->append_column("unique", SQLType::Integer, Order::Ascending);
+ s_index_def->append_column("table_hash", SQLType::Integer, AST::Order::Ascending);
+ s_index_def->append_column("index_name", SQLType::Text, AST::Order::Ascending);
+ s_index_def->append_column("unique", SQLType::Integer, AST::Order::Ascending);
}
return s_index_def;
}
@@ -149,7 +149,7 @@ TupleDescriptor TableDef::to_tuple_descriptor() const
{
TupleDescriptor ret;
for (auto& part : m_columns) {
- ret.append({ part.name(), part.type(), Order::Ascending });
+ ret.append({ part.name(), part.type(), AST::Order::Ascending });
}
return ret;
}
@@ -192,8 +192,8 @@ NonnullRefPtr<IndexDef> TableDef::index_def()
{
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$table", true, 0);
if (!s_index_def->size()) {
- s_index_def->append_column("schema_hash", SQLType::Integer, Order::Ascending);
- s_index_def->append_column("table_name", SQLType::Text, Order::Ascending);
+ s_index_def->append_column("schema_hash", SQLType::Integer, AST::Order::Ascending);
+ s_index_def->append_column("table_name", SQLType::Text, AST::Order::Ascending);
}
return s_index_def;
}
diff --git a/Userland/Libraries/LibSQL/Meta.h b/Userland/Libraries/LibSQL/Meta.h
index 06a01b18db..468e34cb95 100644
--- a/Userland/Libraries/LibSQL/Meta.h
+++ b/Userland/Libraries/LibSQL/Meta.h
@@ -13,7 +13,7 @@
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/Object.h>
-#include <LibSQL/AST.h>
+#include <LibSQL/AST/AST.h>
#include <LibSQL/Forward.h>
#include <LibSQL/Key.h>
#include <LibSQL/Type.h>
@@ -90,11 +90,11 @@ class KeyPartDef : public ColumnDef {
C_OBJECT(KeyPartDef);
public:
- KeyPartDef(IndexDef*, String, SQLType, Order = Order::Ascending);
- Order sort_order() const { return m_sort_order; }
+ KeyPartDef(IndexDef*, String, SQLType, AST::Order = AST::Order::Ascending);
+ AST::Order sort_order() const { return m_sort_order; }
private:
- Order m_sort_order { Order::Ascending };
+ AST::Order m_sort_order { AST::Order::Ascending };
};
class IndexDef : public Relation {
@@ -106,7 +106,7 @@ public:
NonnullRefPtrVector<KeyPartDef> key_definition() const { return m_key_definition; }
bool unique() const { return m_unique; }
[[nodiscard]] size_t size() const { return m_key_definition.size(); }
- void append_column(String, SQLType, Order = Order::Ascending);
+ void append_column(String, SQLType, AST::Order = AST::Order::Ascending);
Key key() const override;
[[nodiscard]] TupleDescriptor to_tuple_descriptor() const;
static NonnullRefPtr<IndexDef> index_def();
diff --git a/Userland/Libraries/LibSQL/Tuple.cpp b/Userland/Libraries/LibSQL/Tuple.cpp
index 406484e924..c9d5da5c1f 100644
--- a/Userland/Libraries/LibSQL/Tuple.cpp
+++ b/Userland/Libraries/LibSQL/Tuple.cpp
@@ -203,7 +203,7 @@ int Tuple::compare(const Tuple& other) const
for (auto ix = 0u; ix < num_values; ix++) {
auto ret = m_data[ix].compare(other.m_data[ix]);
if (ret != 0) {
- if ((ix < m_descriptor.size()) && m_descriptor[ix].order == Order::Descending)
+ if ((ix < m_descriptor.size()) && m_descriptor[ix].order == AST::Order::Descending)
ret = -ret;
return ret;
}
@@ -223,7 +223,7 @@ int Tuple::match(const Tuple& other) const
return -1;
auto ret = m_data[my_index.value()].compare(other_value);
if (ret != 0)
- return (m_descriptor[my_index.value()].order == Order::Descending) ? -ret : ret;
+ return (m_descriptor[my_index.value()].order == AST::Order::Descending) ? -ret : ret;
other_index++;
}
return 0;
diff --git a/Userland/Libraries/LibSQL/TupleDescriptor.h b/Userland/Libraries/LibSQL/TupleDescriptor.h
index 2fc34172f8..c95dcfcb07 100644
--- a/Userland/Libraries/LibSQL/TupleDescriptor.h
+++ b/Userland/Libraries/LibSQL/TupleDescriptor.h
@@ -7,7 +7,7 @@
#pragma once
#include <AK/Vector.h>
-#include <LibSQL/AST.h>
+#include <LibSQL/AST/AST.h>
#include <LibSQL/Type.h>
namespace SQL {
@@ -15,7 +15,7 @@ namespace SQL {
struct TupleElement {
String name { "" };
SQLType type { SQLType::Text };
- Order order { Order::Ascending };
+ AST::Order order { AST::Order::Ascending };
bool operator==(TupleElement const&) const = default;
};