ast updated
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
build/
|
||||||
+8
-11
@@ -1,17 +1,14 @@
|
|||||||
cmake_minimum_required(VERSION 3.25.1)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(mdtex-compiler)
|
|
||||||
|
project(mdtex_compiler)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_FLAGS "-g -Wall -Wextra -Werror -pedantic -fsanitize=address")
|
|
||||||
|
|
||||||
file(GLOB SRC source/*.cpp)
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -Wpedantic -fsanitize=address")
|
||||||
|
|
||||||
add_executable(mdtex-compiler
|
file(GLOB SRC CONFIGURE_DEPENDS source/*.cpp)
|
||||||
${SRC}
|
|
||||||
main.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
include_directories(
|
add_executable(mdtex_compiler ${SRC} main.cpp)
|
||||||
./include/
|
|
||||||
)
|
target_include_directories(mdtex_compiler PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Node.h"
|
#include "ast/Node.h"
|
||||||
#include "InlineNode.h"
|
// #include "ast/InlineNode/InlineNode.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "BlockNode.h"
|
#include "ast/BlockNode/BlockNode.h"
|
||||||
|
#include "utils/shorten.hpp"
|
||||||
|
|
||||||
struct CodeBlock : public BlockNode {
|
struct CodeBlock : public BlockNode {
|
||||||
|
std::string language;
|
||||||
std::string code;
|
std::string code;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::CodeBlock;
|
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';
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,12 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "BlockNode.h"
|
#include "ast/BlockNode/BlockNode.h"
|
||||||
|
#include "ast/InlineNode/InlineNode.h"
|
||||||
|
|
||||||
struct Header : public BlockNode {
|
struct Header : public BlockNode {
|
||||||
std::string headerText;
|
int level = 1;
|
||||||
std::vector<std::unique_ptr<InlineNode>> children;
|
std::vector<std::unique_ptr<InlineNode>> children;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::Header;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,12 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "BlockNode.h"
|
#include "ast/BlockNode/BlockNode.h"
|
||||||
#include "ListItem.h"
|
#include "ast/BlockNode/ListItem.h"
|
||||||
|
|
||||||
struct List : public BlockNode {
|
struct List : public BlockNode {
|
||||||
std::vector<std::unique_ptr<ListItem>> children;
|
std::vector<std::unique_ptr<ListItem>> children;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::List;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,11 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "BlockNode.h"
|
#include "ast/BlockNode/BlockNode.h"
|
||||||
|
|
||||||
struct ListItem : public BlockNode {
|
struct ListItem : public BlockNode {
|
||||||
std::vector<std::unique_ptr<InlineNode>> children;
|
std::vector<std::unique_ptr<BlockNode>> children;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::ListItem;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,11 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "BlockNode.h"
|
#include "ast/BlockNode/BlockNode.h"
|
||||||
|
#include "ast/InlineNode/InlineNode.h"
|
||||||
|
|
||||||
struct Paragraph : public BlockNode {
|
struct Paragraph : public BlockNode {
|
||||||
std::vector<std::unique_ptr<InlineNode>> children;
|
std::vector<std::unique_ptr<InlineNode>> children;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::Paragraph;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <ast/BlockNode/BlockNode.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,11 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "InlineNode.h"
|
#include "ast/InlineNode/InlineNode.h"
|
||||||
|
|
||||||
struct Bold : public InlineNode {
|
struct Bold : public InlineNode {
|
||||||
std::vector<std::unique_ptr<InlineNode>> children;
|
std::vector<std::unique_ptr<InlineNode>> children;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::Bold;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "InlineNode.h"
|
#include "ast/InlineNode/InlineNode.h"
|
||||||
|
|
||||||
struct InlineCode : public InlineNode {
|
struct InlineCode : public InlineNode {
|
||||||
std::string inlineCodeString;
|
std::string inlineCodeString;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::InlineCode;
|
return NodeKind::InlineCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void repr(std::ostream &os, int indent) const override {
|
||||||
|
os << getIndent(indent) << toString(getKind()) << " inline code: " << this->inlineCodeString << '\n';
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Node.h"
|
#include "ast/Node.h"
|
||||||
|
|
||||||
struct InlineNode : public Node {
|
struct InlineNode : public Node {
|
||||||
virtual ~InlineNode() = default;
|
virtual ~InlineNode() = default;
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "InlineNode.h"
|
#include "ast/InlineNode/InlineNode.h"
|
||||||
|
|
||||||
struct Italic : public InlineNode {
|
struct Italic : public InlineNode {
|
||||||
std::vector<std::unique_ptr<InlineNode>> children;
|
std::vector<std::unique_ptr<InlineNode>> children;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::Italic;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,12 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "InlineNode.h"
|
#include "ast/InlineNode/InlineNode.h"
|
||||||
|
#include "utils/shorten.hpp"
|
||||||
|
|
||||||
struct Link : public InlineNode {
|
struct Link : public InlineNode {
|
||||||
std::string url;
|
std::string url;
|
||||||
std::vector<std::unique_ptr<InlineNode>> children;
|
std::vector<std::unique_ptr<InlineNode>> children;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::Link;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,11 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "InlineNode.h"
|
#include "ast/InlineNode/InlineNode.h"
|
||||||
|
#include "utils/shorten.hpp"
|
||||||
|
|
||||||
struct Text : public InlineNode {
|
struct Text : public InlineNode {
|
||||||
std::string text;
|
std::string text;
|
||||||
|
|
||||||
NodeKind getKind() const {
|
NodeKind getKind() const override {
|
||||||
return NodeKind::Text;
|
return NodeKind::Text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void repr(std::ostream &os, int indent) const override {
|
||||||
|
os << getIndent(indent) << toString(this->getKind()) << " text: " << shorten(this->text) << '\n';
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,10 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "NodeKind.h"
|
#include "NodeKind.h"
|
||||||
|
#include "utils/getIndent.hpp"
|
||||||
|
|
||||||
struct Node {
|
struct Node {
|
||||||
virtual ~Node() = default;
|
virtual ~Node() = default;
|
||||||
|
|
||||||
virtual NodeKind getKind() const = 0;
|
virtual NodeKind getKind() const = 0;
|
||||||
|
virtual void repr(std::ostream& os, int indent = 0) const = 0;
|
||||||
};
|
};
|
||||||
+33
-1
@@ -1,5 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
enum class NodeKind {
|
enum class NodeKind {
|
||||||
CodeBlock,
|
CodeBlock,
|
||||||
Header,
|
Header,
|
||||||
@@ -12,4 +15,33 @@ enum class NodeKind {
|
|||||||
Italic,
|
Italic,
|
||||||
Link,
|
Link,
|
||||||
Text
|
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";
|
||||||
|
|
||||||
|
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 "";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string getIndent(int indentSize) {
|
||||||
|
std::string indents;
|
||||||
|
for (int i = 0; i < indentSize; ++i) {
|
||||||
|
indents += " ";
|
||||||
|
}
|
||||||
|
return indents;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string shorten(const std::string &string) {
|
||||||
|
if (string.size() > 9) {
|
||||||
|
std::string shortString;
|
||||||
|
for (int i = 0; i < 5; ++i) {
|
||||||
|
shortString.push_back(string[i]);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
shortString.push_back('.');
|
||||||
|
}
|
||||||
|
for (int i = string.size() - 6; i < string.size(); ++i) {
|
||||||
|
shortString.push_back(string[i]);
|
||||||
|
}
|
||||||
|
return shortString;
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user