Skip to content

Commit aff8375

Browse files
committed
refactor Node and Token classes; remove sibling handling, add level management, and update token type string method
1 parent 5d50553 commit aff8375

File tree

9 files changed

+234
-41
lines changed

9 files changed

+234
-41
lines changed

parser_gui/Data/include/Node.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,27 @@ class Node {
4040
NodeType getType() const;
4141
QStringView getNodeTypeString() const;
4242
Node *getParent() const;
43-
QList<Node *> getSiblings() const;
4443
QList<Node *> getChildren() const;
44+
int getLevel() const;
4545

4646
// setters
4747
void setValue(QString value);
4848
void setType(NodeType type);
4949
void setParent(Node *parent);
50+
void setLevel(int level);
5051

5152
// add children
5253
void addChild(Node *child);
5354

54-
// add siblings
55-
void addSibling(Node *sibling);
56-
5755
// print tree
5856
void printTree(int depth = 0) const;
5957

6058
private:
6159
QString value;
6260
NodeType type;
6361
Node *parent;
64-
QList<Node *> siblings;
6562
QList<Node *> children;
66-
63+
int level;
6764

6865

6966

parser_gui/Data/include/Token.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ class Token {
128128

129129
// destructor
130130
~Token() = default;
131-
private:
132-
TokenType type; /**< The type of the token */
133-
QString value; /**< The value of the token */
134-
int line; /**< The line number of the token */
135-
int column; /**< The column number of the token */
136131

137132
/**
138133
* @brief Converts the token type to a string representation.
@@ -141,7 +136,15 @@ class Token {
141136
*
142137
* @throws std::out_of_range if the token type is invalid.
143138
*/
144-
QStringView getTokenTypeString() const;
139+
static QStringView getTokenTypeString(TokenType type);
140+
141+
private:
142+
TokenType type; /**< The type of the token */
143+
QString value; /**< The value of the token */
144+
int line; /**< The line number of the token */
145+
int column; /**< The column number of the token */
146+
147+
145148

146149
/**
147150
* @brief Static constexpr lookup table for token type string representations.

parser_gui/Data/src/Node.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ Node *Node::getParent() const
4343
return parent;
4444
}
4545

46-
QList<Node *> Node::getSiblings() const
46+
QList<Node *> Node::getChildren() const
4747
{
48-
return siblings;
48+
return children;
4949
}
5050

51-
QList<Node *> Node::getChildren() const
51+
int Node::getLevel() const
5252
{
53-
return children;
53+
return level;
5454
}
5555

5656
void Node::setValue(QString value)
@@ -66,17 +66,18 @@ void Node::setType(NodeType type)
6666
void Node::setParent(Node *parent)
6767
{
6868
this->parent = parent;
69+
parent->addChild(this);
6970
}
7071

71-
void Node::addChild(Node *child)
72+
void Node::setLevel(int level)
7273
{
73-
this->children.append(child);
74+
this->level = level;
7475
}
7576

76-
void Node::addSibling(Node *sibling)
77+
void Node::addChild(Node *child)
7778
{
78-
this->siblings.append(sibling);
79-
sibling->addSibling(this);
79+
this->children.append(child);
80+
child->setParent(this);
8081
}
8182

8283
void Node::printTree(int depth) const

parser_gui/Data/src/Token.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ Token &Token::operator=(Token &&other)
4040
return *this;
4141
}
4242

43+
QStringView Token::getTokenTypeString(TokenType type)
44+
{
45+
size_t index = static_cast<size_t>(type);
46+
47+
// Ensure the index is valid and within range
48+
if (index >= tokenTypeStrings.size()) {
49+
throw std::out_of_range("Invalid TokenType index");
50+
}
51+
52+
// Return the string representation of the token type
53+
return QStringView(tokenTypeStrings[index].toString());
54+
}
55+
4356
// Returns the type of the token
4457
Token::TokenType Token::getType() const {
4558
return type;
@@ -60,26 +73,13 @@ int Token::getColumn() const {
6073
return column;
6174
}
6275

63-
// Converts the token type to its string representation using a lookup table
64-
QStringView Token::getTokenTypeString() const {
65-
size_t index = static_cast<size_t>(type);
66-
67-
// Ensure the index is valid and within range
68-
if (index >= tokenTypeStrings.size()) {
69-
throw std::out_of_range("Invalid TokenType index");
70-
}
71-
72-
// Return the string representation of the token type
73-
return QStringView(tokenTypeStrings[index].toString());
74-
}
75-
7676
// Converts the token to a detailed string representation
7777
QString Token::toString(bool includePosition) const {
7878
QString str;
7979
QTextStream stream(&str);
8080

8181
// Include the token type and value
82-
stream << value << ", " << getTokenTypeString();
82+
stream << value << ", " << getTokenTypeString(type);
8383

8484
// Optionally include the token's line and column positions
8585
if (includePosition) {
@@ -96,18 +96,23 @@ QString Token::toHTMLString(bool includePosition) const
9696
QTextStream stream(&str);
9797

9898
// Use light gray for token value
99+
if (value == "<"){
100+
stream << "<span style='color:#c0c0c0;'>&lt;" << value << "&gt;</span>, ";
101+
102+
} else {
99103
stream << "<span style='color:#c0c0c0;'>" << value << "</span>, ";
104+
}
100105

101106
// Conditional formatting for token type
102107
if (type == TokenType::UNKNOWN) {
103108
// Bright red for unknown type
104-
stream << "<span style='color:#ff5555; font-weight:bold;'>" << getTokenTypeString() << "</span>";
109+
stream << "<span style='color:#ff5555; font-weight:bold;'>" << getTokenTypeString(type) << "</span>";
105110
} else if (type != TokenType::NUMBER && type != TokenType::IDENTIFIER && type != TokenType::UNKNOWN) {
106111
// Light blue for reserved types
107-
stream << "<span style='color:#5555ff; font-weight:bold;'>" << getTokenTypeString() << "</span>";
112+
stream << "<span style='color:#5555ff; font-weight:bold;'>" << getTokenTypeString(type) << "</span>";
108113
} else {
109114
// Default white for known types
110-
stream << "<span style='color:#ffffff; font-weight:bold;'>" << getTokenTypeString() << "</span>";
115+
stream << "<span style='color:#ffffff; font-weight:bold;'>" << getTokenTypeString(type) << "</span>";
111116
}
112117

113118
// Optionally include the token's line and column positions

parser_gui/Parser/include/Parser.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,49 @@
33
#define PARSER_H
44

55
#include <QObject>
6+
#include <QList>
7+
8+
#include "Token.h"
9+
#include "Node.h"
10+
11+
using namespace Tiny::Data;
612

713
namespace Tiny::Parser {
814
class Parser : public QObject {
915
Q_OBJECT
1016
public:
17+
explicit Parser(QObject *parent = nullptr);
18+
~Parser() override;
19+
20+
void setTokens(QList<Token> tokens);
21+
Node* parse();
22+
signals:
23+
void error(QString message);
24+
private:
25+
QList<Token> tokens;
26+
int currentTokenIndex;
27+
int currentLevel;
28+
29+
void advance();
30+
31+
void expectAndSkip(Token::TokenType type);
32+
Node* consume(Token::TokenType tokenType, Tiny::Data::Node::NodeType nodeType);
1133

34+
// rules
35+
Node* parseStmtSequence();
36+
Node* parseStatement();
37+
Node* parseIfStatement();
38+
Node* parseRepeatStatement();
39+
Node* parseAssignStatement();
40+
Node* parseReadStatement();
41+
Node* parseWriteStatement();
42+
Node* parseExp();
43+
Node* parseSimpleExp();
44+
Node* parseComparisonOp();
45+
Node* parseAddop();
46+
Node* parseTerm();
47+
Node* parseMulop();
48+
Node* parseFactor();
1249
};
1350

1451

parser_gui/Parser/src/Parser.cpp

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,146 @@
11
// Parser.cpp
22
#include "Parser.h"
33

4-
namespace Tiny::Parser {
4+
using namespace Tiny::Parser;
5+
using namespace Tiny::Data;
6+
7+
Parser::Parser(QObject *parent) : QObject(parent)
8+
{
9+
10+
}
11+
12+
Parser::~Parser()
13+
{}
14+
15+
void Parser::setTokens(QList<Token> tokens)
16+
{
17+
this->tokens = tokens;
18+
currentTokenIndex = 0;
19+
currentLevel = 0;
20+
}
21+
22+
Node *Parser::parse()
23+
{
24+
return parseStmtSequence();
25+
}
26+
27+
void Parser::advance()
28+
{
29+
currentTokenIndex++;
30+
}
31+
32+
void Parser::expectAndSkip(Token::TokenType type)
33+
{
34+
if(tokens[currentTokenIndex].getType() != type)
35+
{
36+
emit error("Expected " + Token::getTokenTypeString(type).toString() + " but got " +
37+
Token::getTokenTypeString(tokens[currentTokenIndex].getType()).toString() +
38+
". At line: " + QString::number(tokens[currentTokenIndex].getLine()) +
39+
" column: " + QString::number(tokens[currentTokenIndex].getColumn()));
40+
}
41+
advance();
42+
}
43+
44+
Node *Parser::consume(Token::TokenType tokenType, Node::NodeType nodeType)
45+
{
46+
Token currentToken = tokens[currentTokenIndex];
47+
if(currentToken.getType() != tokenType){
48+
emit error("Expected " + Token::getTokenTypeString(tokenType).toString() + " but got " +
49+
Token::getTokenTypeString(currentToken.getType()).toString() +
50+
". At line: " + QString::number(currentToken.getLine()) +
51+
" column: " + QString::number(currentToken.getColumn()));
52+
return nullptr;
53+
}
54+
55+
Node* node = new Node(nodeType, currentToken.getValue());
56+
advance();
57+
return node;
58+
}
59+
60+
Node *Parser::parseStmtSequence()
61+
{
62+
Node* firstStmt = parseStatement();
63+
Node* currentStmt = firstStmt;
64+
// set level
65+
currentStmt->setLevel(currentLevel);
66+
67+
while(tokens[currentTokenIndex].getType() == Token::TokenType::SEMICOLON)
68+
{
69+
advance(); // consume the semicolon
70+
Node* nextStmt = parseStatement();
71+
if(nextStmt != nullptr)
72+
{
73+
currentStmt->addChild(nextStmt);
74+
nextStmt->setLevel(currentLevel);
75+
currentStmt = nextStmt;
76+
}
77+
}
78+
79+
return firstStmt;
80+
}
81+
82+
Node *Parser::parseStatement()
83+
{
84+
85+
}
86+
87+
Node *Parser::parseIfStatement()
88+
{
89+
90+
}
91+
92+
Node *Parser::parseRepeatStatement()
93+
{
94+
95+
}
96+
97+
Node *Parser::parseAssignStatement()
98+
{
99+
100+
}
101+
102+
Node *Parser::parseReadStatement()
103+
{
104+
105+
}
106+
107+
Node *Parser::parseWriteStatement()
108+
{
109+
110+
}
111+
112+
Node *Parser::parseExp()
113+
{
114+
115+
}
116+
117+
Node *Parser::parseSimpleExp()
118+
{
119+
120+
}
121+
122+
Node *Parser::parseComparisonOp()
123+
{
124+
125+
}
126+
127+
Node *Parser::parseAddop()
128+
{
129+
130+
}
131+
132+
Node *Parser::parseTerm()
133+
{
134+
135+
}
136+
137+
Node *Parser::parseMulop()
138+
{
139+
140+
}
141+
142+
Node *Parser::parseFactor()
143+
{
144+
145+
}
5146

6-
} // namespace Tiny::Parser

parser_gui/Widgets/include/TreeVisualiser.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
#include "Node.h"
99

10+
using namespace Tiny::Data;
1011
namespace Tiny::Widgets {
1112
class TreeVisualiser : public QWidget {
1213
Q_OBJECT
1314
public:
1415
TreeVisualiser(QWidget *parent = nullptr);
15-
void drawTree(Node* root);
16+
void setRoot(Node* root);
17+
1618

1719
}; // class TreeVisualiser
1820
} // namespace Tiny::Widgets

0 commit comments

Comments
 (0)