Skip to content

Commit a19344c

Browse files
authored
Merge pull request #4 from OrcaLinux/parser
refactor Node class; simplify constructor, update node type strings, …
2 parents f709562 + aaf5582 commit a19344c

File tree

6 files changed

+523
-130
lines changed

6 files changed

+523
-130
lines changed

parser_gui/Data/include/Node.h

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,18 @@ class Node {
1515

1616
public:
1717
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
18+
Read,
19+
Write,
20+
If,
21+
Repeat,
22+
Assign,
23+
Op,
24+
Id,
25+
Const
3226
}; // enum class NodeType
3327

3428

35-
explicit Node(NodeType type, QString value, Node *parent = nullptr);
29+
explicit Node(NodeType type);
3630
~Node();
3731

3832
// getters
@@ -64,21 +58,15 @@ class Node {
6458

6559

6660

67-
static constexpr std::array<QLatin1StringView, 14> nodeTypeStrings = {
68-
QLatin1StringView("Program"),
69-
QLatin1StringView("Statement"),
70-
QLatin1StringView("IfStatement"),
71-
QLatin1StringView("RepeatStatement"),
72-
QLatin1StringView("AssignStatement"),
73-
QLatin1StringView("ReadStatement"),
74-
QLatin1StringView("WriteStatement"),
75-
QLatin1StringView("Expression"),
76-
QLatin1StringView("Comparison"),
77-
QLatin1StringView("Addition"),
78-
QLatin1StringView("Multiplication"),
79-
QLatin1StringView("Factor"),
80-
QLatin1StringView("Identifier"),
81-
QLatin1StringView("Number")
61+
static constexpr std::array<QLatin1StringView, 8> nodeTypeStrings = {
62+
QLatin1StringView("Read"),
63+
QLatin1StringView("Write"),
64+
QLatin1StringView("If"),
65+
QLatin1StringView("Repeat"),
66+
QLatin1StringView("Assign"),
67+
QLatin1StringView("Op"),
68+
QLatin1StringView("Id"),
69+
QLatin1StringView("Const")
8270
}; // nodeTypeStrings Lookup Table
8371

8472
}; // class Node

parser_gui/Data/src/Node.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
using namespace Tiny::Data;
44

5-
Node::Node(NodeType type, QString value, Node *parent)
6-
: type(type), value(value), parent(parent)
7-
{}
5+
Node::Node(NodeType type)
6+
: type(type)
7+
{
8+
this->value = "";
9+
this->parent = nullptr;
10+
this->level = 0;
11+
this->children = QList<Node*>();
12+
}
813

914
Node::~Node()
1015
{
@@ -66,7 +71,6 @@ void Node::setType(NodeType type)
6671
void Node::setParent(Node *parent)
6772
{
6873
this->parent = parent;
69-
parent->addChild(this);
7074
}
7175

7276
void Node::setLevel(int level)
@@ -77,12 +81,11 @@ void Node::setLevel(int level)
7781
void Node::addChild(Node *child)
7882
{
7983
this->children.append(child);
80-
child->setParent(this);
8184
}
8285

8386
void Node::printTree(int depth) const
8487
{
85-
qDebug().noquote() << QString(depth * 2, ' ') << this->getNodeTypeString() << ": " << value;
88+
qDebug().noquote() << QString(depth * 2, ' ') << this->getNodeTypeString() << ": " << value << " level: " << level;
8689
for (auto child : children)
8790
{
8891
child->printTree(depth + 1);

parser_gui/Parser/include/Parser.h

Lines changed: 84 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,93 @@
1-
// Parser.h
21
#ifndef PARSER_H
32
#define PARSER_H
43

54
#include <QObject>
65
#include <QList>
6+
#include <stdexcept>
7+
#include <QListIterator>
78

89
#include "Token.h"
910
#include "Node.h"
1011

11-
using namespace Tiny::Data;
12-
13-
namespace Tiny::Parser {
14-
class Parser : public QObject {
15-
Q_OBJECT
16-
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);
33-
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();
49-
};
50-
51-
52-
} // namespace Tiny::Parser
53-
54-
#endif // PARSER_H
12+
namespace Tiny {
13+
namespace Parser {
14+
15+
/**
16+
* @class Parser
17+
* @brief A parser for the TINY language that constructs a syntax tree from a list of tokens.
18+
*
19+
* The Parser class takes a list of tokens (produced by the scanner) and attempts to construct a
20+
* syntax tree according to the TINY language grammar rules. It emits errors or throws exceptions
21+
* if unexpected tokens or end-of-file conditions occur.
22+
*
23+
* Usage:
24+
* 1. Create a Parser object.
25+
* 2. Call setTokens() with the token list.
26+
* 3. Call parse() to parse the entire token list and produce a syntax tree root Node.
27+
* 4. If parsing is successful, you get a syntax tree. If errors occur, handle them as needed.
28+
*/
29+
class Parser : public QObject {
30+
Q_OBJECT
31+
public:
32+
explicit Parser(QObject *parent = nullptr);
33+
~Parser() override;
34+
35+
/**
36+
* @brief Set the token list to be parsed.
37+
* @param tokens The list of tokens produced by the scanner.
38+
*/
39+
void setTokens(QList<Tiny::Data::Token> tokens);
40+
41+
/**
42+
* @brief Parse the entire set of tokens and build a syntax tree.
43+
* @return A pointer to the root Node of the syntax tree if successful, or nullptr if empty.
44+
* @throws std::out_of_range if unexpected end of file (no more tokens) is encountered improperly.
45+
* @throws std::invalid_argument if a token does not match the expected type.
46+
*/
47+
Tiny::Data::Node* parse();
48+
49+
signals:
50+
/**
51+
* @brief Emitted when a parsing error occurs (e.g., unexpected token or EOF).
52+
* @param message A descriptive error message.
53+
*/
54+
void error(QString message);
55+
56+
private:
57+
QList<Tiny::Data::Token> tokens; ///< The list of tokens to parse.
58+
QListIterator<Tiny::Data::Token> tokenIterator; ///< Iterator for the token list.
59+
int currentLevel; ///< Current nesting level for the syntax tree.
60+
bool hasError; ///< Flag indicating if an error occurred during parsing.
61+
62+
/**
63+
* @brief Peek at the current token without consuming it.
64+
* @return The current token.
65+
* @throws std::out_of_range if no more tokens.
66+
*/
67+
Tiny::Data::Token peek();
68+
69+
void match(Tiny::Data::Token::TokenType expectedType);
70+
71+
void assignLevels(Tiny::Data::Node* node, int baseLevel);
72+
73+
// Grammar rules:
74+
Tiny::Data::Node* parseStmtSequence();
75+
Tiny::Data::Node* parseStatement();
76+
Tiny::Data::Node* parseIfStatement();
77+
Tiny::Data::Node* parseRepeatStatement();
78+
Tiny::Data::Node* parseAssignStatement();
79+
Tiny::Data::Node* parseReadStatement();
80+
Tiny::Data::Node* parseWriteStatement();
81+
Tiny::Data::Node* parseExp();
82+
Tiny::Data::Node* parseSimpleExp();
83+
Tiny::Data::Node* parseComparisonOp();
84+
Tiny::Data::Node* parseAddop();
85+
Tiny::Data::Node* parseTerm();
86+
Tiny::Data::Node* parseMulop();
87+
Tiny::Data::Node* parseFactor();
88+
};
89+
90+
} // namespace Parser
91+
} // namespace Tiny
92+
93+
#endif // PARSER_H

0 commit comments

Comments
 (0)