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 "";
}
}
}
}
+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';
}
}