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
+2
View File
@@ -5,6 +5,8 @@
#include <vector>
#include <string>
namespace ast {
struct BlockNode : public Node {
virtual ~BlockNode() = default;
};
}
+4 -7
View File
@@ -3,15 +3,12 @@
#include "ast/BlockNode/BlockNode.h"
#include "utils/shorten.hpp"
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;
};
}
+4 -10
View File
@@ -3,18 +3,12 @@
#include "ast/BlockNode/BlockNode.h"
#include "ast/InlineNode/InlineNode.h"
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;
};
}
+4 -10
View File
@@ -3,17 +3,11 @@
#include "ast/BlockNode/BlockNode.h"
#include "ast/BlockNode/ListItem.h"
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;
};
}
+4 -10
View File
@@ -2,17 +2,11 @@
#include "ast/BlockNode/BlockNode.h"
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;
};
}
+4 -10
View File
@@ -3,17 +3,11 @@
#include "ast/BlockNode/BlockNode.h"
#include "ast/InlineNode/InlineNode.h"
namespace ast {
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);
}
}
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+2
View File
@@ -6,6 +6,7 @@
#include <vector>
#include <memory>
namespace ast {
struct Document {
std::vector<std::unique_ptr<BlockNode>> children;
@@ -15,3 +16,4 @@ struct Document {
}
}
};
}
+4 -10
View File
@@ -2,17 +2,11 @@
#include "ast/InlineNode/InlineNode.h"
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;
};
}
+4 -7
View File
@@ -2,14 +2,11 @@
#include "ast/InlineNode/InlineNode.h"
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;
};
}
+2
View File
@@ -2,6 +2,8 @@
#include "ast/Node.h"
namespace ast {
struct InlineNode : public Node {
virtual ~InlineNode() = default;
};
}
+4 -10
View File
@@ -2,17 +2,11 @@
#include "ast/InlineNode/InlineNode.h"
namespace ast {
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);
}
}
NodeKind getKind() const override;
void repr(std::ostream &os, int indent) const override;
};
}
+4 -10
View File
@@ -3,18 +3,12 @@
#include "ast/InlineNode/InlineNode.h"
#include "utils/shorten.hpp"
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;
};
}
+4 -7
View File
@@ -3,14 +3,11 @@
#include "ast/InlineNode/InlineNode.h"
#include "utils/shorten.hpp"
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;
};
}
+2
View File
@@ -6,9 +6,11 @@
#include "NodeKind.h"
#include "utils/getIndent.hpp"
namespace ast {
struct Node {
virtual ~Node() = default;
virtual NodeKind getKind() const = 0;
virtual void repr(std::ostream& os, int indent = 0) const = 0;
};
}
+2
View File
@@ -3,6 +3,7 @@
#include <unordered_map>
#include <string>
namespace ast {
enum class NodeKind {
CodeBlock,
Header,
@@ -45,3 +46,4 @@ const std::string toString(NodeKind kind) {
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';
}
}