refactored ast, started lexer

This commit is contained in:
2026-05-18 20:19:17 +03:00
parent 24750ddcbe
commit eb85acf224
29 changed files with 315 additions and 181 deletions
+5 -3
View File
@@ -5,6 +5,8 @@
#include <vector>
#include <string>
struct BlockNode : public Node {
virtual ~BlockNode() = default;
};
namespace ast {
struct BlockNode : public Node {
virtual ~BlockNode() = default;
};
}
+8 -11
View File
@@ -3,15 +3,12 @@
#include "ast/BlockNode/BlockNode.h"
#include "utils/shorten.hpp"
struct CodeBlock : public BlockNode {
std::string language;
std::string code;
namespace ast {
struct CodeBlock : public BlockNode {
std::string language;
std::string code;
NodeKind getKind() const override {
return NodeKind::CodeBlock;
}
void repr(std::ostream &os, int indent = 0) const override {
os << getIndent(indent) << toString(this->getKind()) << " lang: " << this->language << " code: " << shorten(this->code) << '\n';
}
};
NodeKind getKind() const override;
void repr(std::ostream &os, int indent = 0) const override;
};
}
+8 -14
View File
@@ -3,18 +3,12 @@
#include "ast/BlockNode/BlockNode.h"
#include "ast/InlineNode/InlineNode.h"
struct Header : public BlockNode {
int level = 1;
std::vector<std::unique_ptr<InlineNode>> children;
namespace ast {
struct Header : public BlockNode {
int level = 1;
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override {
return NodeKind::Header;
}
void repr(std::ostream &os, int indent = 0) const override {
os << getIndent(indent) << toString(this->getKind()) << " level: " << this->level << '\n';
for (const std::unique_ptr<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
NodeKind getKind() const override;
void repr(std::ostream &os, int indent = 0) const override;
};
}
+7 -13
View File
@@ -3,17 +3,11 @@
#include "ast/BlockNode/BlockNode.h"
#include "ast/BlockNode/ListItem.h"
struct List : public BlockNode {
std::vector<std::unique_ptr<ListItem>> children;
namespace ast {
struct List : public BlockNode {
std::vector<std::unique_ptr<ListItem>> children;
NodeKind getKind() const override {
return NodeKind::List;
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(getKind()) << '\n';
for (const std::unique_ptr<ListItem> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+7 -13
View File
@@ -2,17 +2,11 @@
#include "ast/BlockNode/BlockNode.h"
struct ListItem : public BlockNode {
std::vector<std::unique_ptr<BlockNode>> children;
namespace ast {
struct ListItem : public BlockNode {
std::vector<std::unique_ptr<BlockNode>> children;
NodeKind getKind() const override {
return NodeKind::ListItem;
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(getKind()) << '\n';
for (const std::unique_ptr<BlockNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+8 -14
View File
@@ -3,17 +3,11 @@
#include "ast/BlockNode/BlockNode.h"
#include "ast/InlineNode/InlineNode.h"
struct Paragraph : public BlockNode {
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override {
return NodeKind::Paragraph;
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(getKind()) << '\n';
for (const std::unique_ptr<InlineNode> &child : children) {
child->repr(os, indent + 1);
}
}
};
namespace ast {
struct Paragraph : public BlockNode {
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+9 -7
View File
@@ -6,12 +6,14 @@
#include <vector>
#include <memory>
struct Document {
std::vector<std::unique_ptr<BlockNode>> children;
namespace ast {
struct Document {
std::vector<std::unique_ptr<BlockNode>> children;
void repr(std::ostream &os) {
for (const std::unique_ptr<BlockNode> &child : children) {
child->repr(os, 0);
void repr(std::ostream &os) {
for (const std::unique_ptr<BlockNode> &child : children) {
child->repr(os, 0);
}
}
}
};
};
}
+7 -13
View File
@@ -2,17 +2,11 @@
#include "ast/InlineNode/InlineNode.h"
struct Bold : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> children;
namespace ast {
struct Bold : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override {
return NodeKind::Bold;
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(getKind()) << '\n';
for (const std::unique_ptr<InlineNode> &child : children) {
child->repr(os, indent + 1);
}
}
};
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+7 -10
View File
@@ -2,14 +2,11 @@
#include "ast/InlineNode/InlineNode.h"
struct InlineCode : public InlineNode {
std::string inlineCodeString;
namespace ast {
struct InlineCode : public InlineNode {
std::string inlineCodeString;
NodeKind getKind() const override {
return NodeKind::InlineCode;
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(getKind()) << " inline code: " << this->inlineCodeString << '\n';
}
};
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+5 -3
View File
@@ -2,6 +2,8 @@
#include "ast/Node.h"
struct InlineNode : public Node {
virtual ~InlineNode() = default;
};
namespace ast {
struct InlineNode : public Node {
virtual ~InlineNode() = default;
};
}
+8 -14
View File
@@ -2,17 +2,11 @@
#include "ast/InlineNode/InlineNode.h"
struct Italic : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override {
return NodeKind::Italic;
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(this->getKind()) << '\n';
for (const std::unique_ptr<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
namespace ast {
struct Italic : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+8 -14
View File
@@ -3,18 +3,12 @@
#include "ast/InlineNode/InlineNode.h"
#include "utils/shorten.hpp"
struct Link : public InlineNode {
std::string url;
std::vector<std::unique_ptr<InlineNode>> children;
namespace ast {
struct Link : public InlineNode {
std::string url;
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override {
return NodeKind::Link;
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(this->getKind()) << " link: " << shorten(this->url) << '\n';
for (const std::unique_ptr<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
};
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+7 -10
View File
@@ -3,14 +3,11 @@
#include "ast/InlineNode/InlineNode.h"
#include "utils/shorten.hpp"
struct Text : public InlineNode {
std::string text;
namespace ast {
struct Text : public InlineNode {
std::string text;
NodeKind getKind() const override {
return NodeKind::Text;
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(this->getKind()) << " text: " << shorten(this->text) << '\n';
}
};
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+7 -5
View File
@@ -6,9 +6,11 @@
#include "NodeKind.h"
#include "utils/getIndent.hpp"
struct Node {
virtual ~Node() = default;
namespace ast {
struct Node {
virtual ~Node() = default;
virtual NodeKind getKind() const = 0;
virtual void repr(std::ostream& os, int indent = 0) const = 0;
};
virtual NodeKind getKind() const = 0;
virtual void repr(std::ostream& os, int indent = 0) const = 0;
};
}
+39 -37
View File
@@ -3,45 +3,47 @@
#include <unordered_map>
#include <string>
enum class NodeKind {
CodeBlock,
Header,
List,
ListItem,
Paragraph,
namespace ast {
enum class NodeKind {
CodeBlock,
Header,
List,
ListItem,
Paragraph,
Bold,
InlineCode,
Italic,
Link,
Text
};
Bold,
InlineCode,
Italic,
Link,
Text
};
const std::string toString(NodeKind kind) {
switch (kind) {
case NodeKind::CodeBlock:
return "CodeBlock";
case NodeKind::Header:
return "Header";
case NodeKind::List:
return "List";
case NodeKind::ListItem:
return "ListItem";
case NodeKind::Paragraph:
return "Paragraph";
const std::string toString(NodeKind kind) {
switch (kind) {
case NodeKind::CodeBlock:
return "CodeBlock";
case NodeKind::Header:
return "Header";
case NodeKind::List:
return "List";
case NodeKind::ListItem:
return "ListItem";
case NodeKind::Paragraph:
return "Paragraph";
case NodeKind::Bold:
return "Bold";
case NodeKind::InlineCode:
return "InlineCode";
case NodeKind::Italic:
return "Italic";
case NodeKind::Link:
return "Link";
case NodeKind::Text:
return "Text";
case NodeKind::Bold:
return "Bold";
case NodeKind::InlineCode:
return "InlineCode";
case NodeKind::Italic:
return "Italic";
case NodeKind::Link:
return "Link";
case NodeKind::Text:
return "Text";
default:
return "";
default:
return "";
}
}
}
}