ast updated
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Node.h"
|
||||
#include "InlineNode.h"
|
||||
#include "ast/Node.h"
|
||||
// #include "ast/InlineNode/InlineNode.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "BlockNode.h"
|
||||
#include "ast/BlockNode/BlockNode.h"
|
||||
#include "utils/shorten.hpp"
|
||||
|
||||
struct CodeBlock : public BlockNode {
|
||||
std::string language;
|
||||
std::string code;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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';
|
||||
}
|
||||
};
|
||||
@@ -1,12 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "BlockNode.h"
|
||||
#include "ast/BlockNode/BlockNode.h"
|
||||
#include "ast/InlineNode/InlineNode.h"
|
||||
|
||||
struct Header : public BlockNode {
|
||||
std::string headerText;
|
||||
int level = 1;
|
||||
std::vector<std::unique_ptr<InlineNode>> children;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,12 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "BlockNode.h"
|
||||
#include "ListItem.h"
|
||||
#include "ast/BlockNode/BlockNode.h"
|
||||
#include "ast/BlockNode/ListItem.h"
|
||||
|
||||
struct List : public BlockNode {
|
||||
std::vector<std::unique_ptr<ListItem>> children;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,11 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "BlockNode.h"
|
||||
#include "ast/BlockNode/BlockNode.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
#include "BlockNode.h"
|
||||
#include "ast/BlockNode/BlockNode.h"
|
||||
#include "ast/InlineNode/InlineNode.h"
|
||||
|
||||
struct Paragraph : public BlockNode {
|
||||
std::vector<std::unique_ptr<InlineNode>> children;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -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
|
||||
|
||||
#include "InlineNode.h"
|
||||
#include "ast/InlineNode/InlineNode.h"
|
||||
|
||||
struct Bold : public InlineNode {
|
||||
std::vector<std::unique_ptr<InlineNode>> children;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,11 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "InlineNode.h"
|
||||
#include "ast/InlineNode/InlineNode.h"
|
||||
|
||||
struct InlineCode : public InlineNode {
|
||||
std::string inlineCodeString;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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';
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Node.h"
|
||||
#include "ast/Node.h"
|
||||
|
||||
struct InlineNode : public Node {
|
||||
virtual ~InlineNode() = default;
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "InlineNode.h"
|
||||
#include "ast/InlineNode/InlineNode.h"
|
||||
|
||||
struct Italic : public InlineNode {
|
||||
std::vector<std::unique_ptr<InlineNode>> children;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,12 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "InlineNode.h"
|
||||
#include "ast/InlineNode/InlineNode.h"
|
||||
#include "utils/shorten.hpp"
|
||||
|
||||
struct Link : public InlineNode {
|
||||
std::string url;
|
||||
std::vector<std::unique_ptr<InlineNode>> children;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,11 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "InlineNode.h"
|
||||
#include "ast/InlineNode/InlineNode.h"
|
||||
#include "utils/shorten.hpp"
|
||||
|
||||
struct Text : public InlineNode {
|
||||
std::string text;
|
||||
|
||||
NodeKind getKind() const {
|
||||
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';
|
||||
}
|
||||
};
|
||||
@@ -1,10 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "NodeKind.h"
|
||||
#include "utils/getIndent.hpp"
|
||||
|
||||
struct Node {
|
||||
virtual ~Node() = default;
|
||||
|
||||
virtual NodeKind getKind() const = 0;
|
||||
virtual void repr(std::ostream& os, int indent = 0) const = 0;
|
||||
};
|
||||
+33
-1
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
enum class NodeKind {
|
||||
CodeBlock,
|
||||
Header,
|
||||
@@ -12,4 +15,33 @@ enum class NodeKind {
|
||||
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";
|
||||
|
||||
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