Skip to content

Commit 13cb919

Browse files
committed
refactor Parser error signal to include token; update parsing logic to support left-associativity; enhance TreeVisualiser for improved node representation
1 parent a19344c commit 13cb919

File tree

5 files changed

+35
-58
lines changed

5 files changed

+35
-58
lines changed

parser_gui/Parser/include/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Parser : public QObject {
5151
* @brief Emitted when a parsing error occurs (e.g., unexpected token or EOF).
5252
* @param message A descriptive error message.
5353
*/
54-
void error(QString message);
54+
void error(Tiny::Data::Token token, QString message);
5555

5656
private:
5757
QList<Tiny::Data::Token> tokens; ///< The list of tokens to parse.

parser_gui/Parser/src/Parser.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -296,26 +296,21 @@ Node *Parser::parseSimpleExp()
296296
// you might build the tree right-associatively:
297297
// a + a + a + a = a + (a + (a + a))
298298

299-
// Start from the right:
300-
Node* root = terms.back(); // last term
301-
terms.pop_back();
302-
303-
// Combine from the rightmost operator backward
304-
for (int i = (int)ops.size() - 1; i >= 0; i--) {
299+
// Start from the left
300+
Node* root = terms[0]; // The first term is the initial root
301+
for (int i = 0; i < (int)ops.size(); i++) {
305302
Node* op = ops[i];
306-
Node* leftTerm = terms[i]; // corresponding left term
303+
Node* rightTerm = terms[i + 1]; // The next term after the operator
307304

308-
// Build the subtree: op(leftTerm, root)
309-
op->addChild(leftTerm);
305+
// For left-associativity: op(root, rightTerm)
310306
op->addChild(root);
311-
root = op; // op now becomes the new root
312-
}
307+
op->addChild(rightTerm);
313308

314-
// Now 'root' represents the full expression tree.
309+
root = op; // Now op becomes the current root
310+
}
315311

316-
// Assign levels now that the full structure is known
312+
// Now 'root' is left-associative
317313
assignLevels(root, currentLevel);
318-
319314
return root;
320315
}
321316

@@ -400,23 +395,19 @@ Node *Parser::parseTerm()
400395
}
401396

402397
// Now build the tree from these operators and factors.
403-
// For example, if you want right-associativity:
404-
Node* root = factors.back();
405-
factors.pop_back();
406-
407-
// Combine from the rightmost operator backward
408-
for (int i = (int)ops.size() - 1; i >= 0; i--) {
398+
Node* root = factors[0]; // Start with the first factor
399+
for (int i = 0; i < (int)ops.size(); i++) {
409400
Node* op = ops[i];
410-
Node* leftFactor = factors[i];
401+
Node* rightFactor = factors[i + 1];
411402

412-
op->addChild(leftFactor);
403+
// Left-associative: ( (firstFactor op secondFactor) op thirdFactor ) op fourthFactor
413404
op->addChild(root);
414-
root = op;
415-
}
405+
op->addChild(rightFactor);
416406

417-
// root now represents the entire term expression tree
418-
assignLevels(root, currentLevel); // Assign levels after the entire tree is known
407+
root = op; // Update root
408+
}
419409

410+
assignLevels(root, currentLevel);
420411
return root;
421412
}
422413

parser_gui/Widgets/include/TreeVisualiser.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,22 @@ namespace Tiny::Widgets {
3636
void wheelEvent(QWheelEvent* event) override;
3737

3838
bool isOval(Node::NodeType type) {
39-
if ((type == Node::NodeType::Identifier) ||
40-
(type == Node::NodeType::Number) ||
41-
(type == Node::NodeType::Comparison)) {
39+
if ((type == Node::NodeType::Id) ||
40+
(type == Node::NodeType::Const) ||
41+
42+
(type == Node::NodeType::Op)) {
4243
return true;
4344
} else {
4445
return false;
4546
}
4647
}
4748

4849
bool hasValue(Node::NodeType type) {
49-
if ((type == Node::NodeType::Identifier) ||
50-
(type == Node::NodeType::Number) ||
51-
(type == Node::NodeType::ReadStatement) ||
52-
(type == Node::NodeType::AssignStatement) ||
53-
(type == Node::NodeType::Comparison)) {
50+
if ((type == Node::NodeType::If) ||
51+
(type == Node::NodeType::Write) ||
52+
(type == Node::NodeType::Repeat)
53+
)
54+
{
5455
return true;
5556
} else {
5657
return false;

parser_gui/Widgets/src/TabContent.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ TabContent::TabContent(bool tokenTextOnly, bool newFile, QWidget *parent) :
5151
parser->setTokens(tokensList);
5252
parser->parse();
5353

54-
connect(parser, &Parser::error, this, [this](QString message) {
54+
connect(parser, &Parser::error, this, [this](Tiny::Data::Token token, QString message) {
5555
// show the error
5656
// TODO
5757
//qDebug() << message;
@@ -191,6 +191,8 @@ void TabContent::textChanged()
191191
// update the tree visualiser
192192
if (root != nullptr) {
193193
this->treeVisualiser->setRoot(root);
194+
} else {
195+
this->treeVisualiser->setRoot(nullptr);
194196
}
195197
}
196198
}

parser_gui/Widgets/src/TreeVisualiser.cpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,7 @@ void TreeVisualiser::paintEvent(QPaintEvent *event) {
2323
}
2424

2525
TreeVisualiser::TreeVisualiser(QWidget *parent) : QWidget(parent) {
26-
Node *nroot = new Node(Node::NodeType::Expression, "+");
27-
nroot->setLevel(1);
28-
Node *child1 = new Node(Node::NodeType::Number, "3");
29-
child1->setLevel(2);
30-
Node *child2 = new Node(Node::NodeType::Number, "4");
31-
child2->setLevel(1);
32-
Node *child3 = new Node(Node::NodeType::Number, "5");
33-
child3->setLevel(3);
34-
35-
Node *child4 = new Node(Node::NodeType::Number, "A7a");
36-
Node *child5 = new Node(Node::NodeType::Addition, "5555555");
37-
38-
this->root = nroot;
39-
this->root->addChild(child1);
40-
this->root->addChild(child2);
41-
child2->addChild(child3);
42-
child2->addChild(child4);
43-
child1->addChild(child5);
26+
4427
}
4528

4629
void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int availableWidth, int level) {
@@ -62,7 +45,7 @@ void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int a
6245
if (!hasValue(node->getType())) {
6346
nodeText = node->getNodeTypeString().toString() + "\n(" + node->getValue() + ")";
6447
} else {
65-
nodeText = node->getValue();
48+
nodeText = node->getNodeTypeString().toString();
6649
}
6750
painter->drawText(QRect(x - 40, y - 20, 80, 40),
6851
Qt::AlignCenter | Qt::TextWordWrap, nodeText);
@@ -94,14 +77,14 @@ void TreeVisualiser::drawTree(QPainter *painter, Node *node, int x, int y, int a
9477
int childWidth = childWidths[i];
9578

9679
// Calculate position for each child
97-
int childX = startX + childWidth / 2;
98-
int childY = (child->getLevel() == node->getLevel())? y : y + verticalSpacing;
80+
int childX = startX + childWidth / 2 - 20;
81+
int childY = y + verticalSpacing;
9982

10083
// Draw connection line to child node
10184
painter->setPen(QPen(Qt::black, 2));
10285
int yLineOffset = (child->getLevel() == node->getLevel())? 0 : 20;
10386

104-
painter->drawLine(x, y + yLineOffset, childX, childY);
87+
painter->drawLine(x, y + 20, childX, childY);
10588

10689
// Recursively draw the child node
10790
drawTree(painter, child, childX, childY, childWidth, child->getLevel());

0 commit comments

Comments
 (0)