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 <vector>
#include <string> #include <string>
struct BlockNode : public Node { namespace ast {
virtual ~BlockNode() = default; struct BlockNode : public Node {
}; virtual ~BlockNode() = default;
};
}
+8 -11
View File
@@ -3,15 +3,12 @@
#include "ast/BlockNode/BlockNode.h" #include "ast/BlockNode/BlockNode.h"
#include "utils/shorten.hpp" #include "utils/shorten.hpp"
struct CodeBlock : public BlockNode { namespace ast {
std::string language; struct CodeBlock : public BlockNode {
std::string code; std::string language;
std::string code;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::CodeBlock; void repr(std::ostream &os, int indent = 0) const override;
} };
}
void repr(std::ostream &os, int indent = 0) const override {
os << getIndent(indent) << toString(this->getKind()) << " lang: " << this->language << " code: " << shorten(this->code) << '\n';
}
};
+8 -14
View File
@@ -3,18 +3,12 @@
#include "ast/BlockNode/BlockNode.h" #include "ast/BlockNode/BlockNode.h"
#include "ast/InlineNode/InlineNode.h" #include "ast/InlineNode/InlineNode.h"
struct Header : public BlockNode { namespace ast {
int level = 1; struct Header : public BlockNode {
std::vector<std::unique_ptr<InlineNode>> children; int level = 1;
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::Header; void repr(std::ostream &os, int indent = 0) const override;
} };
}
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);
}
}
};
+7 -13
View File
@@ -3,17 +3,11 @@
#include "ast/BlockNode/BlockNode.h" #include "ast/BlockNode/BlockNode.h"
#include "ast/BlockNode/ListItem.h" #include "ast/BlockNode/ListItem.h"
struct List : public BlockNode { namespace ast {
std::vector<std::unique_ptr<ListItem>> children; struct List : public BlockNode {
std::vector<std::unique_ptr<ListItem>> children;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::List; void repr(std::ostream &os, int indent) const override;
} };
}
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);
}
}
};
+7 -13
View File
@@ -2,17 +2,11 @@
#include "ast/BlockNode/BlockNode.h" #include "ast/BlockNode/BlockNode.h"
struct ListItem : public BlockNode { namespace ast {
std::vector<std::unique_ptr<BlockNode>> children; struct ListItem : public BlockNode {
std::vector<std::unique_ptr<BlockNode>> children;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::ListItem; void repr(std::ostream &os, int indent) const override;
} };
}
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);
}
}
};
+7 -13
View File
@@ -3,17 +3,11 @@
#include "ast/BlockNode/BlockNode.h" #include "ast/BlockNode/BlockNode.h"
#include "ast/InlineNode/InlineNode.h" #include "ast/InlineNode/InlineNode.h"
struct Paragraph : public BlockNode { namespace ast {
std::vector<std::unique_ptr<InlineNode>> children; struct Paragraph : public BlockNode {
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::Paragraph; void repr(std::ostream &os, int indent) const override;
} };
}
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);
}
}
};
+9 -7
View File
@@ -6,12 +6,14 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
struct Document { namespace ast {
std::vector<std::unique_ptr<BlockNode>> children; struct Document {
std::vector<std::unique_ptr<BlockNode>> children;
void repr(std::ostream &os) { void repr(std::ostream &os) {
for (const std::unique_ptr<BlockNode> &child : children) { for (const std::unique_ptr<BlockNode> &child : children) {
child->repr(os, 0); child->repr(os, 0);
}
} }
} };
}; }
+7 -13
View File
@@ -2,17 +2,11 @@
#include "ast/InlineNode/InlineNode.h" #include "ast/InlineNode/InlineNode.h"
struct Bold : public InlineNode { namespace ast {
std::vector<std::unique_ptr<InlineNode>> children; struct Bold : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::Bold; void repr(std::ostream &os, int indent) const override;
} };
}
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);
}
}
};
+7 -10
View File
@@ -2,14 +2,11 @@
#include "ast/InlineNode/InlineNode.h" #include "ast/InlineNode/InlineNode.h"
struct InlineCode : public InlineNode { namespace ast {
std::string inlineCodeString; struct InlineCode : public InlineNode {
std::string inlineCodeString;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::InlineCode; void repr(std::ostream &os, int indent) const override;
} };
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(getKind()) << " inline code: " << this->inlineCodeString << '\n';
}
};
+5 -3
View File
@@ -2,6 +2,8 @@
#include "ast/Node.h" #include "ast/Node.h"
struct InlineNode : public Node { namespace ast {
virtual ~InlineNode() = default; struct InlineNode : public Node {
}; virtual ~InlineNode() = default;
};
}
+7 -13
View File
@@ -2,17 +2,11 @@
#include "ast/InlineNode/InlineNode.h" #include "ast/InlineNode/InlineNode.h"
struct Italic : public InlineNode { namespace ast {
std::vector<std::unique_ptr<InlineNode>> children; struct Italic : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::Italic; void repr(std::ostream &os, int indent) const override;
} };
}
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);
}
}
};
+8 -14
View File
@@ -3,18 +3,12 @@
#include "ast/InlineNode/InlineNode.h" #include "ast/InlineNode/InlineNode.h"
#include "utils/shorten.hpp" #include "utils/shorten.hpp"
struct Link : public InlineNode { namespace ast {
std::string url; struct Link : public InlineNode {
std::vector<std::unique_ptr<InlineNode>> children; std::string url;
std::vector<std::unique_ptr<InlineNode>> children;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::Link; void repr(std::ostream &os, int indent) const override;
} };
}
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);
}
}
};
+7 -10
View File
@@ -3,14 +3,11 @@
#include "ast/InlineNode/InlineNode.h" #include "ast/InlineNode/InlineNode.h"
#include "utils/shorten.hpp" #include "utils/shorten.hpp"
struct Text : public InlineNode { namespace ast {
std::string text; struct Text : public InlineNode {
std::string text;
NodeKind getKind() const override { NodeKind getKind() const override;
return NodeKind::Text; void repr(std::ostream &os, int indent) const override;
} };
}
void repr(std::ostream &os, int indent) const override {
os << getIndent(indent) << toString(this->getKind()) << " text: " << shorten(this->text) << '\n';
}
};
+7 -5
View File
@@ -6,9 +6,11 @@
#include "NodeKind.h" #include "NodeKind.h"
#include "utils/getIndent.hpp" #include "utils/getIndent.hpp"
struct Node { namespace ast {
virtual ~Node() = default; struct Node {
virtual ~Node() = default;
virtual NodeKind getKind() const = 0; virtual NodeKind getKind() const = 0;
virtual void repr(std::ostream& os, int indent = 0) const = 0; virtual void repr(std::ostream& os, int indent = 0) const = 0;
}; };
}
+38 -36
View File
@@ -3,45 +3,47 @@
#include <unordered_map> #include <unordered_map>
#include <string> #include <string>
enum class NodeKind { namespace ast {
CodeBlock, enum class NodeKind {
Header, CodeBlock,
List, Header,
ListItem, List,
Paragraph, ListItem,
Paragraph,
Bold, Bold,
InlineCode, InlineCode,
Italic, Italic,
Link, Link,
Text Text
}; };
const std::string toString(NodeKind kind) { const std::string toString(NodeKind kind) {
switch (kind) { switch (kind) {
case NodeKind::CodeBlock: case NodeKind::CodeBlock:
return "CodeBlock"; return "CodeBlock";
case NodeKind::Header: case NodeKind::Header:
return "Header"; return "Header";
case NodeKind::List: case NodeKind::List:
return "List"; return "List";
case NodeKind::ListItem: case NodeKind::ListItem:
return "ListItem"; return "ListItem";
case NodeKind::Paragraph: case NodeKind::Paragraph:
return "Paragraph"; return "Paragraph";
case NodeKind::Bold: case NodeKind::Bold:
return "Bold"; return "Bold";
case NodeKind::InlineCode: case NodeKind::InlineCode:
return "InlineCode"; return "InlineCode";
case NodeKind::Italic: case NodeKind::Italic:
return "Italic"; return "Italic";
case NodeKind::Link: case NodeKind::Link:
return "Link"; return "Link";
case NodeKind::Text: case NodeKind::Text:
return "Text"; return "Text";
default: default:
return ""; return "";
}
} }
} }
+2
View File
@@ -0,0 +1,2 @@
#pragma once
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <string>
namespace lexer {
struct State {
std::string sourceText;
char* currentPosition;
int line;
int column;
};
struct Lexer {
};
}
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include <string>
#include "lexer/TokenKind.h"
namespace lexer {
struct Token {
TokenKind kind;
std::string lexeme;
int line;
int column;
};
}
+13
View File
@@ -0,0 +1,13 @@
#pragma once
namespace lexer {
enum class TokenKind {
Text, // text
NewLine, // new line
EOFile, // end of file
HeaderMark, // header mark ("#")
ListMark, // list mark
CodeFence // code fence
};
}
+11
View File
@@ -0,0 +1,11 @@
#include "ast/BlockNode/CodeBlock.h"
namespace ast {
NodeKind CodeBlock::getKind() const {
return NodeKind::CodeBlock;
}
void CodeBlock::repr(std::ostream &os, int indent = 0) const {
os << getIndent(indent) << toString(this->getKind()) << " lang: " << this->language << " code: " << shorten(this->code) << '\n';
}
}
+14
View File
@@ -0,0 +1,14 @@
#include "ast/BlockNode/Header.h"
namespace ast {
NodeKind Header::getKind() const {
return NodeKind::Header;
}
void Header::repr(std::ostream &os, int indent = 0) const {
os << getIndent(indent) << toString(this->getKind()) << " level: " << this->level << '\n';
for (const std::unique_ptr<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
}
+14
View File
@@ -0,0 +1,14 @@
#include "ast/BlockNode/List.h"
namespace ast {
NodeKind List::getKind() const {
return NodeKind::List;
}
void List::repr(std::ostream &os, int indent) const {
os << getIndent(indent) << toString(getKind()) << '\n';
for (const std::unique_ptr<ListItem> &child : this->children) {
child->repr(os, indent + 1);
}
}
}
+14
View File
@@ -0,0 +1,14 @@
#include "ast/BlockNode/ListItem.h"
namespace ast {
NodeKind ListItem::getKind() const {
return NodeKind::ListItem;
}
void ListItem::repr(std::ostream &os, int indent) const {
os << getIndent(indent) << toString(getKind()) << '\n';
for (const std::unique_ptr<BlockNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
}
+14
View File
@@ -0,0 +1,14 @@
#include "ast/BlockNode/Paragraph.h"
namespace ast {
NodeKind Paragraph::getKind() const {
return NodeKind::Paragraph;
}
void Paragraph::repr(std::ostream &os, int indent) const {
os << getIndent(indent) << toString(getKind()) << '\n';
for (const std::unique_ptr<InlineNode> &child : children) {
child->repr(os, indent + 1);
}
}
}
+14
View File
@@ -0,0 +1,14 @@
#include "ast/InlineNode/Bold.h"
namespace ast {
NodeKind Bold::getKind() const {
return NodeKind::Bold;
}
void Bold::repr(std::ostream &os, int indent) const {
os << getIndent(indent) << toString(getKind()) << '\n';
for (const std::unique_ptr<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
}
+11
View File
@@ -0,0 +1,11 @@
#include "ast/InlineNode/InlineCode.h"
namespace ast {
NodeKind InlineCode::getKind() const {
return NodeKind::InlineCode;
}
void InlineCode::repr(std::ostream &os, int indent) const {
os << getIndent(indent) << toString(getKind()) << " inline code: " << this->inlineCodeString << '\n';
}
}
+14
View File
@@ -0,0 +1,14 @@
#include "ast/InlineNode/Italic.h"
namespace ast {
NodeKind Italic::getKind() const {
return NodeKind::Italic;
}
void Italic::repr(std::ostream &os, int indent) const {
os << getIndent(indent) << toString(this->getKind()) << '\n';
for (const std::unique_ptr<InlineNode> &child : this->children) {
child->repr(os, indent + 1);
}
}
}
+14
View File
@@ -0,0 +1,14 @@
#include "ast/InlineNode/Link.h"
namespace ast {
NodeKind Link::getKind() const {
return NodeKind::Link;
}
void Link::repr(std::ostream &os, int indent) const {
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);
}
}
}
+11
View File
@@ -0,0 +1,11 @@
#include "ast/InlineNode/Text.h"
namespace ast {
NodeKind Text::getKind() const {
return NodeKind::Text;
}
void Text::repr(std::ostream &os, int indent) const {
os << getIndent(indent) << toString(this->getKind()) << " text: " << shorten(this->text) << '\n';
}
}