From eb85acf224cab2d9d814c2ae614c852bec4a7530 Mon Sep 17 00:00:00 2001 From: German Mikheev Date: Mon, 18 May 2026 20:19:17 +0300 Subject: [PATCH] refactored ast, started lexer --- include/ast/BlockNode/BlockNode.h | 8 +-- include/ast/BlockNode/CodeBlock.h | 19 +++---- include/ast/BlockNode/Header.h | 22 +++----- include/ast/BlockNode/List.h | 20 +++----- include/ast/BlockNode/ListItem.h | 20 +++----- include/ast/BlockNode/Paragraph.h | 22 +++----- include/ast/Document.h | 16 +++--- include/ast/InlineNode/Bold.h | 20 +++----- include/ast/InlineNode/InlineCode.h | 17 +++---- include/ast/InlineNode/InlineNode.h | 8 +-- include/ast/InlineNode/Italic.h | 22 +++----- include/ast/InlineNode/Link.h | 22 +++----- include/ast/InlineNode/Text.h | 17 +++---- include/ast/Node.h | 12 +++-- include/ast/NodeKind.h | 76 ++++++++++++++-------------- include/lexer/Cursor.h | 2 + include/lexer/Lexer.h | 15 ++++++ include/lexer/Token.h | 14 +++++ include/lexer/TokenKind.h | 13 +++++ source/ast/BlockNode/CodeBlock.cpp | 11 ++++ source/ast/BlockNode/Header.cpp | 14 +++++ source/ast/BlockNode/List.cpp | 14 +++++ source/ast/BlockNode/ListItem.cpp | 14 +++++ source/ast/BlockNode/Paragraph.cpp | 14 +++++ source/ast/InlineNode/Bold.cpp | 14 +++++ source/ast/InlineNode/InlineCode.cpp | 11 ++++ source/ast/InlineNode/Italic.cpp | 14 +++++ source/ast/InlineNode/Link.cpp | 14 +++++ source/ast/InlineNode/Text.cpp | 11 ++++ 29 files changed, 315 insertions(+), 181 deletions(-) create mode 100644 include/lexer/Cursor.h create mode 100644 include/lexer/Lexer.h create mode 100644 include/lexer/Token.h create mode 100644 include/lexer/TokenKind.h create mode 100644 source/ast/BlockNode/CodeBlock.cpp create mode 100644 source/ast/BlockNode/Header.cpp create mode 100644 source/ast/BlockNode/List.cpp create mode 100644 source/ast/BlockNode/ListItem.cpp create mode 100644 source/ast/BlockNode/Paragraph.cpp create mode 100644 source/ast/InlineNode/Bold.cpp create mode 100644 source/ast/InlineNode/InlineCode.cpp create mode 100644 source/ast/InlineNode/Italic.cpp create mode 100644 source/ast/InlineNode/Link.cpp create mode 100644 source/ast/InlineNode/Text.cpp diff --git a/include/ast/BlockNode/BlockNode.h b/include/ast/BlockNode/BlockNode.h index 84b810e..bafac69 100644 --- a/include/ast/BlockNode/BlockNode.h +++ b/include/ast/BlockNode/BlockNode.h @@ -5,6 +5,8 @@ #include #include -struct BlockNode : public Node { - virtual ~BlockNode() = default; -}; \ No newline at end of file +namespace ast { + struct BlockNode : public Node { + virtual ~BlockNode() = default; + }; +} \ No newline at end of file diff --git a/include/ast/BlockNode/CodeBlock.h b/include/ast/BlockNode/CodeBlock.h index 0dfcaf0..b2a2c87 100644 --- a/include/ast/BlockNode/CodeBlock.h +++ b/include/ast/BlockNode/CodeBlock.h @@ -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'; - } -}; \ No newline at end of file + NodeKind getKind() const override; + void repr(std::ostream &os, int indent = 0) const override; + }; +} \ No newline at end of file diff --git a/include/ast/BlockNode/Header.h b/include/ast/BlockNode/Header.h index e696b38..77d08ef 100644 --- a/include/ast/BlockNode/Header.h +++ b/include/ast/BlockNode/Header.h @@ -3,18 +3,12 @@ #include "ast/BlockNode/BlockNode.h" #include "ast/InlineNode/InlineNode.h" -struct Header : public BlockNode { - int level = 1; - std::vector> children; +namespace ast { + struct Header : public BlockNode { + int level = 1; + std::vector> 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 &child : this->children) { - child->repr(os, indent + 1); - } - } -}; \ No newline at end of file + NodeKind getKind() const override; + void repr(std::ostream &os, int indent = 0) const override; + }; +} \ No newline at end of file diff --git a/include/ast/BlockNode/List.h b/include/ast/BlockNode/List.h index 5d16f1d..fef4dbf 100644 --- a/include/ast/BlockNode/List.h +++ b/include/ast/BlockNode/List.h @@ -3,17 +3,11 @@ #include "ast/BlockNode/BlockNode.h" #include "ast/BlockNode/ListItem.h" -struct List : public BlockNode { - std::vector> children; +namespace ast { + struct List : public BlockNode { + std::vector> 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 &child : this->children) { - child->repr(os, indent + 1); - } - } -}; \ No newline at end of file + NodeKind getKind() const override; + void repr(std::ostream &os, int indent) const override; + }; +} \ No newline at end of file diff --git a/include/ast/BlockNode/ListItem.h b/include/ast/BlockNode/ListItem.h index ee4e2d6..4778dc6 100644 --- a/include/ast/BlockNode/ListItem.h +++ b/include/ast/BlockNode/ListItem.h @@ -2,17 +2,11 @@ #include "ast/BlockNode/BlockNode.h" -struct ListItem : public BlockNode { - std::vector> children; +namespace ast { + struct ListItem : public BlockNode { + std::vector> 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 &child : this->children) { - child->repr(os, indent + 1); - } - } -}; \ No newline at end of file + NodeKind getKind() const override; + void repr(std::ostream &os, int indent) const override; + }; +} \ No newline at end of file diff --git a/include/ast/BlockNode/Paragraph.h b/include/ast/BlockNode/Paragraph.h index 2d7ba18..49baffa 100644 --- a/include/ast/BlockNode/Paragraph.h +++ b/include/ast/BlockNode/Paragraph.h @@ -3,17 +3,11 @@ #include "ast/BlockNode/BlockNode.h" #include "ast/InlineNode/InlineNode.h" -struct Paragraph : public BlockNode { - std::vector> 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 &child : children) { - child->repr(os, indent + 1); - } - } -}; \ No newline at end of file +namespace ast { + struct Paragraph : public BlockNode { + std::vector> children; + + NodeKind getKind() const override; + void repr(std::ostream &os, int indent) const override; + }; +} \ No newline at end of file diff --git a/include/ast/Document.h b/include/ast/Document.h index 18b3c21..6e5c311 100644 --- a/include/ast/Document.h +++ b/include/ast/Document.h @@ -6,12 +6,14 @@ #include #include -struct Document { - std::vector> children; +namespace ast { + struct Document { + std::vector> children; - void repr(std::ostream &os) { - for (const std::unique_ptr &child : children) { - child->repr(os, 0); + void repr(std::ostream &os) { + for (const std::unique_ptr &child : children) { + child->repr(os, 0); + } } - } -}; \ No newline at end of file + }; +} \ No newline at end of file diff --git a/include/ast/InlineNode/Bold.h b/include/ast/InlineNode/Bold.h index 9c89699..71f8a86 100644 --- a/include/ast/InlineNode/Bold.h +++ b/include/ast/InlineNode/Bold.h @@ -2,17 +2,11 @@ #include "ast/InlineNode/InlineNode.h" -struct Bold : public InlineNode { - std::vector> children; +namespace ast { + struct Bold : public InlineNode { + std::vector> 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 &child : children) { - child->repr(os, indent + 1); - } - } -}; \ No newline at end of file + NodeKind getKind() const override; + void repr(std::ostream &os, int indent) const override; + }; +} \ No newline at end of file diff --git a/include/ast/InlineNode/InlineCode.h b/include/ast/InlineNode/InlineCode.h index 62fc4f9..532552a 100644 --- a/include/ast/InlineNode/InlineCode.h +++ b/include/ast/InlineNode/InlineCode.h @@ -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'; - } -}; \ No newline at end of file + NodeKind getKind() const override; + void repr(std::ostream &os, int indent) const override; + }; +} \ No newline at end of file diff --git a/include/ast/InlineNode/InlineNode.h b/include/ast/InlineNode/InlineNode.h index 12d775a..07a808a 100644 --- a/include/ast/InlineNode/InlineNode.h +++ b/include/ast/InlineNode/InlineNode.h @@ -2,6 +2,8 @@ #include "ast/Node.h" -struct InlineNode : public Node { - virtual ~InlineNode() = default; -}; \ No newline at end of file +namespace ast { + struct InlineNode : public Node { + virtual ~InlineNode() = default; + }; +} \ No newline at end of file diff --git a/include/ast/InlineNode/Italic.h b/include/ast/InlineNode/Italic.h index 6311ad3..53d2071 100644 --- a/include/ast/InlineNode/Italic.h +++ b/include/ast/InlineNode/Italic.h @@ -2,17 +2,11 @@ #include "ast/InlineNode/InlineNode.h" -struct Italic : public InlineNode { - std::vector> 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 &child : this->children) { - child->repr(os, indent + 1); - } - } -}; \ No newline at end of file +namespace ast { + struct Italic : public InlineNode { + std::vector> children; + + NodeKind getKind() const override; + void repr(std::ostream &os, int indent) const override; + }; +} \ No newline at end of file diff --git a/include/ast/InlineNode/Link.h b/include/ast/InlineNode/Link.h index 9a83662..9dedcba 100644 --- a/include/ast/InlineNode/Link.h +++ b/include/ast/InlineNode/Link.h @@ -3,18 +3,12 @@ #include "ast/InlineNode/InlineNode.h" #include "utils/shorten.hpp" -struct Link : public InlineNode { - std::string url; - std::vector> children; +namespace ast { + struct Link : public InlineNode { + std::string url; + std::vector> 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 &child : this->children) { - child->repr(os, indent + 1); - } - } -}; \ No newline at end of file + NodeKind getKind() const override; + void repr(std::ostream &os, int indent) const override; + }; +} \ No newline at end of file diff --git a/include/ast/InlineNode/Text.h b/include/ast/InlineNode/Text.h index 8980e7d..1fcb3c3 100644 --- a/include/ast/InlineNode/Text.h +++ b/include/ast/InlineNode/Text.h @@ -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'; - } -}; \ No newline at end of file + NodeKind getKind() const override; + void repr(std::ostream &os, int indent) const override; + }; +} \ No newline at end of file diff --git a/include/ast/Node.h b/include/ast/Node.h index e545661..b227040 100644 --- a/include/ast/Node.h +++ b/include/ast/Node.h @@ -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; -}; \ No newline at end of file + virtual NodeKind getKind() const = 0; + virtual void repr(std::ostream& os, int indent = 0) const = 0; + }; +} \ No newline at end of file diff --git a/include/ast/NodeKind.h b/include/ast/NodeKind.h index 9cde75a..f27800d 100644 --- a/include/ast/NodeKind.h +++ b/include/ast/NodeKind.h @@ -3,45 +3,47 @@ #include #include -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 ""; + } } -} \ No newline at end of file +} diff --git a/include/lexer/Cursor.h b/include/lexer/Cursor.h new file mode 100644 index 0000000..3f59c93 --- /dev/null +++ b/include/lexer/Cursor.h @@ -0,0 +1,2 @@ +#pragma once + diff --git a/include/lexer/Lexer.h b/include/lexer/Lexer.h new file mode 100644 index 0000000..730b77b --- /dev/null +++ b/include/lexer/Lexer.h @@ -0,0 +1,15 @@ +#pragma once +#include + +namespace lexer { + struct State { + std::string sourceText; + char* currentPosition; + int line; + int column; + }; + + struct Lexer { + + }; +} \ No newline at end of file diff --git a/include/lexer/Token.h b/include/lexer/Token.h new file mode 100644 index 0000000..48bc2f4 --- /dev/null +++ b/include/lexer/Token.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include "lexer/TokenKind.h" + +namespace lexer { + struct Token { + TokenKind kind; + std::string lexeme; + + int line; + int column; + }; +} \ No newline at end of file diff --git a/include/lexer/TokenKind.h b/include/lexer/TokenKind.h new file mode 100644 index 0000000..7adca2f --- /dev/null +++ b/include/lexer/TokenKind.h @@ -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 + }; +} \ No newline at end of file diff --git a/source/ast/BlockNode/CodeBlock.cpp b/source/ast/BlockNode/CodeBlock.cpp new file mode 100644 index 0000000..b48a198 --- /dev/null +++ b/source/ast/BlockNode/CodeBlock.cpp @@ -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'; + } +} \ No newline at end of file diff --git a/source/ast/BlockNode/Header.cpp b/source/ast/BlockNode/Header.cpp new file mode 100644 index 0000000..3ec9e21 --- /dev/null +++ b/source/ast/BlockNode/Header.cpp @@ -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 &child : this->children) { + child->repr(os, indent + 1); + } + } +} \ No newline at end of file diff --git a/source/ast/BlockNode/List.cpp b/source/ast/BlockNode/List.cpp new file mode 100644 index 0000000..b5eb343 --- /dev/null +++ b/source/ast/BlockNode/List.cpp @@ -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 &child : this->children) { + child->repr(os, indent + 1); + } + } +} \ No newline at end of file diff --git a/source/ast/BlockNode/ListItem.cpp b/source/ast/BlockNode/ListItem.cpp new file mode 100644 index 0000000..3827d8c --- /dev/null +++ b/source/ast/BlockNode/ListItem.cpp @@ -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 &child : this->children) { + child->repr(os, indent + 1); + } + } +} \ No newline at end of file diff --git a/source/ast/BlockNode/Paragraph.cpp b/source/ast/BlockNode/Paragraph.cpp new file mode 100644 index 0000000..9d0a609 --- /dev/null +++ b/source/ast/BlockNode/Paragraph.cpp @@ -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 &child : children) { + child->repr(os, indent + 1); + } + } +} \ No newline at end of file diff --git a/source/ast/InlineNode/Bold.cpp b/source/ast/InlineNode/Bold.cpp new file mode 100644 index 0000000..df52943 --- /dev/null +++ b/source/ast/InlineNode/Bold.cpp @@ -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 &child : this->children) { + child->repr(os, indent + 1); + } + } +} \ No newline at end of file diff --git a/source/ast/InlineNode/InlineCode.cpp b/source/ast/InlineNode/InlineCode.cpp new file mode 100644 index 0000000..39c92fb --- /dev/null +++ b/source/ast/InlineNode/InlineCode.cpp @@ -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'; + } +} \ No newline at end of file diff --git a/source/ast/InlineNode/Italic.cpp b/source/ast/InlineNode/Italic.cpp new file mode 100644 index 0000000..5980531 --- /dev/null +++ b/source/ast/InlineNode/Italic.cpp @@ -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 &child : this->children) { + child->repr(os, indent + 1); + } + } +} \ No newline at end of file diff --git a/source/ast/InlineNode/Link.cpp b/source/ast/InlineNode/Link.cpp new file mode 100644 index 0000000..5841dfe --- /dev/null +++ b/source/ast/InlineNode/Link.cpp @@ -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 &child : this->children) { + child->repr(os, indent + 1); + } + } +} \ No newline at end of file diff --git a/source/ast/InlineNode/Text.cpp b/source/ast/InlineNode/Text.cpp new file mode 100644 index 0000000..03fe8fa --- /dev/null +++ b/source/ast/InlineNode/Text.cpp @@ -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'; + } +} \ No newline at end of file