Skip to content

Commit 5d50553

Browse files
committed
remove ParseTree class and its implementation
1 parent f02a7ce commit 5d50553

File tree

20 files changed

+917
-53
lines changed

20 files changed

+917
-53
lines changed

parser_gui/Data/include/Node.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#ifndef NODE_H
2+
#define NODE_H
3+
4+
#include <QString>
5+
#include <QList>
6+
#include <QStringView>
7+
#include <QLatin1StringView>
8+
#include <QDebug>
9+
10+
#include <array>
11+
12+
13+
namespace Tiny::Data {
14+
class Node {
15+
16+
public:
17+
enum class NodeType {
18+
Program,
19+
Statement,
20+
IfStatement,
21+
RepeatStatement,
22+
AssignStatement,
23+
ReadStatement,
24+
WriteStatement,
25+
Expression,
26+
Comparison,
27+
Addition,
28+
Multiplication,
29+
Factor,
30+
Identifier,
31+
Number
32+
}; // enum class NodeType
33+
34+
35+
explicit Node(NodeType type, QString value, Node *parent = nullptr);
36+
~Node();
37+
38+
// getters
39+
QString getValue() const;
40+
NodeType getType() const;
41+
QStringView getNodeTypeString() const;
42+
Node *getParent() const;
43+
QList<Node *> getSiblings() const;
44+
QList<Node *> getChildren() const;
45+
46+
// setters
47+
void setValue(QString value);
48+
void setType(NodeType type);
49+
void setParent(Node *parent);
50+
51+
// add children
52+
void addChild(Node *child);
53+
54+
// add siblings
55+
void addSibling(Node *sibling);
56+
57+
// print tree
58+
void printTree(int depth = 0) const;
59+
60+
private:
61+
QString value;
62+
NodeType type;
63+
Node *parent;
64+
QList<Node *> siblings;
65+
QList<Node *> children;
66+
67+
68+
69+
70+
static constexpr std::array<QLatin1StringView, 14> nodeTypeStrings = {
71+
QLatin1StringView("Program"),
72+
QLatin1StringView("Statement"),
73+
QLatin1StringView("IfStatement"),
74+
QLatin1StringView("RepeatStatement"),
75+
QLatin1StringView("AssignStatement"),
76+
QLatin1StringView("ReadStatement"),
77+
QLatin1StringView("WriteStatement"),
78+
QLatin1StringView("Expression"),
79+
QLatin1StringView("Comparison"),
80+
QLatin1StringView("Addition"),
81+
QLatin1StringView("Multiplication"),
82+
QLatin1StringView("Factor"),
83+
QLatin1StringView("Identifier"),
84+
QLatin1StringView("Number")
85+
}; // nodeTypeStrings Lookup Table
86+
87+
}; // class Node
88+
89+
} // namespace Tiny::Data
90+
91+
#endif // NODE_H

parser_gui/Data/include/ParseTree.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

parser_gui/Data/include/Token.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Token {
4141
enum class TokenType {
4242
IF, /**< "if" keyword */
4343
THEN, /**< "then" keyword */
44+
ELSE, /**< "else" keyword */
4445
END, /**< "end" keyword */
4546
REPEAT, /**< "repeat" keyword */
4647
UNTIL, /**< "until" keyword */
@@ -110,6 +111,8 @@ class Token {
110111
*/
111112
QString toString(bool includePosition = false) const;
112113

114+
QString toHTMLString(bool includePosition = false) const;
115+
113116

114117
// copy constructor
115118
Token(const Token &other) = default;
@@ -146,9 +149,10 @@ class Token {
146149
* This table maps each TokenType enum value to its corresponding string
147150
* representation for use in debugging and output.
148151
*/
149-
static constexpr std::array<QLatin1StringView, 20> tokenTypeStrings = {
152+
static constexpr std::array<QLatin1StringView, 21> tokenTypeStrings = {
150153
QLatin1StringView("IF"), /**< "if" keyword */
151154
QLatin1StringView("THEN"), /**< "then" keyword */
155+
QLatin1StringView("ELSE"), /**< "else" keyword */
152156
QLatin1StringView("END"), /**< "end" keyword */
153157
QLatin1StringView("REPEAT"), /**< "repeat" keyword */
154158
QLatin1StringView("UNTIL"), /**< "until" keyword */

parser_gui/Data/src/Node.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "Node.h"
2+
3+
using namespace Tiny::Data;
4+
5+
Node::Node(NodeType type, QString value, Node *parent)
6+
: type(type), value(value), parent(parent)
7+
{}
8+
9+
Node::~Node()
10+
{
11+
for (auto child : children)
12+
{
13+
delete child;
14+
}
15+
}
16+
17+
QString Node::getValue() const
18+
{
19+
return value;
20+
}
21+
22+
Node::NodeType Node::getType() const
23+
{
24+
return type;
25+
}
26+
27+
QStringView Node::getNodeTypeString() const
28+
{
29+
size_t index = static_cast<size_t>(type);
30+
31+
// Ensure the index is valid and within range
32+
if (index >= nodeTypeStrings.size())
33+
{
34+
throw std::out_of_range("Invalid NodeType index");
35+
}
36+
37+
// Return the string representation of the node type
38+
return QStringView(nodeTypeStrings[index].toString());
39+
}
40+
41+
Node *Node::getParent() const
42+
{
43+
return parent;
44+
}
45+
46+
QList<Node *> Node::getSiblings() const
47+
{
48+
return siblings;
49+
}
50+
51+
QList<Node *> Node::getChildren() const
52+
{
53+
return children;
54+
}
55+
56+
void Node::setValue(QString value)
57+
{
58+
this->value = value;
59+
}
60+
61+
void Node::setType(NodeType type)
62+
{
63+
this->type = type;
64+
}
65+
66+
void Node::setParent(Node *parent)
67+
{
68+
this->parent = parent;
69+
}
70+
71+
void Node::addChild(Node *child)
72+
{
73+
this->children.append(child);
74+
}
75+
76+
void Node::addSibling(Node *sibling)
77+
{
78+
this->siblings.append(sibling);
79+
sibling->addSibling(this);
80+
}
81+
82+
void Node::printTree(int depth) const
83+
{
84+
qDebug().noquote() << QString(depth * 2, ' ') << this->getNodeTypeString() << ": " << value;
85+
for (auto child : children)
86+
{
87+
child->printTree(depth + 1);
88+
}
89+
90+
}

parser_gui/Data/src/ParseTree.cpp

Lines changed: 0 additions & 7 deletions
This file was deleted.

parser_gui/Data/src/Token.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,32 @@ QString Token::toString(bool includePosition) const {
9090
return str;
9191
}
9292

93+
QString Token::toHTMLString(bool includePosition) const
94+
{
95+
QString str;
96+
QTextStream stream(&str);
97+
98+
// Use light gray for token value
99+
stream << "<span style='color:#c0c0c0;'>" << value << "</span>, ";
100+
101+
// Conditional formatting for token type
102+
if (type == TokenType::UNKNOWN) {
103+
// Bright red for unknown type
104+
stream << "<span style='color:#ff5555; font-weight:bold;'>" << getTokenTypeString() << "</span>";
105+
} else if (type != TokenType::NUMBER && type != TokenType::IDENTIFIER && type != TokenType::UNKNOWN) {
106+
// Light blue for reserved types
107+
stream << "<span style='color:#5555ff; font-weight:bold;'>" << getTokenTypeString() << "</span>";
108+
} else {
109+
// Default white for known types
110+
stream << "<span style='color:#ffffff; font-weight:bold;'>" << getTokenTypeString() << "</span>";
111+
}
112+
113+
// Optionally include the token's line and column positions
114+
if (includePosition) {
115+
stream << " <span style='color:#ff9900;'>[Line: " << line << ", Column: " << column << "]</span>";
116+
}
117+
118+
return str;
119+
}
120+
93121
} // namespace Tiny::Data

parser_gui/MainWindow.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ void MainWindow::initTabWidget() {
4343
// connect tolbar buttons to tab widget slots
4444
connect(toolbar, &Tiny::Widgets::ToolBar::newTextTab, tabWidget, &Tiny::Widgets::TabWidget::newTextTab);
4545
connect(toolbar, &Tiny::Widgets::ToolBar::newTokensTab, tabWidget, &Tiny::Widgets::TabWidget::newTokensTab);
46+
47+
// set the toolbar in the tab widget
48+
tabWidget->setToolBar(toolbar);
4649
}

parser_gui/Parser/include/Parser.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22
#ifndef PARSER_H
33
#define PARSER_H
44

5+
#include <QObject>
6+
57
namespace Tiny::Parser {
8+
class Parser : public QObject {
9+
Q_OBJECT
10+
public:
11+
12+
};
13+
614

715
} // namespace Tiny::Parser
816

9-
#endif // PARSER_H
17+
#endif // PARSER_H

parser_gui/Scanner/include/Scanner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Scanner : public QObject
6161
*/
6262
bool hasMoreTokens();
6363

64+
void setInput(const QString &input);
6465
private:
6566
QString input; /**< The source code to be tokenized. */
6667
size_t pos = 0; /**< Current position in the input string. */

parser_gui/Scanner/src/Scanner.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ Token Scanner::getNextToken()
9696
return Token(Token::TokenType::READ, identifier, line, column);
9797
if (identifier == "write")
9898
return Token(Token::TokenType::WRITE, identifier, line, column);
99-
99+
if (identifier == "else")
100+
return Token(Token::TokenType::ELSE, identifier, line, column);
100101
// If not a keyword, it's an identifier
101102
return Token(Token::TokenType::IDENTIFIER, identifier, line, column);
102103
}
@@ -141,6 +142,15 @@ bool Scanner::hasMoreTokens()
141142
return hasMore;
142143
}
143144

145+
void Scanner::setInput(const QString &input)
146+
{
147+
// reset the scanner state
148+
this->input = input;
149+
pos = 0;
150+
line = 1;
151+
column = 1;
152+
}
153+
144154
QChar Scanner::peek() const
145155
{
146156
// Return the next character if within bounds, or '\0' if at the end

0 commit comments

Comments
 (0)